1 /* 2 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 /* 26 * $Id: DOMCryptoContext.java,v 1.3 2005/05/09 18:33:26 mullan Exp $ 27 */ 28 package javax.xml.crypto.dom; 29 30 import javax.xml.crypto.KeySelector; 31 import javax.xml.crypto.URIDereferencer; 32 import javax.xml.crypto.XMLCryptoContext; 33 import java.util.Collections; 34 import java.util.HashMap; 35 import java.util.Iterator; 36 import org.w3c.dom.Element; 37 38 /** 39 * This class provides a DOM-specific implementation of the 40 * {@link XMLCryptoContext} interface. It also includes additional 41 * methods that are specific to a DOM-based implementation for registering 42 * and retrieving elements that contain attributes of type ID. 43 * 44 * @author Sean Mullan 45 * @author JSR 105 Expert Group 46 * @since 1.6 47 */ 48 public class DOMCryptoContext implements XMLCryptoContext { 49 50 private HashMap nsMap = new HashMap(); 51 private HashMap idMap = new HashMap(); 52 private HashMap objMap = new HashMap(); 53 private String baseURI; 54 private KeySelector ks; 55 private URIDereferencer dereferencer; 56 private HashMap propMap = new HashMap(); 57 private String defaultPrefix; 58 59 /** 60 * Default constructor. (For invocation by subclass constructors). 61 */ 62 protected DOMCryptoContext() {} 63 64 /** 65 * This implementation uses an internal {@link HashMap} to get the prefix 66 * that the specified URI maps to. It returns the <code>defaultPrefix</code> 67 * if it maps to <code>null</code>. 68 * 69 * @throws NullPointerException {@inheritDoc} 70 */ 71 public String getNamespacePrefix(String namespaceURI, 72 String defaultPrefix) { 73 if (namespaceURI == null) { 74 throw new NullPointerException("namespaceURI cannot be null"); 75 } 76 String prefix = (String) nsMap.get(namespaceURI); 77 return (prefix != null ? prefix : defaultPrefix); 78 } 79 80 /** 81 * This implementation uses an internal {@link HashMap} to map the URI 82 * to the specified prefix. 83 * 84 * @throws NullPointerException {@inheritDoc} 85 */ 86 public String putNamespacePrefix(String namespaceURI, String prefix) { 87 if (namespaceURI == null) { 88 throw new NullPointerException("namespaceURI is null"); 89 } 90 return (String) nsMap.put(namespaceURI, prefix); 91 } 92 93 public String getDefaultNamespacePrefix() { 94 return defaultPrefix; 95 } 96 97 public void setDefaultNamespacePrefix(String defaultPrefix) { 98 this.defaultPrefix = defaultPrefix; 99 } 100 101 public String getBaseURI() { 102 return baseURI; 103 } 104 105 /** 106 * @throws IllegalArgumentException {@inheritDoc} 107 */ 108 public void setBaseURI(String baseURI) { 109 if (baseURI != null) { 110 java.net.URI.create(baseURI); 111 } 112 this.baseURI = baseURI; 113 } 114 115 public URIDereferencer getURIDereferencer() { 116 return dereferencer; 117 } 118 119 public void setURIDereferencer(URIDereferencer dereferencer) { 120 this.dereferencer = dereferencer; 121 } 122 123 /** 124 * This implementation uses an internal {@link HashMap} to get the object 125 * that the specified name maps to. 126 * 127 * @throws NullPointerException {@inheritDoc} 128 */ 129 public Object getProperty(String name) { 130 if (name == null) { 131 throw new NullPointerException("name is null"); 132 } 133 return propMap.get(name); 134 } 135 136 /** 137 * This implementation uses an internal {@link HashMap} to map the name 138 * to the specified object. 139 * 140 * @throws NullPointerException {@inheritDoc} 141 */ 142 public Object setProperty(String name, Object value) { 143 if (name == null) { 144 throw new NullPointerException("name is null"); 145 } 146 return propMap.put(name, value); 147 } 148 149 public KeySelector getKeySelector() { 150 return ks; 151 } 152 153 public void setKeySelector(KeySelector ks) { 154 this.ks = ks; 155 } 156 157 /** 158 * Returns the <code>Element</code> with the specified ID attribute value. 159 * 160 * <p>This implementation uses an internal {@link HashMap} to get the 161 * element that the specified attribute value maps to. 162 * 163 * @param idValue the value of the ID 164 * @return the <code>Element</code> with the specified ID attribute value, 165 * or <code>null</code> if none. 166 * @throws NullPointerException if <code>idValue</code> is <code>null</code> 167 * @see #setIdAttributeNS 168 */ 169 public Element getElementById(String idValue) { 170 if (idValue == null) { 171 throw new NullPointerException("idValue is null"); 172 } 173 return (Element) idMap.get(idValue); 174 } 175 176 /** 177 * Registers the element's attribute specified by the namespace URI and 178 * local name to be of type ID. The attribute must have a non-empty value. 179 * 180 * <p>This implementation uses an internal {@link HashMap} to map the 181 * attribute's value to the specified element. 182 * 183 * @param element the element 184 * @param namespaceURI the namespace URI of the attribute (specify 185 * <code>null</code> if not applicable) 186 * @param localName the local name of the attribute 187 * @throws IllegalArgumentException if <code>localName</code> is not an 188 * attribute of the specified element or it does not contain a specific 189 * value 190 * @throws NullPointerException if <code>element</code> or 191 * <code>localName</code> is <code>null</code> 192 * @see #getElementById 193 */ 194 public void setIdAttributeNS(Element element, String namespaceURI, 195 String localName) { 196 if (element == null) { 197 throw new NullPointerException("element is null"); 198 } 199 if (localName == null) { 200 throw new NullPointerException("localName is null"); 201 } 202 String idValue = element.getAttributeNS(namespaceURI, localName); 203 if (idValue == null || idValue.length() == 0) { 204 throw new IllegalArgumentException(localName + " is not an " + 205 "attribute"); 206 } 207 idMap.put(idValue, element); 208 } 209 210 /** 211 * Returns a read-only iterator over the set of Id/Element mappings of 212 * this <code>DOMCryptoContext</code>. Attempts to modify the set via the 213 * {@link Iterator#remove} method throw an 214 * <code>UnsupportedOperationException</code>. The mappings are returned 215 * in no particular order. Each element in the iteration is represented as a 216 * {@link java.util.Map.Entry}. If the <code>DOMCryptoContext</code> is 217 * modified while an iteration is in progress, the results of the 218 * iteration are undefined. 219 * 220 * @return a read-only iterator over the set of mappings 221 */ 222 public Iterator iterator() { 223 return Collections.unmodifiableMap(idMap).entrySet().iterator(); 224 } 225 226 /** 227 * This implementation uses an internal {@link HashMap} to get the object 228 * that the specified key maps to. 229 */ 230 public Object get(Object key) { 231 return objMap.get(key); 232 } 233 234 /** 235 * This implementation uses an internal {@link HashMap} to map the key 236 * to the specified object. 237 * 238 * @throws IllegalArgumentException {@inheritDoc} 239 */ 240 public Object put(Object key, Object value) { 241 return objMap.put(key, value); 242 } 243 }