Package org.hl7.fhir.utilities.xml
Class XMLUtil
- java.lang.Object
-
- org.hl7.fhir.utilities.xml.XMLUtil
-
-
Field Summary
Fields Modifier and Type Field Description static StringAPACHE_XML_FEATURES_DISALLOW_DOCTYPE_DECLstatic StringSAX_FEATURES_EXTERNAL_GENERAL_ENTITIESstatic StringSPACE_CHAR
-
Constructor Summary
Constructors Constructor Description XMLUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ElementaddChild(Document doc, Element element, String name, String namespace, int indent)static ElementaddTextTag(Document doc, Element element, String name, String text, int indent)static voidaddTextTag(Document doc, Element element, String name, String namespace, String text, int indent)static booleancharSetImpliesAscii(String charset)static Node[]children(Element ed)static voidclearChildren(Node node)static voiddeleteAttr(Element e, String namespaceURI, String localName)static voiddeleteByName(Element e, String name)static StringdetermineEncoding(InputStream stream)static StringelementToString(Element el)static StringescapeXML(String rawContent, String charset, boolean isNoLines)Converts the raw characters to XML escape characters.static StringgetDirectText(Element node)static ElementgetFirstChild(Element e)static ElementgetFirstChild(Element res, String... names)static ElementgetLastChild(Element e)static ElementgetLastChild(Element res, String... names)static ElementgetNamedChild(Element e, String name)static StringgetNamedChildAttribute(Element element, String name, String aname)static ElementgetNamedChildByAttribute(Element e, String name, String nname, String nvalue)static List<Element>getNamedChildren(Element e, String name)static voidgetNamedChildren(Element e, String name, List<Element> set)static voidgetNamedChildrenWithTails(Element focus, String name, List<Element> children, Set<String> typeTails)static voidgetNamedChildrenWithWildcard(Element focus, String name, List<Element> children)static StringgetNamedChildText(Element element, String name)static StringgetNamedChildValue(Element element, String name)static ElementgetNextSibling(Element e)static ElementgetPrevSibling(Element e)static StringgetXsiType(Element element)static XMLReadergetXXEProtectedXMLReader(SAXParserFactory spf)This method is used to create a new XMLReader instance from a passed SAXParserFactory with external processing features configured securely.static booleanhasNamedChild(Element e, String name)static StringhtmlToXmlEscapedPlainText(String definition)static StringhtmlToXmlEscapedPlainText(Element r)static ElementinsertChild(Document doc, Element element, String name, String namespace, int indent)static ElementinsertChild(Document doc, Element element, String name, String namespace, Node before, int indent)static booleanisNMToken(String name)static booleanisNMTokenChar(char c)static DocumentBuilderFactorynewXXEProtectedDocumentBuilderFactory()This method is used to create a new DocumentBuilderFactory instance with external processing features configured securely.static SAXParserFactorynewXXEProtectedSaxParserFactory()This method is used to create a new SAXParserFactory instance with external processing features configured securely.static TransformerFactorynewXXEProtectedTransformerFactory()This method is used to create a new TransformerFactory instance with external processing features configured securely.static DocumentparseFileToDom(String filename)static DocumentparseFileToDom(String filename, boolean ns)static DocumentparseToDom(byte[] content)static DocumentparseToDom(byte[] content, boolean ns)static DocumentparseToDom(String content)static voidsaveToFile(Element root, OutputStream stream)static voidsetNamedChildValue(Element element, String name, String value)static voidspacer(Document doc, Element element, int indent)static voidwriteDomToFile(Document doc, String filename)
-
-
-
Field Detail
-
SPACE_CHAR
public static final String SPACE_CHAR
- See Also:
- Constant Field Values
-
SAX_FEATURES_EXTERNAL_GENERAL_ENTITIES
public static final String SAX_FEATURES_EXTERNAL_GENERAL_ENTITIES
- See Also:
- Constant Field Values
-
APACHE_XML_FEATURES_DISALLOW_DOCTYPE_DECL
public static final String APACHE_XML_FEATURES_DISALLOW_DOCTYPE_DECL
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
XMLUtil
public XMLUtil()
-
-
Method Detail
-
isNMTokenChar
public static boolean isNMTokenChar(char c)
-
determineEncoding
public static String determineEncoding(InputStream stream) throws IOException
- Throws:
IOException
-
charSetImpliesAscii
public static boolean charSetImpliesAscii(String charset)
-
escapeXML
public static String escapeXML(String rawContent, String charset, boolean isNoLines)
Converts the raw characters to XML escape characters.- Parameters:
rawContent-charset- Null when charset is not known, so we assume it's unicodeisNoLines-- Returns:
- escape string
-
getFirstChild
public static Element getFirstChild(Element e)
-
getNamedChild
public static Element getNamedChild(Element e, String name)
-
getNamedChildByAttribute
public static Element getNamedChildByAttribute(Element e, String name, String nname, String nvalue)
-
getNextSibling
public static Element getNextSibling(Element e)
-
getNamedChildren
public static void getNamedChildren(Element e, String name, List<Element> set)
-
getNamedChildren
public static List<Element> getNamedChildren(Element e, String name)
-
htmlToXmlEscapedPlainText
public static String htmlToXmlEscapedPlainText(Element r)
-
htmlToXmlEscapedPlainText
public static String htmlToXmlEscapedPlainText(String definition) throws ParserConfigurationException, SAXException, IOException
-
elementToString
public static String elementToString(Element el)
-
getNamedChildValue
public static String getNamedChildValue(Element element, String name)
-
setNamedChildValue
public static void setNamedChildValue(Element element, String name, String value) throws FHIRException
- Throws:
FHIRException
-
getNamedChildrenWithWildcard
public static void getNamedChildrenWithWildcard(Element focus, String name, List<Element> children)
-
getNamedChildrenWithTails
public static void getNamedChildrenWithTails(Element focus, String name, List<Element> children, Set<String> typeTails)
-
hasNamedChild
public static boolean hasNamedChild(Element e, String name)
-
parseToDom
public static Document parseToDom(String content) throws ParserConfigurationException, SAXException, IOException
-
parseToDom
public static Document parseToDom(byte[] content) throws ParserConfigurationException, SAXException, IOException
-
parseToDom
public static Document parseToDom(byte[] content, boolean ns) throws ParserConfigurationException, SAXException, IOException
-
parseFileToDom
public static Document parseFileToDom(String filename) throws ParserConfigurationException, SAXException, IOException
-
parseFileToDom
public static Document parseFileToDom(String filename, boolean ns) throws ParserConfigurationException, SAXException, IOException
-
getLastChild
public static Element getLastChild(Element e)
-
getPrevSibling
public static Element getPrevSibling(Element e)
-
getNamedChildAttribute
public static String getNamedChildAttribute(Element element, String name, String aname)
-
newXXEProtectedTransformerFactory
public static TransformerFactory newXXEProtectedTransformerFactory()
This method is used to create a new TransformerFactory instance with external processing features configured securely. IMPORTANT This method should be the only place where TransformerFactory is instantiated in this project.- Returns:
- A TransformerFactory instance external processing features configured securely.
-
newXXEProtectedDocumentBuilderFactory
public static DocumentBuilderFactory newXXEProtectedDocumentBuilderFactory() throws ParserConfigurationException
This method is used to create a new DocumentBuilderFactory instance with external processing features configured securely. IMPORTANT This method should be the only place where DocumentBuilderFactory is instantiated in this project.- Returns:
- A DocumentBuilderFactory instance external processing features configured securely.
- Throws:
ParserConfigurationException- If a DocumentBuilder cannot be configured with the requested features.
-
newXXEProtectedSaxParserFactory
public static SAXParserFactory newXXEProtectedSaxParserFactory() throws SAXNotSupportedException, SAXNotRecognizedException, ParserConfigurationException
This method is used to create a new SAXParserFactory instance with external processing features configured securely. IMPORTANT This method should be the only place where SAXParserFactory is instantiated in this project.- Returns:
- A SAXParserFactory instance external processing features configured securely.
- Throws:
SAXNotSupportedException- When the underlying XMLReader recognizes the property name but doesn't support the property.SAXNotRecognizedException- When the underlying XMLReader does not recognize the property name.ParserConfigurationException- If a SAXParser cannot be configured with the requested features.
-
getXXEProtectedXMLReader
public static XMLReader getXXEProtectedXMLReader(SAXParserFactory spf) throws ParserConfigurationException, SAXException
This method is used to create a new XMLReader instance from a passed SAXParserFactory with external processing features configured securely.- Parameters:
spf- The SAXParserFactory to create the XMLReader from.- Returns:
- A XMLReader instance external processing features configured securely.
- Throws:
ParserConfigurationException- If a SAXParser cannot be configured with the requested features.SAXException- If any SAX exceptions occur during the creation of the XMLReader.
-
writeDomToFile
public static void writeDomToFile(Document doc, String filename) throws TransformerException, IOException
- Throws:
TransformerExceptionIOException
-
getXsiType
public static String getXsiType(Element element)
-
getDirectText
public static String getDirectText(Element node)
-
deleteByName
public static void deleteByName(Element e, String name)
-
deleteAttr
public static void deleteAttr(Element e, String namespaceURI, String localName)
-
addChild
public static Element addChild(Document doc, Element element, String name, String namespace, int indent)
-
insertChild
public static Element insertChild(Document doc, Element element, String name, String namespace, int indent)
-
insertChild
public static Element insertChild(Document doc, Element element, String name, String namespace, Node before, int indent)
-
addTextTag
public static void addTextTag(Document doc, Element element, String name, String namespace, String text, int indent)
-
addTextTag
public static Element addTextTag(Document doc, Element element, String name, String text, int indent)
-
saveToFile
public static void saveToFile(Element root, OutputStream stream) throws TransformerException
- Throws:
TransformerException
-
getNamedChildText
public static String getNamedChildText(Element element, String name)
-
getFirstChild
public static Element getFirstChild(Element res, String... names)
-
getLastChild
public static Element getLastChild(Element res, String... names)
-
clearChildren
public static void clearChildren(Node node)
-
-