1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.exolab.castor.xml.schema.reader;
47
48 import org.exolab.castor.xml.AttributeSet;
49 import org.exolab.castor.xml.NamespacesStack;
50 import org.exolab.castor.xml.XMLException;
51 import org.exolab.castor.xml.util.AttributeSetImpl;
52 import org.xml.sax.AttributeList;
53 import org.xml.sax.DocumentHandler;
54 import org.xml.sax.Locator;
55 import org.xml.sax.SAXException;
56 import org.xml.sax.SAXParseException;
57
58
59
60
61
62
63
64
65 @SuppressWarnings("deprecation")
66 public final class Sax2ComponentReader implements DocumentHandler, org.xml.sax.ErrorHandler {
67
68 private static final String XMLNS = "xmlns";
69 private static final String XMLNS_PREFIX = XMLNS + ":";
70 private static final String XML_PREFIX = "xml";
71
72 private ComponentReader componentReader = null;
73
74
75
76
77 private NamespacesStack namespacesStack = null;
78
79 public Sax2ComponentReader(ComponentReader compReader) {
80 super();
81 componentReader = compReader;
82 namespacesStack = new NamespacesStack();
83 }
84
85
86
87
88
89
90
91
92
93
94 private AttributeSet processAttributeList(AttributeList atts) throws SAXException {
95
96 if (atts == null)
97 return new AttributeSetImpl(0);
98
99
100 int attCount = 0;
101 boolean[] validAtts = new boolean[atts.getLength()];
102 for (int i = 0; i < validAtts.length; i++) {
103 String attName = atts.getName(i);
104 if (attName.equals(XMLNS)) {
105 namespacesStack.addNamespace("", atts.getValue(i));
106 } else if (attName.startsWith(XMLNS_PREFIX)) {
107 String prefix = attName.substring(XMLNS_PREFIX.length());
108 namespacesStack.addNamespace(prefix, atts.getValue(i));
109 } else {
110 validAtts[i] = true;
111 ++attCount;
112 }
113 }
114
115 AttributeSetImpl attSet = null;
116 if (attCount > 0) {
117 attSet = new AttributeSetImpl(attCount);
118 for (int i = 0; i < validAtts.length; i++) {
119 if (!validAtts[i])
120 continue;
121 String namespace = null;
122 String attName = atts.getName(i);
123 int idx = attName.indexOf(':');
124 if (idx > 0) {
125 String prefix = attName.substring(0, idx);
126 if (!prefix.equals(XML_PREFIX)) {
127 attName = attName.substring(idx + 1);
128 namespace = namespacesStack.getNamespaceURI(prefix);
129 if (namespace == null) {
130 String error = "The namespace associated with " + "the prefix '" + prefix
131 + "' could not be resolved.";
132 throw new SAXException(error);
133
134 }
135 }
136 }
137 attSet.setAttribute(attName, atts.getValue(i), namespace);
138 }
139 } else
140 attSet = new AttributeSetImpl(0);
141
142 return attSet;
143
144 }
145
146 public void characters(char[] ch, int start, int length) throws org.xml.sax.SAXException {
147 try {
148 componentReader.characters(ch, start, length);
149 } catch (XMLException ex) {
150 throw new SAXException(ex);
151 }
152
153 }
154
155 public void endDocument() throws org.xml.sax.SAXException {
156
157 }
158
159 public void endElement(String name) throws org.xml.sax.SAXException {
160 String namespace = null;
161 int idx = name.indexOf(':');
162 if (idx >= 0) {
163 String prefix = name.substring(0, idx);
164 name = name.substring(idx + 1);
165 namespace = namespacesStack.getNamespaceURI(prefix);
166 } else
167 namespace = namespacesStack.getDefaultNamespaceURI();
168
169
170 namespacesStack.removeNamespaceScope();
171
172 try {
173 componentReader.endElement(name, namespace);
174 } catch (XMLException ex) {
175 throw new SAXException(ex);
176 }
177 }
178
179 public void ignorableWhitespace(char[] ch, int start, int length) throws org.xml.sax.SAXException {
180
181 }
182
183 public void processingInstruction(String target, String data) throws org.xml.sax.SAXException {
184
185 }
186
187 public void setDocumentLocator(Locator locator) {
188 componentReader.setDocumentLocator(locator);
189 }
190
191 public void startDocument() throws org.xml.sax.SAXException {
192
193 }
194
195 public void startElement(String name, AttributeList atts) throws org.xml.sax.SAXException {
196
197 namespacesStack.addNewNamespaceScope();
198
199
200 AttributeSet attSet = processAttributeList(atts);
201
202 String namespace = null;
203 int idx = name.indexOf(':');
204 if (idx >= 0) {
205 String prefix = name.substring(0, idx);
206 name = name.substring(idx + 1);
207 namespace = namespacesStack.getNamespaceURI(prefix);
208 } else {
209 namespace = namespacesStack.getNamespaceURI("");
210 }
211
212 try {
213 componentReader.startElement(name, namespace, attSet, namespacesStack.getCurrentNamespaceScope());
214 } catch (XMLException ex) {
215 throw new SAXException(ex);
216 }
217 }
218
219 public void error(SAXParseException exception) throws org.xml.sax.SAXException {
220 String systemId = exception.getSystemId();
221 String err = "Parsing Error : " + exception.getMessage() + '\n' + "Line : " + exception.getLineNumber() + '\n'
222 + "Column : " + exception.getColumnNumber() + '\n';
223 if (systemId != null) {
224 err = "In document: '" + systemId + "'\n" + err;
225 }
226
227 throw new SAXException(err);
228 }
229
230 public void fatalError(SAXParseException exception) throws org.xml.sax.SAXException {
231 String systemId = exception.getSystemId();
232 String err = "Parsing Error : " + exception.getMessage() + '\n' + "Line : " + exception.getLineNumber() + '\n'
233 + "Column : " + exception.getColumnNumber() + '\n';
234 if (systemId != null) {
235 err = "In document: '" + systemId + "'\n" + err;
236 }
237 throw new SAXException(err);
238 }
239
240 public void warning(SAXParseException exception) throws org.xml.sax.SAXException {
241 String systemId = exception.getSystemId();
242 String err = "Parsing Error : " + exception.getMessage() + '\n' + "Line : " + exception.getLineNumber() + '\n'
243 + "Column : " + exception.getColumnNumber() + '\n';
244 if (systemId != null) {
245 err = "In document: '" + systemId + "'\n" + err;
246 }
247 throw new SAXException(err);
248 }
249
250 }