com.sapportals.wcm.util.xml

Class SimpleSerializer

java.lang.Object
  extended by com.sapportals.wcm.util.xml.SimpleSerializer

public final class SimpleSerializer
extends Object

Simple DOM serializer. Only supports UTF-8 encoding and simple indentation (whitespace preservation not implemented - if whitespace preservation is required simply do not indent).

Copyright (c) SAP AG 2001-2005


Nested Class Summary
static class SimpleSerializer.IllegalXmlNameException
           
 
Field Summary
static String NS_XML
          Namespace name for the namespace hardwired to the prefix "xml:"
static String NS_XMLNS
          Namespace name for the namespace hardwired to the prefix "xmlns:"
 
Constructor Summary
SimpleSerializer(Writer writer, String indent)
          Construct a new Serializer
 
Method Summary
static void checkLegalNCName(String name)
          Check whether a name is a legal
 void closeElement()
          Closes the currently open element.
static String escape(String s, boolean escapeQuoteChar)
          Escape characters in a string for inclusion into XML text content.
 SimpleSerializer openElement(Element element)
          Serializes the open tag of the specified element and returns a new SimpleSerializer object for use in serializing any child elements (and writing the closing tag).
 void serialize(Node n)
          Serialize a node and it's children.
 void setNsAware(boolean nsAware)
          Set to false to switch off namespace processing (enabling strict DOM level 1 serialization)
 void setOptimizeNSDeclarations(boolean doOptimize)
          When set to true, the serializer attempts to remove superfluous namespace declarations.
 void setSortAttributes(boolean sortAttributes)
          When set to true, the serializer sorts attributes (by name).
static String toString(Document doc, boolean indent)
          Serialize the document to a string.
static String toString(Document doc, boolean indent, boolean doNsProcessing)
          Serialize the document to a string.
static String toString(Node n, boolean indent)
          Serialize the element (and it's children) to a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NS_XML

public static final String NS_XML
Namespace name for the namespace hardwired to the prefix "xml:"

See Also:
Constant Field Values

NS_XMLNS

public static final String NS_XMLNS
Namespace name for the namespace hardwired to the prefix "xmlns:"

See Also:
Constant Field Values
Constructor Detail

SimpleSerializer

public SimpleSerializer(Writer writer,
                        String indent)
Construct a new Serializer

Parameters:
writer - output
indent - indentation string
Method Detail

toString

public static String toString(Document doc,
                              boolean indent)
Serialize the document to a string.

Parameters:
doc - the document
indent - whether to produce indented XML or not
Returns:
the XML string or an empty string in case of error

toString

public static String toString(Document doc,
                              boolean indent,
                              boolean doNsProcessing)
Serialize the document to a string.

Parameters:
doc - the document
indent - whether to produce indented XML or not
doNsProcessing - enable/disable special namespace support in the serializer
Returns:
the XML string or an empty string in case of error

toString

public static String toString(Node n,
                              boolean indent)
                       throws IllegalArgumentException
Serialize the element (and it's children) to a string.

Parameters:
indent - whether to produce indented XML or not
n - node to serialize
Returns:
the XML string or an empty string in case of error
Throws:
IllegalArgumentException - when node is a AttributeNode or DOM contains illegal names

setNsAware

public void setNsAware(boolean nsAware)
Set to false to switch off namespace processing (enabling strict DOM level 1 serialization)

Parameters:
nsAware - flag

setOptimizeNSDeclarations

public void setOptimizeNSDeclarations(boolean doOptimize)
When set to true, the serializer attempts to remove superfluous namespace declarations.

Parameters:
doOptimize - flag

setSortAttributes

public void setSortAttributes(boolean sortAttributes)
When set to true, the serializer sorts attributes (by name).

Parameters:
sortAttributes - flag

openElement

public SimpleSerializer openElement(Element element)
                             throws IOException
Serializes the open tag of the specified element and returns a new SimpleSerializer object for use in serializing any child elements (and writing the closing tag).

Parameters:
element - Element for which the start tag shall be written
Returns:
new Serializer object
Throws:
IOException

closeElement

public void closeElement()
                  throws IOException
Closes the currently open element. Do not use this object after calling this method!

Throws:
IOException

escape

public static String escape(String s,
                            boolean escapeQuoteChar)
                     throws IOException
Escape characters in a string for inclusion into XML text content. The generated string is guaranteed to only contain characters from the US-ASCII character set.

Parameters:
s - un-escaped string
escapeQuoteChar - whether to escape the quote character or not (for attribute values). Note that attributes are supposed to be quoted using double quotes, so apostroph characters are not replaced.
Returns:
escaped string
Throws:
IOException - when quoting not possible

serialize

public void serialize(Node n)
               throws IOException
Serialize a node and it's children.

Parameters:
n - start node
Throws:
IOException

checkLegalNCName

public static void checkLegalNCName(String name)
                             throws SimpleSerializer.IllegalXmlNameException
Check whether a name is a legal SimpleSerializer.IllegalXmlNameException
Access Rights

This class can be accessed from:


SC DC Public Part ACH
[sap.com] KMC-CM [sap.com] tc/km/frwk api EP-KM-CM
[sap.com] KMC-WPC [sap.com] tc/kmc/wpc/wpcfacade api EP-PIN-WPC-WCM


Copyright 2014 SAP AG Complete Copyright Notice