Class Util


  • public class Util
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Constructor Description
      Util()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.regex.Pattern compile​(SrxDocument document, java.lang.String regex)  
      static void copyAll​(java.io.Reader reader, java.io.Writer writer)
      Copies the whole content of a reader to a writer.
      static java.lang.String finitize​(java.lang.String pattern, int infinity)
      Changes unlimited length pattern to limited length pattern.
      static javax.xml.bind.JAXBContext getContext​(java.lang.Class<?>... classesToBeBound)  
      static javax.xml.bind.JAXBContext getContext​(java.lang.String context)  
      static javax.xml.bind.JAXBContext getContext​(java.lang.String context, java.lang.ClassLoader classLoader)  
      static java.util.Map<java.lang.String,​java.lang.Object> getEmptyParameterMap()  
      static java.io.FileInputStream getFileInputStream​(java.lang.String fileName)
      Opens a file for reading and returns input stream associated with it.
      static java.io.FileOutputStream getFileOutputStream​(java.lang.String fileName)
      Opens a file for writing and returns output stream associated with it.
      static java.util.jar.Manifest getJarManifest​(java.lang.Class<?> klass)
      Returns Manifest of a jar containing given class.
      static <T> T getParameter​(java.lang.Object value, T defaultValue)
      Returns value if it is not null or default value if it is null.
      static java.io.Reader getReader​(java.io.InputStream inputStream)  
      static java.io.InputStream getResourceStream​(java.lang.String name)
      Finds a resource using system classloader and returns it as input stream.
      static javax.xml.validation.Schema getSchema​(java.io.Reader reader)
      Reads a XML schema from given reader.
      static javax.xml.validation.Schema getSchema​(java.io.Reader[] readerArray)
      Reads a XML schema from given readers.
      static javax.xml.transform.Source getSource​(java.io.Reader reader, javax.xml.validation.Schema schema)  
      static javax.xml.transform.Templates getTemplates​(java.io.Reader reader)
      Returns XML transform templates from given reader containing XSLT stylesheet.
      static java.io.Writer getWriter​(java.io.OutputStream outputStream)  
      static org.xml.sax.XMLReader getXmlReader()  
      static org.xml.sax.XMLReader getXmlReader​(javax.xml.validation.Schema schema)
      Returns XMLReader validating against given XML schema.
      static java.lang.String readAll​(java.io.Reader reader)
      Reads whole contents of a reader to a string.
      static java.lang.String removeBlockQuotes​(java.lang.String pattern)
      Replaces block quotes in regular expressions with normal quotes.
      static java.lang.String removeCapturingGroups​(java.lang.String pattern)
      Replaces capturing groups with non-capturing groups in the given regular expression.
      static void transform​(javax.xml.transform.Templates templates, java.io.Reader reader, java.io.Writer writer)
      Performs XSLT transformation.
      static void transform​(javax.xml.transform.Templates templates, java.io.Reader reader, java.io.Writer writer, java.util.Map<java.lang.String,​java.lang.Object> parameterMap)
      Performs XSLT transformation.
      static void transform​(javax.xml.transform.Templates templates, javax.xml.validation.Schema schema, java.io.Reader reader, java.io.Writer writer)
      Performs XML schema validation and XSLT transformation.
      static void transform​(javax.xml.transform.Templates templates, javax.xml.validation.Schema schema, java.io.Reader reader, java.io.Writer writer, java.util.Map<java.lang.String,​java.lang.Object> parameterMap)
      Performs XML schema validation and XSLT transformation.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • STAR_PATTERN

        private static final java.util.regex.Pattern STAR_PATTERN
      • PLUS_PATTERN

        private static final java.util.regex.Pattern PLUS_PATTERN
      • RANGE_PATTERN

        private static final java.util.regex.Pattern RANGE_PATTERN
      • CAPTURING_GROUP_PATTERN

        private static final java.util.regex.Pattern CAPTURING_GROUP_PATTERN
    • Constructor Detail

      • Util

        public Util()
    • Method Detail

      • getReader

        public static java.io.Reader getReader​(java.io.InputStream inputStream)
        Parameters:
        inputStream -
        Returns:
        UTF-8 encoded reader from given input stream
        Throws:
        IORuntimeException - if IO error occurs
      • getWriter

        public static java.io.Writer getWriter​(java.io.OutputStream outputStream)
        Parameters:
        outputStream -
        Returns:
        UTF-8 encoded writer to a given output stream
        Throws:
        IORuntimeException - if IO error occurs
      • getFileInputStream

        public static java.io.FileInputStream getFileInputStream​(java.lang.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 java.io.FileOutputStream getFileOutputStream​(java.lang.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 java.io.InputStream getResourceStream​(java.lang.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 java.lang.String readAll​(java.io.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​(java.io.Reader reader,
                                   java.io.Writer writer)
        Copies the whole content of a reader to a writer.
        Parameters:
        reader -
        writer -
        Throws:
        IORuntimeException - on IO error
      • getJarManifest

        public static java.util.jar.Manifest getJarManifest​(java.lang.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 org.xml.sax.XMLReader getXmlReader​(javax.xml.validation.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 org.xml.sax.XMLReader getXmlReader()
        Returns:
        XMLReader without XML schema associated with it
        Throws:
        XMLException - when SAX error occurs
        See Also:
        getXmlReader(Schema)
      • getSchema

        public static javax.xml.validation.Schema getSchema​(java.io.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 javax.xml.validation.Schema getSchema​(java.io.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 javax.xml.transform.Source getSource​(java.io.Reader reader,
                                                           javax.xml.validation.Schema schema)
        Parameters:
        reader -
        schema - XML schema
        Returns:
        XML source from given reader and with given schema
      • getContext

        public static javax.xml.bind.JAXBContext getContext​(java.lang.String context)
        Parameters:
        context - context package name
        Returns:
        JAXB context
        Throws:
        XMLException - if JAXB error occurs
      • getContext

        public static javax.xml.bind.JAXBContext getContext​(java.lang.String context,
                                                            java.lang.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 javax.xml.bind.JAXBContext getContext​(java.lang.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 javax.xml.transform.Templates getTemplates​(java.io.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​(javax.xml.transform.Templates templates,
                                     javax.xml.validation.Schema schema,
                                     java.io.Reader reader,
                                     java.io.Writer writer,
                                     java.util.Map<java.lang.String,​java.lang.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​(javax.xml.transform.Templates templates,
                                     javax.xml.validation.Schema schema,
                                     java.io.Reader reader,
                                     java.io.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​(javax.xml.transform.Templates templates,
                                     java.io.Reader reader,
                                     java.io.Writer writer,
                                     java.util.Map<java.lang.String,​java.lang.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​(javax.xml.transform.Templates templates,
                                     java.io.Reader reader,
                                     java.io.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 java.lang.String removeBlockQuotes​(java.lang.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 java.lang.String finitize​(java.lang.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 java.util.regex.Pattern compile​(SrxDocument document,
                                                      java.lang.String regex)
      • removeCapturingGroups

        public static java.lang.String removeCapturingGroups​(java.lang.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​(java.lang.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:
        java.lang.ClassCastException - when value cannot be cast to default value type
      • getEmptyParameterMap

        public static java.util.Map<java.lang.String,​java.lang.Object> getEmptyParameterMap()