uk.me.nxg.xoxa
Class MessageDigestXMLReader

java.lang.Object
  extended by org.xml.sax.helpers.XMLFilterImpl
      extended by uk.me.nxg.xoxa.MessageDigestXMLReader
All Implemented Interfaces:
ContentHandler, DTDHandler, EntityResolver, ErrorHandler, XMLFilter, XMLReader

public class MessageDigestXMLReader
extends org.xml.sax.helpers.XMLFilterImpl

An XML reader which can sign and verify XML in passing. This is an implementation of the SAX XMLReader interface, and may be used as such.

Normally, you obtain a SAX reader using a SAXParserFactory; if you instead obtain a reader using the getXMLReader(String) method, then it can be used in the same way as any other parser, except that we additionally calculate a cryptographic message digest of the normalised version of the XML content.

After the parse has finished, the digest information can be extracted using the getDigest() and getDigestAsString() methods.


Method Summary
 void characters(char[] ch, int start, int length)
           
 void endDocument()
           
 void endElement(String uri, String localName, String qName)
           
 void endPrefixMapping(String prefix)
           
 String getAlgorithm()
          Obtains the name of the message digest algorithm which was calculated when parsing the XML.
static String getAlgorithm(XMLReader r)
          Obtains the name of the message digest algorithm which was calculated when parsing the XML.
 byte[] getDigest()
          Return the message digest.
static byte[] getDigest(XMLReader r)
          Return the message digest.
 String getDigestAsString()
          Return the message digest as a string of hex digits.
static String getDigestAsString(XMLReader r)
          Return the message digest as a string of hex digits.
 EntityResolver getEntityResolver()
           
static XMLReader getXMLReader(String digestName)
          Obtains a new XML reader which can calculate a message digest of the normalised form of an input XML source.
static XMLReader getXMLReader(XMLReader reader, String digestName)
          Obtains a new XMLReader which can calculate a message digest of the normalised form of an input XML source.
 void ignorableWhitespace(char[] ch, int start, int length)
           
 void processingInstruction(String target, String data)
           
 InputSource resolveEntity(String publicId, String systemId)
           
 void setDocumentLocator(Locator locator)
           
 void setEntityResolver(EntityResolver resolver)
           
 void skippedEntity(String name)
           
 void startDocument()
           
 void startElement(String uri, String localName, String qName, Attributes atts)
           
 void startPrefixMapping(String prefix, String uri)
           
 
Methods inherited from class org.xml.sax.helpers.XMLFilterImpl
error, fatalError, getContentHandler, getDTDHandler, getErrorHandler, getFeature, getParent, getProperty, notationDecl, parse, parse, setContentHandler, setDTDHandler, setErrorHandler, setFeature, setParent, setProperty, unparsedEntityDecl, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getXMLReader

public static XMLReader getXMLReader(XMLReader reader,
                                     String digestName)
                              throws SAXException
Obtains a new XMLReader which can calculate a message digest of the normalised form of an input XML source.

Parameters:
reader - an existing XMLReader instance
digestName - the name of a message digest algorithm
Returns:
a new XMLReader instance
Throws:
SAXException - if SAX does

getXMLReader

public static XMLReader getXMLReader(String digestName)
                              throws SAXException
Obtains a new XML reader which can calculate a message digest of the normalised form of an input XML source.

Parameters:
digestName - the name of a message digest algorithm
Returns:
a new XMLReader instance
Throws:
SAXException - if SAX does

getAlgorithm

public String getAlgorithm()
Obtains the name of the message digest algorithm which was calculated when parsing the XML.

Returns:
the canonical name for the algorithm

getAlgorithm

public static String getAlgorithm(XMLReader r)
Obtains the name of the message digest algorithm which was calculated when parsing the XML.

Parameters:
r - a XMLReader object; if this is not an implementation of MessageDigestXMLReader, then return null
Returns:
the canonical name for the algorithm

getDigest

public byte[] getDigest()
Return the message digest.

Returns:
an array of bytes which represent the message digest

getDigest

public static byte[] getDigest(XMLReader r)
Return the message digest.

Parameters:
r - a XMLReader object; if this is not an implementation of MessageDigestXMLReader, then return null
Returns:
an array of bytes which represent the message digest

getDigestAsString

public String getDigestAsString()
Return the message digest as a string of hex digits.

Returns:
an array of bytes which represent the message digest

getDigestAsString

public static String getDigestAsString(XMLReader r)
Return the message digest as a string of hex digits.

Parameters:
r - a XMLReader object; if this is not an implementation of MessageDigestXMLReader, then return null
Returns:
an array of bytes which represent the message digest

characters

public void characters(char[] ch,
                       int start,
                       int length)
                throws SAXException
Specified by:
characters in interface ContentHandler
Overrides:
characters in class org.xml.sax.helpers.XMLFilterImpl
Throws:
SAXException

ignorableWhitespace

public void ignorableWhitespace(char[] ch,
                                int start,
                                int length)
                         throws SAXException
Specified by:
ignorableWhitespace in interface ContentHandler
Overrides:
ignorableWhitespace in class org.xml.sax.helpers.XMLFilterImpl
Throws:
SAXException

processingInstruction

public void processingInstruction(String target,
                                  String data)
                           throws SAXException
Specified by:
processingInstruction in interface ContentHandler
Overrides:
processingInstruction in class org.xml.sax.helpers.XMLFilterImpl
Throws:
SAXException

setDocumentLocator

public void setDocumentLocator(Locator locator)
Specified by:
setDocumentLocator in interface ContentHandler
Overrides:
setDocumentLocator in class org.xml.sax.helpers.XMLFilterImpl

skippedEntity

public void skippedEntity(String name)
                   throws SAXException
Specified by:
skippedEntity in interface ContentHandler
Overrides:
skippedEntity in class org.xml.sax.helpers.XMLFilterImpl
Throws:
SAXException

startDocument

public void startDocument()
                   throws SAXException
Specified by:
startDocument in interface ContentHandler
Overrides:
startDocument in class org.xml.sax.helpers.XMLFilterImpl
Throws:
SAXException

endDocument

public void endDocument()
                 throws SAXException
Specified by:
endDocument in interface ContentHandler
Overrides:
endDocument in class org.xml.sax.helpers.XMLFilterImpl
Throws:
SAXException

startElement

public void startElement(String uri,
                         String localName,
                         String qName,
                         Attributes atts)
                  throws SAXException
Specified by:
startElement in interface ContentHandler
Overrides:
startElement in class org.xml.sax.helpers.XMLFilterImpl
Throws:
SAXException

endElement

public void endElement(String uri,
                       String localName,
                       String qName)
                throws SAXException
Specified by:
endElement in interface ContentHandler
Overrides:
endElement in class org.xml.sax.helpers.XMLFilterImpl
Throws:
SAXException

startPrefixMapping

public void startPrefixMapping(String prefix,
                               String uri)
                        throws SAXException
Specified by:
startPrefixMapping in interface ContentHandler
Overrides:
startPrefixMapping in class org.xml.sax.helpers.XMLFilterImpl
Throws:
SAXException

endPrefixMapping

public void endPrefixMapping(String prefix)
                      throws SAXException
Specified by:
endPrefixMapping in interface ContentHandler
Overrides:
endPrefixMapping in class org.xml.sax.helpers.XMLFilterImpl
Throws:
SAXException

setEntityResolver

public void setEntityResolver(EntityResolver resolver)
Specified by:
setEntityResolver in interface XMLReader
Overrides:
setEntityResolver in class org.xml.sax.helpers.XMLFilterImpl

getEntityResolver

public EntityResolver getEntityResolver()
Specified by:
getEntityResolver in interface XMLReader
Overrides:
getEntityResolver in class org.xml.sax.helpers.XMLFilterImpl

resolveEntity

public InputSource resolveEntity(String publicId,
                                 String systemId)
                          throws SAXException,
                                 IOException
Specified by:
resolveEntity in interface EntityResolver
Overrides:
resolveEntity in class org.xml.sax.helpers.XMLFilterImpl
Throws:
SAXException
IOException


Copyright © 2015. All rights reserved.