Package com.norconex.commons.lang.xml
Class EnhancedXmlStreamWriter
java.lang.Object
com.norconex.commons.lang.xml.EnhancedXmlStreamWriter
- All Implemented Interfaces:
XMLStreamWriter
A version of XMLStreamWriter that adds convenience methods
for adding simple elements and typed attributes, as well as offering
pretty-printing. Can be used on its own with a Writer,
or as a wrapper to an existing XMLStreamWriter instance.
Also support XML with no root.
Since 2.0.0 checked exceptions are wrapped in an XmlException.
- Since:
- 1.5.0
-
Constructor Summary
ConstructorsConstructorDescriptionEnhancedXmlStreamWriter(Writer writer) EnhancedXmlStreamWriter(Writer writer, boolean writeBlanks) Creates a new xml stream writer.EnhancedXmlStreamWriter(Writer writer, boolean writeBlanks, int indent) Creates a new xml stream writer. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()voidflush()getProperty(String name) Gets the underlying writer, after flushing this XML stream writer.voidvoidsetNamespaceContext(NamespaceContext context) voidvoidwriteAttribute(String localName, String value) voidwriteAttribute(String namespaceURI, String localName, String value) voidwriteAttribute(String prefix, String namespaceURI, String localName, String value) voidwriteAttributeBoolean(String localName, Boolean value) voidwriteAttributeBoolean(String localName, Boolean value, boolean writeBlanks) Writes a Boolean attribute.voidwriteAttributeClass(Class<?> value) Write a "class" attribute with the value obtained from getting it by invokingClass.getCanonicalName().voidwriteAttributeClass(Class<?> value, boolean writeBlanks) Write a "class" attribute with the value obtained from getting it by invokingClass.getCanonicalName().voidwriteAttributeClass(String localName, Class<?> value) voidwriteAttributeClass(String localName, Class<?> value, boolean writeBlanks) Writes an attribute containing a class name, getting it by invokingClass.getCanonicalName().voidwriteAttributeDelimited(String localName, List<?> values) Writes an attribute by converting a list into a comma-separated string.voidwriteAttributeDelimited(String localName, List<?> values, String delimiter) Writes an attribute by converting a list into a comma-separated string.voidWrite a "disabled" attribute set totrue.voidwriteAttributeDisabled(boolean value) Write a "disabled" attribute.voidwriteAttributeDisabled(Boolean value, boolean writeBlanks) Write a "disabled" attribute.voidwriteAttributeDouble(String localName, Double value) voidwriteAttributeDouble(String localName, Double value, boolean writeBlanks) Writes a Double attribute.voidwriteAttributeFloat(String localName, Float value) voidwriteAttributeFloat(String localName, Float value, boolean writeBlanks) Writes a Float attribute.voidwriteAttributeInteger(String localName, Integer value) voidwriteAttributeInteger(String localName, Integer value, boolean writeBlanks) Writes a Integer attribute.voidwriteAttributeLong(String localName, Long value) voidwriteAttributeLong(String localName, Long value, boolean writeBlanks) Writes a Long attribute.voidwriteAttributeObject(String localName, Object value) Writes an attribute object by first converting it to string using its "toString()" method.voidwriteAttributeObject(String localName, Object value, boolean writeBlanks) Writes an attribute object by first converting it to string using its "toString()" method.voidwriteAttributeString(String localName, String value) voidwriteAttributeString(String localName, String value, boolean writeBlanks) Writes a String attribute.voidwriteCData(String data) voidwriteCharacters(char[] text, int start, int len) voidwriteCharacters(String text) voidwriteComment(String data) voidwriteDefaultNamespace(String namespaceURI) voidvoidwriteElementBoolean(String localName, Boolean value) voidwriteElementBoolean(String localName, Boolean value, boolean writeBlanks) Writes a simple Boolean element.voidwriteElementClass(String localName, Class<?> value) voidwriteElementClass(String localName, Class<?> value, boolean writeBlanks) Writes a simple element containing a class name, getting it by invokingClass.getCanonicalName().voidwriteElementDelimited(String localName, List<?> values) Writes an element by converting a list into a comma-separated string.voidwriteElementDelimited(String localName, List<?> values, String delimiter) Writes an element by converting a list into a comma-separated string.voidwriteElementDimension(String localName, Dimension value) Writes a simple element containing a Dimension.voidwriteElementDimension(String localName, Dimension value, boolean writeBlanks) Writes a simple element containing a Dimension.voidwriteElementDisabled(String localName) Writes an empty element with a "disabled" attribute set totrue.voidwriteElementDisabled(String localName, Class<?> clazz) Writes an empty element with a "disabled" attribute set totrueand a "class" attribute matching the class name.voidwriteElementDouble(String localName, Double value) voidwriteElementDouble(String localName, Double value, boolean writeBlanks) Writes a simple Double element.voidwriteElementFile(String localName, File value) Writes aFileelement.voidwriteElementFile(String localName, File value, boolean writeBlanks) Writes aFileelement.voidwriteElementFloat(String localName, Float value) voidwriteElementFloat(String localName, Float value, boolean writeBlanks) Writes a simple Float element.voidwriteElementInteger(String localName, Integer value) voidwriteElementInteger(String localName, Integer value, boolean writeBlanks) Writes a simple Integer element.voidwriteElementLong(String localName, Long value) voidwriteElementLong(String localName, Long value, boolean writeBlanks) Writes a simple Long element.voidwriteElementObject(String localName, Object value) Writes an element object as string.voidwriteElementObject(String localName, Object value, boolean writeBlanks) Writes an element object as string.voidwriteElementObjectList(String parentLocalName, String localName, List<?> values) Writes a list of objects.voidwriteElementObjectList(String parentLocalName, String localName, List<?> values, boolean writeBlanks) Writes a list of objects.voidwriteElementString(String localName, String value) voidwriteElementString(String localName, String value, boolean writeBlanks) Writes a simple string element.voidwriteEmptyElement(String localName) voidwriteEmptyElement(String namespaceURI, String localName) voidwriteEmptyElement(String prefix, String localName, String namespaceURI) voidvoidvoidwriteEntityRef(String name) voidwriteNamespace(String prefix, String namespaceURI) voidwriteObject(String localName, Object value) Writes an object.voidwriteObject(String localName, Object value, boolean disabled) Writes an object.voidwriteObjectList(String parentLocalName, String localName, List<? extends Object> values) Writes a list of objects.voidwriteProcessingInstruction(String target) voidwriteProcessingInstruction(String target, String data) voidvoidwriteStartDocument(String version) voidwriteStartDocument(String encoding, String version) voidwriteStartElement(String localName) voidwriteStartElement(String localName, Class<?> clazz) Writes a start element of the supplied name, with a "class" attribute for the given class.voidwriteStartElement(String localName, Class<?> clazz, boolean disabled) Writes a start element of the supplied name, with a "class" attribute for the given class, and a "disabled" attribute iftrue.voidwriteStartElement(String namespaceURI, String localName) voidwriteStartElement(String prefix, String localName, String namespaceURI)
-
Constructor Details
-
EnhancedXmlStreamWriter
-
EnhancedXmlStreamWriter
Creates a new xml stream writer.- Parameters:
writer- writer used to write XMLwriteBlanks-trueto write attributes/elements with no values when invoking methods without the "writeBlanks" argument. This sets the default behavior which can be overwritten using methods with "writeBlanks" argument.
-
EnhancedXmlStreamWriter
Creates a new xml stream writer.- Parameters:
writer- writer used to write XMLwriteBlanks-trueto write attributes/elements with no values when invoking methods without the "writeBlanks" argument. This sets the default behavior which can be overwritten using methods with "writeBlanks" argument.indent- how many spaces to use for indentation (-1=no indent; 0=newline only; 1+=number of spaces after newline)- Since:
- 1.13.0
-
-
Method Details
-
getWriter
Gets the underlying writer, after flushing this XML stream writer.- Returns:
- the writer
- Since:
- 2.0.0
-
writeAttributeInteger
Writes a Integer attribute.- Parameters:
localName- attribute namevalue- Integer attribute valuewriteBlanks- whether a blank value should be written as an empty attribute.- Since:
- 1.14.0
-
writeAttributeInteger
-
writeAttributeLong
Writes a Long attribute.- Parameters:
localName- attribute namevalue- Long attribute valuewriteBlanks- whether a blank value should be written as an empty attribute.- Since:
- 1.14.0
-
writeAttributeLong
-
writeAttributeFloat
Writes a Float attribute.- Parameters:
localName- attribute namevalue- Float attribute valuewriteBlanks- whether a blank value should be written as an empty attribute.- Since:
- 1.14.0
-
writeAttributeFloat
-
writeAttributeDouble
Writes a Double attribute.- Parameters:
localName- attribute namevalue- Double attribute valuewriteBlanks- whether a blank value should be written as an empty attribute.- Since:
- 1.14.0
-
writeAttributeDouble
-
writeAttributeBoolean
Writes a Boolean attribute.- Parameters:
localName- attribute namevalue- Boolean attribute valuewriteBlanks- whether a blank value should be written as an empty attribute.- Since:
- 1.14.0
-
writeAttributeBoolean
-
writeAttributeString
Writes a String attribute.- Parameters:
localName- attribute namevalue- String attribute valuewriteBlanks- whether a blank value should be written as an empty attribute.- Since:
- 1.14.0
-
writeAttributeString
-
writeAttributeClass
Write a "class" attribute with the value obtained from getting it by invokingClass.getCanonicalName().- Parameters:
value- the class to writewriteBlanks- whether a blank value should be written as an empty attribute.- Since:
- 2.0.0
-
writeAttributeClass
Write a "class" attribute with the value obtained from getting it by invokingClass.getCanonicalName().- Parameters:
value- the class to write- Since:
- 2.0.0
-
writeAttributeClass
Writes an attribute containing a class name, getting it by invokingClass.getCanonicalName().- Parameters:
localName- attribute namevalue- Class attribute valuewriteBlanks- whether a blank value should be written as an empty attribute.- Since:
- 1.14.0
-
writeAttributeClass
-
writeAttributeDisabled
Write a "disabled" attribute.- Parameters:
value-trueorfalsewriteBlanks- whether a blank value should be written as an empty attribute.- Since:
- 2.0.0
-
writeAttributeDisabled
public void writeAttributeDisabled(boolean value) Write a "disabled" attribute.- Parameters:
value-trueorfalse- Since:
- 2.0.0
-
writeAttributeDisabled
public void writeAttributeDisabled()Write a "disabled" attribute set totrue.- Since:
- 2.0.0
-
writeAttributeObject
Writes an attribute object by first converting it to string using its "toString()" method.- Parameters:
localName- attribute namevalue- attribute value- Since:
- 1.14.0
-
writeAttributeObject
Writes an attribute object by first converting it to string using its "toString()" method.- Parameters:
localName- attribute namevalue- attribute valuewriteBlanks- whether a blank value should be written as an empty attribute.- Since:
- 1.14.0
-
writeAttributeDelimited
Writes an attribute by converting a list into a comma-separated string.- Parameters:
localName- attribute namevalues- the values to write- Since:
- 2.0.0
-
writeAttributeDelimited
Writes an attribute by converting a list into a comma-separated string.- Parameters:
localName- attribute namevalues- the values to writedelimiter- the string separating each values- Since:
- 2.0.0
-
writeElementDisabled
Writes an empty element with a "disabled" attribute set totrue.- Parameters:
localName- element (tag) name- Since:
- 2.0.0
-
writeElementDisabled
Writes an empty element with a "disabled" attribute set totrueand a "class" attribute matching the class name.- Parameters:
localName- element (tag) nameclazz- the class- Since:
- 2.0.0
-
writeElementInteger
Writes a simple Integer element.- Parameters:
localName- element (tag) namevalue- the Integer valuewriteBlanks- whether a blank value should be written as an empty element.- Since:
- 1.14.0
-
writeElementInteger
-
writeElementLong
Writes a simple Long element.- Parameters:
localName- element (tag) namevalue- the Long valuewriteBlanks- whether a blank value should be written as an empty element.- Since:
- 1.14.0
-
writeElementLong
-
writeElementFloat
Writes a simple Float element.- Parameters:
localName- element (tag) namevalue- the Float valuewriteBlanks- whether a blank value should be written as an empty element.- Since:
- 1.14.0
-
writeElementFloat
-
writeElementDouble
Writes a simple Double element.- Parameters:
localName- element (tag) namevalue- the Double valuewriteBlanks- whether a blank value should be written as an empty element.- Since:
- 1.14.0
-
writeElementDouble
-
writeElementBoolean
Writes a simple Boolean element.- Parameters:
localName- element (tag) namevalue- the Boolean valuewriteBlanks- whether a blank value should be written as an empty element.- Since:
- 1.14.0
-
writeElementBoolean
-
writeElementString
Writes a simple string element.- Parameters:
localName- element (tag) namevalue- the string valuewriteBlanks- whether a blank value should be written as an empty element.- Since:
- 1.14.0
-
writeElementString
-
writeElementClass
Writes a simple element containing a class name, getting it by invokingClass.getCanonicalName().- Parameters:
localName- element (tag) namevalue- the classwriteBlanks- whether a blank value should be written as an empty element.- Since:
- 1.14.0
-
writeElementClass
-
writeElementFile
Writes aFileelement.- Parameters:
localName- element (tag) namevalue- the FilewriteBlanks- whether a blank value should be written as an empty element.- Since:
- 2.0.0
-
writeElementFile
Writes aFileelement.- Parameters:
localName- element (tag) namevalue- the File- Since:
- 2.0.0
-
writeElementDimension
Writes a simple element containing a Dimension. The dimension will be written as [width]x[height] (e.g., 400x300) or just one numeric value if width and height are the same.- Parameters:
localName- element (tag) namevalue- the dimension- Since:
- 1.14.0
-
writeElementDimension
Writes a simple element containing a Dimension. The dimension will be written as [width]x[height] (e.g., 400x300) or just one numeric value if width and height are the same.- Parameters:
localName- element (tag) namevalue- the dimensionwriteBlanks- whether a blank value should be written as an empty element.- Since:
- 1.14.0
-
writeElementObjectList
public void writeElementObjectList(String parentLocalName, String localName, List<?> values, boolean writeBlanks) Writes a list of objects.- Parameters:
parentLocalName- parent tag wrapping child elements (set tonullfor no parent)localName- element (tag) namevalues- the listwriteBlanks- whether a blank value should be written as an empty element.- Since:
- 2.0.0
-
writeElementObjectList
Writes a list of objects.- Parameters:
parentLocalName- parent tag wrapping child elements (set tonullfor no parent)localName- element (tag) namevalues- the list- Since:
- 2.0.0
-
writeElementObject
Writes an element object as string. The object is converted to string using its "toString()" method and written as a characters into a single element.- Parameters:
localName- element (tag) namevalue- element (tag) value- Since:
- 1.14.0
- See Also:
-
writeElementObject
Writes an element object as string. The object is converted to string using its "toString()" method and written as a characters into a single element.- Parameters:
localName- element (tag) namevalue- element (tag) valuewriteBlanks- whether a blank value should be written as an empty element.- Since:
- 1.14.0
- See Also:
-
writeElementDelimited
Writes an element by converting a list into a comma-separated string.- Parameters:
localName- element namevalues- the values to write- Since:
- 2.0.0
-
writeElementDelimited
Writes an element by converting a list into a comma-separated string.- Parameters:
localName- element namevalues- the values to writedelimiter- the string separating each values- Since:
- 2.0.0
-
writeObject
Writes an object. If the object implementsXmlConfigurable, its is responsible for creating its own XML through itsXmlConfigurable.saveToXML(Xml)method. Otherwise, an empty element is created with the "class" attribute matching the object class canonical name.- Parameters:
localName- element (tag) namevalue- the object to write- Since:
- 2.0.0
- See Also:
-
writeObject
Writes an object. If the object implementsXmlConfigurable, its is responsible for creating its own XML through itsXmlConfigurable.saveToXML(Xml)method. Otherwise, an empty element is created with the "class" attribute matching the object class canonical name.- Parameters:
localName- element (tag) namevalue- the object to writedisabled- whether the object should have the "diabled" attribute- Since:
- 2.0.0
- See Also:
-
writeObjectList
public void writeObjectList(String parentLocalName, String localName, List<? extends Object> values) Writes a list of objects. If an object implementsXmlConfigurable, its is responsible for creating its own XML through itsXmlConfigurable.saveToXML(Xml)method. Otherwise, an empty element is created with the "class" attribute matching the object class canonical name.- Parameters:
parentLocalName- parent tag wrapping child elements (set tonullfor no parent)localName- element (tag) namevalues- the objects to write- Since:
- 2.0.0
- See Also:
-
writeStartElement
- Specified by:
writeStartElementin interfaceXMLStreamWriter
-
writeStartElement
Writes a start element of the supplied name, with a "class" attribute for the given class.- Parameters:
localName- tag nameclazz- name of class, ornull- Since:
- 2.0.0
-
writeStartElement
Writes a start element of the supplied name, with a "class" attribute for the given class, and a "disabled" attribute iftrue.- Parameters:
localName- tag nameclazz- name of class, ornulldisabled-trueto disable this class- Since:
- 2.0.0
-
writeStartElement
- Specified by:
writeStartElementin interfaceXMLStreamWriter
-
writeStartElement
- Specified by:
writeStartElementin interfaceXMLStreamWriter
-
writeEmptyElement
- Specified by:
writeEmptyElementin interfaceXMLStreamWriter
-
writeEmptyElement
- Specified by:
writeEmptyElementin interfaceXMLStreamWriter
-
writeEmptyElement
- Specified by:
writeEmptyElementin interfaceXMLStreamWriter
-
writeEndElement
public void writeEndElement()- Specified by:
writeEndElementin interfaceXMLStreamWriter
-
writeEndDocument
public void writeEndDocument()- Specified by:
writeEndDocumentin interfaceXMLStreamWriter
-
close
public void close()- Specified by:
closein interfaceXMLStreamWriter
-
flush
public void flush()- Specified by:
flushin interfaceXMLStreamWriter
-
writeAttribute
- Specified by:
writeAttributein interfaceXMLStreamWriter
-
writeAttribute
- Specified by:
writeAttributein interfaceXMLStreamWriter
-
writeAttribute
- Specified by:
writeAttributein interfaceXMLStreamWriter
-
writeNamespace
- Specified by:
writeNamespacein interfaceXMLStreamWriter
-
writeDefaultNamespace
- Specified by:
writeDefaultNamespacein interfaceXMLStreamWriter
-
writeComment
- Specified by:
writeCommentin interfaceXMLStreamWriter
-
writeProcessingInstruction
- Specified by:
writeProcessingInstructionin interfaceXMLStreamWriter
-
writeProcessingInstruction
- Specified by:
writeProcessingInstructionin interfaceXMLStreamWriter
-
writeCData
- Specified by:
writeCDatain interfaceXMLStreamWriter
-
writeDTD
- Specified by:
writeDTDin interfaceXMLStreamWriter
-
writeEntityRef
- Specified by:
writeEntityRefin interfaceXMLStreamWriter
-
writeStartDocument
public void writeStartDocument()- Specified by:
writeStartDocumentin interfaceXMLStreamWriter
-
writeStartDocument
- Specified by:
writeStartDocumentin interfaceXMLStreamWriter
-
writeStartDocument
- Specified by:
writeStartDocumentin interfaceXMLStreamWriter
-
writeCharacters
- Specified by:
writeCharactersin interfaceXMLStreamWriter
-
writeCharacters
public void writeCharacters(char[] text, int start, int len) - Specified by:
writeCharactersin interfaceXMLStreamWriter
-
getPrefix
- Specified by:
getPrefixin interfaceXMLStreamWriter
-
setPrefix
- Specified by:
setPrefixin interfaceXMLStreamWriter
-
setDefaultNamespace
- Specified by:
setDefaultNamespacein interfaceXMLStreamWriter
-
setNamespaceContext
- Specified by:
setNamespaceContextin interfaceXMLStreamWriter
-
getNamespaceContext
- Specified by:
getNamespaceContextin interfaceXMLStreamWriter
-
getProperty
- Specified by:
getPropertyin interfaceXMLStreamWriter
-