Package lumis.util
Class XmlUtil
- java.lang.Object
-
- lumis.util.XmlUtil
-
@StableMinor(version="14.2", sinceVersion="4.0") public class XmlUtil extends Object
Utility file for xml- Since:
- 4.0.0
- Version:
- $Revision: 24985 $ $Date: 2022-05-02 23:04:30 -0300 (Mon, 02 May 2022) $
-
-
Field Summary
Fields Modifier and Type Field Description static String
XML_HEADER
-
Constructor Summary
Constructors Constructor Description XmlUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static Node
addNode(Node xmlParentNode, String newNodeName)
static Node
addNode(Node xmlParentNode, String xpathExpressionParentNode, String newNodeName)
static Attr
addNodeAttr(Node xmlParentNode, String attributeName)
static Attr
addNodeAttr(Node xmlParentNode, String attributeName, String value)
static Node
addNodeFromXmlString(Node node, String xmlString)
static Element
addTextNode(Node xmlParentNode, String newNodeName, String newNodeText)
static Element
addTextNode(Node xmlParentNode, String xpathExpressionParentNode, String newNodeName, String newNodeText)
static Node[]
addTextNodes(Node xmlParentNode, String newNodesName, String[] newNodesValue)
static Node[]
addTextNodes(Node xmlParentNode, String xpathExpressionParentNode, String newNodesName, String[] newNodesValue)
protected static boolean
checkCondition(String leftValue, String rightValue, String conditionOperator)
static boolean
checkCondition(Node conditionNode, String condition)
static boolean
checkSingleCondition(Node conditionNode, String condition)
static byte[]
convertToUTFBytes(byte[] content)
Deprecated.Since 4.2.0, this method is no longer used and may be removed in the future.static Node
copyNode(Node xmlNodeFrom, String xpathExpressionFrom, Node xmlNodeTo)
static Node
copyNode(Node xmlNodeFrom, String xpathExpressionFrom, Node xmlNodeTo, String xpathExpressionTo)
static Node
copyNode(Node xmlNodeFrom, Node xmlNodeTo)
static Node
copyNode(Node xmlNodeFrom, Node xmlNodeTo, String xpathExpressionTo)
static PortalException
createPortalException(SAXException e)
Creates a PortalException based on the given SAXException.static String
encodeXml(String pStr)
Encodes a string to be used inside a XML document.static Document
getDocument(File xmlFile)
static Document
getDocument(InputStream inputStream)
Parses a XML document from an input stream.static Document
getDocument(String xmlString)
static Document
getDocument(HttpServletRequest request)
static Document
getDocument(IFile xmlFile)
Returns theDocument
associated with the given file.static Document
getDocumentFromFile(String filePath)
static Node
getOrCreateNode(String path, Node parentNode)
static String
getTextContent(Node node)
static DocumentBuilder
getXmlDocumentBuilder()
static Document
getXMLDocumentFromString(String xml)
Deprecated.since 4.2.0 replaced bygetDocument(String)
.static String
getXmlEncoding(String xmlString)
Returns the XML encoding for the xml with the given String.static String
getXmlString(Document document)
static String
getXmlString(Element element)
static String
getXmlString(Node node)
Returns the xml string representation for a node.static Node
importIntoNewDocument(Node originalNode)
Clones a node, and adopts it into a new document.static void
initializeDocumentBuilderPool(int minSize, int maxSize, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, int minEvictableIdleTimeMillis)
Internal use only.static boolean
isValidXmlString(String xml)
static void
mergeAttributes(Node destinationNode, Node sourceNode, boolean replace)
static void
mergeElements(Node destinationNode, Node sourceNode, boolean replace)
Merges the children elements from a node to another.static boolean
nodeExists(String xpathExpression, Node sourceNode)
static boolean
readAttributeBoolean(String attributeName, Node node)
static boolean
readAttributeBoolean(String attributeName, Node node, boolean valueForNull)
static int
readAttributeInt(String attributeName, Node node, int valueForNull)
Returns the attribute integer value in the given node.static String
readAttributeOrNodeString(Node node, String fieldName)
static String
readAttributeOrNodeString(Node node, String fieldName, String valueForNull)
static String
readAttributeString(String attributeName, Node node)
static String
readAttributeString(String attributeName, Node node, String valueForNull)
static boolean
readNodeBoolean(String xpathExpression, Node sourceNode, boolean valueForNull)
static int
readNodeInt(String xpathExpression, Node sourceNode, int valueForNull)
static Integer
readNodeInteger(String xpathExpression, Node sourceNode, Integer valueForNull)
static String[]
readNodesString(String xpathExpression, Node sourceNode)
static String
readNodeString(String xpathExpression, Node sourceNode)
static String
readNodeString(String xpathExpression, Node sourceNode, String valueForNull)
static void
replaceChildNode(String childNodeName, Node node, String xmlString)
static void
replaceChildNode(Node parentNode, Node newChildNode)
static Node[]
selectAncestorNodes(String ancestorNameQuery, Node childNode)
static Node[]
selectNodes(String childNameQuery, Node parentNode)
static Node
selectSingleAncestorNode(String ancestorNameQuery, Node childNode)
static Node
selectSingleNode(String childNameQuery, Node parentNode)
protected static Node
selectSingleNodeRecursiveByName(String nodeName, Node parentNode)
static void
setTextContent(Node parentNode, String textValue)
static Node
writeNodeString(String xpathExpression, Node sourceNode, String nodeValue)
-
-
-
Field Detail
-
XML_HEADER
public static final String XML_HEADER
- See Also:
- Constant Field Values
-
-
Method Detail
-
initializeDocumentBuilderPool
public static void initializeDocumentBuilderPool(int minSize, int maxSize, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, int minEvictableIdleTimeMillis)
Internal use only.This method may be incompatible in future versions.
- Parameters:
minSize
- minimum pool size.maxSize
- maximum pool size.timeBetweenEvictionRunsMillis
- the number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive, no idle object evictor thread will be run.numTestsPerEvictionRun
- the number of objects to examine during each run of the idle object evictor thread (if any).minEvictableIdleTimeMillis
- The minimum amount of time an object may sit idle in the pool before it is eligable for eviction by the idle object evictor (if any).- Since:
- 5.5.0
-
getXmlDocumentBuilder
public static DocumentBuilder getXmlDocumentBuilder() throws PortalException
- Throws:
PortalException
-
getDocument
public static Document getDocument(File xmlFile) throws PortalException
- Throws:
PortalException
-
getDocument
public static Document getDocument(IFile xmlFile) throws PortalException
Returns theDocument
associated with the given file.- Parameters:
xmlFile
- the XML file.- Returns:
- the
Document
associated with the given file. - Throws:
PortalException
- Since:
- 8.0.0
-
getDocument
public static Document getDocument(HttpServletRequest request) throws Exception
- Throws:
Exception
-
getDocument
public static Document getDocument(InputStream inputStream) throws SAXException, IOException
Parses a XML document from an input stream.This method will read the input stream's data, but will not close it.
- Parameters:
inputStream
- the input stream.- Returns:
- the parsed XML document.
- Throws:
SAXException
- if any parse errors occur.IOException
- if any I/O errors occur.IllegalArgumentException
- ifinputStream
isnull
.- Since:
- 8.0.0
-
getDocument
public static Document getDocument(String xmlString) throws PortalException
- Throws:
PortalException
-
createPortalException
public static PortalException createPortalException(SAXException e)
Creates a PortalException based on the given SAXException. The PortalException created contains a message describing the error, according to the information in the SAXException.- Parameters:
e
- the SAXException- Returns:
- the PortalException created.
- Since:
- 4.0.8
-
getXmlString
public static String getXmlString(Node node) throws PortalException
Returns the xml string representation for a node.- Parameters:
node
- the node. Must be of typeDocument
,Element
,Text
orComment
.- Returns:
- the xml string.
- Throws:
PortalException
- Since:
- 4.2.0
-
getXmlString
public static String getXmlString(Element element) throws PortalException
- Throws:
PortalException
-
getXmlString
public static String getXmlString(Document document) throws Exception
- Throws:
Exception
-
readNodeBoolean
public static boolean readNodeBoolean(String xpathExpression, Node sourceNode, boolean valueForNull) throws PortalException
- Throws:
PortalException
-
readAttributeBoolean
public static boolean readAttributeBoolean(String attributeName, Node node) throws PortalException
- Throws:
PortalException
-
readAttributeBoolean
public static boolean readAttributeBoolean(String attributeName, Node node, boolean valueForNull) throws PortalException
- Throws:
PortalException
-
readNodeInt
public static int readNodeInt(String xpathExpression, Node sourceNode, int valueForNull) throws PortalException
- Throws:
PortalException
-
readNodeInteger
public static Integer readNodeInteger(String xpathExpression, Node sourceNode, Integer valueForNull) throws PortalException
- Throws:
PortalException
-
readAttributeString
public static String readAttributeString(String attributeName, Node node) throws PortalException
- Throws:
PortalException
-
readAttributeInt
public static int readAttributeInt(String attributeName, Node node, int valueForNull) throws PortalException
Returns the attribute integer value in the given node.- Parameters:
attributeName
-node
-valueForNull
-- Returns:
- Throws:
PortalException
-
readAttributeString
public static String readAttributeString(String attributeName, Node node, String valueForNull) throws PortalException
- Throws:
PortalException
-
readNodeString
public static String readNodeString(String xpathExpression, Node sourceNode) throws PortalException
- Throws:
PortalException
-
readNodeString
public static String readNodeString(String xpathExpression, Node sourceNode, String valueForNull) throws PortalException
- Throws:
PortalException
-
readNodesString
public static String[] readNodesString(String xpathExpression, Node sourceNode) throws PortalException
- Throws:
PortalException
-
nodeExists
public static boolean nodeExists(String xpathExpression, Node sourceNode) throws PortalException
- Throws:
PortalException
-
addTextNode
public static Element addTextNode(Node xmlParentNode, String newNodeName, String newNodeText) throws PortalException
- Throws:
PortalException
-
addTextNode
public static Element addTextNode(Node xmlParentNode, String xpathExpressionParentNode, String newNodeName, String newNodeText) throws PortalException
- Throws:
PortalException
-
addTextNodes
public static Node[] addTextNodes(Node xmlParentNode, String newNodesName, String[] newNodesValue) throws PortalException
- Throws:
PortalException
-
addTextNodes
public static Node[] addTextNodes(Node xmlParentNode, String xpathExpressionParentNode, String newNodesName, String[] newNodesValue) throws Exception
- Throws:
Exception
-
addNode
public static Node addNode(Node xmlParentNode, String xpathExpressionParentNode, String newNodeName) throws PortalException
- Throws:
PortalException
-
copyNode
public static Node copyNode(Node xmlNodeFrom, String xpathExpressionFrom, Node xmlNodeTo) throws Exception
- Throws:
Exception
-
copyNode
public static Node copyNode(Node xmlNodeFrom, Node xmlNodeTo, String xpathExpressionTo) throws Exception
- Throws:
Exception
-
copyNode
public static Node copyNode(Node xmlNodeFrom, String xpathExpressionFrom, Node xmlNodeTo, String xpathExpressionTo) throws Exception
- Throws:
Exception
-
getDocumentFromFile
public static Document getDocumentFromFile(String filePath) throws Exception
- Throws:
Exception
-
selectSingleAncestorNode
public static Node selectSingleAncestorNode(String ancestorNameQuery, Node childNode) throws Exception
- Throws:
Exception
-
selectAncestorNodes
public static Node[] selectAncestorNodes(String ancestorNameQuery, Node childNode) throws Exception
- Throws:
Exception
-
importIntoNewDocument
public static Node importIntoNewDocument(Node originalNode) throws PortalException
Clones a node, and adopts it into a new document.- Parameters:
originalNode
-- Returns:
- Throws:
PortalException
- Since:
- 4.0.6
-
selectSingleNode
public static Node selectSingleNode(String childNameQuery, Node parentNode) throws PortalException
- Throws:
PortalException
-
selectNodes
public static Node[] selectNodes(String childNameQuery, Node parentNode) throws PortalException
- Throws:
PortalException
-
checkCondition
public static boolean checkCondition(Node conditionNode, String condition) throws Exception
- Throws:
Exception
-
checkSingleCondition
public static boolean checkSingleCondition(Node conditionNode, String condition) throws Exception
- Throws:
Exception
-
checkCondition
protected static boolean checkCondition(String leftValue, String rightValue, String conditionOperator)
-
selectSingleNodeRecursiveByName
protected static Node selectSingleNodeRecursiveByName(String nodeName, Node parentNode) throws Exception
- Throws:
Exception
-
getXMLDocumentFromString
@Deprecated public static Document getXMLDocumentFromString(String xml) throws PortalException
Deprecated.since 4.2.0 replaced bygetDocument(String)
.- Throws:
PortalException
-
convertToUTFBytes
@Deprecated public static byte[] convertToUTFBytes(byte[] content)
Deprecated.Since 4.2.0, this method is no longer used and may be removed in the future.
-
getOrCreateNode
public static Node getOrCreateNode(String path, Node parentNode) throws PortalException
- Throws:
PortalException
-
writeNodeString
public static Node writeNodeString(String xpathExpression, Node sourceNode, String nodeValue) throws PortalException
- Throws:
PortalException
-
readAttributeOrNodeString
public static String readAttributeOrNodeString(Node node, String fieldName) throws PortalException
- Throws:
PortalException
-
readAttributeOrNodeString
public static String readAttributeOrNodeString(Node node, String fieldName, String valueForNull) throws PortalException
- Throws:
PortalException
-
replaceChildNode
public static void replaceChildNode(Node parentNode, Node newChildNode) throws Exception
- Throws:
Exception
-
replaceChildNode
public static void replaceChildNode(String childNodeName, Node node, String xmlString) throws PortalException
- Throws:
PortalException
-
addNodeFromXmlString
public static Node addNodeFromXmlString(Node node, String xmlString) throws PortalException
- Throws:
PortalException
-
mergeAttributes
public static void mergeAttributes(Node destinationNode, Node sourceNode, boolean replace) throws PortalException
- Throws:
PortalException
-
mergeElements
public static void mergeElements(Node destinationNode, Node sourceNode, boolean replace) throws PortalException
Merges the children elements from a node to another.- Parameters:
destinationNode
- the destination node, where the elements will be appended or replaced.sourceNode
- the source node, where the elements will be copied from.replace
- if true, elements with the same name will be replaced; if false they will not be copied if an element in the destinationNode with the same name already exists.- Throws:
PortalException
- Since:
- 4.0.10
-
encodeXml
public static String encodeXml(String pStr)
Encodes a string to be used inside a XML document. Special XML characters are converted to their corresponding entities. Characters that are invalid in XML (even as entity) are eliminated.- Returns:
- the encoded string, or an empty string if pStr is null.
-
getXmlEncoding
public static String getXmlEncoding(String xmlString)
Returns the XML encoding for the xml with the given String.The encoding is read from the XML declaration.
- Parameters:
xmlString
- the xml.- Returns:
- the encoding or null if it was not found.
-
isValidXmlString
public static boolean isValidXmlString(String xml)
-
-