Modifier and Type | Field and Description |
---|---|
static String |
ATTR_SCHEMA_LOCATION |
static String |
ATTR_SCHEMA_LOCATION_LOCAL_PART |
static String |
ATTR_XSI_NAMESPACE |
static String |
QNAME_SEPARATOR |
static Pattern |
XSD_FILE_NAME_PATTERN |
Constructor and Description |
---|
XMLUtils() |
Modifier and Type | Method and Description |
---|---|
static FormatVersion |
extractFormatVersion(StartElement startElement)
Extracts the format version from the
xsi:schemaLocation attribute (e.g. |
static String |
extractXSDFileName(StartElement startElement)
Extracts the XSD file name from the
xsi:schemaLocation attribute (e.g. |
static boolean |
isCharacterType(int type) |
static boolean |
reachElementEnd(XMLEventReader reader)
Reads all events from the
XMLEventReader until one more end element than start elements is found. |
static boolean |
reachElementEnd(XMLStreamReader reader)
Reads all events from the
XMLStreamReader until one more end element than start elements is found. |
static boolean |
readBooleanAttr(StartElement element,
QName name,
boolean oldValue) |
static boolean |
readCharactersAsBoolean(XMLEventReader reader)
Reads the characters from the next event of the
XMLEventReader a parses them as a boolean value. |
static boolean |
readCharactersAsBoolean(XMLStreamReader reader)
Reads the characters from the next event of the
XMLStreamReader a parses them as a boolean value. |
static double |
readCharactersAsDouble(XMLEventReader reader)
Reads the characters from the next event of the
XMLEventReader a parses them as a double value. |
static double |
readCharactersAsDouble(XMLStreamReader reader)
Reads the characters from the next event of the
XMLStreamReader a parses them as a double value. |
static float |
readCharactersAsFloat(XMLEventReader reader)
Reads the characters from the next event of the
XMLEventReader a parses them as a float value. |
static float |
readCharactersAsFloat(XMLStreamReader reader)
Reads the characters from the next event of the
XMLStreamReader a parses them as a float value. |
static int |
readCharactersAsInt(XMLEventReader reader)
Reads the characters from the next event of the
XMLEventReader a parses them as an int value. |
static int |
readCharactersAsInt(XMLStreamReader reader)
Reads the characters from the next event of the
XMLStreamReader a parses them as a int value. |
static long |
readCharactersAsLong(XMLEventReader reader)
Reads the characters from the next event of the
XMLEventReader a parses them as an long value. |
static long |
readCharactersAsLong(XMLStreamReader reader)
Reads the characters from the next event of the
XMLStreamReader a parses them as a long value. |
static String |
readCharactersAsString(XMLEventReader reader)
Reads the characters from the next event of the
XMLEventReader . |
static String |
readCharactersAsString(XMLStreamReader reader)
Reads the characters from the next event of the
XMLStreamReader . |
static Color |
readColorAttr(StartElement element,
QName name,
Color oldValue) |
static double |
readDoubleAttr(StartElement element,
QName name,
double oldValue) |
static float |
readFloatAttr(StartElement element,
QName name,
float oldValue) |
static int |
readIntAttr(StartElement element,
QName name,
int oldValue) |
static StartElement |
readRootElement(File file) |
static StartElement |
readRootElement(InputStream stream)
Returns the root element of the XML file that can be read from the specified stream and closes it.
|
static String |
readStringAttr(StartElement element,
QName name,
String oldValue) |
static void |
writeAttribute(XMLStreamWriter writer,
QName name,
String value)
Writes an attribute with a name defined by a
QName to an XMLStreamWriter . |
static void |
writeNamespaceAttr(XMLStreamWriter writer,
String namespaceURI)
Adds the
xmlns attribute to the start element which was last written to the specified
writer. |
static void |
writeNamespaceXSDAttr(XMLStreamWriter writer,
String namespaceURI,
String xsdURI)
Adds the attributes
xmlns xmlns:xsi and xsi:schemaLocation
and the according values to the start element which was last written to the specified writer. |
static void |
writeStartElement(XMLStreamWriter writer,
QName name)
Writes a start element defined by a
QName to an XMLStreamWriter . |
public static final String QNAME_SEPARATOR
public static final String ATTR_XSI_NAMESPACE
public static final String ATTR_SCHEMA_LOCATION_LOCAL_PART
public static final String ATTR_SCHEMA_LOCATION
public static final Pattern XSD_FILE_NAME_PATTERN
public XMLUtils()
public static boolean reachElementEnd(XMLEventReader reader) throws XMLStreamException
XMLEventReader
until one more end element than start elements is found.reader
- the reader to read the events fromtrue
if anything else than ignorable whitespace is found before the next end elementXMLStreamException
public static boolean reachElementEnd(XMLStreamReader reader) throws XMLStreamException
XMLStreamReader
until one more end element than start elements is found.reader
- the reader to read the events fromtrue
if anything else than ignorable whitespace is found before the next end elementXMLStreamException
public static String readStringAttr(StartElement element, QName name, String oldValue)
public static boolean readBooleanAttr(StartElement element, QName name, boolean oldValue)
public static int readIntAttr(StartElement element, QName name, int oldValue)
public static float readFloatAttr(StartElement element, QName name, float oldValue)
public static double readDoubleAttr(StartElement element, QName name, double oldValue)
public static Color readColorAttr(StartElement element, QName name, Color oldValue)
public static boolean isCharacterType(int type)
public static String readCharactersAsString(XMLEventReader reader) throws XMLStreamException
XMLEventReader
. If the next event is not a character event an empty
string ("") is returned. All encountered comment events will be skipped.
After calling this method the next element returned by reader
will be the first non-character event after the
iterator's position before calling this method.
reader
- the reader to read the next element fromXMLStreamException
- if thrown by reader
public static String readCharactersAsString(XMLStreamReader reader) throws XMLStreamException
XMLStreamReader
. If the next event is not a character event an empty
string ("") is returned. All encountered comment events will be skipped.
After calling this method reader
will be positioned at the first non-character event after the cursor's position
before calling this method.
reader
- the reader to read the next element fromXMLStreamException
- if thrown by reader
public static boolean readCharactersAsBoolean(XMLEventReader reader) throws XMLStreamException
XMLEventReader
a parses them as a boolean
value.
After calling this method the next element returned by reader
will be the first non-character event after the
iterator's position before calling this method.
reader
- the reader to read the next element fromXMLStreamException
- if thrown by reader
public static boolean readCharactersAsBoolean(XMLStreamReader reader) throws XMLStreamException
XMLStreamReader
a parses them as a boolean
value.
After calling this method reader
will be positioned at the first non-character event after the cursor's position
before calling this method.
reader
- the reader to read the next element fromXMLStreamException
- if thrown by reader
public static int readCharactersAsInt(XMLEventReader reader) throws XMLStreamException
XMLEventReader
a parses them as an int
value.
After calling this method the next element returned by reader
will be the first non-character event after the
iterator's position before calling this method.
reader
- the reader to read the next element fromXMLStreamException
- if thrown by reader
NumberFormatException
- if no int
can be parsed from the current characterspublic static int readCharactersAsInt(XMLStreamReader reader) throws XMLStreamException
XMLStreamReader
a parses them as a int
value.
After calling this method reader
will be positioned at the first non-character event after the cursor's position
before calling this method.
reader
- the reader to read the next element fromXMLStreamException
- if thrown by reader
NumberFormatException
- if no int
can be parsed from the current characterspublic static long readCharactersAsLong(XMLEventReader reader) throws XMLStreamException
XMLEventReader
a parses them as an long
value.
After calling this method the next element returned by reader
will be the first non-character event after the
iterator's position before calling this method.
reader
- the reader to read the next element fromXMLStreamException
- if thrown by reader
NumberFormatException
- if no long
can be parsed from the current characterspublic static long readCharactersAsLong(XMLStreamReader reader) throws XMLStreamException
XMLStreamReader
a parses them as a long
value.
After calling this method reader
will be positioned at the first non-character event after the cursor's position
before calling this method.
reader
- the reader to read the next element fromXMLStreamException
- if thrown by reader
NumberFormatException
- if no long
can be parsed from the current characterspublic static float readCharactersAsFloat(XMLEventReader reader) throws XMLStreamException
XMLEventReader
a parses them as a float
value.
After calling this method the next element returned by reader
will be the first non-character event after the
iterator's position before calling this method.
reader
- the reader to read the next element fromXMLStreamException
- if thrown by reader
NumberFormatException
- if no float
can be parsed from the current characterspublic static float readCharactersAsFloat(XMLStreamReader reader) throws XMLStreamException
XMLStreamReader
a parses them as a float
value.
After calling this method reader
will be positioned at the first non-character event after the cursor's position
before calling this method.
reader
- the reader to read the next element fromXMLStreamException
- if thrown by reader
NumberFormatException
- if no float
can be parsed from the current characterspublic static double readCharactersAsDouble(XMLEventReader reader) throws XMLStreamException
XMLEventReader
a parses them as a double
value.
After calling this method the next element returned by reader
will be the first non-character event after the
iterator's position before calling this method.
reader
- the reader to read the next element fromXMLStreamException
- if thrown by reader
NumberFormatException
- if no double
can be parsed from the current characterspublic static double readCharactersAsDouble(XMLStreamReader reader) throws XMLStreamException
XMLStreamReader
a parses them as a double
value.
After calling this method reader
will be positioned at the first non-character event after the cursor's position
before calling this method.
reader
- the reader to read the next element fromXMLStreamException
- if thrown by reader
NumberFormatException
- if no double
can be parsed from the current characterspublic static StartElement readRootElement(InputStream stream) throws IOException, XMLStreamException
stream
- the stream to read from (closed after the execution of this method)IOException
- if the stream cannot be closedXMLStreamException
- if the XML format is invalidpublic static StartElement readRootElement(File file) throws IOException, XMLStreamException
IOException
XMLStreamException
public static void writeNamespaceAttr(XMLStreamWriter writer, String namespaceURI) throws XMLStreamException
xmlns
attribute to the start element which was last written to the specified
writer.writer
- the writer which generates the XML outputnamespaceURI
- the URI of the namespace (e.g. http://www.w3.org/1999/xhtml)XMLStreamException
- if fired by writer
public static void writeNamespaceXSDAttr(XMLStreamWriter writer, String namespaceURI, String xsdURI) throws XMLStreamException
xmlns
xmlns:xsi
and xsi:schemaLocation
and the according values to the start element which was last written to the specified writer.writer
- the writer which generates the XML outputnamespaceURI
- the URI of the namespace (e.g. http://www.w3.org/1999/xhtml)xsdURI
- the URI which specifies the XML schema (xsd) which defines the XML code under the
last start elementXMLStreamException
- if fired by writer
public static String extractXSDFileName(StartElement startElement) throws InvalidXSDPathException
xsi:schemaLocation
attribute (e.g. 1.1
from
http://bioinfweb.info/xmlns/xtg http://bioinfweb.info/xmlns/xtg/1.1.xsd
).startElement
- the element containing the xsi:schemaLocation
attributenull
if startElement
has no
xsi:schemaLocation
attributeInvalidXSDPathException
- if the path to the XSD file in malformedpublic static FormatVersion extractFormatVersion(StartElement startElement) throws InvalidXSDPathException
xsi:schemaLocation
attribute (e.g. 1.1
from
http://bioinfweb.info/xmlns/xtg http://bioinfweb.info/xmlns/xtg/1.1.xsd
). The name of the XSD
file has to consist of the format version where major and minor version are separated by a dot.startElement
- the element containing the xsi:schemaLocation
attributenull
if startElement
has no
xsi:schemaLocation
attributeInvalidXSDPathException
- if the path to the XSD file in malformedpublic static void writeStartElement(XMLStreamWriter writer, QName name) throws XMLStreamException
QName
to an XMLStreamWriter
.writer
- the writer to write the start tagname
- the name of the tagXMLStreamException
- thrown by the specified writerpublic static void writeAttribute(XMLStreamWriter writer, QName name, String value) throws XMLStreamException, IllegalStateException
QName
to an XMLStreamWriter
.writer
- the writer to write the start tagname
- the name of the tagvalue
- the value of the attributeIllegalStateException
- if the current state does not allow attribute writingXMLStreamException
- if the namespace URI has not been bound to a prefix and javax.xml.stream.isRepairingNamespaces
has not been set to true