Class XmlUtil
java.lang.Object
com.pnfsoftware.jeb.util.encoding.xml.XmlUtil
XML utility methods.
-
Field Summary
FieldsModifier and TypeFieldDescriptionThe five well-known XML entities(lt, gt, amp, apos, quot)to their String representations(<, >, &, ', "). -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanCompare a substring with an expected literal.static booleanCompare a substring with an expected literal, ignoring case.static DocumentBuilderFactorycreateSecureDocumentBuilderFactory(boolean disallowDoctypeDecl) Create a safeDocumentBuilderFactoryobject.static StringescapeAttributeValue(String s, boolean sanitizeBadChars) Escape an XLM attribute value.static StringescapeTextData(String s, boolean sanitizeBadChars) Escape XLM text data.static StringFormat a document as text.getChildrenElements(Element e, String childName) Find all direct child elements with the requested name.static ElementgetFirstChildElement(Element e, String childName) Find the first direct child element with the requested name.static booleanstatic booleanisLegalEscape(String s, Map<String, String> allowedEntities) static booleanstatic booleanisLegalXmlName(String s, int beg, int end) static booleanisSpace(int codePoint) Determine whether the provided code point is a white space character.static booleanisXmlChar(int c) static booleanisXmlNameChar(int c) static booleanisXmlNameStartChar(int c) static intreadToken(String str, int idx, Collection<Integer> stoppers, String[] atoken) Read a token.static intreadToken(String str, int idx, Collection<Integer> stoppers, String[] atoken, boolean mustBeQuoted, boolean handleBackslashAxmlStyle) Read a token.static StringSanitize a supposedly legal (escaped if necessary) XML attribute value.static StringsanitizeAttributeValue(String s, Map<String, String> allowedEntities) Sanitize a supposedly legal (escaped if necessary) XML attribute value.static StringSanitize a supposedly legal XML name (e.g.static StringSanitize a supposedly legal (escaped if necessary) XML text data.static StringsanitizeTextData(String s, Map<String, String> allowedEntities) Sanitize a supposedly legal (escaped if necessary) XML text data.static intskipSomeSpaces(String str, int idx) Skip 1+ WSP characters.static intskipSpaces(String str, int idx) Skip 0+ WSP characters.static intSkip WSP characters.static StringUnescape an escaped XML string.static StringUnescape an escaped XML string.
-
Field Details
-
PREDEFINED_ENTITIES
The five well-known XML entities(lt, gt, amp, apos, quot)to their String representations(<, >, &, ', ").
-
-
Method Details
-
createSecureDocumentBuilderFactory
public static DocumentBuilderFactory createSecureDocumentBuilderFactory(boolean disallowDoctypeDecl) throws ParserConfigurationException Create a safeDocumentBuilderFactoryobject.- Parameters:
disallowDoctypeDecl-- Returns:
- the factory object
- Throws:
ParserConfigurationException
-
isSpace
public static boolean isSpace(int codePoint) Determine whether the provided code point is a white space character.- Parameters:
codePoint- Unicode code point- Returns:
- true if the code point is XML whitespace or a Java space character
-
skipSomeSpaces
Skip 1+ WSP characters.- Parameters:
str- input stringidx- start index- Returns:
- new index after skipped whitespace
-
skipSpaces
Skip 0+ WSP characters.- Parameters:
str- input stringidx- start index- Returns:
- new index after skipped whitespace
-
skipWSP
Skip WSP characters.- Parameters:
str- stringidx- start indexminSpaceCount- the method will raise if fewer WSP chars were skipped- Returns:
- new index, after skipping WSP chars
-
readToken
Read a token.- Parameters:
str- input stringidx- token start (cannot be a wsp); may be a single-quote or double-quotestoppers- optional code points that terminate an unquoted tokenatoken- output token- Returns:
- index immediately after the token
-
readToken
public static int readToken(String str, int idx, Collection<Integer> stoppers, String[] atoken, boolean mustBeQuoted, boolean handleBackslashAxmlStyle) Read a token.- Parameters:
str- input stringidx- token start (cannot be a wsp); may be a single-quote or double-quotestoppers- optional code points that terminate an unquoted tokenatoken- output tokenmustBeQuoted- true if the token must start with a quotehandleBackslashAxmlStyle- true to decode Android resource backslash escapes- Returns:
- index immediately after the token
-
compare
Compare a substring with an expected literal.- Parameters:
str- input stringidx- substring start indexexpected- expected literal- Returns:
- true if
expectedstarts atidx
-
compareCI
Compare a substring with an expected literal, ignoring case.- Parameters:
str- input stringidx- substring start indexexpected- expected literal- Returns:
- true if
expectedstarts atidx, ignoring case
-
getFirstChildElement
Find the first direct child element with the requested name.- Parameters:
e- parent elementchildName- child tag name- Returns:
- first matching child element, or null
-
getChildrenElements
Find all direct child elements with the requested name.- Parameters:
e- parent elementchildName- child tag name- Returns:
- list of matching child elements
-
format
Format a document as text.- Parameters:
doc- input document- Returns:
- textual representation of the document
-
isXmlChar
public static boolean isXmlChar(int c) -
isXmlNameStartChar
public static boolean isXmlNameStartChar(int c) -
isXmlNameChar
public static boolean isXmlNameChar(int c) -
isLegalXmlName
-
isLegalXmlName
-
isLegalEscape
- Parameters:
s-- Returns:
-
isLegalEscape
- Parameters:
s- the entity located between & and ;allowedEntities- null means accept any syntactically valid entity; else, a set of accepted entities beyond the5 predefined entities- Returns:
-
escapeAttributeValue
Escape an XLM attribute value.- Parameters:
s- the attribute valuesanitizeBadChars- true to also sanitize bad chars (illegal XML characters will be replaced by the underscore char)- Returns:
-
escapeTextData
Escape XLM text data.- Parameters:
s- the text datasanitizeBadChars- true to also sanitize bad chars (illegal XML characters will be replaced by the underscore char)- Returns:
-
unescape
Unescape an escaped XML string. Illegal chars are replaced by underscore. Unknown entities are resolved to the empty string.- Parameters:
s- the escaped string- Returns:
-
unescape
Unescape an escaped XML string. Illegal chars are replaced by underscore.- Parameters:
s- the escaped stringallowedEntities- additional map of entities allowed (on top of the 5 standard entities); if null is passed, all non-predefined entities resolve to the empty string- Returns:
-
sanitizeName
Sanitize a supposedly legal XML name (e.g. tag or attribute name) into a legal name.- Parameters:
s-- Returns:
-
sanitizeAttributeValue
Sanitize a supposedly legal (escaped if necessary) XML attribute value.- Parameters:
s- an unsafe, supposedly escaped attribute value- Returns:
- an attribute value string that should not crash XML parsers
-
sanitizeAttributeValue
Sanitize a supposedly legal (escaped if necessary) XML attribute value.- Parameters:
s- an unsafe, supposedly escaped attribute valueallowedEntities-- Returns:
- an attribute value string that should not crash XML parsers
-
sanitizeTextData
Sanitize a supposedly legal (escaped if necessary) XML text data.- Parameters:
s- an unsafe, supposedly escaped text data- Returns:
- a text data string that should not crash XML parsers
-
sanitizeTextData
Sanitize a supposedly legal (escaped if necessary) XML text data.- Parameters:
s- an unsafe, supposedly escaped text dataallowedEntities-- Returns:
- a text data string that should not crash XML parsers
-