Package com.norconex.commons.lang.xml
Class EnhancedXMLStreamWriter
- java.lang.Object
-
- com.norconex.commons.lang.xml.EnhancedXMLStreamWriter
-
- All Implemented Interfaces:
XMLStreamWriter
public class EnhancedXMLStreamWriter extends Object implements XMLStreamWriter
A version of
XMLStreamWriterthat 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 existingXMLStreamWriterinstance.Since 2.0.0 checked exceptions are wrapped in an
XMLException.- Since:
- 1.5.0
- Author:
- Pascal Essiembre
-
-
Constructor Summary
Constructors Constructor Description EnhancedXMLStreamWriter(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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()voidflush()NamespaceContextgetNamespaceContext()StringgetPrefix(String uri)ObjectgetProperty(String name)WritergetWriter()Gets the underlying writer, after flushing this XML stream writer.voidsetDefaultNamespace(String uri)voidsetNamespaceContext(NamespaceContext context)voidsetPrefix(String prefix, String uri)voidwriteAttribute(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.voidwriteAttributeDisabled()Write 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)voidwriteDTD(String dtd)voidwriteElementBoolean(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)voidwriteEndDocument()voidwriteEndElement()voidwriteEntityRef(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)voidwriteStartDocument()voidwriteStartDocument(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 Detail
-
EnhancedXMLStreamWriter
public EnhancedXMLStreamWriter(Writer writer)
-
EnhancedXMLStreamWriter
public EnhancedXMLStreamWriter(Writer writer, boolean writeBlanks)
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
public EnhancedXMLStreamWriter(Writer writer, boolean writeBlanks, int indent)
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 Detail
-
getWriter
public Writer getWriter()
Gets the underlying writer, after flushing this XML stream writer.- Returns:
- the writer
- Since:
- 2.0.0
-
writeAttributeInteger
public void writeAttributeInteger(String localName, Integer value, boolean writeBlanks)
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
-
writeAttributeLong
public void writeAttributeLong(String localName, Long value, boolean writeBlanks)
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
-
writeAttributeFloat
public void writeAttributeFloat(String localName, Float value, boolean writeBlanks)
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
-
writeAttributeDouble
public void writeAttributeDouble(String localName, Double value, boolean writeBlanks)
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
-
writeAttributeBoolean
public void writeAttributeBoolean(String localName, Boolean value, boolean writeBlanks)
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
-
writeAttributeString
public void writeAttributeString(String localName, String value, boolean writeBlanks)
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
-
writeAttributeClass
public void writeAttributeClass(Class<?> value, boolean writeBlanks)
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
public void writeAttributeClass(Class<?> value)
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
public void writeAttributeClass(String localName, Class<?> value, boolean writeBlanks)
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
-
writeAttributeDisabled
public void writeAttributeDisabled(Boolean value, boolean writeBlanks)
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
public void writeAttributeObject(String localName, Object value)
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
public void writeAttributeObject(String localName, Object value, boolean writeBlanks)
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
public void writeAttributeDelimited(String localName, List<?> values)
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
public void writeAttributeDelimited(String localName, List<?> values, String delimiter)
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
public void writeElementDisabled(String localName)
Writes an empty element with a "disabled" attribute set totrue.- Parameters:
localName- element (tag) name- Since:
- 2.0.0
-
writeElementDisabled
public void writeElementDisabled(String localName, Class<?> clazz)
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
public void writeElementInteger(String localName, Integer value, boolean writeBlanks)
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
-
writeElementLong
public void writeElementLong(String localName, Long value, boolean writeBlanks)
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
-
writeElementFloat
public void writeElementFloat(String localName, Float value, boolean writeBlanks)
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
-
writeElementDouble
public void writeElementDouble(String localName, Double value, boolean writeBlanks)
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
-
writeElementBoolean
public void writeElementBoolean(String localName, Boolean value, boolean writeBlanks)
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
-
writeElementString
public void writeElementString(String localName, String value, boolean writeBlanks)
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
-
writeElementClass
public void writeElementClass(String localName, Class<?> value, boolean writeBlanks)
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
-
writeElementFile
public void writeElementFile(String localName, File value, boolean writeBlanks)
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
public void writeElementFile(String localName, File value)
Writes aFileelement.- Parameters:
localName- element (tag) namevalue- the File- Since:
- 2.0.0
-
writeElementDimension
public void writeElementDimension(String localName, Dimension value)
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
public void writeElementDimension(String localName, Dimension value, boolean writeBlanks)
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
public void writeElementObjectList(String parentLocalName, String localName, List<?> values)
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
public void writeElementObject(String localName, Object value)
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:
writeObject(String, Object)
-
writeElementObject
public void writeElementObject(String localName, Object value, boolean writeBlanks)
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:
writeObject(String, Object)
-
writeElementDelimited
public void writeElementDelimited(String localName, List<?> values)
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
public void writeElementDelimited(String localName, List<?> values, String delimiter)
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
public void writeObject(String localName, Object value)
Writes an object. If the object implementsIXMLConfigurable, its is responsible for creating its own XML through itsIXMLConfigurable.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:
writeElementObject(String, Object)
-
writeObject
public void writeObject(String localName, Object value, boolean disabled)
Writes an object. If the object implementsIXMLConfigurable, its is responsible for creating its own XML through itsIXMLConfigurable.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:
writeElementObject(String, Object)
-
writeObjectList
public void writeObjectList(String parentLocalName, String localName, List<? extends Object> values)
Writes a list of objects. If an object implementsIXMLConfigurable, its is responsible for creating its own XML through itsIXMLConfigurable.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:
writeElementObjectList(String, String, List)
-
writeStartElement
public void writeStartElement(String localName)
- Specified by:
writeStartElementin interfaceXMLStreamWriter
-
writeStartElement
public void writeStartElement(String localName, Class<?> clazz)
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
public void writeStartElement(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.- Parameters:
localName- tag nameclazz- name of class, ornulldisabled-trueto disable this class- Since:
- 2.0.0
-
writeStartElement
public void writeStartElement(String namespaceURI, String localName)
- Specified by:
writeStartElementin interfaceXMLStreamWriter
-
writeStartElement
public void writeStartElement(String prefix, String localName, String namespaceURI)
- Specified by:
writeStartElementin interfaceXMLStreamWriter
-
writeEmptyElement
public void writeEmptyElement(String namespaceURI, String localName)
- Specified by:
writeEmptyElementin interfaceXMLStreamWriter
-
writeEmptyElement
public void writeEmptyElement(String prefix, String localName, String namespaceURI)
- Specified by:
writeEmptyElementin interfaceXMLStreamWriter
-
writeEmptyElement
public void writeEmptyElement(String localName)
- 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
public void writeAttribute(String localName, String value)
- Specified by:
writeAttributein interfaceXMLStreamWriter
-
writeAttribute
public void writeAttribute(String prefix, String namespaceURI, String localName, String value)
- Specified by:
writeAttributein interfaceXMLStreamWriter
-
writeAttribute
public void writeAttribute(String namespaceURI, String localName, String value)
- Specified by:
writeAttributein interfaceXMLStreamWriter
-
writeNamespace
public void writeNamespace(String prefix, String namespaceURI)
- Specified by:
writeNamespacein interfaceXMLStreamWriter
-
writeDefaultNamespace
public void writeDefaultNamespace(String namespaceURI)
- Specified by:
writeDefaultNamespacein interfaceXMLStreamWriter
-
writeComment
public void writeComment(String data)
- Specified by:
writeCommentin interfaceXMLStreamWriter
-
writeProcessingInstruction
public void writeProcessingInstruction(String target)
- Specified by:
writeProcessingInstructionin interfaceXMLStreamWriter
-
writeProcessingInstruction
public void writeProcessingInstruction(String target, String data)
- Specified by:
writeProcessingInstructionin interfaceXMLStreamWriter
-
writeCData
public void writeCData(String data)
- Specified by:
writeCDatain interfaceXMLStreamWriter
-
writeDTD
public void writeDTD(String dtd)
- Specified by:
writeDTDin interfaceXMLStreamWriter
-
writeEntityRef
public void writeEntityRef(String name)
- Specified by:
writeEntityRefin interfaceXMLStreamWriter
-
writeStartDocument
public void writeStartDocument()
- Specified by:
writeStartDocumentin interfaceXMLStreamWriter
-
writeStartDocument
public void writeStartDocument(String version)
- Specified by:
writeStartDocumentin interfaceXMLStreamWriter
-
writeStartDocument
public void writeStartDocument(String encoding, String version)
- Specified by:
writeStartDocumentin interfaceXMLStreamWriter
-
writeCharacters
public void writeCharacters(String text)
- Specified by:
writeCharactersin interfaceXMLStreamWriter
-
writeCharacters
public void writeCharacters(char[] text, int start, int len)- Specified by:
writeCharactersin interfaceXMLStreamWriter
-
getPrefix
public String getPrefix(String uri)
- Specified by:
getPrefixin interfaceXMLStreamWriter
-
setPrefix
public void setPrefix(String prefix, String uri)
- Specified by:
setPrefixin interfaceXMLStreamWriter
-
setDefaultNamespace
public void setDefaultNamespace(String uri)
- Specified by:
setDefaultNamespacein interfaceXMLStreamWriter
-
setNamespaceContext
public void setNamespaceContext(NamespaceContext context)
- Specified by:
setNamespaceContextin interfaceXMLStreamWriter
-
getNamespaceContext
public NamespaceContext getNamespaceContext()
- Specified by:
getNamespaceContextin interfaceXMLStreamWriter
-
getProperty
public Object getProperty(String name)
- Specified by:
getPropertyin interfaceXMLStreamWriter
-
-