KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > crypto > dom > DOMCryptoContext


1 /*
2  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
3  */

4 /*
5  * $Id: DOMCryptoContext.java,v 1.3 2005/05/09 18:33:26 mullan Exp $
6  */

7 package javax.xml.crypto.dom;
8
9 import javax.xml.crypto.KeySelector;
10 import javax.xml.crypto.URIDereferencer;
11 import javax.xml.crypto.XMLCryptoContext;
12 import java.util.Collections JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import org.w3c.dom.Element JavaDoc;
16
17 /**
18  * This class provides a DOM-specific implementation of the
19  * {@link XMLCryptoContext} interface. It also includes additional
20  * methods that are specific to a DOM-based implementation for registering
21  * and retrieving elements that contain attributes of type ID.
22  *
23  * @author Sean Mullan
24  * @author JSR 105 Expert Group
25  * @since 1.6
26  */

27 public class DOMCryptoContext implements XMLCryptoContext {
28
29     private HashMap JavaDoc nsMap = new HashMap JavaDoc();
30     private HashMap JavaDoc idMap = new HashMap JavaDoc();
31     private HashMap JavaDoc objMap = new HashMap JavaDoc();
32     private String JavaDoc baseURI;
33     private KeySelector ks;
34     private URIDereferencer dereferencer;
35     private HashMap JavaDoc propMap = new HashMap JavaDoc();
36     private String JavaDoc defaultPrefix;
37
38     /**
39      * Default constructor. (For invocation by subclass constructors).
40      */

41     protected DOMCryptoContext() {}
42
43     /**
44      * This implementation uses an internal {@link HashMap} to get the prefix
45      * that the specified URI maps to. It returns the <code>defaultPrefix</code>
46      * if it maps to <code>null</code>.
47      *
48      * @throws NullPointerException {@inheritDoc}
49      */

50     public String JavaDoc getNamespacePrefix(String JavaDoc namespaceURI,
51     String JavaDoc defaultPrefix) {
52         if (namespaceURI == null) {
53             throw new NullPointerException JavaDoc("namespaceURI cannot be null");
54         }
55     String JavaDoc prefix = (String JavaDoc) nsMap.get(namespaceURI);
56     return (prefix != null ? prefix : defaultPrefix);
57     }
58
59     /**
60      * This implementation uses an internal {@link HashMap} to map the URI
61      * to the specified prefix.
62      *
63      * @throws NullPointerException {@inheritDoc}
64      */

65     public String JavaDoc putNamespacePrefix(String JavaDoc namespaceURI, String JavaDoc prefix) {
66         if (namespaceURI == null) {
67             throw new NullPointerException JavaDoc("namespaceURI is null");
68         }
69         return (String JavaDoc) nsMap.put(namespaceURI, prefix);
70     }
71
72     public String JavaDoc getDefaultNamespacePrefix() {
73     return defaultPrefix;
74     }
75
76     public void setDefaultNamespacePrefix(String JavaDoc defaultPrefix) {
77     this.defaultPrefix = defaultPrefix;
78     }
79
80     public String JavaDoc getBaseURI() {
81         return baseURI;
82     }
83
84     /**
85      * @throws IllegalArgumentException {@inheritDoc}
86      */

87     public void setBaseURI(String JavaDoc baseURI) {
88     if (baseURI != null) {
89         java.net.URI.create(baseURI);
90     }
91         this.baseURI = baseURI;
92     }
93
94     public URIDereferencer getURIDereferencer() {
95         return dereferencer;
96     }
97
98     public void setURIDereferencer(URIDereferencer dereferencer) {
99         this.dereferencer = dereferencer;
100     }
101
102     /**
103      * This implementation uses an internal {@link HashMap} to get the object
104      * that the specified name maps to.
105      *
106      * @throws NullPointerException {@inheritDoc}
107      */

108     public Object JavaDoc getProperty(String JavaDoc name) {
109         if (name == null) {
110             throw new NullPointerException JavaDoc("name is null");
111         }
112         return propMap.get(name);
113     }
114
115     /**
116      * This implementation uses an internal {@link HashMap} to map the name
117      * to the specified object.
118      *
119      * @throws NullPointerException {@inheritDoc}
120      */

121     public Object JavaDoc setProperty(String JavaDoc name, Object JavaDoc value) {
122         if (name == null) {
123             throw new NullPointerException JavaDoc("name is null");
124         }
125         return propMap.put(name, value);
126     }
127
128     public KeySelector getKeySelector() {
129         return ks;
130     }
131
132     public void setKeySelector(KeySelector ks) {
133         this.ks = ks;
134     }
135
136     /**
137      * Returns the <code>Element</code> with the specified ID attribute value.
138      *
139      * <p>This implementation uses an internal {@link HashMap} to get the
140      * element that the specified attribute value maps to.
141      *
142      * @param idValue the value of the ID
143      * @return the <code>Element</code> with the specified ID attribute value,
144      * or <code>null</code> if none.
145      * @throws NullPointerException if <code>idValue</code> is <code>null</code>
146      * @see #setIdAttributeNS
147      */

148     public Element JavaDoc getElementById(String JavaDoc idValue) {
149         if (idValue == null) {
150             throw new NullPointerException JavaDoc("idValue is null");
151         }
152         return (Element JavaDoc) idMap.get(idValue);
153     }
154
155     /**
156      * Registers the element's attribute specified by the namespace URI and
157      * local name to be of type ID. The attribute must have a non-empty value.
158      *
159      * <p>This implementation uses an internal {@link HashMap} to map the
160      * attribute's value to the specified element.
161      *
162      * @param element the element
163      * @param namespaceURI the namespace URI of the attribute (specify
164      * <code>null</code> if not applicable)
165      * @param localName the local name of the attribute
166      * @throws IllegalArgumentException if <code>localName</code> is not an
167      * attribute of the specified element or it does not contain a specific
168      * value
169      * @throws NullPointerException if <code>element</code> or
170      * <code>localName</code> is <code>null</code>
171      * @see #getElementById
172      */

173     public void setIdAttributeNS(Element JavaDoc element, String JavaDoc namespaceURI,
174     String JavaDoc localName) {
175     if (element == null) {
176         throw new NullPointerException JavaDoc("element is null");
177     }
178     if (localName == null) {
179         throw new NullPointerException JavaDoc("localName is null");
180     }
181     String JavaDoc idValue = element.getAttributeNS(namespaceURI, localName);
182     if (idValue == null || idValue.length() == 0) {
183         throw new IllegalArgumentException JavaDoc(localName + " is not an " +
184         "attribute");
185     }
186     idMap.put(idValue, element);
187     }
188
189     /**
190      * Returns a read-only iterator over the set of Id/Element mappings of
191      * this <code>DOMCryptoContext</code>. Attempts to modify the set via the
192      * {@link Iterator#remove} method throw an
193      * <code>UnsupportedOperationException</code>. The mappings are returned
194      * in no particular order. Each element in the iteration is represented as a
195      * {@link java.util.Map.Entry}. If the <code>DOMCryptoContext</code> is
196      * modified while an iteration is in progress, the results of the
197      * iteration are undefined.
198      *
199      * @return a read-only iterator over the set of mappings
200      */

201     public Iterator JavaDoc iterator() {
202     return Collections.unmodifiableMap(idMap).entrySet().iterator();
203     }
204
205     /**
206      * This implementation uses an internal {@link HashMap} to get the object
207      * that the specified key maps to.
208      */

209     public Object JavaDoc get(Object JavaDoc key) {
210     return objMap.get(key);
211     }
212
213     /**
214      * This implementation uses an internal {@link HashMap} to map the key
215      * to the specified object.
216      *
217      * @throws IllegalArgumentException {@inheritDoc}
218      */

219     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
220     return objMap.put(key, value);
221     }
222 }
223
Popular Tags