Mercurial > dive4elements > framework
comparison artifacts/src/main/java/de/intevation/artifacts/ArtifactNamespaceContext.java @ 298:a5e6d1923c95
Made namespace resolution more compatible.
artifacts/trunk@2383 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 21 Jul 2011 08:32:34 +0000 |
parents | 933bbc9fc11f |
children | b0a949d3fe09 |
comparison
equal
deleted
inserted
replaced
297:694d818e99b2 | 298:a5e6d1923c95 |
---|---|
7 */ | 7 */ |
8 | 8 |
9 package de.intevation.artifacts; | 9 package de.intevation.artifacts; |
10 | 10 |
11 import java.util.Iterator; | 11 import java.util.Iterator; |
12 import java.util.Map; | |
13 import java.util.HashMap; | |
14 import java.util.ArrayList; | |
12 | 15 |
13 import javax.xml.XMLConstants; | 16 import javax.xml.XMLConstants; |
14 | 17 |
15 import javax.xml.namespace.NamespaceContext; | 18 import javax.xml.namespace.NamespaceContext; |
16 | 19 |
37 * of instances. | 40 * of instances. |
38 */ | 41 */ |
39 public static final ArtifactNamespaceContext INSTANCE = | 42 public static final ArtifactNamespaceContext INSTANCE = |
40 new ArtifactNamespaceContext(); | 43 new ArtifactNamespaceContext(); |
41 | 44 |
45 protected Map<String, String> map; | |
46 | |
42 /** | 47 /** |
43 * The default constructor. | 48 * The default constructor. |
44 */ | 49 */ |
45 public ArtifactNamespaceContext() { | 50 public ArtifactNamespaceContext() { |
51 map = new HashMap<String, String>(); | |
52 map.put( | |
53 XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI); | |
54 map.put( | |
55 XMLConstants.DEFAULT_NS_PREFIX, XMLConstants.DEFAULT_NS_PREFIX); | |
56 map.put( | |
57 XMLConstants.XMLNS_ATTRIBUTE, XMLConstants.XMLNS_ATTRIBUTE_NS_URI); | |
58 map.put( | |
59 NAMESPACE_PREFIX, NAMESPACE_URI); | |
60 } | |
61 | |
62 public void add(String prefix, String uri) { | |
63 map.put(prefix, uri); | |
46 } | 64 } |
47 | 65 |
48 /** | 66 /** |
49 * @see javax.xml.namespace.NamespaceContext#getNamespaceURI(String) | 67 * @see javax.xml.namespace.NamespaceContext#getNamespaceURI(String) |
50 * @param prefix The prefix | 68 * @param prefix The prefix |
51 * @return The corresponing URI | 69 * @return The corresponing URI |
52 */ | 70 */ |
53 public String getNamespaceURI(String prefix) { | 71 public String getNamespaceURI(String prefix) { |
54 | 72 |
55 if (prefix == null) { | 73 if (prefix == null) { |
56 throw new NullPointerException("Null prefix"); | 74 throw new IllegalArgumentException("Null prefix"); |
57 } | 75 } |
58 | 76 |
59 if (NAMESPACE_PREFIX.equals(prefix)) { | 77 String namespace = map.get(prefix); |
60 return NAMESPACE_URI; | |
61 } | |
62 | 78 |
63 if ("xml".equals(prefix)) { | 79 return namespace != null ? namespace : XMLConstants.NULL_NS_URI; |
64 return XMLConstants.XML_NS_URI; | |
65 } | |
66 | |
67 return XMLConstants.NULL_NS_URI; | |
68 } | 80 } |
69 | 81 |
70 /** | 82 /** |
71 * @see javax.xml.namespace.NamespaceContext#getPrefix(String) | 83 * @see javax.xml.namespace.NamespaceContext#getPrefix(String) |
72 * @param uri The URI | 84 * @param uri The URI |
73 * @return nothing. | |
74 * @throws java.lang.UnsupportedOperationException | |
75 */ | 85 */ |
76 public String getPrefix(String uri) { | 86 public String getPrefix(String uri) { |
77 throw new UnsupportedOperationException(); | 87 |
88 if (uri == null) { | |
89 throw new IllegalArgumentException("Null uri"); | |
90 } | |
91 | |
92 for (Map.Entry<String, String> entry: map.entrySet()) { | |
93 if (entry.getValue().equals(uri)) { | |
94 return entry.getKey(); | |
95 } | |
96 } | |
97 | |
98 return XMLConstants.DEFAULT_NS_PREFIX; | |
78 } | 99 } |
79 | 100 |
80 /** | 101 /** |
81 * @see javax.xml.namespace.NamespaceContext#getPrefixes(java.lang.String) | 102 * @see javax.xml.namespace.NamespaceContext#getPrefixes(java.lang.String) |
82 * @param uri The URI | 103 * @param uri The URI |
83 * @return nothing | |
84 * @throws java.lang.UnsupportedOperationException | |
85 */ | 104 */ |
86 public Iterator getPrefixes(String uri) { | 105 public Iterator getPrefixes(String uri) { |
87 throw new UnsupportedOperationException(); | 106 ArrayList<String> results = new ArrayList<String>(); |
107 for (Map.Entry<String, String> entry: map.entrySet()) { | |
108 if (entry.getValue().equals(uri)) { | |
109 results.add(entry.getKey()); | |
110 } | |
111 } | |
112 return results.iterator(); | |
88 } | 113 } |
89 } | 114 } |
90 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 115 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |