View Javadoc

1   /*
2    * $Id: NamespaceContextMap.java,v 1.3 2006/05/13 10:25:24 edankert Exp $
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  
22  
23  package org.xmlhammer.gui.xpath;
24  
25  import java.util.ArrayList;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.List;
29  
30  import javax.xml.XMLConstants;
31  import javax.xml.namespace.NamespaceContext;
32  
33  /***
34   * Implementation of the a Namespace Context as a HashMap.
35   * 
36   * @version $Revision: 1.3 $, $Date: 2006/05/13 10:25:24 $
37   * @author Edwin Dankert <edankert@gmail.com>
38   */
39  
40  public class NamespaceContextMap extends HashMap<String,String> implements NamespaceContext {
41  
42  	private static final long serialVersionUID = 3257568403886650425L;
43      
44      public NamespaceContextMap() {
45          put(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
46          put(XMLConstants.XMLNS_ATTRIBUTE, XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
47      }
48  
49  	/***
50  	 * Get Namespace URI bound to a prefix in the current scope.
51  	 * 
52  	 * @param prefix the namespace prefix.
53  	 * @return the URI found for the prefix. 
54  	 * 
55  	 * @see javax.xml.namespace.NamespaceContext#getNamespaceURI(java.lang.String)
56  	 */
57  	public String getNamespaceURI(String prefix) {
58  		if (prefix == null) {
59  			throw new IllegalArgumentException("Prefix cannot be null!");
60  		}
61  
62  		String uri = get(prefix);
63          
64          if (uri != null) {
65              return uri;
66          }
67  		
68  		return XMLConstants.NULL_NS_URI;
69  	}
70  
71  	/***
72  	 * Return the prefix bound to the namespace uri, null if no prefix could be found.
73  	 * 
74  	 * @param namespaceURI the namespace URI.
75  	 * @return the prefix found for the URI.
76  	 * 
77  	 * @see javax.xml.namespace.NamespaceContext#getPrefix(java.lang.String)
78  	 */
79  	public String getPrefix(String namespaceURI) {
80  		if ( namespaceURI == null) {
81  			throw new IllegalArgumentException("Namespace URI cannot be null.");
82  		}
83  
84  		for (String prefix : keySet()) {
85  			if (get(prefix).equals(namespaceURI)) {
86  				return prefix;
87  			}
88  		}
89  		
90  		return null;
91  	}
92  
93  	/***
94  	 * Return the list of prefixes bound to the namespace uri, null if no prefix 
95  	 * could be found.
96  	 * 
97  	 * @param namespaceURI the namespace URI.
98  	 * @return the prefixs bound to the URI.
99  	 * 
100 	 * @see javax.xml.namespace.NamespaceContext#getPrefix(java.lang.String)
101 	 */
102 	public Iterator getPrefixes(String namespaceURI) {
103 		if ( namespaceURI == null) {
104 			throw new IllegalArgumentException("Namespace URI cannot be null.");
105 		}
106 
107 		List<String> prefixes = new ArrayList<String>();
108 		
109 		for ( String prefix : keySet()) {
110 			if (get(prefix).equals(namespaceURI)) {
111 				prefixes.add( prefix);
112 			}
113 		}
114 		
115 		return prefixes.iterator();
116 	}
117 }