KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > validate > xerces > ValidatorImpl


1 package com.thaiopensource.validate.xerces;
2
3 import com.thaiopensource.util.PropertyMap;
4 import com.thaiopensource.validate.ValidateProperty;
5 import com.thaiopensource.validate.Validator;
6 import org.apache.xerces.impl.XMLErrorReporter;
7 import org.apache.xerces.impl.validation.EntityState;
8 import org.apache.xerces.impl.validation.ValidationManager;
9 import org.apache.xerces.impl.xs.XMLSchemaValidator;
10 import org.apache.xerces.util.NamespaceSupport;
11 import org.apache.xerces.util.ParserConfigurationSettings;
12 import org.apache.xerces.util.SymbolTable;
13 import org.apache.xerces.util.XMLAttributesImpl;
14 import org.apache.xerces.util.XMLSymbols;
15 import org.apache.xerces.util.ErrorHandlerWrapper;
16 import org.apache.xerces.xni.NamespaceContext;
17 import org.apache.xerces.xni.QName;
18 import org.apache.xerces.xni.XMLAttributes;
19 import org.apache.xerces.xni.XMLLocator;
20 import org.apache.xerces.xni.XMLResourceIdentifier;
21 import org.apache.xerces.xni.XMLString;
22 import org.apache.xerces.xni.XNIException;
23 import org.apache.xerces.xni.grammars.XMLGrammarPool;
24 import org.apache.xerces.xni.parser.XMLComponent;
25 import org.apache.xerces.xni.parser.XMLEntityResolver;
26 import org.apache.xerces.xni.parser.XMLInputSource;
27 import org.apache.xerces.xni.parser.XMLParseException;
28 import org.apache.xerces.xni.parser.XMLErrorHandler;
29 import org.xml.sax.Attributes JavaDoc;
30 import org.xml.sax.Locator JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32 import org.xml.sax.SAXParseException JavaDoc;
33 import org.xml.sax.DTDHandler JavaDoc;
34 import org.xml.sax.ContentHandler JavaDoc;
35
36 import java.io.IOException JavaDoc;
37 import java.util.Hashtable JavaDoc;
38
39 class ValidatorImpl extends ParserConfigurationSettings implements Validator, ContentHandler JavaDoc, DTDHandler JavaDoc, XMLLocator, XMLEntityResolver, EntityState {
40
41   private final XMLSchemaValidator schemaValidator = new XMLSchemaValidator();
42   private final XMLErrorReporter errorReporter = new XMLErrorReporter();
43   private final ValidationManager validationManager = new ValidationManager();
44   private final NamespaceContext namespaceContext = new NamespaceSupport();
45   private final XMLAttributes attributes = new XMLAttributesImpl();
46   private final SymbolTable symbolTable;
47   private final XMLComponent[] components;
48   private Locator locator;
49   private final Hashtable JavaDoc entityTable = new Hashtable JavaDoc();
50   private boolean pushedContext = false;
51
52   // XXX deal with baseURI
53

54   static private final String JavaDoc[] recognizedFeatures = {
55     Features.SCHEMA_AUGMENT_PSVI,
56     Features.SCHEMA_FULL_CHECKING,
57     Features.VALIDATION,
58     Features.SCHEMA_VALIDATION,
59   };
60
61   static private final String JavaDoc[] recognizedProperties = {
62     Properties.XMLGRAMMAR_POOL,
63     Properties.SYMBOL_TABLE,
64     Properties.ERROR_REPORTER,
65     Properties.ERROR_HANDLER,
66     Properties.VALIDATION_MANAGER,
67     Properties.ENTITY_MANAGER,
68     Properties.ENTITY_RESOLVER,
69   };
70
71   ValidatorImpl(SymbolTable symbolTable, XMLGrammarPool grammarPool, PropertyMap properties) {
72     this.symbolTable = symbolTable;
73     XMLErrorHandler errorHandlerWrapper = new ErrorHandlerWrapper(ValidateProperty.ERROR_HANDLER.get(properties));
74     components = new XMLComponent[] { errorReporter, schemaValidator };
75     for (int i = 0; i < components.length; i++) {
76       addRecognizedFeatures(components[i].getRecognizedFeatures());
77       addRecognizedProperties(components[i].getRecognizedProperties());
78     }
79     addRecognizedFeatures(recognizedFeatures);
80     addRecognizedProperties(recognizedProperties);
81     setFeature(Features.SCHEMA_AUGMENT_PSVI, false);
82     setFeature(Features.SCHEMA_FULL_CHECKING, true);
83     setFeature(Features.VALIDATION, true);
84     setFeature(Features.SCHEMA_VALIDATION, true);
85     setProperty(Properties.XMLGRAMMAR_POOL, grammarPool);
86     setProperty(Properties.SYMBOL_TABLE, symbolTable);
87     errorReporter.setDocumentLocator(this);
88     setProperty(Properties.ERROR_REPORTER, errorReporter);
89     setProperty(Properties.ERROR_HANDLER, errorHandlerWrapper);
90     setProperty(Properties.VALIDATION_MANAGER, validationManager);
91     // In Xerces 2.4.0, XMLSchemaValidator uses ENTITY_MANAGER when
92
// it should use ENTITY_RESOLVER
93
setProperty(Properties.ENTITY_MANAGER, this);
94     setProperty(Properties.ENTITY_RESOLVER, this);
95     reset();
96   }
97
98   public void reset() {
99     validationManager.reset();
100     namespaceContext.reset();
101     for (int i = 0; i < components.length; i++)
102       components[i].reset(this);
103     validationManager.setEntityState(this);
104   }
105
106   public ContentHandler JavaDoc getContentHandler() {
107     return this;
108   }
109
110   public DTDHandler JavaDoc getDTDHandler() {
111     return this;
112   }
113
114   public void setDocumentLocator(Locator locator) {
115     this.locator = locator;
116   }
117
118   public void notationDecl(String JavaDoc name,
119                            String JavaDoc publicId,
120                            String JavaDoc systemId) {
121     // nothing needed
122
}
123
124   public void unparsedEntityDecl(String JavaDoc name,
125                                  String JavaDoc publicId,
126                                  String JavaDoc systemId,
127                                  String JavaDoc notationName) {
128     entityTable.put(name, name);
129   }
130
131   public boolean isEntityDeclared(String JavaDoc name) {
132     return entityTable.get(name) != null;
133   }
134
135   public boolean isEntityUnparsed(String JavaDoc name) {
136     return entityTable.get(name) != null;
137   }
138
139   public void startDocument()
140           throws SAXException JavaDoc {
141     try {
142       schemaValidator.startDocument(locator == null ? null : this, null, namespaceContext, null);
143     }
144     catch (XNIException e) {
145       throw toSAXException(e);
146     }
147   }
148
149   public void endDocument()
150           throws SAXException JavaDoc {
151     try {
152       schemaValidator.endDocument(null);
153     }
154     catch (XNIException e) {
155       throw toSAXException(e);
156     }
157   }
158
159   public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
160                            String JavaDoc qName, Attributes atts)
161           throws SAXException JavaDoc {
162     try {
163       if (!pushedContext)
164         namespaceContext.pushContext();
165       else
166         pushedContext = false;
167       for (int i = 0, len = atts.getLength(); i < len; i++)
168         attributes.addAttribute(makeQName(atts.getURI(i), atts.getLocalName(i), atts.getQName(i)),
169                                 symbolTable.addSymbol(atts.getType(i)),
170                                 atts.getValue(i));
171       schemaValidator.startElement(makeQName(namespaceURI, localName, qName), attributes, null);
172       attributes.removeAllAttributes();
173     }
174     catch (XNIException e) {
175       throw toSAXException(e);
176     }
177   }
178
179   public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
180                          String JavaDoc qName)
181           throws SAXException JavaDoc {
182     try {
183       schemaValidator.endElement(makeQName(namespaceURI, localName, qName), null);
184       namespaceContext.popContext();
185     }
186     catch (XNIException e) {
187       throw toSAXException(e);
188     }
189   }
190
191   public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
192           throws SAXException JavaDoc {
193     try {
194       if (!pushedContext) {
195         namespaceContext.pushContext();
196         pushedContext = true;
197       }
198       if (prefix == null)
199         prefix = XMLSymbols.EMPTY_STRING;
200       else
201         prefix = symbolTable.addSymbol(prefix);
202       if (uri != null) {
203         if (uri.equals(""))
204           uri = null;
205         else
206           uri = symbolTable.addSymbol(uri);
207       }
208       namespaceContext.declarePrefix(prefix, uri);
209     }
210     catch (XNIException e) {
211       throw toSAXException(e);
212     }
213   }
214
215   public void endPrefixMapping(String JavaDoc prefix)
216           throws SAXException JavaDoc {
217     // do nothing
218
}
219
220   public void characters(char ch[], int start, int length)
221           throws SAXException JavaDoc {
222     try {
223       schemaValidator.characters(new XMLString(ch, start, length), null);
224     }
225     catch (XNIException e) {
226       throw toSAXException(e);
227     }
228   }
229
230   public void ignorableWhitespace(char ch[], int start, int length)
231           throws SAXException JavaDoc {
232     try {
233       schemaValidator.ignorableWhitespace(new XMLString(ch, start, length), null);
234     }
235     catch (XNIException e) {
236       throw toSAXException(e);
237     }
238   }
239
240   public void processingInstruction(String JavaDoc target, String JavaDoc data)
241           throws SAXException JavaDoc {
242     // do nothing
243
}
244
245   public void skippedEntity(String JavaDoc name)
246           throws SAXException JavaDoc {
247     // do nothing
248
}
249
250   private QName makeQName(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) {
251     localName = symbolTable.addSymbol(localName);
252     String JavaDoc prefix;
253     if (namespaceURI.equals("")) {
254       namespaceURI = null;
255       prefix = XMLSymbols.EMPTY_STRING;
256       qName = localName;
257     }
258     else {
259       namespaceURI = symbolTable.addSymbol(namespaceURI);
260       if (qName.equals("")) {
261         prefix = namespaceContext.getPrefix(namespaceURI);
262         if (prefix == XMLSymbols.EMPTY_STRING)
263           qName = localName;
264         else if (prefix == null)
265           qName = localName; // XXX what to do?
266
else
267           qName = symbolTable.addSymbol(prefix + ":" + localName);
268       }
269       else {
270         qName = symbolTable.addSymbol(qName);
271         int colon = qName.indexOf(':');
272         if (colon > 0)
273           prefix = symbolTable.addSymbol(qName.substring(0, colon));
274         else
275           prefix = XMLSymbols.EMPTY_STRING;
276       }
277     }
278     return new QName(prefix, localName, qName, namespaceURI);
279   }
280
281   public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier)
282           throws XNIException, IOException JavaDoc {
283     return null;
284   }
285
286   public String JavaDoc getPublicId() {
287     return locator.getPublicId();
288   }
289
290   public String JavaDoc getEncoding() {
291     return null;
292   }
293
294   public String JavaDoc getBaseSystemId() {
295     return null;
296   }
297
298   public String JavaDoc getLiteralSystemId() {
299     return null;
300   }
301
302   public String JavaDoc getExpandedSystemId() {
303     return locator.getSystemId();
304   }
305
306   public int getLineNumber() {
307     return locator.getLineNumber();
308   }
309
310   public int getColumnNumber() {
311     return locator.getColumnNumber();
312   }
313
314   static SAXException JavaDoc toSAXException(XNIException e) {
315     if (e instanceof XMLParseException) {
316       XMLParseException pe = (XMLParseException)e;
317       return new SAXParseException JavaDoc(pe.getMessage(),
318                                    pe.getPublicId(),
319                                    pe.getExpandedSystemId(),
320                                    pe.getLineNumber(),
321                                    pe.getColumnNumber(),
322                                    pe.getException());
323     }
324     Exception JavaDoc nested = e.getException();
325     if (nested == null)
326       return new SAXException JavaDoc(e.getMessage());
327     if (nested instanceof SAXException JavaDoc)
328       return (SAXException JavaDoc)nested;
329     if (nested instanceof RuntimeException JavaDoc)
330       throw (RuntimeException JavaDoc)nested;
331     return new SAXException JavaDoc(nested);
332   }
333 }
334
Popular Tags