blob: 2fad506050b88b4db5ba4d86e71f9992569554e8 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Copyright 1999-2004 The Apache Software Foundation.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21package com.sun.org.apache.xml.internal.security.utils.resolver.implementations;
22
23
24
25import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
26import com.sun.org.apache.xml.internal.security.utils.IdResolver;
27import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException;
28import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverSpi;
29import org.w3c.dom.Attr;
30import org.w3c.dom.Document;
31import org.w3c.dom.Node;
32
33
34/**
35 * Handles barename XPointer Reference URIs.
36 * <BR />
37 * To retain comments while selecting an element by an identifier ID,
38 * use the following full XPointer: URI='#xpointer(id('ID'))'.
39 * <BR />
40 * To retain comments while selecting the entire document,
41 * use the following full XPointer: URI='#xpointer(/)'.
42 * This XPointer contains a simple XPath expression that includes
43 * the root node, which the second to last step above replaces with all
44 * nodes of the parse tree (all descendants, plus all attributes,
45 * plus all namespaces nodes).
46 *
47 * @author $Author: dims $
48 */
49public class ResolverXPointer extends ResourceResolverSpi {
50
51 /** {@link java.util.logging} logging facility */
52 static java.util.logging.Logger log =
53 java.util.logging.Logger.getLogger(
54 ResolverXPointer.class.getName());
55
56 /**
57 * @inheritDoc
58 */
59 public XMLSignatureInput engineResolve(Attr uri, String BaseURI)
60 throws ResourceResolverException {
61
62 Node resultNode = null;
63 Document doc = uri.getOwnerElement().getOwnerDocument();
64
65 String uriStr=uri.getNodeValue();
66 if (isXPointerSlash(uriStr)) {
67 resultNode = doc;
68
69 } else if (isXPointerId(uriStr)) {
70 String id = getXPointerId(uriStr);
71 resultNode =IdResolver.getElementById(doc, id);
72
73 // if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Use #xpointer(id('" + id + "')) on element " + selectedElem);
74
75 if (resultNode == null) {
76 Object exArgs[] = { id };
77
78 throw new ResourceResolverException(
79 "signature.Verification.MissingID", exArgs, uri, BaseURI);
80 }
81 /*
82 resultNodes =
83 cXPathAPI
84 .selectNodeList(selectedElem, Canonicalizer
85 .XPATH_C14N_WITH_COMMENTS_SINGLE_NODE);*/
86 }
87
88
89 XMLSignatureInput result = new XMLSignatureInput(resultNode);
90
91 result.setMIMEType("text/xml");
92 if (BaseURI != null && BaseURI.length() > 0) {
93 result.setSourceURI(BaseURI.concat(uri.getNodeValue()));
94 } else {
95 result.setSourceURI(uri.getNodeValue());
96 }
97
98 return result;
99 }
100
101 /**
102 * @inheritDoc
103 */
104 public boolean engineCanResolve(Attr uri, String BaseURI) {
105
106 if (uri == null) {
107 return false;
108 }
109 String uriStr =uri.getNodeValue();
110 if (isXPointerSlash(uriStr) || isXPointerId(uriStr)) {
111 return true;
112 }
113
114 return false;
115 }
116
117 /**
118 * Method isXPointerSlash
119 *
120 * @param uri
121 * @return true if begins with xpointer
122 */
123 private static boolean isXPointerSlash(String uri) {
124
125 if (uri.equals("#xpointer(/)")) {
126 return true;
127 }
128
129 return false;
130 }
131
132
133 private static final String XP="#xpointer(id(";
134 private static final int XP_LENGTH=XP.length();
135 /**
136 * Method isXPointerId
137 *
138 * @param uri
139 * @return it it has an xpointer id
140 *
141 */
142 private static boolean isXPointerId(String uri) {
143
144
145 if (uri.startsWith(XP)
146 && uri.endsWith("))")) {
147 String idPlusDelim = uri.substring(XP_LENGTH,
148 uri.length()
149 - 2);
150
151 // if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "idPlusDelim=" + idPlusDelim);
152 int idLen=idPlusDelim.length() -1;
153 if (((idPlusDelim.charAt(0) == '"') && (idPlusDelim
154 .charAt(idLen) == '"')) || ((idPlusDelim
155 .charAt(0) == '\'') && (idPlusDelim
156 .charAt(idLen) == '\''))) {
157 if (true)
158 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Id="
159 + idPlusDelim.substring(1, idLen));
160
161 return true;
162 }
163 }
164
165 return false;
166 }
167
168 /**
169 * Method getXPointerId
170 *
171 * @param uri
172 * @return xpointerId to search.
173 */
174 private static String getXPointerId(String uri) {
175
176
177 if (uri.startsWith(XP)
178 && uri.endsWith("))")) {
179 String idPlusDelim = uri.substring(XP_LENGTH,uri.length()
180 - 2);
181 int idLen=idPlusDelim.length() -1;
182 if (((idPlusDelim.charAt(0) == '"') && (idPlusDelim
183 .charAt(idLen) == '"')) || ((idPlusDelim
184 .charAt(0) == '\'') && (idPlusDelim
185 .charAt(idLen) == '\''))) {
186 return idPlusDelim.substring(1, idLen);
187 }
188 }
189
190 return null;
191 }
192}