1 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 ; 26 import org.w3c.dom.Document ; 27 import org.w3c.dom.Node ; 28 29 30 45 public class ResolverXPointer extends ResourceResolverSpi { 46 47 48 static java.util.logging.Logger log = 49 java.util.logging.Logger.getLogger( 50 ResolverXPointer.class.getName()); 51 52 55 public XMLSignatureInput engineResolve(Attr uri, String BaseURI) 56 throws ResourceResolverException { 57 58 Node resultNode = null; 59 Document doc = uri.getOwnerElement().getOwnerDocument(); 60 61 String uriStr=uri.getNodeValue(); 62 if (isXPointerSlash(uriStr)) { 63 resultNode = doc; 64 65 } else if (isXPointerId(uriStr)) { 66 String id = getXPointerId(uriStr); 67 resultNode =IdResolver.getElementById(doc, id); 68 69 71 if (resultNode == null) { 72 Object exArgs[] = { id }; 73 74 throw new ResourceResolverException( 75 "signature.Verification.MissingID", exArgs, uri, BaseURI); 76 } 77 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 100 public boolean engineCanResolve(Attr uri, String BaseURI) { 101 102 if (uri == null) { 103 return false; 104 } 105 String uriStr =uri.getNodeValue(); 106 if (isXPointerSlash(uriStr) || isXPointerId(uriStr)) { 107 return true; 108 } 109 110 return false; 111 } 112 113 119 private static boolean isXPointerSlash(String uri) { 120 121 if (uri.equals("#xpointer(/)")) { 122 return true; 123 } 124 125 return false; 126 } 127 128 129 private static final String XP="#xpointer(id("; 130 private static final int XP_LENGTH=XP.length(); 131 138 private static boolean isXPointerId(String uri) { 139 140 141 if (uri.startsWith(XP) 142 && uri.endsWith("))")) { 143 String idPlusDelim = uri.substring(XP_LENGTH, 144 uri.length() 145 - 2); 146 147 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 170 private static String getXPointerId(String uri) { 171 172 173 if (uri.startsWith(XP) 174 && uri.endsWith("))")) { 175 String 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 |