Package com.groiss.xml
Class XMLUtil
java.lang.Object
com.groiss.xml.XMLUtil
XML utils
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
addToContentCollection
(Collection<Content> target, Object o) Add an Object to a Collection of Content.static void
checkXPathExpression
(String xpath) Check, if an an XPath expression is compileable.filterElements
(Element root, Predicate<Element> test) recursively walks the dom-tree and returns all elements matchingtest
for simple per-element-filters, this is 10 times faster than a//xxx
XPath expressionstatic <T> T
Select a single node via xpath from a context.static <T> T
Select a single node via xpath from a context using an empty namespace.static <T> T
Select a single node via xpath from a context.static <T> T
Select a single node via xpath from a context.static Element
getElement
(String xpath, Object context) Select a single Element via xpath from a context using an empty namespace.static Element
getElement
(String xpath, Object context, Collection<Namespace> namespaces) Select a single Element via xpath from a context.static Element
getElement
(String xpath, Object context, Namespace namespace) Select a single Element via xpath from a context.getElementList
(String xpath, Object context) Select a List of Element nodes via xpath from a context using an empty namespace.getElementList
(String xpath, Object context, Collection<Namespace> namespaces) Select a List of Element nodes via xpath from a context.getElementList
(String xpath, Object context, Namespace namespace) Select a List of Element nodes via xpath from a context.static XMLOutputter
This is a customized outputter that does not encode the \r chars and does not expand empty elements like br,img or input.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.static <T> List<T>
Select a List of Nodes via xpath from a context.static <T> List<T>
Select a List of Nodes via xpath from a context using an empty namespace.static <T> List<T>
Select a List of Nodes via xpath from a context.static <T> List<T>
Select a List of Nodes via xpath from a context.static SAXBuilder
getLocationAwareSAXBuilder
(boolean validate) static SAXBuilder
returns aSAXBuilder
that's not allowed to load any 'external' resources to avoid XXE attacks.static SAXBuilder
returns aSAXBuilder
that might load 'external' resources - but from classpath onlystatic SAXBuilder
returns aSAXBuilder
that's not allowed to load any 'external' resources to avoid XXE attacks and validates the input against the passed XSDstatic void
Loads the content of a schema filestatic void
localize an xml document. every text and attribute value is localized.static Document
parse
(InputStream is, ErrorHandler eh, boolean validate) Use this method for creating an xml document that references resources in the classpath.static Document
parse
(Reader is, ErrorHandler eh, boolean validate) static Document
static Document
static Document
static Document
static Document
parseXML
(InputStream is, String xsdpath) parse an xml document with given schema.static Document
parse an xml document with given schema.static Document
parse an xml document with given schema.static Page
Shows the schema of a XML file readonly in a popupstatic Collection<Content>
toContent
(Collection<Object> collection) Transform a Collection of Objects to a (new) Collection of Content.
-
Field Details
-
SYSTEM_PREFIX
- See Also:
-
-
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
- Throws:
IOException
JDOMException
-
parseSafeResource
- Throws:
IOException
JDOMException
-
parseUnsafeResource
- Throws:
IOException
JDOMException
-
parseSafeResource
- Throws:
IOException
JDOMException
-
parse
public static Document parse(Reader is, ErrorHandler eh, boolean validate) throws IOException, JDOMException - Throws:
IOException
JDOMException
-
getSAXBuilderThatAllowsReferencingResources
returns aSAXBuilder
that might load 'external' resources - but from classpath only- Returns:
-
getSAXBuilder
returns aSAXBuilder
that's not allowed to load any 'external' resources to avoid XXE attacks.- Returns:
-
getValidationSAXBuilder
returns aSAXBuilder
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
-
localize
localize an xml document. every text and attribute value is localized. String starting wit @@@ and ending with @@ are translated.- Parameters:
el
- the elementr
- the resourcel
- 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
parse an xml document with given schema.- Parameters:
is
- read the document from the streamxsdpath
-- Returns:
- Throws:
JDOMException
IOException
-
parseXML
parse an xml document with given schema.- Parameters:
is
- read the document from the streamxsdpath
-- Returns:
- Throws:
JDOMException
IOException
-
filterElements
recursively walks the dom-tree and returns all elements matchingtest
for simple per-element-filters, this is 10 times faster than a//xxx
XPath expression- Parameters:
root
-test
-- Returns:
-
get
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
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
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
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
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
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
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
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
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
Check, if an an XPath expression is compileable.- Parameters:
xpath
-- Throws:
NullPointerException
IllegalArgumentException
-
addToContentCollection
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
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
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
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
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
-