Package net.loomchild.segment.util
Class Util
- java.lang.Object
-
- net.loomchild.segment.util.Util
-
public class Util extends Object
-
-
Field Summary
Fields Modifier and Type Field Description static String
MANIFEST_PATH
static int
READ_BUFFER_SIZE
-
Constructor Summary
Constructors Constructor Description Util()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Pattern
compile(SrxDocument document, String regex)
static void
copyAll(Reader reader, Writer writer)
Copies the whole content of a reader to a writer.static String
finitize(String pattern, int infinity)
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 Map<String,Object>
getEmptyParameterMap()
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
getSchema(Reader reader)
Reads a XML schema from given reader.static Schema
getSchema(Reader[] readerArray)
Reads a XML schema from given readers.static Source
getSource(Reader reader, Schema schema)
static Templates
getTemplates(Reader reader)
Returns XML transform templates from given reader containing XSLT stylesheet.static Writer
getWriter(OutputStream outputStream)
static XMLReader
getXmlReader()
static XMLReader
getXmlReader(Schema schema)
Returns XMLReader validating against given XML schema.static String
readAll(Reader reader)
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
transform(Templates templates, Reader reader, Writer writer)
Performs XSLT transformation.static void
transform(Templates templates, Reader reader, Writer writer, Map<String,Object> parameterMap)
Performs XSLT transformation.static void
transform(Templates templates, Schema schema, Reader reader, Writer writer)
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 Detail
-
READ_BUFFER_SIZE
public static final int READ_BUFFER_SIZE
- See Also:
- Constant Field Values
-
MANIFEST_PATH
public static final String MANIFEST_PATH
- See Also:
- Constant Field Values
-
-
Method Detail
-
getReader
public static Reader getReader(InputStream inputStream)
- Parameters:
inputStream
-- Returns:
- UTF-8 encoded reader from given input stream
- Throws:
IORuntimeException
- if IO error occurs
-
getWriter
public static Writer getWriter(OutputStream outputStream)
- Parameters:
outputStream
-- Returns:
- UTF-8 encoded writer to a given output stream
- Throws:
IORuntimeException
- if IO error occurs
-
getFileInputStream
public static FileInputStream getFileInputStream(String fileName)
Opens a file for reading and returns input stream associated with it.- Parameters:
fileName
-- Returns:
- input stream
- Throws:
IORuntimeException
- if IO error occurs
-
getFileOutputStream
public static FileOutputStream getFileOutputStream(String fileName)
Opens a file for writing and returns output stream associated with it.- Parameters:
fileName
-- Returns:
- output stream
- Throws:
IORuntimeException
- if IO error occurs
-
getResourceStream
public static InputStream getResourceStream(String name)
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:
ClassLoader
-
readAll
public static String readAll(Reader reader)
Reads whole contents of a reader to a string.- Parameters:
reader
-- Returns:
- a string containing reader contents
- Throws:
IORuntimeException
- on IO error
-
copyAll
public static void copyAll(Reader reader, Writer writer)
Copies the whole content of a reader to a writer.- Parameters:
reader
-writer
-- Throws:
IORuntimeException
- on IO error
-
getJarManifest
public static Manifest getJarManifest(Class<?> klass)
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
public static XMLReader getXmlReader(Schema schema)
Returns XMLReader validating against given XML schema. The reader ignores DTD defined in XML file.- Parameters:
schema
-- Returns:
- XMLReader
- Throws:
XMLException
- when SAX error occurs
-
getXmlReader
public static XMLReader getXmlReader()
- Returns:
- XMLReader without XML schema associated with it
- Throws:
XMLException
- when SAX error occurs- See Also:
getXmlReader(Schema)
-
getSchema
public static Schema getSchema(Reader reader)
Reads a XML schema from given reader.- Parameters:
reader
-- Returns:
- XML Schema
- Throws:
XMLException
- when XML schema parsing error occurs
-
getSchema
public static Schema getSchema(Reader[] readerArray)
Reads a XML schema from given readers. Schema files can depend on one another.- Parameters:
readerArray
- readers containing XML schemas- Returns:
- XML Schema object
- Throws:
XMLException
- when XML schema parsing error occurs
-
getSource
public static Source getSource(Reader reader, Schema schema)
- Parameters:
reader
-schema
- XML schema- Returns:
- XML source from given reader and with given schema
-
getContext
public static JAXBContext getContext(String context)
- Parameters:
context
- context package name- Returns:
- JAXB context
- Throws:
XMLException
- if JAXB error occurs
-
getContext
public static JAXBContext getContext(String context, ClassLoader classLoader)
- Parameters:
context
- context package nameclassLoader
- class loader used to load classes from the context- Returns:
- JAXB context; loads the classes using given classloader
- Throws:
XMLException
- if JAXB error occurs
-
getContext
public static JAXBContext getContext(Class<?>... classesToBeBound)
- Parameters:
classesToBeBound
-- Returns:
- JAXBContext according to classes to bind Dependency classes are also loaded automatically.
- Throws:
XMLException
- if JAXB error occurs
-
getTemplates
public static Templates getTemplates(Reader reader)
Returns XML transform templates from given reader containing XSLT stylesheet.- Parameters:
reader
-- Returns:
- templates; they can be reused many times to perform the transformation
- Throws:
XMLException
- if XML parsing error occurs
-
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- Throws:
XMLException
- if transformation error occurs
-
transform
public static void transform(Templates templates, Schema schema, Reader reader, Writer writer)
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- Throws:
XMLException
- if transformation error occurs
-
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- Throws:
XMLException
- if transformation error occurs
-
transform
public static void transform(Templates templates, Reader reader, Writer writer)
Performs XSLT transformation.- Parameters:
templates
- XSLT stylesheetreader
- reader with input documentwriter
- writer which will be used to write output- Throws:
XMLException
- if transformation error occurs
-
removeBlockQuotes
public static String removeBlockQuotes(String pattern)
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
public static String finitize(String pattern, int infinity)
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
public static Pattern compile(SrxDocument document, String regex)
-
removeCapturingGroups
public static String removeCapturingGroups(String pattern)
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
public static <T> T getParameter(Object value, T defaultValue)
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
-
-