Package com.twelvemonkeys.xml
Class XMLSerializer
- java.lang.Object
-
- com.twelvemonkeys.xml.XMLSerializer
-
public class XMLSerializer extends java.lang.Object
XMLSerializer- Version:
- $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/xml/XMLSerializer.java#1 $
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
XMLSerializer.SerializationContext
-
Field Summary
Fields Modifier and Type Field Description private XMLSerializer.SerializationContext
context
private java.nio.charset.Charset
encoding
private java.io.OutputStream
output
-
Constructor Summary
Constructors Constructor Description XMLSerializer(java.io.OutputStream pOutput, java.lang.String pEncoding)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static int
appendAndEscape(java.lang.String pString, int pStart, int pEnd, java.lang.StringBuilder pBuilder, java.lang.String pEntity)
XMLSerializer
indentation(java.lang.String pIndent)
private static void
indentToLevel(java.io.PrintWriter pOut, XMLSerializer.SerializationContext pContext)
static void
main(java.lang.String[] pArgs)
private static java.lang.String
maybeEscapeAttributeValue(java.lang.String pValue)
(package private) static java.lang.String
maybeEscapeElementValue(java.lang.String pValue)
Returns an escaped version of the input string.private static int
needsEscapeAttribute(java.lang.String pString)
Returns an the first index from the input string that should be escaped if escaping is needed, otherwise-1
.private static int
needsEscapeElement(java.lang.String pString)
Returns an the first index from the input string that should be escaped if escaping is needed, otherwise-1
.void
serialize(org.w3c.dom.Document pDocument)
Serializes the entire document, along with the XML declaration (<?xml version="1.0" encoding="..."?>
).void
serialize(org.w3c.dom.Node pRootNode, boolean pWriteXMLDeclaration)
Serializes the entire sub tree starting atpRootNode
, along with an optional XML declaration (<?xml version="1.0" encoding="..."?>
).XMLSerializer
stripComments(boolean pStrip)
private static void
updatePreserveSpace(org.w3c.dom.Node pNode, XMLSerializer.SerializationContext pContext)
private static java.lang.String
validateCDataValue(java.lang.String pValue)
private static java.lang.String
validateCommentValue(java.lang.String pValue)
private void
writeCData(java.io.PrintWriter pOut, org.w3c.dom.Node pNode)
private void
writeComment(java.io.PrintWriter pOut, org.w3c.dom.Node pNode, XMLSerializer.SerializationContext pContext)
private void
writeDoctype(java.io.PrintWriter pOut, org.w3c.dom.DocumentType pDoctype)
private void
writeDocument(java.io.PrintWriter pOut, org.w3c.dom.Node pNode, XMLSerializer.SerializationContext pContext)
private void
writeElement(java.io.PrintWriter pOut, org.w3c.dom.Element pNode, XMLSerializer.SerializationContext pContext)
private void
writeNodeRecursive(java.io.PrintWriter pOut, org.w3c.dom.Node pNode, XMLSerializer.SerializationContext pContext)
private void
writeProcessingInstruction(java.io.PrintWriter pOut, org.w3c.dom.ProcessingInstruction pNode)
private void
writeText(java.io.PrintWriter pOut, org.w3c.dom.Node pNode, XMLSerializer.SerializationContext pContext)
private void
writeXML(java.io.PrintWriter pOut, org.w3c.dom.Node pDocument, XMLSerializer.SerializationContext pContext)
private void
writeXMLDeclaration(java.io.PrintWriter pOut)
-
-
-
Field Detail
-
output
private final java.io.OutputStream output
-
encoding
private final java.nio.charset.Charset encoding
-
context
private final XMLSerializer.SerializationContext context
-
-
Method Detail
-
indentation
public final XMLSerializer indentation(java.lang.String pIndent)
-
stripComments
public final XMLSerializer stripComments(boolean pStrip)
-
serialize
public void serialize(org.w3c.dom.Document pDocument)
Serializes the entire document, along with the XML declaration (<?xml version="1.0" encoding="..."?>
).- Parameters:
pDocument
- the document to serialize.
-
serialize
public void serialize(org.w3c.dom.Node pRootNode, boolean pWriteXMLDeclaration)
Serializes the entire sub tree starting atpRootNode
, along with an optional XML declaration (<?xml version="1.0" encoding="..."?>
).- Parameters:
pRootNode
- the root node to serialize.pWriteXMLDeclaration
-true
if the XML declaration should be included, otherwisefalse
.
-
writeXMLDeclaration
private void writeXMLDeclaration(java.io.PrintWriter pOut)
-
writeXML
private void writeXML(java.io.PrintWriter pOut, org.w3c.dom.Node pDocument, XMLSerializer.SerializationContext pContext)
-
writeNodeRecursive
private void writeNodeRecursive(java.io.PrintWriter pOut, org.w3c.dom.Node pNode, XMLSerializer.SerializationContext pContext)
-
writeProcessingInstruction
private void writeProcessingInstruction(java.io.PrintWriter pOut, org.w3c.dom.ProcessingInstruction pNode)
-
writeText
private void writeText(java.io.PrintWriter pOut, org.w3c.dom.Node pNode, XMLSerializer.SerializationContext pContext)
-
writeCData
private void writeCData(java.io.PrintWriter pOut, org.w3c.dom.Node pNode)
-
updatePreserveSpace
private static void updatePreserveSpace(org.w3c.dom.Node pNode, XMLSerializer.SerializationContext pContext)
-
indentToLevel
private static void indentToLevel(java.io.PrintWriter pOut, XMLSerializer.SerializationContext pContext)
-
writeComment
private void writeComment(java.io.PrintWriter pOut, org.w3c.dom.Node pNode, XMLSerializer.SerializationContext pContext)
-
maybeEscapeElementValue
static java.lang.String maybeEscapeElementValue(java.lang.String pValue)
Returns an escaped version of the input string. The string is guaranteed to not contain illegal XML characters (&<>
). If no escaping is needed, the input string is returned as is.- Parameters:
pValue
- the input string that might need escaping.- Returns:
- an escaped version of the input string.
-
appendAndEscape
private static int appendAndEscape(java.lang.String pString, int pStart, int pEnd, java.lang.StringBuilder pBuilder, java.lang.String pEntity)
-
needsEscapeElement
private static int needsEscapeElement(java.lang.String pString)
Returns an the first index from the input string that should be escaped if escaping is needed, otherwise-1
.- Parameters:
pString
- the input string that might need escaping.- Returns:
- the first index from the input string that should be escaped,
or
-1
.
-
maybeEscapeAttributeValue
private static java.lang.String maybeEscapeAttributeValue(java.lang.String pValue)
-
needsEscapeAttribute
private static int needsEscapeAttribute(java.lang.String pString)
Returns an the first index from the input string that should be escaped if escaping is needed, otherwise-1
.- Parameters:
pString
- the input string that might need escaping.- Returns:
- the first index from the input string that should be escaped,
or
-1
.
-
validateCDataValue
private static java.lang.String validateCDataValue(java.lang.String pValue)
-
validateCommentValue
private static java.lang.String validateCommentValue(java.lang.String pValue)
-
writeDocument
private void writeDocument(java.io.PrintWriter pOut, org.w3c.dom.Node pNode, XMLSerializer.SerializationContext pContext)
-
writeElement
private void writeElement(java.io.PrintWriter pOut, org.w3c.dom.Element pNode, XMLSerializer.SerializationContext pContext)
-
writeDoctype
private void writeDoctype(java.io.PrintWriter pOut, org.w3c.dom.DocumentType pDoctype)
-
main
public static void main(java.lang.String[] pArgs) throws java.io.IOException, org.xml.sax.SAXException
- Throws:
java.io.IOException
org.xml.sax.SAXException
-
-