J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 21 | package com.sun.org.apache.xml.internal.security.utils.resolver.implementations; |
| 22 | |
| 23 | |
| 24 | |
| 25 | import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput; |
| 26 | import com.sun.org.apache.xml.internal.security.utils.IdResolver; |
| 27 | import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException; |
| 28 | import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverSpi; |
| 29 | import org.w3c.dom.Attr; |
| 30 | import org.w3c.dom.Document; |
| 31 | import org.w3c.dom.Node; |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * This resolver is used for resolving same-document URIs like URI="" of URI="#id". |
| 36 | * |
| 37 | * @author $Author: dims $ |
| 38 | * @see <A HREF="http://www.w3.org/TR/xmldsig-core/#sec-ReferenceProcessingModel">The Reference processing model in the XML Signature spec</A> |
| 39 | * @see <A HREF="http://www.w3.org/TR/xmldsig-core/#sec-Same-Document">Same-Document URI-References in the XML Signature spec</A> |
| 40 | * @see <A HREF="http://www.ietf.org/rfc/rfc2396.txt">Section 4.2 of RFC 2396</A> |
| 41 | */ |
| 42 | public class ResolverFragment extends ResourceResolverSpi { |
| 43 | |
| 44 | /** {@link java.util.logging} logging facility */ |
| 45 | static java.util.logging.Logger log = |
| 46 | java.util.logging.Logger.getLogger( |
| 47 | ResolverFragment.class.getName()); |
| 48 | |
| 49 | /** |
| 50 | * Method engineResolve |
| 51 | * |
| 52 | * Wird das gleiche Dokument referenziert? |
| 53 | * Wird ein anderes Dokument referenziert? |
| 54 | * @inheritDoc |
| 55 | * @param uri |
| 56 | * @param BaseURI |
| 57 | * |
| 58 | */ |
| 59 | public XMLSignatureInput engineResolve(Attr uri, String BaseURI) |
| 60 | throws ResourceResolverException |
| 61 | { |
| 62 | |
| 63 | String uriNodeValue = uri.getNodeValue(); |
| 64 | Document doc = uri.getOwnerElement().getOwnerDocument(); |
| 65 | |
| 66 | |
| 67 | Node selectedElem = null; |
| 68 | if (uriNodeValue.equals("")) { |
| 69 | |
| 70 | /* |
| 71 | * Identifies the node-set (minus any comment nodes) of the XML |
| 72 | * resource containing the signature |
| 73 | */ |
| 74 | |
| 75 | if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "ResolverFragment with empty URI (means complete document)"); |
| 76 | selectedElem = doc; |
| 77 | } else { |
| 78 | |
| 79 | /* |
| 80 | * URI="#chapter1" |
| 81 | * Identifies a node-set containing the element with ID attribute |
| 82 | * value 'chapter1' of the XML resource containing the signature. |
| 83 | * XML Signature (and its applications) modify this node-set to |
| 84 | * include the element plus all descendents including namespaces and |
| 85 | * attributes -- but not comments. |
| 86 | */ |
| 87 | String id = uriNodeValue.substring(1); |
| 88 | |
| 89 | // Element selectedElem = doc.getElementById(id); |
| 90 | selectedElem = IdResolver.getElementById(doc, id); |
| 91 | if (selectedElem==null) { |
| 92 | Object exArgs[] = { id }; |
| 93 | throw new ResourceResolverException( |
| 94 | "signature.Verification.MissingID", exArgs, uri, BaseURI); |
| 95 | } |
| 96 | if (true) |
| 97 | 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); |
| 98 | } |
| 99 | |
| 100 | XMLSignatureInput result = new XMLSignatureInput(selectedElem); |
| 101 | result.setExcludeComments(true); |
| 102 | |
| 103 | //if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "We return a nodeset with " + resultSet.size() + " nodes"); |
| 104 | result.setMIMEType("text/xml"); |
| 105 | result.setSourceURI((BaseURI != null) ? BaseURI.concat(uri.getNodeValue()) : |
| 106 | uri.getNodeValue()); |
| 107 | return result; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Method engineCanResolve |
| 112 | * @inheritDoc |
| 113 | * @param uri |
| 114 | * @param BaseURI |
| 115 | * |
| 116 | */ |
| 117 | public boolean engineCanResolve(Attr uri, String BaseURI) { |
| 118 | |
| 119 | if (uri == null) { |
| 120 | if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Quick fail for null uri"); |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | String uriNodeValue = uri.getNodeValue(); |
| 125 | |
| 126 | if (uriNodeValue.equals("") |
| 127 | || ((uriNodeValue.charAt(0)=='#') |
| 128 | &&!uriNodeValue.startsWith("#xpointer("))) { |
| 129 | if (true) |
| 130 | if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "State I can resolve reference: \"" + uriNodeValue + "\""); |
| 131 | return true; |
| 132 | } |
| 133 | if (true) |
| 134 | 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 + "\""); |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | } |