Class XMLUtil

java.lang.Object
com.groiss.xml.XMLUtil

public class XMLUtil extends Object
XML utils
  • Field Details

  • Method Details

    • parse

      public static Document parse(InputStream is, ErrorHandler eh, boolean validate) throws IOException, JDOMException
      Use this method for creating an xml document that references resources in the classpath. Most underlying xml parsers (xerxes, crimson) close the input stream after reading.
      Parameters:
      is -
      Returns:
      a JDOM document
      Throws:
      IOException
      JDOMException
    • parseUnsafeResource

      public static Document parseUnsafeResource(Reader is) throws IOException, JDOMException
      Throws:
      IOException
      JDOMException
    • parseSafeResource

      public static Document parseSafeResource(Reader is) throws IOException, JDOMException
      Throws:
      IOException
      JDOMException
    • parseUnsafeResource

      public static Document parseUnsafeResource(InputStream is) throws IOException, JDOMException
      Throws:
      IOException
      JDOMException
    • parseSafeResource

      public static Document parseSafeResource(InputStream is) throws IOException, JDOMException
      Throws:
      IOException
      JDOMException
    • parse

      public static Document parse(Reader is, ErrorHandler eh, boolean validate) throws IOException, JDOMException
      Throws:
      IOException
      JDOMException
    • getSAXBuilderThatAllowsReferencingResources

      public static SAXBuilder getSAXBuilderThatAllowsReferencingResources()
      returns a SAXBuilder that might load 'external' resources - but from classpath only
      Returns:
    • getSAXBuilder

      public static SAXBuilder getSAXBuilder()
      returns a SAXBuilder that's not allowed to load any 'external' resources to avoid XXE attacks.
      Returns:
    • getValidationSAXBuilder

      public static SAXBuilder getValidationSAXBuilder(String xsd) throws JDOMException
      returns a SAXBuilder that's not allowed to load any 'external' resources to avoid XXE attacks and validates the input against the passed XSD
      Returns:
      Throws:
      JDOMException
    • getLocationAwareSAXBuilder

      public static SAXBuilder getLocationAwareSAXBuilder(boolean validate)
    • localize

      public static void localize(Element el, Resource r, Locale l)
      localize an xml document. every text and attribute value is localized. String starting wit @@@ and ending with @@ are translated.
      Parameters:
      el - the element
      r - the resource
      l - the locale
    • parseXML

      public static Document parseXML(String resource, String xsdpath, boolean localize) throws JDOMException, IOException
      parse an xml document with given schema.
      Parameters:
      resource - resource path (in classpath)
      xsdpath - path to xsd (in classpath)
      Returns:
      the xml document
      Throws:
      FileNotFoundException
      JDOMException
      IOException
    • parseXML

      public static Document parseXML(InputStream is, String xsdpath) throws JDOMException, IOException
      parse an xml document with given schema.
      Parameters:
      is - read the document from the stream
      xsdpath -
      Returns:
      Throws:
      JDOMException
      IOException
    • parseXML

      public static Document parseXML(Reader is, String xsdpath) throws JDOMException, IOException
      parse an xml document with given schema.
      Parameters:
      is - read the document from the stream
      xsdpath -
      Returns:
      Throws:
      JDOMException
      IOException
    • filterElements

      public static List<Element> filterElements(Element root, Predicate<Element> test)
      recursively walks the dom-tree and returns all elements matching test for simple per-element-filters, this is 10 times faster than a //xxx XPath expression
      Parameters:
      root -
      test -
      Returns:
    • get

      public static <T> T get(String xpath, Object context, Filter<T> filter)
      Select a single node via xpath from a context using an empty namespace.
      Type Parameters:
      T - The generic type of the results that the XPathExpression will produce.
      Parameters:
      xpath - The XPath expression.
      context - The context against which to process the query.
      filter - The Filter that is used to coerce the xpath result data in to the generic-typed results.
      Returns:
      the single content node, may be null
    • get

      public static <T> T get(String xpath, Object context, Namespace namespace, Filter<T> filter)
      Select a single node via xpath from a context.
      Type Parameters:
      T - The generic type of the results that the XPathExpression will produce.
      Parameters:
      xpath - The XPath expression.
      context - The context against which to process the query.
      namespace - The namespace that may be referenced from the query.
      filter - The Filter that is used to coerce the xpath result data in to the generic-typed results.
      Returns:
      the single content node, may be null
    • get

      public static <T> T get(String xpath, Object context, Namespace[] namespaces, Filter<T> filter)
      Select a single node via xpath from a context.
      Type Parameters:
      T - The generic type of the results that the XPathExpression will produce.
      Parameters:
      xpath - The XPath expression.
      context - The context against which to process the query.
      namespaces - The namespaces that may be referenced from the query.
      filter - The Filter that is used to coerce the xpath result data in to the generic-typed results.
      Returns:
      the single content node, may be null
    • get

      public static <T> T get(String xpath, Object context, Collection<Namespace> namespaces, Filter<T> filter)
      Select a single node via xpath from a context.
      Type Parameters:
      T - The generic type of the results that the XPathExpression will produce.
      Parameters:
      xpath - The XPath expression.
      context - The context against which to process the query.
      namespaces - The namespaces that may be referenced from the query.
      filter - The Filter that is used to coerce the xpath result data in to the generic-typed results.
      Returns:
      the single content node, may be null
    • getElement

      public static Element getElement(String xpath, Object context)
      Select a single Element via xpath from a context using an empty namespace.
      Parameters:
      xpath - The XPath expression.
      context - The context against which to process the query.
      Returns:
      the single Element node, may be null
    • getElement

      public static Element getElement(String xpath, Object context, Namespace namespace)
      Select a single Element via xpath from a context.
      Parameters:
      xpath - The XPath expression.
      context - The context against which to process the query.
      namespace - The namespace that may be referenced from the query.
      Returns:
      the single Element node, may be null
    • getElement

      public static Element getElement(String xpath, Object context, Collection<Namespace> namespaces)
      Select a single Element via xpath from a context.
      Parameters:
      xpath - The XPath expression.
      context - The context against which to process the query.
      namespaces - The namespaces that may be referenced from the query.
      Returns:
      the single Element node, may be null
    • getElementList

      public static List<Element> getElementList(String xpath, Object context)
      Select a List of Element nodes via xpath from a context using an empty namespace.
      Parameters:
      xpath - The XPath expression.
      context - The context against which to process the query.
      Returns:
      the List of selected Element nodes.
    • getElementList

      public static List<Element> getElementList(String xpath, Object context, Namespace namespace)
      Select a List of Element nodes via xpath from a context.
      Parameters:
      xpath - The XPath expression.
      context - The context against which to process the query.
      namespace - The namespace that may be referenced from the query.
      Returns:
      the List of selected Element nodes.
    • getElementList

      public static List<Element> getElementList(String xpath, Object context, Collection<Namespace> namespaces)
      Select a List of Element nodes via xpath from a context.
      Parameters:
      xpath - The XPath expression.
      context - The context against which to process the query.
      namespaces - The namespaces that may be referenced from the query.
      Returns:
      the List of selected Element nodes.
    • getList

      public static <T> List<T> getList(String xpath, Object context, Filter<T> filter)
      Select a List of Nodes via xpath from a context using an empty namespace.
      Type Parameters:
      T - The generic type of the results that the XPathExpression will produce.
      Parameters:
      xpath - The XPath expression.
      context - The context against which to process the query.
      filter - The Filter that is used to coerce the xpath result data in to the generic-typed results.
      Returns:
      the List of selected Nodes nodes.
    • getList

      public static <T> List<T> getList(String xpath, Object context, Namespace namespace, Filter<T> filter)
      Select a List of Nodes via xpath from a context.
      Type Parameters:
      T - The generic type of the results that the XPathExpression will produce.
      Parameters:
      xpath - The XPath expression.
      context - The context against which to process the query.
      namespace - The namespace that may be referenced from the query.
      filter - The Filter that is used to coerce the xpath result data in to the generic-typed results.
      Returns:
      the List of selected Nodes nodes.
    • getList

      public static <T> List<T> getList(String xpath, Object context, Namespace[] namespaces, Filter<T> filter)
      Select a List of Nodes via xpath from a context.
      Type Parameters:
      T - The generic type of the results that the XPathExpression will produce.
      Parameters:
      xpath - The XPath expression.
      context - The context against which to process the query.
      namespaces - The namespaces that may be referenced from the query.
      filter - The Filter that is used to coerce the xpath result data in to the generic-typed results.
      Returns:
      the List of selected Nodes nodes.
    • getList

      public static <T> List<T> getList(String xpath, Object context, Collection<Namespace> namespaces, Filter<T> filter)
      Select a List of Nodes via xpath from a context.
      Type Parameters:
      T - The generic type of the results that the XPathExpression will produce.
      Parameters:
      xpath - The XPath expression.
      context - The context against which to process the query.
      namespaces - The namespaces that may be referenced from the query.
      filter - The Filter that is used to coerce the xpath result data in to the generic-typed results.
      Returns:
      the List of selected Nodes nodes.
    • checkXPathExpression

      public static void checkXPathExpression(String xpath)
      Check, if an an XPath expression is compileable.
      Parameters:
      xpath -
      Throws:
      NullPointerException
      IllegalArgumentException
    • addToContentCollection

      public static void addToContentCollection(Collection<Content> target, Object o)
      Add an Object to a Collection of Content. An object of type Content is added without special action. Component typed objects get their root Element added. Other Types are added encapsulated as Text nodes with the values of their toString() methods. If o is an Iterable, each of the elements is added (recursively).
      Parameters:
      target - the collection to add the object to.
      o - the object to be added to the collection.
    • toContent

      public static Collection<Content> toContent(Collection<Object> collection)
      Transform a Collection of Objects to a (new) Collection of Content. Each of the elements of Collection is trasformed according to #addToContentCollection
      Parameters:
      collection - the elements to transform.
      Returns:
      a new Collection of Content
    • getHTMLOutputter

      public static XMLOutputter getHTMLOutputter()
      This is a customized outputter that does not encode the \r chars and does not expand empty elements like br,img or input. The outputter is used if you call show() on a com.groiss.gui.Component subclass.
    • getHTMLOutputter

      public static XMLOutputter getHTMLOutputter(boolean escape)
      This is a customized outputter that does not encode the \r chars and does not expand empty elements like br,img or input. Set escape to false if you don't want to escape entities. This causes big trouble: security leaks, unparsable documents, etc. The function is for compatibility only.
      Parameters:
      escape - controls escape behavior
    • showXSDFile

      public static Page showXSDFile(HttpServletRequest req)
      Shows the schema of a XML file readonly in a popup
    • getXSDFileContent

      public static void getXSDFileContent(HttpServletRequest req, HttpServletResponse res) throws Exception
      Loads the content of a schema file
      Throws:
      Exception