com.rsa.certj.xml

Class ParserHandler

java.lang.Object
  |
  +--com.rsa.certj.xml.ParserHandler

public class ParserHandler
extends Object

This class defines methods for reading and writing XML signatures to or from an XML document. It processes an XML document by either loading XMLSignature objects from a file or by generating the signed document based on information provided by the XMLSignature object. After processing, it writes the document.

Copyright © RSA Security Inc., 2001-2003. All rights reserved.


Field Summary

static String

DTD_LOCATION

A String object defining the XML signature schema definition file and its path.

static String

PRINTWRITER_ENCODING

Default encoding for an XML signature document.

static String

PUBLIC_ID

A String object that defines the public ID when reading an XML file, to be signed or verified.

static boolean

setNameSpaces

A boolean setting for a parser feature.

static boolean

setValidation

A boolean setting for a parser feature.

static String

SYSTEM_ID

A String object that defines the system ID when reading an XML file, to be signed or verified.

static String

TEMPLATE_ID

The unique ID attribute value of the signature template that specifies the template to fill with SignatureValue and DigestValue values.

 

Constructor Summary

ParserHandler()

 

 

Method Summary

static org.w3c.dom.Node

byteArrayToNode(byte[] input, int inOffset, int inputLength)

Converts a byte array to a node.

static int

checkSignatureType(XMLSignature xmlSig)

Checks the signature type of the passed-in XMLSignature object.

static org.w3c.dom.Element

generateKeyInfo(XMLSignature xmlSignature, org.w3c.dom.Document document)

Generates a Element object containing the key information of xmlSignature.

static org.w3c.dom.Element

generateSignatureElement(XMLSignature xmlSignature, org.w3c.dom.Document document)

Generates a signature element of the particular XMLSignature object.

static void

getStringValue(org.w3c.dom.NodeList nodes, StringBuffer valueString)

Retrieves the information stored in a NodeList object and stores it in a StringBuffer.

static void

getStringValue(org.w3c.dom.Node node, StringBuffer valueString)

Converts the node-set node to a String value.

static byte[]

nodeToByteArray(org.w3c.dom.Node node, boolean canonical, boolean withComments, String encoding)

Converts a node to a byte array.

static byte[]

nodeToByteArray(org.w3c.dom.NodeList node, boolean canonical, boolean withComments, String encoding)

Converts a node-set to a byte array.

static void

parseSignedInfo(XMLSignature xmlSignature, org.w3c.dom.Element sigEle)

Parses the sigEle, retrieves the signedInfo information and stores in the xmlSignature.

static org.w3c.dom.Document

parseXML(org.xml.sax.InputSource inputSource)

Parses input source with a DOM parser configured as specified by the user settings.

static XMLSignature[]

read(File file)

Reads data from an XML document containing XML signatures specified by a File object.

static XMLSignature[]

read(InputStream inputStream)

Reads data from an XML document containing XML signatures specified by an InputStream object.

static XMLSignature[]

read(String fileName)

Reads data from an XML document file or URL containing XML signatures specified by the fileName.

static void

write(File signedDocument, XMLSignature xmlSignature)

Writes an XMLSignature to a File object.

static void

write(OutputStream signedDocument, XMLSignature xmlSignature)

Writes an XMLSignature to an OutputStream object.

static void

write(String signedDocument, XMLSignature xmlSignature)

Writes a list of XMLSignature objects to a file or URL with the name signedDocument.

 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PRINTWRITER_ENCODING

public static String PRINTWRITER_ENCODING
Default encoding for an XML signature document.

TEMPLATE_ID

public static String TEMPLATE_ID
The unique ID attribute value of the signature template that specifies the template to fill with SignatureValue and DigestValue values. If this value is null, then the first empty signature template will be processed.

SYSTEM_ID

public static String SYSTEM_ID
A String object that defines the system ID when reading an XML file, to be signed or verified. This variable is optional. When it is set to null (the default value), then any DTD element declarations in the document must be fully qualified. Otherwise, the value set for this String can be used by the parser to help resolve relative URIs in the document.

PUBLIC_ID

public static String PUBLIC_ID
A String object that defines the public ID when reading an XML file, to be signed or verified. This variable is optional. When it is set to null (the default value), then any DTD element declarations in the document must be fully qualified. Otherwise, the value set for this String can be used by the parser to help resolve relative URIs in the document.

DTD_LOCATION

public static String DTD_LOCATION
A String object defining the XML signature schema definition file and its path. It is optional. If it is null, then the schema will not be specified in the output file. Note: XMLException will be thrown if a protocol handler cannot be found. For instance ParserHandler.DTD_LOCATION="file:///C:/xmldsig-core-schema.dtd" or ParserHandler.DTD_LOCATION="http://location.com/xmldsig-core-schema.dtd" can be set.

setValidation

public static boolean setValidation
A boolean setting for a parser feature. This sets the parser feature "http://xml.org/sax/features/validation". Default value is false.

setNameSpaces

public static boolean setNameSpaces
A boolean setting for a parser feature. This sets the parser feature "http://xml.org/sax/features/namespaces". Default value is true.
Constructor Detail

ParserHandler

public ParserHandler()
Method Detail

read

public static XMLSignature[] read(String fileName)
                           throws XMLException
Reads data from an XML document file or URL containing XML signatures specified by the fileName. It parses data from the file or URL and instantiates a list of XMLSignature objects associated with this document.

Parameters

         fileName  

A String object used to specify the input signed document.

Returns

A list of XMLSignature objects retreived from the input XML document.

Throws

XMLException - If an error occurs in reading and processing this document.

read

public static XMLSignature[] read(File file)
                           throws XMLException
Reads data from an XML document containing XML signatures specified by a File object. It parses data from the file and instantiates a list of XMLSignature objects associated with this document.

Parameters

         file  

A File object used to specify the signed document.

Returns

A list of XMLSignature objects retreived from the input XML document.

Throws

XMLException - If an error occurs in reading and processing this document.

read

public static XMLSignature[] read(InputStream inputStream)
                           throws XMLException
Reads data from an XML document containing XML signatures specified by an InputStream object. It parses data from the inputStream and instantiates a list of XMLSignature objects associated with this document.

Parameters

         inputStream  

An InputStream object containing the XML document to read.

Returns

A list of XMLSignature objects retreived from the input XML document.

Throws

XMLException - If an error occurs in reading and processing this document.

parseXML

public static org.w3c.dom.Document parseXML(org.xml.sax.InputSource inputSource)
                                     throws XMLException
Parses input source with a DOM parser configured as specified by the user settings. Internal method, do not use

Parameters

         inputSoure  

An InputSource object of XML.

Returns

A Document object.

Throws

XMLException - If an error occurs while creating the parser or during the actual parsing.

parseSignedInfo

public static void parseSignedInfo(XMLSignature xmlSignature,
                                   org.w3c.dom.Element sigEle)
                            throws XMLException
Parses the sigEle, retrieves the signedInfo information and stores in the xmlSignature.

Internal method, do not use


checkSignatureType

public static int checkSignatureType(XMLSignature xmlSig)
Checks the signature type of the passed-in XMLSignature object.

Internal method, do not use

Returns

An integer value specifing the signature type.

write

public static void write(String signedDocument,
                         XMLSignature xmlSignature)
                  throws XMLException
Writes a list of XMLSignature objects to a file or URL with the name signedDocument.

Parameters

         signedDocument  

A String object indicating the output file or URL name.

         xmlSignature  

An XMLSignature object array to output as an XML document.

Throws

XMLException - If an error occurs while writing the xmlSignature as an XML document.

write

public static void write(File signedDocument,
                         XMLSignature xmlSignature)
                  throws XMLException
Writes an XMLSignature to a File object.

Parameters

         signedDocument  

A File object indicating the output file.

         xmlSignature  

An XMLSignature object array to write as an XML document.

Throws

XMLException - If an error occurs while writing the xmlSignature out as an XML document.

write

public static void write(OutputStream signedDocument,
                         XMLSignature xmlSignature)
                  throws XMLException
Writes an XMLSignature to an OutputStream object.

Parameters

         signedDocument  

An OutputStream to be written to.

         xmlSignature  

An XMLSignature object array to write as an XML document.

Throws

XMLException - If an error occurs while writing the xmlSignature out as an XML document.

generateSignatureElement

public static org.w3c.dom.Element generateSignatureElement(XMLSignature xmlSignature,
                                                           org.w3c.dom.Document document)
                                                    throws XMLException
Generates a signature element of the particular XMLSignature object.

Internal method, do not use

Parameters

         xmlSignature  

An XMLSignature object the generated signature element is based on.

         document  

The Document object which is the root of the signature element.

Returns

An Element object.

Throws

XMLException - If an error occurs while generating the Signature element.

generateKeyInfo

public static org.w3c.dom.Element generateKeyInfo(XMLSignature xmlSignature,
                                                  org.w3c.dom.Document document)
                                           throws XMLException
Generates a Element object containing the key information of xmlSignature. The generated element will be attached to document.

Internal method, do not use


nodeToByteArray

public static byte[] nodeToByteArray(org.w3c.dom.NodeList node,
                                     boolean canonical,
                                     boolean withComments,
                                     String encoding)
                              throws XMLException
Converts a node-set to a byte array.

Parameters

         node  

A NodeList object containing some data.

         canonical  

A Boolean variable indicating whether the result needs to be canonicalized or not. Set to true if the result should be canonicalized.

         withComments  

A Boolean variable indicating whether the result is canonicalized with comments or not. This variable will be checked only when canonical is true.

         encoding  

A String object specifying the character encoding of the input node-set.

Returns

A byte array storing all the data of the passed-in NodeList object.

Throws

XMLException - If an error ocurrs while converting.

nodeToByteArray

public static byte[] nodeToByteArray(org.w3c.dom.Node node,
                                     boolean canonical,
                                     boolean withComments,
                                     String encoding)
                              throws XMLException
Converts a node to a byte array.

Parameters

         node  

A node object containing some data.

         canonical  

A Boolean variable indicating whether the result needs to be canonicalized or not. Set to true if the result should be canonicalized.

         withComments  

A Boolean variable indicating whether the result is canonicalized with comments or not. This variable will be checked only when canonical is true.

         encoding  

A String object specifying the character encoding of the input node.

Returns

A byte array storing all the data of the passed-in node object.

Throws

XMLException - If an error occurrs while converting.

byteArrayToNode

public static org.w3c.dom.Node byteArrayToNode(byte[] input,
                                               int inOffset,
                                               int inputLength)
                                        throws XMLException
Converts a byte array to a node.

Parameters

         input  

A byte array to be converted to a node.

         inOffset  

An integer indicating the position where the byte array starts.

         inputLength  

An integer indicating how many bytes there are to convert.

Throws

XMLException - If an error occurs while converting.

getStringValue

public static void getStringValue(org.w3c.dom.Node node,
                                  StringBuffer valueString)
                           throws XMLException
Converts the node-set node to a String value. All data including start and end tags are converted.

Parameters

         node  

A Node object to be converted to a String.

         valueString  

A StringBuffer object used to store the String

Throws

XMLException - If an error occurs while converting the node to a String value.

getStringValue

public static void getStringValue(org.w3c.dom.NodeList nodes,
                                  StringBuffer valueString)
                           throws XMLException
Retrieves the information stored in a NodeList object and stores it in a StringBuffer.

Parameters

         nodes  

The NodeList object where the information is stored.

         valueString  

A StringBuffer object used to store the String value of the node.

Throws

XMLException - If an error occurs while converting the node-set to a String value.


RSA BSAFE ® Cert-J 2.1.1 001-047007-211-001-000