001    /*
002     * Common usable utilities
003     *
004     * Copyright (c) 2006 Petr Hadraba <hadrabap@gmail.com>
005     *
006     * Author: Petr Hadraba
007     *
008     * --
009     *
010     * XML Utilities
011     */
012    
013    package global.sandbox.xmlutilities;
014    
015    import java.util.ArrayList;
016    import java.util.HashMap;
017    import java.util.Iterator;
018    import java.util.Map;
019    import javax.xml.XMLConstants;
020    import javax.xml.namespace.NamespaceContext;
021    
022    /**
023     * Simple implementation of the NamespaceContext.
024     *
025     * @author Petr Hadraba
026     *
027     * @version 1.1
028     */
029    public class NamespaceContextImpl implements NamespaceContext {
030    
031        /**
032         * Factory method which creates empty name space context <em>without</em> defaults.
033         *
034         * @return empty name space context
035         */
036        public static NamespaceContextImpl emptyNamespaceContext() {
037            return new NamespaceContextImpl(null);
038        }
039    
040        /**
041         * Factory method which creates empty name space context <em>with</em> defaults.
042         *
043         * @return new name space context with defaults
044         */
045        public static NamespaceContextImpl namespaceContextWithDefaults() {
046            return new NamespaceContextImpl();
047        }
048    
049        /**
050         * Stores name space mappings.
051         */
052        private final Map<String, String> namespaces = new HashMap<String, String>(4);
053    
054        /**
055         * Creates a new instance of NamespaceContextImpl and adds default name space URIs.
056         */
057        public NamespaceContextImpl() {
058            addDefaultNamespaces0();
059        }
060    
061        /**
062         * Internal constructor for empty context without defaults.
063         *
064         * @param v {@code null}
065         */
066        private NamespaceContextImpl(Void v) {
067            // do not populate defaults
068        }
069    
070        /**
071         * Adds name space URI specified with prefix and URI.
072         *
073         * @param prefix prefix of the name space
074         * @param namespaceURI URI of the name space
075         */
076        public void addNamespace(String prefix, String namespaceURI) {
077            namespaces.put(prefix, namespaceURI);
078        }
079    
080        /**
081         * Adds all the name space URIs that are stored in the specified Map.
082         *
083         * @param namespaceURIs URIs to add, key is prefix, value is URI
084         */
085        public void addNamespaces(Map<String, String> namespaceURIs) {
086            namespaces.putAll(namespaceURIs);
087        }
088    
089        /**
090         * Adds default name space URIs.
091         *
092         * Default name space URIs are {@code DEFAULT_NS_PREFIX}, {@code XML_NS_PREFIX} and {@code XMLNS_ATTRIBUTE}.
093         */
094        public void addDefaultNamespaces() {
095            addDefaultNamespaces0();
096        }
097    
098        /**
099         * Adds default name space URIs.
100         *
101         * Default name space URIs are {@code DEFAULT_NS_PREFIX}, {@code XML_NS_PREFIX} and {@code XMLNS_ATTRIBUTE}.
102         */
103        private void addDefaultNamespaces0() {
104            namespaces.put(XMLConstants.DEFAULT_NS_PREFIX, XMLConstants.NULL_NS_URI);
105            namespaces.put(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
106            namespaces.put(XMLConstants.XMLNS_ATTRIBUTE, XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
107        }
108    
109        /**
110         * Removes specified name space specified by prefix.
111         *
112         * @param prefix of the name space
113         */
114        public void removeNamespaceByPrefix(String prefix) {
115            namespaces.remove(prefix);
116        }
117    
118        /**
119         * Removes specified name space specified by URI.
120         *
121         * @param uri URI of the name space to remove
122         */
123        public void removeNamespaceByURI(String uri) {
124            final Iterator<String> it = namespaces.keySet().iterator();
125    
126            while (it.hasNext()) {
127                if (it.next().equals(uri)) {
128                    it.remove();
129                }
130            }
131        }
132    
133        /**
134         * Removes all the name space URIs including default ones.
135         */
136        public void removeAllNamespaces() {
137            namespaces.clear();
138        }
139    
140        /**
141         * Returns name space URI that corresponds to the specified prefix.
142         *
143         * @param prefix URI of the name space to obtain
144         *
145         * @return URI of the name space or {@code NULL_NS_URI} if not found
146         */
147        @Override
148        public String getNamespaceURI(String prefix) {
149            if (prefix == null) {
150                throw new IllegalArgumentException("Prefix can't be null.");
151            }
152    
153            final String uri = namespaces.get(prefix);
154    
155            if (uri == null) {
156                return XMLConstants.NULL_NS_URI;
157            }
158    
159            return uri;
160        }
161    
162        /**
163         * Returns the prefix for the corresponding name space URI.
164         *
165         * @param namespaceURI name space URI
166         *
167         * @return prefix for the URI or {@code null} if not found
168         */
169        @Override
170        public String getPrefix(String namespaceURI) {
171            if (namespaceURI == null) {
172                throw new IllegalArgumentException("namespaceURI can't be null.");
173            }
174    
175            final Iterator<String> it = namespaces.keySet().iterator();
176    
177            while (it.hasNext()) {
178                final String prefix = it.next();
179                final String uri = namespaces.get(prefix);
180    
181                if (uri.equals(namespaceURI)) {
182                    return prefix;
183                }
184            }
185    
186            return null;
187        }
188    
189        /**
190         * Returns iterator for the set of all the prefixes that correspond to the specified URI.
191         *
192         * @param namespaceURI name space URI
193         *
194         * @return set of prefixes (iterator) or empty set
195         */
196        @Override
197        public Iterator<String> getPrefixes(String namespaceURI) {
198            if (namespaceURI == null) {
199                throw new IllegalArgumentException("namespaceURI can't be null.");
200            }
201    
202            ArrayList<String> prefixes = new ArrayList<String>();
203    
204            final Iterator<String> it = namespaces.keySet().iterator();
205    
206            while (it.hasNext()) {
207                final String prefix = it.next();
208                final String uri = namespaces.get(prefix);
209    
210                if (uri.equals(namespaceURI)) {
211                    prefixes.add(prefix);
212                }
213            }
214    
215            return prefixes.iterator();
216        }
217    
218    }