Class Util

java.lang.Object
net.loomchild.segment.util.Util

public class Util extends Object
  • Field Details

    • READ_BUFFER_SIZE

      public static final int READ_BUFFER_SIZE
      See Also:
    • MANIFEST_PATH

      public static final String MANIFEST_PATH
      See Also:
    • STAR_PATTERN

      private static final Pattern STAR_PATTERN
    • PLUS_PATTERN

      private static final Pattern PLUS_PATTERN
    • RANGE_PATTERN

      private static final Pattern RANGE_PATTERN
    • CAPTURING_GROUP_PATTERN

      private static final Pattern CAPTURING_GROUP_PATTERN
  • Constructor Details

    • Util

      public Util()
  • Method Details

    • 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:
    • 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, throws ResourceNotFoundException.
      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:
    • 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 name
      classLoader - 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 stylesheet
      schema - XML schema to validate against
      reader - reader with input document
      writer - writer which will be used to write output
      parameterMap - 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 stylesheet
      schema - XML schema to validate against
      reader - reader with input document
      writer - 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 stylesheet
      reader - reader with input document
      writer - writer which will be used to write output
      parameterMap - 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 stylesheet
      reader - reader with input document
      writer - 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 using removeBlockQuotes(String).
      Parameters:
      pattern - pattern to be finitized
      infinity - "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 using removeBlockQuotes(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 - object
      defaultValue - 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

      public static Map<String,Object> getEmptyParameterMap()