KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > utils > resolver > implementations > ResolverFragment


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package com.sun.org.apache.xml.internal.security.utils.resolver.implementations;
18
19
20
21 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
22 import com.sun.org.apache.xml.internal.security.utils.IdResolver;
23 import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException;
24 import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverSpi;
25 import org.w3c.dom.Attr JavaDoc;
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.Node JavaDoc;
28
29
30 /**
31  * This resolver is used for resolving same-document URIs like URI="" of URI="#id".
32  *
33  * @author $Author: dims $
34  * @see <A HREF="http://www.w3.org/TR/xmldsig-core/#sec-ReferenceProcessingModel">The Reference processing model in the XML Signature spec</A>
35  * @see <A HREF="http://www.w3.org/TR/xmldsig-core/#sec-Same-Document">Same-Document URI-References in the XML Signature spec</A>
36  * @see <A HREF="http://www.ietf.org/rfc/rfc2396.txt">Section 4.2 of RFC 2396</A>
37  */

38 public class ResolverFragment extends ResourceResolverSpi {
39
40    /** {@link java.util.logging} logging facility */
41     static java.util.logging.Logger JavaDoc log =
42         java.util.logging.Logger.getLogger(
43                             ResolverFragment.class.getName());
44
45    /**
46     * Method engineResolve
47     *
48     * Wird das gleiche Dokument referenziert?
49     * Wird ein anderes Dokument referenziert?
50     * @inheritDoc
51     * @param uri
52     * @param BaseURI
53     *
54     */

55    public XMLSignatureInput engineResolve(Attr JavaDoc uri, String JavaDoc BaseURI)
56        throws ResourceResolverException
57    {
58
59       String JavaDoc uriNodeValue = uri.getNodeValue();
60       Document JavaDoc doc = uri.getOwnerElement().getOwnerDocument();
61
62
63       Node JavaDoc selectedElem = null;
64       if (uriNodeValue.equals("")) {
65
66          /*
67           * Identifies the node-set (minus any comment nodes) of the XML
68           * resource containing the signature
69           */

70
71          if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "ResolverFragment with empty URI (means complete document)");
72      selectedElem = doc;
73       } else {
74
75          /*
76           * URI="#chapter1"
77           * Identifies a node-set containing the element with ID attribute
78           * value 'chapter1' of the XML resource containing the signature.
79           * XML Signature (and its applications) modify this node-set to
80           * include the element plus all descendents including namespaces and
81           * attributes -- but not comments.
82           */

83          String JavaDoc id = uriNodeValue.substring(1);
84
85          // Element selectedElem = doc.getElementById(id);
86
selectedElem = IdResolver.getElementById(doc, id);
87          if (selectedElem==null) {
88             Object JavaDoc exArgs[] = { id };
89             throw new ResourceResolverException(
90                "signature.Verification.MissingID", exArgs, uri, BaseURI);
91          }
92          if (true)
93             if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Try to catch an Element with ID " + id + " and Element was " + selectedElem);
94       }
95
96       XMLSignatureInput result = new XMLSignatureInput(selectedElem);
97       result.setExcludeComments(true);
98
99       //if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "We return a nodeset with " + resultSet.size() + " nodes");
100
result.setMIMEType("text/xml");
101       result.setSourceURI((BaseURI != null) ? BaseURI.concat(uri.getNodeValue()) :
102           uri.getNodeValue());
103       return result;
104    }
105
106    /**
107     * Method engineCanResolve
108     * @inheritDoc
109     * @param uri
110     * @param BaseURI
111     *
112     */

113    public boolean engineCanResolve(Attr JavaDoc uri, String JavaDoc BaseURI) {
114
115       if (uri == null) {
116          if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Quick fail for null uri");
117          return false;
118       }
119
120       String JavaDoc uriNodeValue = uri.getNodeValue();
121
122       if (uriNodeValue.equals("")
123               || ((uriNodeValue.charAt(0)=='#')
124                   &&!uriNodeValue.startsWith("#xpointer("))) {
125          if (true)
126             if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "State I can resolve reference: \"" + uriNodeValue + "\"");
127          return true;
128       }
129       if (true)
130         if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Do not seem to be able to resolve reference: \"" + uriNodeValue + "\"");
131       return false;
132    }
133
134 }
135
Popular Tags