comparison artifacts/src/main/java/org/dive4elements/artifacts/ArtifactNamespaceContext.java @ 471:1a87cb24a446

Moved directories to org.dive4elements
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 10:50:31 +0200
parents artifacts/src/main/java/de/intevation/artifacts/ArtifactNamespaceContext.java@c40729bfe06d
children 415df0fc4fa1
comparison
equal deleted inserted replaced
470:19cb9729bd17 471:1a87cb24a446
1 /*
2 * Copyright (c) 2010 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8
9 package de.intevation.artifacts;
10
11 import java.util.Iterator;
12 import java.util.Map;
13 import java.util.HashMap;
14 import java.util.ArrayList;
15
16 import javax.xml.XMLConstants;
17
18 import javax.xml.namespace.NamespaceContext;
19
20 /**
21 * The namespace used in artifact documents.
22 * @author <a href="mailto:sascha.teichmann@intevation">Sascha L. Teichmann</a>
23 */
24 public class ArtifactNamespaceContext
25 implements NamespaceContext
26 {
27 /**
28 * The URI of the namespace of the artifacts.
29 */
30 public final static String NAMESPACE_URI =
31 "http://www.intevation.de/2009/artifacts";
32
33 /**
34 * The XML prefix for the artifacts namespace.
35 */
36 public final static String NAMESPACE_PREFIX = "art";
37
38 /**
39 * Final instance to be easily used to avoid creation
40 * of instances.
41 */
42 public static final ArtifactNamespaceContext INSTANCE =
43 new ArtifactNamespaceContext();
44
45 protected Map<String, String> map;
46
47 /**
48 * The default constructor.
49 */
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);
64 }
65
66 /**
67 * @see javax.xml.namespace.NamespaceContext#getNamespaceURI(String)
68 * @param prefix The prefix
69 * @return The corresponing URI
70 */
71 @Override
72 public String getNamespaceURI(String prefix) {
73
74 if (prefix == null) {
75 throw new IllegalArgumentException("Null prefix");
76 }
77
78 String namespace = map.get(prefix);
79
80 return namespace != null ? namespace : XMLConstants.NULL_NS_URI;
81 }
82
83 /**
84 * @see javax.xml.namespace.NamespaceContext#getPrefix(String)
85 * @param uri The URI
86 */
87 @Override
88 public String getPrefix(String uri) {
89
90 if (uri == null) {
91 throw new IllegalArgumentException("Null uri");
92 }
93
94 for (Map.Entry<String, String> entry: map.entrySet()) {
95 if (entry.getValue().equals(uri)) {
96 return entry.getKey();
97 }
98 }
99
100 return XMLConstants.DEFAULT_NS_PREFIX;
101 }
102
103 /**
104 * @see javax.xml.namespace.NamespaceContext#getPrefixes(java.lang.String)
105 * @param uri The URI
106 */
107 @Override
108 public Iterator getPrefixes(String uri) {
109 ArrayList<String> results = new ArrayList<String>();
110 for (Map.Entry<String, String> entry: map.entrySet()) {
111 if (entry.getValue().equals(uri)) {
112 results.add(entry.getKey());
113 }
114 }
115 return results.iterator();
116 }
117 }
118 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org