public class Canonicalizer
extends Object
Writes XML in the format specified by Canonical XML Version 1.0 or Exclusive XML Canonicalization Version 1.0. Canonical XML 1.1 support is in progress.
Modifier and Type | Field and Description |
---|---|
static String | CANONICAL_XML |
static String | CANONICAL_XML_11 |
static String | CANONICAL_XML_11_WITH_COMMENTS |
static String | CANONICAL_XML_WITH_COMMENTS |
static String | EXCLUSIVE_XML_CANONICALIZATION |
static String | EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS |
Constructor and Description |
---|
Canonicalizer(OutputStream out)
Creates a
Canonicalizer that outputs a
canonical XML document with comments. |
Canonicalizer(OutputStream out,
boolean withComments)
Creates a
Canonicalizer that outputs a
canonical XML document with or without comments. |
Canonicalizer(OutputStream out,
String algorithm)
Creates a
Canonicalizer that outputs a
canonical XML document using the specified algorithm. |
Modifier and Type | Method and Description |
---|---|
void | setInclusiveNamespacePrefixList(String inclusiveNamespacePrefixes)
Specifies the prefixes that will be output as specified in
regular canonical XML, even when doing exclusive
XML canonicalization.
|
void | write(Node node)
Serializes a node onto the output stream using the specified
canonicalization algorithm.
|
void | write(Nodes documentSubset)
Serializes a document subset onto the output stream using the
canonical XML algorithm.
|
public static final String CANONICAL_XML
public static final String CANONICAL_XML_WITH_COMMENTS
public static final String EXCLUSIVE_XML_CANONICALIZATION
public static final String EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS
public static final String CANONICAL_XML_11
public static final String CANONICAL_XML_11_WITH_COMMENTS
public Canonicalizer(OutputStream out)
Creates a Canonicalizer
that outputs a
canonical XML document with comments.
out
- the output stream the document
is written ontopublic Canonicalizer(OutputStream out, boolean withComments)
Creates a Canonicalizer
that outputs a
canonical XML document with or without comments.
out
- the output stream the document
is written ontowithComments
- true if comments should be included
in the output, false otherwisepublic Canonicalizer(OutputStream out, String algorithm)
Creates a Canonicalizer
that outputs a
canonical XML document using the specified algorithm.
Currently, four algorithms are defined and supported:
http://www.w3.org/TR/2001/REC-xml-c14n-20010315
http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments
http://www.w3.org/2001/10/xml-exc-c14n#
http://www.w3.org/2001/10/xml-exc-c14n#WithComments
out
- the output stream the document
is written ontoalgorithm
- the URI for the canonicalization algorithmCanonicalizationException
- if the algorithm is
not recognizedNullPointerException
- if the algorithm is nullpublic final void write(Node node) throws IOException
Serializes a node onto the output stream using the specified canonicalization algorithm. If the node is a document or an element, then the node's entire subtree is written out.
node
- the node to canonicalizeIOException
- if the underlying OutputStream
encounters an I/O errorpublic final void write(Nodes documentSubset) throws IOException
Serializes a document subset onto the output stream using the canonical XML algorithm. All nodes in the list must come from same document. Furthermore, they must come from a document. They cannot be detached. The nodes need not be sorted. This method will sort them into the appropriate order for canonicalization.
In most common use cases, these nodes will be the result of evaluating an XPath expression. For example,
Canonicalizer canonicalizer
= new Canonicalizer(System.out, Canonicalizer.CANONICAL_XML);
Nodes result = doc.query("//. | //@* | //namespace::*");
canonicalizer.write(result);
Children are not output unless the subset also includes them. Including an element in the subset does not automatically select all the element's children, attributes, and namespaces. Furthermore, not selecting an element does not imply that its children, namespaces, attributes will not be output.
documentSubset
- the nodes to serializeIOException
- if the underlying OutputStream
encounters an I/O errorCanonicalizationException
- if the nodes come from more
than one document; or if a detached node is in the listpublic final void setInclusiveNamespacePrefixList(String inclusiveNamespacePrefixes)
Specifies the prefixes that will be output as specified in regular canonical XML, even when doing exclusive XML canonicalization.
inclusiveNamespacePrefixes
- a whitespace separated list
of namespace prefixes that will always be included in the
output, even in exclusive canonicalizationCopyright 2002-2023 Elliotte Rusty Harold
elharo@ibiblio.org