Package io.permazen.util
Class AbstractXMLStreaming
java.lang.Object
io.permazen.util.AbstractXMLStreaming
- Direct Known Subclasses:
SchemaItem
,SchemaModel
,XMLObjectSerializer
,XMLSerializer
Support superclass for classes that serialize and deserialize via XML.
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
expect
(XMLStreamReader reader, boolean closingOK, QName... names) Scan forward until we see an opening or closing tag.protected void
expectClose
(XMLStreamReader reader) Scan forward expecting to see a closing tag.protected String
getAttr
(XMLStreamReader reader, QName name) Get a requried attribute from the current element.protected String
getAttr
(XMLStreamReader reader, QName name, boolean required) Get an attribute from the current element.protected boolean
getBooleanAttr
(XMLStreamReader reader, QName name) Get a requried boolean attribute from the current element.protected Boolean
getBooleanAttr
(XMLStreamReader reader, QName name, boolean required) Get an attribute from the current element and parse as a boolean value.protected int
getIntAttr
(XMLStreamReader reader, QName name) Get a requried integer attribute from the current element.protected Integer
getIntAttr
(XMLStreamReader reader, QName name, boolean required) Get an attribute from the current element and parse as a decimal integer value.protected Long
getLongAttr
(XMLStreamReader reader, QName name, boolean required) Get an attribute from the current element and parse as a decimal long value.protected XMLStreamException
newInvalidAttributeException
(XMLStreamReader reader, QName name, String description, Throwable... cause) Build aXMLStreamException
caused by invalid content in an attribute.protected XMLStreamException
newInvalidInputException
(XMLStreamReader reader, String format, Object... args) Build aXMLStreamException
caused by invalid input.protected XMLStreamException
newInvalidInputException
(XMLStreamReader reader, Throwable cause, String format, Object... args) Build aXMLStreamException
caused by invalid input.protected QName
next
(XMLStreamReader reader) Skip forward until either the next opening tag is reached, or the currently open tag is closed.protected void
skip
(XMLStreamReader reader) Skip over the remainder of the current XML element, including any nested elements, until the closing XML tag is seen and consumed.protected void
writeAttr
(XMLStreamWriter writer, QName name, Object value) Write out an attribute, and also verify that no illegal characters are written.protected void
writeCData
(XMLStreamWriter writer, String text) Write out CDATA text with proper escaping of nested]]>
sequences, and also verify that no illegal characters are written.protected void
writeCharacters
(XMLStreamWriter writer, String text) Write out text, and also verify that no illegal characters are written.protected void
writeElement
(XMLStreamWriter writer, QName name, String content) Write out a simple XML element containing the given content.protected void
writeEmptyElement
(XMLStreamWriter writer, QName name) Start an empty XML element.protected void
writeStartElement
(XMLStreamWriter writer, QName name) Start a non-empty XML element.
-
Field Details
-
log
-
-
Constructor Details
-
AbstractXMLStreaming
protected AbstractXMLStreaming()
-
-
Method Details
-
expect
protected boolean expect(XMLStreamReader reader, boolean closingOK, QName... names) throws XMLStreamException Scan forward until we see an opening or closing tag. If opening tag is seen, it must match one ofnames
and then we return true, if not or ifnames
is empty throw an exception; if a closing tag is seen, return false ifclosingOK
, else throw exception.- Parameters:
reader
- XML inputclosingOK
- true if a closing tag is OK, otherwise falsenames
- expected opening tag, or null if we expected a closing tag- Returns:
- true if matching opening tag seen, false otherwise
- Throws:
XMLStreamException
- if something unexpected is encountered
-
next
Skip forward until either the next opening tag is reached, or the currently open tag is closed.- Parameters:
reader
- XML input- Returns:
- the XML opening tag found, or null if a closing tag was seen first
- Throws:
XMLStreamException
- if no opening tag is found before the current tag closesXMLStreamException
- if something unexpected is encountered
-
skip
Skip over the remainder of the current XML element, including any nested elements, until the closing XML tag is seen and consumed.- Parameters:
reader
- XML input- Throws:
XMLStreamException
- if something unexpected is encountered
-
expectClose
Scan forward expecting to see a closing tag.Equivalant to:
expect
(reader, true)
.- Parameters:
reader
- XML input- Throws:
XMLStreamException
- if something other than a closing tag is encountered
-
getAttr
protected String getAttr(XMLStreamReader reader, QName name, boolean required) throws XMLStreamException Get an attribute from the current element.- Parameters:
reader
- XML inputname
- attribute namerequired
- whether attribute must be present- Returns:
- attribute value, or null if not
required
and no attribute is present - Throws:
IllegalStateException
- if the current event is not a start element eventXMLStreamException
- ifrequired
is true and no such attribute is found
-
getAttr
Get a requried attribute from the current element. Equivalent to:getAttr(reader, name, true)
.- Parameters:
reader
- XML inputname
- attribute name- Returns:
- attribute value
- Throws:
IllegalStateException
- if the current event is not a start element eventXMLStreamException
- if no such attribute is found
-
getIntAttr
protected Integer getIntAttr(XMLStreamReader reader, QName name, boolean required) throws XMLStreamException Get an attribute from the current element and parse as a decimal integer value.- Parameters:
reader
- XML inputname
- attribute namerequired
- whether attribute must be present- Returns:
- attribute value, or null if not
required
and no attribute is present - Throws:
IllegalStateException
- if the current event is not a start element eventXMLStreamException
- ifrequired
is true and no such attribute is foundXMLStreamException
- if attribute is not an integer value
-
getLongAttr
protected Long getLongAttr(XMLStreamReader reader, QName name, boolean required) throws XMLStreamException Get an attribute from the current element and parse as a decimal long value.- Parameters:
reader
- XML inputname
- attribute namerequired
- whether attribute must be present- Returns:
- attribute value, or null if not
required
and no attribute is present - Throws:
IllegalStateException
- if the current event is not a start element eventXMLStreamException
- ifrequired
is true and no such attribute is foundXMLStreamException
- if attribute is not an integer value
-
getIntAttr
Get a requried integer attribute from the current element. Equivalent to:getIntAttr(reader, name, true)
.- Parameters:
reader
- XML inputname
- attribute name- Returns:
- attribute value
- Throws:
IllegalStateException
- if the current event is not a start element eventXMLStreamException
- if no such attribute is foundXMLStreamException
- if attribute is not an integer value
-
getBooleanAttr
protected Boolean getBooleanAttr(XMLStreamReader reader, QName name, boolean required) throws XMLStreamException Get an attribute from the current element and parse as a boolean value.- Parameters:
reader
- XML inputname
- attribute namerequired
- whether attribute must be present- Returns:
- attribute value, or null if not
required
and no attribute is present - Throws:
IllegalStateException
- if the current event is not a start element eventXMLStreamException
- ifrequired
is true and no such attribute is foundXMLStreamException
- if attribute is not"true"
or"false"
-
getBooleanAttr
Get a requried boolean attribute from the current element. Equivalent to:getBooleanAttr(reader, name, true)
.- Parameters:
reader
- XML inputname
- attribute name- Returns:
- attribute value
- Throws:
IllegalStateException
- if the current event is not a start element eventXMLStreamException
- if no such attribute is foundXMLStreamException
- if attribute is not"true"
or"false"
-
newInvalidAttributeException
protected XMLStreamException newInvalidAttributeException(XMLStreamReader reader, QName name, String description, Throwable... cause) throws XMLStreamException Build aXMLStreamException
caused by invalid content in an attribute.- Parameters:
reader
- XML inputname
- attribute namedescription
- a description of the problemcause
- optional underlying exception for chaining- Returns:
- exception to throw
- Throws:
IllegalArgumentException
- if more than onecause
is givenXMLStreamException
- if error occurs accessing the attribute value
-
newInvalidInputException
protected XMLStreamException newInvalidInputException(XMLStreamReader reader, String format, Object... args) Build aXMLStreamException
caused by invalid input.- Parameters:
reader
- XML inputformat
- format string forString.format()
args
- format arguments forString.format()
- Returns:
- exception to throw
-
newInvalidInputException
protected XMLStreamException newInvalidInputException(XMLStreamReader reader, Throwable cause, String format, Object... args) Build aXMLStreamException
caused by invalid input.This method exists to help workaround JDK-8322027.
- Parameters:
reader
- XML inputcause
- optional underlying exception for chaining, or null for noneformat
- format string forString.format()
args
- format arguments forString.format()
- Returns:
- exception to throw
-
writeElement
protected void writeElement(XMLStreamWriter writer, QName name, String content) throws XMLStreamException Write out a simple XML element containing the given content.- Parameters:
writer
- XML outputname
- element namecontent
- simple content- Throws:
XMLStreamException
- if error occurs writing outputIllegalArgumentException
- if any parameter is nullIllegalArgumentException
- ifname
is invalid
-
writeEmptyElement
Start an empty XML element.- Parameters:
writer
- XML outputname
- element name- Throws:
XMLStreamException
- if error occurs writing outputIllegalArgumentException
- if any parameter is nullIllegalArgumentException
- ifname
is invalid
-
writeStartElement
Start a non-empty XML element.- Parameters:
writer
- XML outputname
- element name- Throws:
XMLStreamException
- if error occurs writing outputIllegalArgumentException
- if any parameter is nullIllegalArgumentException
- ifname
is invalid
-
writeAttr
protected void writeAttr(XMLStreamWriter writer, QName name, Object value) throws XMLStreamException Write out an attribute, and also verify that no illegal characters are written.- Parameters:
writer
- XML outputname
- attribute qualified namevalue
- attribute value- Throws:
XMLStreamException
- if error occurs writing outputIllegalArgumentException
- if any parameter is nullIllegalArgumentException
- ifname
is invalidIllegalArgumentException
- if theString
value ofvalue
contains an illegal character
-
writeCharacters
Write out text, and also verify that no illegal characters are written.- Parameters:
writer
- XML outputtext
- text to output- Throws:
XMLStreamException
- if error occurs writing outputIllegalArgumentException
- if any parameter is nullIllegalArgumentException
- iftext
contains an illegal character
-
writeCData
Write out CDATA text with proper escaping of nested]]>
sequences, and also verify that no illegal characters are written.Note: The method
XMLStreamWriter.writeCData(java.lang.String)
does not escape nested]]>
sequences. This method handles them.- Parameters:
writer
- XML outputtext
- text to output- Throws:
XMLStreamException
- if error occurs writing outputIllegalArgumentException
- if any parameter is nullIllegalArgumentException
- iftext
contains an illegal character
-