Package net.loomchild.segment.util
Class Util
java.lang.Object
net.loomchild.segment.util.Util
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Pattern
compile
(SrxDocument document, String regex) static void
Copies the whole content of a reader to a writer.static String
Changes unlimited length pattern to limited length pattern.static JAXBContext
getContext
(Class<?>... classesToBeBound) static JAXBContext
getContext
(String context) static JAXBContext
getContext
(String context, ClassLoader classLoader) static FileInputStream
getFileInputStream
(String fileName) Opens a file for reading and returns input stream associated with it.static FileOutputStream
getFileOutputStream
(String fileName) Opens a file for writing and returns output stream associated with it.static Manifest
getJarManifest
(Class<?> klass) Returns Manifest of a jar containing given class.static <T> T
getParameter
(Object value, T defaultValue) Returns value if it is not null or default value if it is null.static Reader
getReader
(InputStream inputStream) static InputStream
getResourceStream
(String name) Finds a resource using system classloader and returns it as input stream.static Schema
Reads a XML schema from given reader.static Schema
Reads a XML schema from given readers.static Source
static Templates
getTemplates
(Reader reader) Returns XML transform templates from given reader containing XSLT stylesheet.static Writer
getWriter
(OutputStream outputStream) static XMLReader
static XMLReader
getXmlReader
(Schema schema) Returns XMLReader validating against given XML schema.static String
Reads whole contents of a reader to a string.static String
removeBlockQuotes
(String pattern) Replaces block quotes in regular expressions with normal quotes.static String
removeCapturingGroups
(String pattern) Replaces capturing groups with non-capturing groups in the given regular expression.static void
Performs XSLT transformation.static void
Performs XSLT transformation.static void
Performs XML schema validation and XSLT transformation.static void
transform
(Templates templates, Schema schema, Reader reader, Writer writer, Map<String, Object> parameterMap) Performs XML schema validation and XSLT transformation.
-
Field Details
-
READ_BUFFER_SIZE
public static final int READ_BUFFER_SIZE- See Also:
-
MANIFEST_PATH
- See Also:
-
STAR_PATTERN
-
PLUS_PATTERN
-
RANGE_PATTERN
-
CAPTURING_GROUP_PATTERN
-
-
Constructor Details
-
Util
public Util()
-
-
Method Details
-
getReader
- Parameters:
inputStream
-- Returns:
- UTF-8 encoded reader from given input stream
- Throws:
IORuntimeException
- if IO error occurs
-
getWriter
- Parameters:
outputStream
-- Returns:
- UTF-8 encoded writer to a given output stream
- Throws:
IORuntimeException
- if IO error occurs
-
getFileInputStream
Opens a file for reading and returns input stream associated with it.- Parameters:
fileName
-- Returns:
- input stream
- Throws:
IORuntimeException
- if IO error occurs
-
getFileOutputStream
Opens a file for writing and returns output stream associated with it.- Parameters:
fileName
-- Returns:
- output stream
- Throws:
IORuntimeException
- if IO error occurs
-
getResourceStream
Finds a resource using system classloader and returns it as input stream.- Parameters:
name
- resource name- Returns:
- resource input stream
- Throws:
ResourceNotFoundException
- if resource can not be found- See Also:
-
readAll
Reads whole contents of a reader to a string.- Parameters:
reader
-- Returns:
- a string containing reader contents
- Throws:
IORuntimeException
- on IO error
-
copyAll
Copies the whole content of a reader to a writer.- Parameters:
reader
-writer
-- Throws:
IORuntimeException
- on IO error
-
getJarManifest
Returns Manifest of a jar containing given class. If class is not in a jar, throwsResourceNotFoundException
.- Parameters:
klass
- class- Returns:
- manifest
- Throws:
ResourceNotFoundException
- if manifest was not found
-
getXmlReader
Returns XMLReader validating against given XML schema. The reader ignores DTD defined in XML file.- Parameters:
schema
-- Returns:
- XMLReader
-
getXmlReader
- Returns:
- XMLReader without XML schema associated with it
- See Also:
-
getSchema
Reads a XML schema from given reader.- Parameters:
reader
-- Returns:
- XML Schema
-
getSchema
Reads a XML schema from given readers. Schema files can depend on one another.- Parameters:
readerArray
- readers containing XML schemas- Returns:
- XML Schema object
-
getSource
- Parameters:
reader
-schema
- XML schema- Returns:
- XML source from given reader and with given schema
-
getContext
- Parameters:
context
- context package name- Returns:
- JAXB context
-
getContext
- Parameters:
context
- context package nameclassLoader
- class loader used to load classes from the context- Returns:
- JAXB context; loads the classes using given classloader
-
getContext
- Parameters:
classesToBeBound
-- Returns:
- JAXBContext according to classes to bind Dependency classes are also loaded automatically.
-
getTemplates
Returns XML transform templates from given reader containing XSLT stylesheet.- Parameters:
reader
-- Returns:
- templates; they can be reused many times to perform the transformation
-
transform
public static void transform(Templates templates, Schema schema, Reader reader, Writer writer, Map<String, Object> parameterMap) Performs XML schema validation and XSLT transformation.- Parameters:
templates
- XSLT stylesheetschema
- XML schema to validate againstreader
- reader with input documentwriter
- writer which will be used to write outputparameterMap
- transformation parameters
-
transform
Performs XML schema validation and XSLT transformation.- Parameters:
templates
- XSLT stylesheetschema
- XML schema to validate againstreader
- reader with input documentwriter
- writer which will be used to write output
-
transform
public static void transform(Templates templates, Reader reader, Writer writer, Map<String, Object> parameterMap) Performs XSLT transformation.- Parameters:
templates
- XSLT stylesheetreader
- reader with input documentwriter
- writer which will be used to write outputparameterMap
- transformation parameters
-
transform
Performs XSLT transformation.- Parameters:
templates
- XSLT stylesheetreader
- reader with input documentwriter
- writer which will be used to write output
-
removeBlockQuotes
Replaces block quotes in regular expressions with normal quotes. For example "\Qabc\E" will be replace with "\a\b\c".- Parameters:
pattern
-- Returns:
- pattern with replaced block quotes
-
finitize
Changes unlimited length pattern to limited length pattern. It is done by replacing constructs with "*" and "+" symbols with their finite counterparts - "{0,n}" and {1,n}. As a side effect block quotes are replaced with normal quotes by usingremoveBlockQuotes(String)
.- Parameters:
pattern
- pattern to be finitizedinfinity
- "n" number- Returns:
- limited length pattern
-
compile
-
removeCapturingGroups
Replaces capturing groups with non-capturing groups in the given regular expression. As a side effect block quotes are replaced with normal quotes by usingremoveBlockQuotes(String)
.- Parameters:
pattern
-- Returns:
- modified pattern
-
getParameter
Returns value if it is not null or default value if it is null. Automatically cast value to the same type as default value.- Parameters:
value
- objectdefaultValue
- default value.- Returns:
- object value or default value if object value is null
- Throws:
ClassCastException
- when value cannot be cast to default value type
-
getEmptyParameterMap
-