background image

Creating a Validation Context

<< Validating an XML Signature | XML Signature Fails to Validate >>
<< Validating an XML Signature | XML Signature Fails to Validate >>
VALIDATE
E
XAMPLE
223
ment.getElementsByTagNameNS
, passing it the XML Signature namespace URI
and the tag name of the
Signature
element, as shown:
NodeList nl = doc.getElementsByTagNameNS
(XMLSignature.XMLNS, "Signature");
if (nl.getLength() == 0) {
throw new Exception("Cannot find Signature element");
}
This returns a list of all
Signature
elements in the document. In this example,
there is only one
Signature
element.
Creating a Validation Context
We create an
XMLValidateContext
instance containing input parameters for val-
idating the signature. Since we are using DOM, we instantiate a
DOMValidate-
Context
instance (a subclass of
XMLValidateContext
), and pass it two
parameters, a
KeyValueKeySelector
object and a reference to the
Signature
element to be validated (which is the first entry of the
NodeList
we generated
earlier):
DOMValidateContext valContext = new DOMValidateContext
(new KeyValueKeySelector(), nl.item(0));
The
KeyValueKeySelector
is
explained
in
greater
detail
in
Using
KeySelectors (page 225).
Unmarshaling the XML Signature
We extract the contents of the
Signature
element into an
XMLSignature
object.
This process is called unmarshalling. The
Signature
element is unmarshalled
using an
XMLSignatureFactory
object. An application can obtain a DOM
implementation of
XMLSignatureFactory
by calling the following line of code:
XMLSignatureFactory factory =
XMLSignatureFactory.getInstance("DOM");