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 | /* |
| 7 | * Copyright 1999-2004 The Apache Software Foundation. |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | * you may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
| 20 | * |
| 21 | */ |
| 22 | package com.sun.org.apache.xml.internal.security.utils.resolver; |
| 23 | |
| 24 | |
| 25 | import java.util.Map; |
| 26 | |
| 27 | import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput; |
| 28 | import org.w3c.dom.Attr; |
| 29 | |
| 30 | |
| 31 | /** |
| 32 | * During reference validation, we have to retrieve resources from somewhere. |
| 33 | * |
| 34 | * @author $Author: raul $ |
| 35 | */ |
| 36 | public abstract class ResourceResolverSpi { |
| 37 | |
| 38 | /** {@link java.util.logging} logging facility */ |
| 39 | static java.util.logging.Logger log = |
| 40 | java.util.logging.Logger.getLogger( |
| 41 | ResourceResolverSpi.class.getName()); |
| 42 | |
| 43 | /** Field _properties */ |
| 44 | protected java.util.Map _properties = new java.util.HashMap(10); |
| 45 | |
| 46 | /** |
| 47 | * This is the workhorse method used to resolve resources. |
| 48 | * |
| 49 | * @param uri |
| 50 | * @param BaseURI |
| 51 | * @return the resource wrapped arround a XMLSignatureInput |
| 52 | * |
| 53 | * @throws ResourceResolverException |
| 54 | */ |
| 55 | public abstract XMLSignatureInput engineResolve(Attr uri, String BaseURI) |
| 56 | throws ResourceResolverException; |
| 57 | |
| 58 | /** |
| 59 | * Method engineSetProperty |
| 60 | * |
| 61 | * @param key |
| 62 | * @param value |
| 63 | */ |
| 64 | public void engineSetProperty(String key, String value) { |
| 65 | |
| 66 | java.util.Iterator i = this._properties.keySet().iterator(); |
| 67 | |
| 68 | while (i.hasNext()) { |
| 69 | String c = (String) i.next(); |
| 70 | |
| 71 | if (c.equals(key)) { |
| 72 | key = c; |
| 73 | |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | this._properties.put(key, value); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Method engineGetProperty |
| 83 | * |
| 84 | * @param key |
| 85 | * @return the value of the property |
| 86 | */ |
| 87 | public String engineGetProperty(String key) { |
| 88 | |
| 89 | java.util.Iterator i = this._properties.keySet().iterator(); |
| 90 | |
| 91 | while (i.hasNext()) { |
| 92 | String c = (String) i.next(); |
| 93 | |
| 94 | if (c.equals(key)) { |
| 95 | key = c; |
| 96 | |
| 97 | break; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return (String) this._properties.get(key); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * |
| 106 | * @param properties |
| 107 | */ |
| 108 | public void engineAddProperies(Map properties) { |
| 109 | this._properties.putAll(properties); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * This method helps the {@link ResourceResolver} to decide whether a |
| 114 | * {@link ResourceResolverSpi} is able to perform the requested action. |
| 115 | * |
| 116 | * @param uri |
| 117 | * @param BaseURI |
| 118 | * @return true if the engine can resolve the uri |
| 119 | */ |
| 120 | public abstract boolean engineCanResolve(Attr uri, String BaseURI); |
| 121 | |
| 122 | /** |
| 123 | * Method engineGetPropertyKeys |
| 124 | * |
| 125 | * @return the property keys |
| 126 | */ |
| 127 | public String[] engineGetPropertyKeys() { |
| 128 | return new String[0]; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Method understandsProperty |
| 133 | * |
| 134 | * @param propertyToTest |
| 135 | * @return true if understands the property |
| 136 | */ |
| 137 | public boolean understandsProperty(String propertyToTest) { |
| 138 | |
| 139 | String[] understood = this.engineGetPropertyKeys(); |
| 140 | |
| 141 | if (understood != null) { |
| 142 | for (int i = 0; i < understood.length; i++) { |
| 143 | if (understood[i].equals(propertyToTest)) { |
| 144 | return true; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | /** |
| 154 | * Fixes a platform dependent filename to standard URI form. |
| 155 | * |
| 156 | * @param str The string to fix. |
| 157 | * |
| 158 | * @return Returns the fixed URI string. |
| 159 | */ |
| 160 | public static String fixURI(String str) { |
| 161 | |
| 162 | // handle platform dependent strings |
| 163 | str = str.replace(java.io.File.separatorChar, '/'); |
| 164 | |
| 165 | if (str.length() >= 4) { |
| 166 | |
| 167 | // str =~ /^\W:\/([^/])/ # to speak perl ;-)) |
| 168 | char ch0 = Character.toUpperCase(str.charAt(0)); |
| 169 | char ch1 = str.charAt(1); |
| 170 | char ch2 = str.charAt(2); |
| 171 | char ch3 = str.charAt(3); |
| 172 | boolean isDosFilename = ((('A' <= ch0) && (ch0 <= 'Z')) |
| 173 | && (ch1 == ':') && (ch2 == '/') |
| 174 | && (ch3 != '/')); |
| 175 | |
| 176 | if (isDosFilename) { |
| 177 | if (true) |
| 178 | if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Found DOS filename: " + str); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // Windows fix |
| 183 | if (str.length() >= 2) { |
| 184 | char ch1 = str.charAt(1); |
| 185 | |
| 186 | if (ch1 == ':') { |
| 187 | char ch0 = Character.toUpperCase(str.charAt(0)); |
| 188 | |
| 189 | if (('A' <= ch0) && (ch0 <= 'Z')) { |
| 190 | str = "/" + str; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // done |
| 196 | return str; |
| 197 | } |
| 198 | } |