001    /*
002     * Common usable utilities
003     *
004     * Copyright (c) 2007
005     *   Petr Hadraba <hadrabap@bluetone.cz>
006     *
007     * Author: Petr Hadraba
008     *
009     * --
010     *
011     * XML Utilities
012     */
013    
014    package global.sandbox.xmlutilities;
015    
016    import java.util.ArrayList;
017    import java.util.HashMap;
018    import java.util.Iterator;
019    import java.util.Map;
020    import javax.xml.XMLConstants;
021    import javax.xml.namespace.NamespaceContext;
022    
023    /**
024     * Simple implementation of the NamespaceContext.
025     *
026     * @author Petr Hadraba
027     * 
028     * @version 1.1
029     */
030    public class NamespaceContextImpl implements NamespaceContext {
031        
032        /** stores namespace mappings */
033        private final Map<String, String> namespaces;
034        
035        /**
036         * Creates a new instance of NamespaceContextImpl and adds default
037         * namespace URIs
038         */
039        public NamespaceContextImpl() {
040            namespaces = new HashMap<String, String>(4);
041            
042            addDefaultNamespaces();
043        }
044        
045        /**
046         * adds namespace URI specified with prefix and URI
047         *
048         * @param prefix        prefix of the namespace
049         * @param namespaceURI  URI of the namespace
050         */
051        public void addNamespace(String prefix, String namespaceURI) {
052            namespaces.put(prefix, namespaceURI);
053        }
054        
055        /**
056         * adds all the namespace URIs that are stored in the specified Map
057         *
058         * @param namespaceURIs  URIs to add, key is prefix, value is URI
059         */
060        public void addNamespaces(Map<String, String> namespaceURIs) {
061            namespaces.putAll(namespaceURIs);
062        }
063        
064        /**
065         * adds default namespace URIs
066         *
067         * Default namespace URIs are DEFAULT_NS_PREFIX, XML_NS_PREFIX and
068         * XMLNS_ATTRIBUTE.
069         */
070        public void addDefaultNamespaces() {
071            namespaces.put(XMLConstants.DEFAULT_NS_PREFIX, XMLConstants.NULL_NS_URI);
072            namespaces.put(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
073            namespaces.put(XMLConstants.XMLNS_ATTRIBUTE, XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
074        }
075        
076        /**
077         * removes specified namespace using prefix
078         *
079         * @param prefix of the namespace
080         */
081        public void removeNamespaceByPrefix(String prefix) {
082            namespaces.remove(prefix);
083        }
084        
085        /**
086         * removes specified namespace using URI
087         *
088         * @param uri  URI of the namespace to remove
089         */
090        public void removeNamespaceByURI(String uri) {
091            final Iterator it = namespaces.keySet().iterator();
092            
093            while (it.hasNext()) {
094                if (((String) it.next()).equals(uri)) {
095                    it.remove();
096                }
097            }
098        }
099        
100        /**
101         * removes all the namespace URIs incl. default ones
102         */
103        public void removeAllNamespaces() {
104            namespaces.clear();
105        }
106        
107        /**
108         * returnes namespace URI that corresponds to the specified prefix
109         *
110         * @param prefix  URI of the namespace to obtain
111         *
112         * @return URI of the namespace or NULL_NS_URI if not found
113         */
114        public String getNamespaceURI(String prefix) {
115            if (prefix == null) {
116                throw new IllegalArgumentException(getClass().getPackage().getName()
117                        + "." + getClass().getName() + ".getNamespaceURI(null)");
118            }
119            
120            final String uri = namespaces.get(prefix);
121            
122            if (uri == null) {
123                return XMLConstants.NULL_NS_URI;
124            }
125            
126            return uri;
127        }
128        
129        /**
130         * returnes the prefix for the corresponding namespace URI
131         *
132         * this method returnes the first occurrence of the prefix for given URI
133         *
134         * @param namespaceURI  namespace URI
135         *
136         * @return prefix for the URI or null if not found
137         */
138        public String getPrefix(String namespaceURI) {
139            if (namespaceURI == null) {
140                throw new IllegalArgumentException(getClass().getPackage().getName()
141                        + "." + getClass().getName() + ".getPrefix(null)");
142            }
143            
144            final Iterator it = namespaces.keySet().iterator();
145            
146            while (it.hasNext()) {
147                final String prefix = (String) it.next();
148                final String uri = namespaces.get(prefix);
149                
150                if (uri.equals(namespaceURI)) {
151                    return prefix;
152                }
153            }
154            
155            return null;
156        }
157        
158        /**
159         * returnes iterator for the set of all the prefixes that correspond to the
160         * specified URI
161         *
162         * @param namespaceURI  namespace URI
163         *
164         * @return set of prefixes (iterator) or empty set
165         */
166        public Iterator getPrefixes(String namespaceURI) {
167            if (namespaceURI == null) {
168                throw new IllegalArgumentException(getClass().getPackage().getName()
169                        + "." + getClass().getName() + ".getPrefixes(null)");
170            }
171            
172            ArrayList<String> prefixes = new ArrayList<String>();
173            
174            final Iterator it = namespaces.keySet().iterator();
175            
176            while (it.hasNext()) {
177                final String prefix = (String) it.next();
178                final String uri = namespaces.get(prefix);
179                
180                if (uri.equals(namespaceURI)) {
181                    prefixes.add(prefix);
182                }
183            }
184            
185            return prefixes.iterator();
186        }
187        
188    }