1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
/* * Copyright 2006 Sun Microsystems, Inc. All rights reserved. */ /* * $Id: DOMCryptoContext.java,v 1.3 2005/05/09 18:33:26 mullan Exp $ */ package javax.xml.crypto.dom; import javax.xml.crypto.KeySelector; import javax.xml.crypto.URIDereferencer; import javax.xml.crypto.XMLCryptoContext; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import org.w3c.dom.Element; /** * This class provides a DOM-specific implementation of the * {@link XMLCryptoContext} interface. It also includes additional * methods that are specific to a DOM-based implementation for registering * and retrieving elements that contain attributes of type ID. * * @author Sean Mullan * @author JSR 105 Expert Group * @since 1.6 */ public class DOMCryptoContext implements XMLCryptoContext { private HashMap nsMap = new HashMap(); private HashMap idMap = new HashMap(); private HashMap objMap = new HashMap(); private String baseURI; private KeySelector ks; private URIDereferencer dereferencer; private HashMap propMap = new HashMap(); private String defaultPrefix; /** * Default constructor. (For invocation by subclass constructors). */ protected DOMCryptoContext() {} /** * This implementation uses an internal {@link HashMap} to get the prefix * that the specified URI maps to. It returns the <code>defaultPrefix</code> * if it maps to <code>null</code>. * * @throws NullPointerException {@inheritDoc} */ public String getNamespacePrefix(String namespaceURI, String defaultPrefix) { if (namespaceURI == null) { throw new NullPointerException("namespaceURI cannot be null"); } String prefix = (String) nsMap.get(namespaceURI); return (prefix != null ? prefix : defaultPrefix); } /** * This implementation uses an internal {@link HashMap} to map the URI * to the specified prefix. * * @throws NullPointerException {@inheritDoc} */ public String putNamespacePrefix(String namespaceURI, String prefix) { if (namespaceURI == null) { throw new NullPointerException("namespaceURI is null"); } return (String) nsMap.put(namespaceURI, prefix); } public String getDefaultNamespacePrefix() { return defaultPrefix; } public void setDefaultNamespacePrefix(String defaultPrefix) { this.defaultPrefix = defaultPrefix; } public String getBaseURI() { return baseURI; } /** * @throws IllegalArgumentException {@inheritDoc} */ public void setBaseURI(String baseURI) { if (baseURI != null) { java.net.URI.create(baseURI); } this.baseURI = baseURI; } public URIDereferencer getURIDereferencer() { return dereferencer; } public void setURIDereferencer(URIDereferencer dereferencer) { this.dereferencer = dereferencer; } /** * This implementation uses an internal {@link HashMap} to get the object * that the specified name maps to. * * @throws NullPointerException {@inheritDoc} */ public Object getProperty(String name) { if (name == null) { throw new NullPointerException("name is null"); } return propMap.get(name); } /** * This implementation uses an internal {@link HashMap} to map the name * to the specified object. * * @throws NullPointerException {@inheritDoc} */ public Object setProperty(String name, Object value) { if (name == null) { throw new NullPointerException("name is null"); } return propMap.put(name, value); } public KeySelector getKeySelector() { return ks; } public void setKeySelector(KeySelector ks) { this.ks = ks; } /** * Returns the <code>Element</code> with the specified ID attribute value. * * <p>This implementation uses an internal {@link HashMap} to get the * element that the specified attribute value maps to. * * @param idValue the value of the ID * @return the <code>Element</code> with the specified ID attribute value, * or <code>null</code> if none. * @throws NullPointerException if <code>idValue</code> is <code>null</code> * @see #setIdAttributeNS */ public Element getElementById(String idValue) { if (idValue == null) { throw new NullPointerException("idValue is null"); } return (Element) idMap.get(idValue); } /** * Registers the element's attribute specified by the namespace URI and * local name to be of type ID. The attribute must have a non-empty value. * * <p>This implementation uses an internal {@link HashMap} to map the * attribute's value to the specified element. * * @param element the element * @param namespaceURI the namespace URI of the attribute (specify * <code>null</code> if not applicable) * @param localName the local name of the attribute * @throws IllegalArgumentException if <code>localName</code> is not an * attribute of the specified element or it does not contain a specific * value * @throws NullPointerException if <code>element</code> or * <code>localName</code> is <code>null</code> * @see #getElementById */ public void setIdAttributeNS(Element element, String namespaceURI, String localName) { if (element == null) { throw new NullPointerException("element is null"); } if (localName == null) { throw new NullPointerException("localName is null"); } String idValue = element.getAttributeNS(namespaceURI, localName); if (idValue == null || idValue.length() == 0) { throw new IllegalArgumentException(localName + " is not an " + "attribute"); } idMap.put(idValue, element); } /** * Returns a read-only iterator over the set of Id/Element mappings of * this <code>DOMCryptoContext</code>. Attempts to modify the set via the * {@link Iterator#remove} method throw an * <code>UnsupportedOperationException</code>. The mappings are returned * in no particular order. Each element in the iteration is represented as a * {@link java.util.Map.Entry}. If the <code>DOMCryptoContext</code> is * modified while an iteration is in progress, the results of the * iteration are undefined. * * @return a read-only iterator over the set of mappings */ public Iterator iterator() { return Collections.unmodifiableMap(idMap).entrySet().iterator(); } /** * This implementation uses an internal {@link HashMap} to get the object * that the specified key maps to. */ public Object get(Object key) { return objMap.get(key); } /** * This implementation uses an internal {@link HashMap} to map the key * to the specified object. * * @throws IllegalArgumentException {@inheritDoc} */ public Object put(Object key, Object value) { return objMap.put(key, value); } }