KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Handles barename XPointer Reference URIs.
32  * <BR />
33  * To retain comments while selecting an element by an identifier ID,
34  * use the following full XPointer: URI='#xpointer(id('ID'))'.
35  * <BR />
36  * To retain comments while selecting the entire document,
37  * use the following full XPointer: URI='#xpointer(/)'.
38  * This XPointer contains a simple XPath expression that includes
39  * the root node, which the second to last step above replaces with all
40  * nodes of the parse tree (all descendants, plus all attributes,
41  * plus all namespaces nodes).
42  *
43  * @author $Author: dims $
44  */

45 public class ResolverXPointer extends ResourceResolverSpi {
46
47    /** {@link java.util.logging} logging facility */
48     static java.util.logging.Logger JavaDoc log =
49         java.util.logging.Logger.getLogger(
50                             ResolverXPointer.class.getName());
51
52    /**
53     * @inheritDoc
54     */

55    public XMLSignatureInput engineResolve(Attr JavaDoc uri, String JavaDoc BaseURI)
56            throws ResourceResolverException {
57
58       Node JavaDoc resultNode = null;
59       Document JavaDoc doc = uri.getOwnerElement().getOwnerDocument();
60
61         String JavaDoc uriStr=uri.getNodeValue();
62          if (isXPointerSlash(uriStr)) {
63             resultNode = doc;
64                
65          } else if (isXPointerId(uriStr)) {
66             String JavaDoc id = getXPointerId(uriStr);
67             resultNode =IdResolver.getElementById(doc, id);
68
69             // if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Use #xpointer(id('" + id + "')) on element " + selectedElem);
70

71             if (resultNode == null) {
72                Object JavaDoc exArgs[] = { id };
73
74                throw new ResourceResolverException(
75                   "signature.Verification.MissingID", exArgs, uri, BaseURI);
76             }
77             /*
78             resultNodes =
79                cXPathAPI
80                   .selectNodeList(selectedElem, Canonicalizer
81                      .XPATH_C14N_WITH_COMMENTS_SINGLE_NODE);*/

82          }
83       
84
85       XMLSignatureInput result = new XMLSignatureInput(resultNode);
86
87       result.setMIMEType("text/xml");
88       if (BaseURI != null && BaseURI.length() > 0) {
89       result.setSourceURI(BaseURI.concat(uri.getNodeValue()));
90       } else {
91       result.setSourceURI(uri.getNodeValue());
92       }
93
94       return result;
95    }
96
97    /**
98     * @inheritDoc
99     */

100    public boolean engineCanResolve(Attr JavaDoc uri, String JavaDoc BaseURI) {
101
102       if (uri == null) {
103          return false;
104       }
105       String JavaDoc uriStr =uri.getNodeValue();
106       if (isXPointerSlash(uriStr) || isXPointerId(uriStr)) {
107          return true;
108       }
109
110       return false;
111    }
112
113    /**
114     * Method isXPointerSlash
115     *
116     * @param uri
117     * @return true if begins with xpointer
118     */

119    private static boolean isXPointerSlash(String JavaDoc uri) {
120
121       if (uri.equals("#xpointer(/)")) {
122          return true;
123       }
124
125       return false;
126    }
127
128    
129    private static final String JavaDoc XP="#xpointer(id(";
130    private static final int XP_LENGTH=XP.length();
131    /**
132     * Method isXPointerId
133     *
134     * @param uri
135     * @return it it has an xpointer id
136     *
137     */

138    private static boolean isXPointerId(String JavaDoc uri) {
139       
140
141       if (uri.startsWith(XP)
142               && uri.endsWith("))")) {
143          String JavaDoc idPlusDelim = uri.substring(XP_LENGTH,
144                                                      uri.length()
145                                                      - 2);
146
147          // if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "idPlusDelim=" + idPlusDelim);
148
int idLen=idPlusDelim.length() -1;
149          if (((idPlusDelim.charAt(0) == '"') && (idPlusDelim
150                  .charAt(idLen) == '"')) || ((idPlusDelim
151                  .charAt(0) == '\'') && (idPlusDelim
152                  .charAt(idLen) == '\''))) {
153             if (true)
154                 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Id="
155                       + idPlusDelim.substring(1, idLen));
156
157             return true;
158          }
159       }
160
161       return false;
162    }
163
164    /**
165     * Method getXPointerId
166     *
167     * @param uri
168     * @return xpointerId to search.
169     */

170    private static String JavaDoc getXPointerId(String JavaDoc uri) {
171
172
173       if (uri.startsWith(XP)
174               && uri.endsWith("))")) {
175          String JavaDoc idPlusDelim = uri.substring(XP_LENGTH,uri.length()
176                                                      - 2);
177          int idLen=idPlusDelim.length() -1;
178          if (((idPlusDelim.charAt(0) == '"') && (idPlusDelim
179                  .charAt(idLen) == '"')) || ((idPlusDelim
180                  .charAt(0) == '\'') && (idPlusDelim
181                  .charAt(idLen) == '\''))) {
182             return idPlusDelim.substring(1, idLen);
183          }
184       }
185
186       return null;
187    }
188 }
189
Popular Tags