1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ken.util;
17
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21
22 import javax.xml.namespace.NamespaceContext;
23
24 import org.apache.log4j.Logger;
25 import org.w3c.dom.Document;
26
27
28
29
30
31
32 public class DocumentNamespaceContext implements NamespaceContext {
33 private static final Logger LOG = Logger.getLogger(DocumentNamespaceContext.class);
34
35
36 private final Document doc;
37
38
39
40
41
42 public DocumentNamespaceContext(Document doc) {
43 this.doc = doc;
44 }
45
46
47
48
49 public String getNamespaceURI(String prefix) {
50 LOG.debug("getNamespaceURI(" + prefix + ")");
51 if (prefix == null) {
52 throw new IllegalArgumentException("The prefix cannot be null.");
53 }
54 if (prefix.length() == 0) {
55 return doc.lookupNamespaceURI(null);
56 }
57 return doc.lookupNamespaceURI(prefix);
58 }
59
60
61
62
63 public String getPrefix(String namespaceURI) {
64 LOG.debug("getPrefix(" + namespaceURI + ")");
65 if (namespaceURI == null) {
66 throw new IllegalArgumentException("The namespace uri cannot be null.");
67 }
68 return doc.lookupPrefix(namespaceURI);
69 }
70
71
72
73
74 public Iterator getPrefixes(String namespaceURI) {
75 LOG.debug("getPrefixes(" + namespaceURI + ")");
76 if (namespaceURI == null) {
77 throw new IllegalArgumentException("The namespace uri cannot be null.");
78 }
79 List<String> list = new ArrayList<String>(1);
80 String s = getPrefix(namespaceURI);
81 if (s != null) {
82 list.add(s);
83 }
84 return list.iterator();
85 }
86 }