001 /*
002 * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 /*
026 * $Id: DOMCryptoContext.java,v 1.3 2005/05/09 18:33:26 mullan Exp $
027 */
028 package javax.xml.crypto.dom;
029
030 import javax.xml.crypto.KeySelector;
031 import javax.xml.crypto.URIDereferencer;
032 import javax.xml.crypto.XMLCryptoContext;
033 import java.util.Collections;
034 import java.util.HashMap;
035 import java.util.Iterator;
036 import org.w3c.dom.Element;
037
038 /**
039 * This class provides a DOM-specific implementation of the
040 * {@link XMLCryptoContext} interface. It also includes additional
041 * methods that are specific to a DOM-based implementation for registering
042 * and retrieving elements that contain attributes of type ID.
043 *
044 * @author Sean Mullan
045 * @author JSR 105 Expert Group
046 * @since 1.6
047 */
048 public class DOMCryptoContext implements XMLCryptoContext {
049
050 private HashMap nsMap = new HashMap();
051 private HashMap idMap = new HashMap();
052 private HashMap objMap = new HashMap();
053 private String baseURI;
054 private KeySelector ks;
055 private URIDereferencer dereferencer;
056 private HashMap propMap = new HashMap();
057 private String defaultPrefix;
058
059 /**
060 * Default constructor. (For invocation by subclass constructors).
061 */
062 protected DOMCryptoContext() {
063 }
064
065 /**
066 * This implementation uses an internal {@link HashMap} to get the prefix
067 * that the specified URI maps to. It returns the <code>defaultPrefix</code>
068 * if it maps to <code>null</code>.
069 *
070 * @throws NullPointerException {@inheritDoc}
071 */
072 public String getNamespacePrefix(String namespaceURI,
073 String defaultPrefix) {
074 if (namespaceURI == null) {
075 throw new NullPointerException(
076 "namespaceURI cannot be null");
077 }
078 String prefix = (String) nsMap.get(namespaceURI);
079 return (prefix != null ? prefix : defaultPrefix);
080 }
081
082 /**
083 * This implementation uses an internal {@link HashMap} to map the URI
084 * to the specified prefix.
085 *
086 * @throws NullPointerException {@inheritDoc}
087 */
088 public String putNamespacePrefix(String namespaceURI, String prefix) {
089 if (namespaceURI == null) {
090 throw new NullPointerException("namespaceURI is null");
091 }
092 return (String) nsMap.put(namespaceURI, prefix);
093 }
094
095 public String getDefaultNamespacePrefix() {
096 return defaultPrefix;
097 }
098
099 public void setDefaultNamespacePrefix(String defaultPrefix) {
100 this .defaultPrefix = defaultPrefix;
101 }
102
103 public String getBaseURI() {
104 return baseURI;
105 }
106
107 /**
108 * @throws IllegalArgumentException {@inheritDoc}
109 */
110 public void setBaseURI(String baseURI) {
111 if (baseURI != null) {
112 java.net.URI.create(baseURI);
113 }
114 this .baseURI = baseURI;
115 }
116
117 public URIDereferencer getURIDereferencer() {
118 return dereferencer;
119 }
120
121 public void setURIDereferencer(URIDereferencer dereferencer) {
122 this .dereferencer = dereferencer;
123 }
124
125 /**
126 * This implementation uses an internal {@link HashMap} to get the object
127 * that the specified name maps to.
128 *
129 * @throws NullPointerException {@inheritDoc}
130 */
131 public Object getProperty(String name) {
132 if (name == null) {
133 throw new NullPointerException("name is null");
134 }
135 return propMap.get(name);
136 }
137
138 /**
139 * This implementation uses an internal {@link HashMap} to map the name
140 * to the specified object.
141 *
142 * @throws NullPointerException {@inheritDoc}
143 */
144 public Object setProperty(String name, Object value) {
145 if (name == null) {
146 throw new NullPointerException("name is null");
147 }
148 return propMap.put(name, value);
149 }
150
151 public KeySelector getKeySelector() {
152 return ks;
153 }
154
155 public void setKeySelector(KeySelector ks) {
156 this .ks = ks;
157 }
158
159 /**
160 * Returns the <code>Element</code> with the specified ID attribute value.
161 *
162 * <p>This implementation uses an internal {@link HashMap} to get the
163 * element that the specified attribute value maps to.
164 *
165 * @param idValue the value of the ID
166 * @return the <code>Element</code> with the specified ID attribute value,
167 * or <code>null</code> if none.
168 * @throws NullPointerException if <code>idValue</code> is <code>null</code>
169 * @see #setIdAttributeNS
170 */
171 public Element getElementById(String idValue) {
172 if (idValue == null) {
173 throw new NullPointerException("idValue is null");
174 }
175 return (Element) idMap.get(idValue);
176 }
177
178 /**
179 * Registers the element's attribute specified by the namespace URI and
180 * local name to be of type ID. The attribute must have a non-empty value.
181 *
182 * <p>This implementation uses an internal {@link HashMap} to map the
183 * attribute's value to the specified element.
184 *
185 * @param element the element
186 * @param namespaceURI the namespace URI of the attribute (specify
187 * <code>null</code> if not applicable)
188 * @param localName the local name of the attribute
189 * @throws IllegalArgumentException if <code>localName</code> is not an
190 * attribute of the specified element or it does not contain a specific
191 * value
192 * @throws NullPointerException if <code>element</code> or
193 * <code>localName</code> is <code>null</code>
194 * @see #getElementById
195 */
196 public void setIdAttributeNS(Element element, String namespaceURI,
197 String localName) {
198 if (element == null) {
199 throw new NullPointerException("element is null");
200 }
201 if (localName == null) {
202 throw new NullPointerException("localName is null");
203 }
204 String idValue = element
205 .getAttributeNS(namespaceURI, localName);
206 if (idValue == null || idValue.length() == 0) {
207 throw new IllegalArgumentException(localName
208 + " is not an " + "attribute");
209 }
210 idMap.put(idValue, element);
211 }
212
213 /**
214 * Returns a read-only iterator over the set of Id/Element mappings of
215 * this <code>DOMCryptoContext</code>. Attempts to modify the set via the
216 * {@link Iterator#remove} method throw an
217 * <code>UnsupportedOperationException</code>. The mappings are returned
218 * in no particular order. Each element in the iteration is represented as a
219 * {@link java.util.Map.Entry}. If the <code>DOMCryptoContext</code> is
220 * modified while an iteration is in progress, the results of the
221 * iteration are undefined.
222 *
223 * @return a read-only iterator over the set of mappings
224 */
225 public Iterator iterator() {
226 return Collections.unmodifiableMap(idMap).entrySet().iterator();
227 }
228
229 /**
230 * This implementation uses an internal {@link HashMap} to get the object
231 * that the specified key maps to.
232 */
233 public Object get(Object key) {
234 return objMap.get(key);
235 }
236
237 /**
238 * This implementation uses an internal {@link HashMap} to map the key
239 * to the specified object.
240 *
241 * @throws IllegalArgumentException {@inheritDoc}
242 */
243 public Object put(Object key, Object value) {
244 return objMap.put(key, value);
245 }
246 }
|