1 package org.codehaus.mojo.jaxb2.schemageneration.postprocessing.schemaenhancement;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.codehaus.mojo.jaxb2.schemageneration.XsdGeneratorHelper;
23 import org.codehaus.mojo.jaxb2.schemageneration.postprocessing.NodeProcessor;
24 import org.codehaus.plexus.util.IOUtil;
25 import org.w3c.dom.Attr;
26 import org.w3c.dom.Document;
27 import org.w3c.dom.Element;
28 import org.w3c.dom.Node;
29
30 import javax.xml.XMLConstants;
31 import javax.xml.namespace.NamespaceContext;
32 import java.io.File;
33 import java.io.FileNotFoundException;
34 import java.io.FileReader;
35 import java.io.Reader;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.Iterator;
39 import java.util.Map;
40
41
42
43
44
45
46
47
48
49 public class SimpleNamespaceResolver implements NamespaceContext {
50
51
52 private static final String DEFAULT_NS = "DEFAULT";
53 private static final String TARGET_NAMESPACE = "targetNamespace";
54 private static final String SCHEMA = "schema";
55
56
57 private String sourceFilename;
58 private String localNamespaceURI;
59 private Map<String, String> prefix2Uri = new HashMap<String, String>();
60 private Map<String, String> uri2Prefix = new HashMap<String, String>();
61
62
63
64
65
66
67
68 public SimpleNamespaceResolver(final File xmlFile) {
69 this.sourceFilename = xmlFile.getName();
70
71 Reader reader = null;
72 try {
73 reader = new FileReader(xmlFile);
74 initialize(reader);
75 } catch (FileNotFoundException e) {
76 throw new IllegalArgumentException("File [" + xmlFile + "] could not be found.");
77 } finally {
78 IOUtil.close(reader);
79 }
80 }
81
82
83
84
85 public String getNamespaceURI(final String prefix) {
86 if (prefix == null) {
87
88 throw new IllegalArgumentException("Cannot handle null prefix argument.");
89 }
90
91 return prefix2Uri.get(XMLConstants.DEFAULT_NS_PREFIX.equals(prefix) ? DEFAULT_NS : prefix);
92 }
93
94
95
96
97 public String getPrefix(final String namespaceURI) {
98 if (namespaceURI == null) {
99
100 throw new IllegalArgumentException("Cannot acquire prefix for null namespaceURI.");
101 }
102
103 return uri2Prefix.get(namespaceURI);
104 }
105
106
107
108
109 public Iterator<String> getPrefixes(String namespaceURI) {
110 if (namespaceURI == null) {
111
112 throw new IllegalArgumentException("Cannot acquire prefixes for null namespaceURI.");
113 }
114
115 return Collections.singletonList(uri2Prefix.get(namespaceURI)).iterator();
116 }
117
118
119
120
121 public Map<String, String> getNamespaceURI2PrefixMap() {
122 return Collections.unmodifiableMap(uri2Prefix);
123 }
124
125
126
127
128 public String getLocalNamespaceURI() {
129 return localNamespaceURI;
130 }
131
132
133
134
135 public String getSourceFilename() {
136 return sourceFilename;
137 }
138
139
140
141
142
143
144
145
146
147
148 private void initialize(final Reader xmlFileStream) {
149
150 final Document parsedDocument = XsdGeneratorHelper.parseXmlStream(xmlFileStream);
151
152
153 XsdGeneratorHelper.process(parsedDocument.getFirstChild(), true, new NamespaceAttributeNodeProcessor());
154 }
155
156 class NamespaceAttributeNodeProcessor
157 implements NodeProcessor {
158
159
160
161
162
163
164 public boolean accept(Node aNode) {
165 if (aNode.getNamespaceURI() != null && XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(
166 aNode.getNamespaceURI())) {
167 return true;
168 }
169
170
171 if (aNode instanceof Attr) {
172
173 final Attr attribute = (Attr) aNode;
174 final Element parent = attribute.getOwnerElement();
175 if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(parent.getNamespaceURI())
176 && SCHEMA.equalsIgnoreCase(parent.getLocalName())
177 && TARGET_NAMESPACE.equals(attribute.getLocalName())) {
178
179 SimpleNamespaceResolver.this.localNamespaceURI = attribute.getNodeValue();
180 }
181 }
182
183
184 return false;
185 }
186
187
188
189
190
191
192 public void process(final Node aNode) {
193
194
195 final String cacheKey = XMLConstants.XMLNS_ATTRIBUTE.equals(aNode.getNodeName())
196 ? DEFAULT_NS
197 : aNode.getLocalName();
198 final String nodeValue = aNode.getNodeValue();
199
200
201 final String oldUriValue = prefix2Uri.put(cacheKey, nodeValue);
202 final String oldPrefixValue = uri2Prefix.put(nodeValue, cacheKey);
203
204
205 if (oldUriValue != null) {
206 throw new IllegalStateException(
207 "Replaced URI [" + oldUriValue + "] with [" + aNode.getNodeValue() + "] for prefix [" + cacheKey
208 + "]");
209 }
210 if (oldPrefixValue != null) {
211 throw new IllegalStateException(
212 "Replaced prefix [" + oldPrefixValue + "] with [" + cacheKey + "] for URI [" + aNode.getNodeValue()
213 + "]");
214 }
215 }
216 }
217 }