Class XMLWriter
- All Implemented Interfaces:
ContentHandler,DTDHandler,EntityResolver,ErrorHandler,LexicalHandler,XMLFilter,XMLReader
- Direct Known Subclasses:
XMLSocketWriter
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final org.dom4j.io.OutputFormatprotected intStores the last type of node written so algorithms can refer to the previous node typeprotected static final String[]protected booleanStores the xml:space attribute value of preserve for whitespace flagprotected WriterThe Writer used to output to -
Constructor Summary
ConstructorsConstructorDescriptionXMLWriter(OutputStream out) XMLWriter(OutputStream out, org.dom4j.io.OutputFormat format) XMLWriter(org.dom4j.io.OutputFormat format) -
Method Summary
Modifier and TypeMethodDescriptionvoidcharacters(char[] ch, int start, int length) voidclose()Closes the underlying Writervoidcomment(char[] ch, int start, int length) protected WritercreateWriter(OutputStream outStream, String encoding) Get an OutputStreamWriter, use preferred encoding.protected intReturns the maximum allowed character code that should be allowed unescaped which defaults to 127 in US-ASCII (7 bit) or 255 in ISO-* (8 bit).voidendCDATA()voidvoidendDTD()voidendElement(String namespaceURI, String localName, String qName) voidvoidendPrefixMapping(String prefix) protected StringThis will take the pre-defined entities in XML 1.0 and convert their character representation to the appropriate entity reference, suitable for XML attributes.protected StringescapeElementEntities(String text) This will take the pre-defined entities in XML 1.0 and convert their character representation to the appropriate entity reference, suitable for XML attributes.voidflush()Flushes the underlying WriterintReturns the maximum allowed character code that should be allowed unescaped which defaults to 127 in US-ASCII (7 bit) or 255 in ISO-* (8 bit).protected org.dom4j.io.OutputFormatLets subclasses get at the current format object, so they can call setTrimText, setNewLines, etc.getProperty(String name) protected voidvoidignorableWhitespace(char[] ch, int start, int length) protected voidindent()protected voidprotected final booleanisElementSpacePreserved(org.dom4j.Element element) Determines if element is a special case of XML elements where it contains an xml:space attribute of "preserve".booleanprotected booleanprotected booleanisNamespaceDeclaration(org.dom4j.Namespace ns) voidnotationDecl(String name, String publicID, String systemID) voidparse(InputSource source) voidprintln()Writes the new line text to the underlying WritervoidprocessingInstruction(String target, String data) booleanvoidsetDocumentLocator(Locator locator) voidsetEscapeText(boolean escapeText) Sets whether text output should be escaped or not.voidsetIndentLevel(int indentLevel) Set the initial indentation level.voidsetLexicalHandler(LexicalHandler handler) voidsetMaximumAllowedCharacter(int maximumAllowedCharacter) Sets the maximum allowed character code that should be allowed unescaped such as 127 in US-ASCII (7 bit) or 255 in ISO-* (8 bit) or -1 to not escape any characters (other than the special XML characters like < > &) If this is not explicitly set then it is defaulted from the encoding.voidvoidsetProperty(String name, Object value) voidsetResolveEntityRefs(boolean resolve) voidprotected booleanshouldEncodeChar(char c) Should the given character be escaped.voidvoidvoidvoidstartElement(String namespaceURI, String localName, String qName, Attributes attributes) voidstartEntity(String name) voidstartPrefixMapping(String prefix, String uri) voidunparsedEntityDecl(String name, String publicID, String systemID, String notationName) voidWrites the given object which should be a String, a Node or a List of Nodes.voidPrint out aString, Perfoms the necessary entity escaping and whitespace stripping.voidwrite(org.dom4j.Attribute attribute) Writes the givenAttribute.voidwrite(org.dom4j.CDATA cdata) Writes the givenCDATA.voidwrite(org.dom4j.Comment comment) Writes the givenComment.voidwrite(org.dom4j.Document doc) This will print theDocumentto the current Writer.voidwrite(org.dom4j.DocumentType docType) Writes the givenDocumentType.voidwrite(org.dom4j.Element element) Writes the, including itsElements, and its value, and all its content (child nodes) to the current Writer.Attributevoidwrite(org.dom4j.Entity entity) Writes the givenEntity.voidwrite(org.dom4j.Namespace namespace) Writes the givenNamespace.voidwrite(org.dom4j.Node node) Writes the givenNode.voidwrite(org.dom4j.ProcessingInstruction processingInstruction) Writes the givenProcessingInstruction.voidwrite(org.dom4j.Text text) Writes the givenText.protected voidwriteAttribute(org.dom4j.Attribute attribute) protected voidwriteAttribute(Attributes attributes, int index) protected voidwriteAttributes(org.dom4j.Element element) Writes the attributes of the given elementprotected voidwriteAttributes(Attributes attributes) protected voidwriteCDATA(String text) protected voidwriteClose(String qualifiedName) voidwriteClose(org.dom4j.Element element) Writes the closing tag of anElementprotected voidwriteComment(String text) protected voidThis will write the declaration to the given Writer.protected voidwriteDocType(String name, String publicID, String systemID) protected voidwriteDocType(org.dom4j.DocumentType docType) protected voidwriteElement(org.dom4j.Element element) protected voidwriteElementContent(org.dom4j.Element element) Outputs the content of the given element.protected voidwriteEmptyElementClose(String qualifiedName) protected voidwriteEntity(org.dom4j.Entity entity) protected voidwriteEntityRef(String name) protected voidprotected voidwriteNamespace(String prefix, String uri) Writes the SAX namepsacesprotected voidwriteNamespace(org.dom4j.Namespace namespace) protected voidWrites the SAX namepsacesprotected voidwriteNode(org.dom4j.Node node) protected voidwriteNodeText(org.dom4j.Node node) This method is used to write out Nodes that contain text and still allow for xml:space to be handled properly.voidwriteOpen(org.dom4j.Element element) Writes the opening tag of anElement, including itsAttributes but without its content.protected voidThis will print a new line only if the newlines flag was set to trueprotected voidwriteProcessingInstruction(org.dom4j.ProcessingInstruction processingInstruction) protected voidwriteString(String text) Methods inherited from class org.xml.sax.helpers.XMLFilterImpl
error, fatalError, getContentHandler, getDTDHandler, getEntityResolver, getErrorHandler, getFeature, getParent, parse, resolveEntity, setContentHandler, setDTDHandler, setEntityResolver, setErrorHandler, setFeature, setParent, skippedEntity, warningMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.xml.sax.ContentHandler
declaration
-
Field Details
-
LEXICAL_HANDLER_NAMES
-
DEFAULT_FORMAT
protected static final org.dom4j.io.OutputFormat DEFAULT_FORMAT -
lastOutputNodeType
protected int lastOutputNodeTypeStores the last type of node written so algorithms can refer to the previous node type -
preserve
protected boolean preserveStores the xml:space attribute value of preserve for whitespace flag -
writer
The Writer used to output to
-
-
Constructor Details
-
XMLWriter
-
XMLWriter
-
XMLWriter
- Throws:
UnsupportedEncodingException
-
XMLWriter
- Throws:
UnsupportedEncodingException
-
XMLWriter
public XMLWriter(OutputStream out, org.dom4j.io.OutputFormat format) throws UnsupportedEncodingException - Throws:
UnsupportedEncodingException
-
XMLWriter
- Throws:
UnsupportedEncodingException
-
-
Method Details
-
setWriter
-
setOutputStream
- Throws:
UnsupportedEncodingException
-
isEscapeText
public boolean isEscapeText()- Returns:
- true if text thats output should be escaped. This is enabled by default. It could be disabled if the output format is textual, like in XSLT where we can have xml, html or text output.
-
setEscapeText
public void setEscapeText(boolean escapeText) Sets whether text output should be escaped or not. This is enabled by default. It could be disabled if the output format is textual, like in XSLT where we can have xml, html or text output.- Parameters:
escapeText-trueto escape text, otherwisefalse
-
setIndentLevel
public void setIndentLevel(int indentLevel) Set the initial indentation level. This can be used to output a document (or, more likely, an element) starting at a given indent level, so it's not always flush against the left margin. Default: 0- Parameters:
indentLevel- the number of indents to start with
-
getMaximumAllowedCharacter
public int getMaximumAllowedCharacter()Returns the maximum allowed character code that should be allowed unescaped which defaults to 127 in US-ASCII (7 bit) or 255 in ISO-* (8 bit).- Returns:
- the maximum character code
-
setMaximumAllowedCharacter
public void setMaximumAllowedCharacter(int maximumAllowedCharacter) Sets the maximum allowed character code that should be allowed unescaped such as 127 in US-ASCII (7 bit) or 255 in ISO-* (8 bit) or -1 to not escape any characters (other than the special XML characters like < > &) If this is not explicitly set then it is defaulted from the encoding.- Parameters:
maximumAllowedCharacter- The maximumAllowedCharacter to set
-
flush
Flushes the underlying Writer- Throws:
IOException- if the writer could not be flushed
-
close
Closes the underlying Writer- Throws:
IOException- if the writer could not be closed
-
println
Writes the new line text to the underlying Writer- Throws:
IOException- if the new line could not be written
-
write
Writes the givenAttribute.- Parameters:
attribute-Attributeto output.- Throws:
IOException- if the attribute could not be written
-
write
This will print the
Documentto the current Writer.Warning: using your own Writer may cause the writer's preferred character encoding to be ignored. If you use encodings other than UTF8, we recommend using the method that takes an OutputStream instead.
Note: as with all Writers, you may need to flush() yours after this method returns.
- Parameters:
doc-Documentto format.- Throws:
IOException- - if there's any problem writing.
-
write
Writes the
, including itsElements, and its value, and all its content (child nodes) to the current Writer.Attribute- Parameters:
element-Elementto output.- Throws:
IOException- if the element could not be written
-
write
Writes the givenCDATA.- Parameters:
cdata-CDATAto output.- Throws:
IOException- if the cdata could not be written
-
write
Writes the givenComment.- Parameters:
comment-Commentto output.- Throws:
IOException- if the comment could not be written
-
write
Writes the givenDocumentType.- Parameters:
docType-DocumentTypeto output.- Throws:
IOException- if the docType could not be written
-
write
Writes the givenEntity.- Parameters:
entity-Entityto output.- Throws:
IOException- if the entity could not be written
-
write
Writes the givenNamespace.- Parameters:
namespace-Namespaceto output.- Throws:
IOException- if the namespace could not be written
-
write
Writes the givenProcessingInstruction.- Parameters:
processingInstruction-ProcessingInstructionto output.- Throws:
IOException- if the instruction could not be written
-
write
Print out a
String, Perfoms the necessary entity escaping and whitespace stripping.- Parameters:
text- is the text to output- Throws:
IOException- if the text could not be written
-
write
Writes the givenText.- Parameters:
text-Textto output.- Throws:
IOException- if the text could not be written
-
write
Writes the givenNode.- Parameters:
node-Nodeto output.- Throws:
IOException- if the node could not be written
-
write
Writes the given object which should be a String, a Node or a List of Nodes.- Parameters:
object- is the object to output.- Throws:
IOException- if the object could not be written
-
writeOpen
Writes the opening tag of an
Element, including itsAttributes but without its content.- Parameters:
element-Elementto output.- Throws:
IOException- if the element could not be written
-
writeClose
Writes the closing tag of an
Element- Parameters:
element-Elementto output.- Throws:
IOException- if the element could not be written
-
parse
- Specified by:
parsein interfaceXMLReader- Overrides:
parsein classXMLFilterImpl- Throws:
IOExceptionSAXException
-
setProperty
public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException - Specified by:
setPropertyin interfaceXMLReader- Overrides:
setPropertyin classXMLFilterImpl- Throws:
SAXNotRecognizedExceptionSAXNotSupportedException
-
getProperty
- Specified by:
getPropertyin interfaceXMLReader- Overrides:
getPropertyin classXMLFilterImpl- Throws:
SAXNotRecognizedExceptionSAXNotSupportedException
-
setLexicalHandler
-
getLexicalHandler
-
setDocumentLocator
- Specified by:
setDocumentLocatorin interfaceContentHandler- Overrides:
setDocumentLocatorin classXMLFilterImpl
-
startDocument
- Specified by:
startDocumentin interfaceContentHandler- Overrides:
startDocumentin classXMLFilterImpl- Throws:
SAXException
-
endDocument
- Specified by:
endDocumentin interfaceContentHandler- Overrides:
endDocumentin classXMLFilterImpl- Throws:
SAXException
-
startPrefixMapping
- Specified by:
startPrefixMappingin interfaceContentHandler- Overrides:
startPrefixMappingin classXMLFilterImpl- Throws:
SAXException
-
endPrefixMapping
- Specified by:
endPrefixMappingin interfaceContentHandler- Overrides:
endPrefixMappingin classXMLFilterImpl- Throws:
SAXException
-
startElement
public void startElement(String namespaceURI, String localName, String qName, Attributes attributes) throws SAXException - Specified by:
startElementin interfaceContentHandler- Overrides:
startElementin classXMLFilterImpl- Throws:
SAXException
-
endElement
- Specified by:
endElementin interfaceContentHandler- Overrides:
endElementin classXMLFilterImpl- Throws:
SAXException
-
characters
- Specified by:
charactersin interfaceContentHandler- Overrides:
charactersin classXMLFilterImpl- Throws:
SAXException
-
ignorableWhitespace
- Specified by:
ignorableWhitespacein interfaceContentHandler- Overrides:
ignorableWhitespacein classXMLFilterImpl- Throws:
SAXException
-
processingInstruction
- Specified by:
processingInstructionin interfaceContentHandler- Overrides:
processingInstructionin classXMLFilterImpl- Throws:
SAXException
-
notationDecl
- Specified by:
notationDeclin interfaceDTDHandler- Overrides:
notationDeclin classXMLFilterImpl- Throws:
SAXException
-
unparsedEntityDecl
public void unparsedEntityDecl(String name, String publicID, String systemID, String notationName) throws SAXException - Specified by:
unparsedEntityDeclin interfaceDTDHandler- Overrides:
unparsedEntityDeclin classXMLFilterImpl- Throws:
SAXException
-
startDTD
- Specified by:
startDTDin interfaceLexicalHandler- Throws:
SAXException
-
endDTD
- Specified by:
endDTDin interfaceLexicalHandler- Throws:
SAXException
-
startCDATA
- Specified by:
startCDATAin interfaceLexicalHandler- Throws:
SAXException
-
endCDATA
- Specified by:
endCDATAin interfaceLexicalHandler- Throws:
SAXException
-
startEntity
- Specified by:
startEntityin interfaceLexicalHandler- Throws:
SAXException
-
endEntity
- Specified by:
endEntityin interfaceLexicalHandler- Throws:
SAXException
-
comment
- Specified by:
commentin interfaceLexicalHandler- Throws:
SAXException
-
writeElement
- Throws:
IOException
-
isElementSpacePreserved
protected final boolean isElementSpacePreserved(org.dom4j.Element element) Determines if element is a special case of XML elements where it contains an xml:space attribute of "preserve". If it does, then retain whitespace.- Parameters:
element- the element to check- Returns:
trueif whitespace should be preserved, otherwisefalse
-
writeElementContent
Outputs the content of the given element. If whitespace trimming is enabled then all adjacent text nodes are appended together before the whitespace trimming occurs to avoid problems with multiple text nodes being created due to text content that spans parser buffers in a SAX parser.- Parameters:
element- the element to write- Throws:
IOException- if the element could not be written
-
writeCDATA
- Throws:
IOException
-
writeDocType
- Throws:
IOException
-
writeNamespace
- Throws:
IOException
-
writeNamespaces
Writes the SAX namepsaces- Throws:
IOException- if the namespaces could not be written
-
writeNamespace
Writes the SAX namepsaces- Parameters:
prefix- the namespace prefixuri- the URL of the namespace- Throws:
IOException- if the namespace could not be written
-
writeProcessingInstruction
protected void writeProcessingInstruction(org.dom4j.ProcessingInstruction processingInstruction) throws IOException - Throws:
IOException
-
writeString
- Throws:
IOException
-
writeNodeText
This method is used to write out Nodes that contain text and still allow for xml:space to be handled properly.- Parameters:
node- the node to write- Throws:
IOException- if the node could not be written
-
writeNode
- Throws:
IOException
-
installLexicalHandler
protected void installLexicalHandler() -
writeDocType
- Throws:
IOException
-
writeEntity
- Throws:
IOException
-
writeEntityRef
- Throws:
IOException
-
writeComment
- Throws:
IOException
-
writeAttributes
Writes the attributes of the given element- Parameters:
element- the element whose attributes should be written- Throws:
IOException- if the element could not be written
-
writeAttribute
- Throws:
IOException
-
writeAttributes
- Throws:
IOException
-
writeAttribute
- Throws:
IOException
-
indent
- Throws:
IOException
-
writePrintln
This will print a new line only if the newlines flag was set to true
- Throws:
IOException- if the new line could not be written
-
createWriter
protected Writer createWriter(OutputStream outStream, String encoding) throws UnsupportedEncodingException Get an OutputStreamWriter, use preferred encoding.- Parameters:
outStream- the outut streamencoding- the encoding of the stream- Returns:
- the IO writer
- Throws:
UnsupportedEncodingException- if the encoding is not support
-
writeDeclaration
This will write the declaration to the given Writer. Assumes XML version 1.0 since we don't directly know.
- Throws:
IOException- if the declaration could not be written
-
writeClose
- Throws:
IOException
-
writeEmptyElementClose
- Throws:
IOException
-
isExpandEmptyElements
protected boolean isExpandEmptyElements() -
escapeElementEntities
This will take the pre-defined entities in XML 1.0 and convert their character representation to the appropriate entity reference, suitable for XML attributes.- Parameters:
text- the entities to escale- Returns:
- the escaped entities
-
writeEscapeAttributeEntities
- Throws:
IOException
-
escapeAttributeEntities
This will take the pre-defined entities in XML 1.0 and convert their character representation to the appropriate entity reference, suitable for XML attributes.- Parameters:
text- the entitie to escape- Returns:
- the escaped entity
-
shouldEncodeChar
protected boolean shouldEncodeChar(char c) Should the given character be escaped. This depends on the encoding of the document.- Parameters:
c- the character to check- Returns:
trueto escape the character, otherwisefalse
-
defaultMaximumAllowedCharacter
protected int defaultMaximumAllowedCharacter()Returns the maximum allowed character code that should be allowed unescaped which defaults to 127 in US-ASCII (7 bit) or 255 in ISO-* (8 bit).- Returns:
- the maximum allow character code
-
isNamespaceDeclaration
protected boolean isNamespaceDeclaration(org.dom4j.Namespace ns) -
handleException
- Throws:
SAXException
-
getOutputFormat
protected org.dom4j.io.OutputFormat getOutputFormat()Lets subclasses get at the current format object, so they can call setTrimText, setNewLines, etc. Put in to support the HTMLWriter, in the way that it pushes the current newline/trim state onto a stack and overrides the state within preformatted tags.- Returns:
- the output format
-
resolveEntityRefs
public boolean resolveEntityRefs() -
setResolveEntityRefs
public void setResolveEntityRefs(boolean resolve)
-