Package org.dspace.app.util
Class XMLUtils
java.lang.Object
org.dspace.app.util.XMLUtils
Simple class to read information from small XML using DOM manipulation
- Author:
- Andrea Bollini
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThis entity resolver accepts one or more path strings in its constructor and throws a SAXException if the entity systemID is not within the allowed path (or a subdirectory). -
Method Summary
Modifier and TypeMethodDescriptionstatic DocumentBuilderInitialize and return the javax DocumentBuilder with some basic security applied to avoid XXE attacks and other unwanted content inclusionstatic DocumentBuilderFactoryInitialize and return the javax DocumentBuilderFactory with some basic security applied to avoid XXE attacks and other unwanted content inclusionstatic StringgetElementAttribute(Element dataRoot, String name, String attr) getElementList(Element dataRoot, String name) static StringgetElementValue(Element dataRoot, String name) getElementValueArrayList(Element rootElement, String subElementName, String... fieldsName) root/subElement[]/field1, field2, fieldNgetElementValueList(Element rootElement, String subElementName) static org.jdom2.input.SAXBuilderInitialize and return the SAX document builder with some basic security applied to avoid XXE attacks and other unwanted content inclusionstatic org.jdom2.input.SAXBuildergetSAXBuilder(boolean validate) Initialize and return the SAX document builder with some basic security applied to avoid XXE attacks and other unwanted content inclusionstatic ElementgetSingleElement(Element dataRoot, String name) Return the first element child with the specified namestatic DocumentBuildergetTrustedDocumentBuilder(String... allowedPaths) Initialize and return a javax DocumentBuilder with less security applied.static DocumentBuilderFactoryInitialize and return a javax DocumentBuilderFactory with NO security applied.static XMLInputFactoryInitialize and return the Java XML Input Factory with some basic security applied to avoid XXE attacks and other unwanted content inclusion
-
Method Details
-
getElementList
- Parameters:
dataRoot- the starting nodename- the tag name of the child element to find.- Returns:
- the list of all DOM Element with the provided name direct child of the starting node
-
getElementAttribute
- Parameters:
dataRoot- the starting nodename- the name of the sub elementattr- the attribute to get- Returns:
- the value of the attribute for the sub element with the specified name in the starting node
-
getElementValue
- Parameters:
dataRoot- the starting nodename- the name of the sub element- Returns:
- the text content of the sub element with the specified name in the starting node
-
getSingleElement
Return the first element child with the specified name- Parameters:
dataRoot- the starting nodename- the name of sub element to look for- Returns:
- the first child element or null if no present
-
getElementValueList
- Parameters:
rootElement- the starting nodesubElementName- the tag name of the child element to find.- Returns:
- a list of string including all the text contents of the sub element with the specified name. If there are not sub element with the supplied name the method will return null
-
getElementValueArrayList
public static List<String[]> getElementValueArrayList(Element rootElement, String subElementName, String... fieldsName) root/subElement[]/field1, field2, fieldN- Parameters:
rootElement- the starting nodesubElementName- the name of the sub element to work onfieldsName- the names of the sub-sub-elements from which get the text content- Returns:
- a list of array strings. The length of the array is equals to the number of fields required. For any fields the first textual value found in the sub element is used, null if no value is present
-
getTrustedDocumentBuilderFactory
public static DocumentBuilderFactory getTrustedDocumentBuilderFactory() throws ParserConfigurationExceptionInitialize and return a javax DocumentBuilderFactory with NO security applied. This is intended only for internal, administrative/configuration use where external entities and other dangerous features are actually purposefully included. The method here is tiny, but may be expanded with other features like whitespace handling, and calling this method name helps to document the fact that the caller knows it is trusting the XML source / factory.- Returns:
- document builder factory to generate new builders
- Throws:
ParserConfigurationException
-
getDocumentBuilderFactory
public static DocumentBuilderFactory getDocumentBuilderFactory() throws ParserConfigurationExceptionInitialize and return the javax DocumentBuilderFactory with some basic security applied to avoid XXE attacks and other unwanted content inclusion- Returns:
- document builder factory to generate new builders
- Throws:
ParserConfigurationException
-
getTrustedDocumentBuilder
public static DocumentBuilder getTrustedDocumentBuilder(String... allowedPaths) throws ParserConfigurationException Initialize and return a javax DocumentBuilder with less security applied. This is intended only for internal, administrative/configuration use where external entities and other dangerous features are actually purposefully included, but are only allowed from specified paths, e.g. dspace.dir or some other path specified by the java caller. The method here is tiny, but may be expanded with other features like whitespace handling, and calling this method name helps to document the fact that the caller knows it is trusting the XML source / builderIf no allowedPaths are passed, then all external entities are rejected
- Returns:
- document builder with no security features set
- Throws:
ParserConfigurationException- if the builder can not be configured
-
getDocumentBuilder
Initialize and return the javax DocumentBuilder with some basic security applied to avoid XXE attacks and other unwanted content inclusion- Returns:
- document builder for use in XML parsing
- Throws:
ParserConfigurationException- if the builder can not be configured
-
getSAXBuilder
public static org.jdom2.input.SAXBuilder getSAXBuilder()Initialize and return the SAX document builder with some basic security applied to avoid XXE attacks and other unwanted content inclusion- Returns:
- SAX document builder for use in XML parsing
-
getSAXBuilder
public static org.jdom2.input.SAXBuilder getSAXBuilder(boolean validate) Initialize and return the SAX document builder with some basic security applied to avoid XXE attacks and other unwanted content inclusion- Parameters:
validate- whether to use JDOM XSD validation- Returns:
- SAX document builder for use in XML parsing
-
getXMLInputFactory
Initialize and return the Java XML Input Factory with some basic security applied to avoid XXE attacks and other unwanted content inclusion- Returns:
- XML input factory for use in XML parsing
-