KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.thaiopensource.validate.xerces;
2
3 import com.thaiopensource.util.PropertyMap;
4 import com.thaiopensource.util.PropertyId;
5 import com.thaiopensource.validate.IncorrectSchemaException;
6 import com.thaiopensource.validate.Schema;
7 import com.thaiopensource.validate.SchemaReader;
8 import com.thaiopensource.validate.ValidateProperty;
9 import com.thaiopensource.validate.Option;
10 import com.thaiopensource.validate.xerces.SAXXMLErrorHandler;
11 import com.thaiopensource.validate.xerces.SchemaImpl;
12 import org.apache.xerces.parsers.CachingParserPool;
13 import org.apache.xerces.parsers.XMLGrammarPreparser;
14 import org.apache.xerces.util.SymbolTable;
15 import org.apache.xerces.util.SynchronizedSymbolTable;
16 import org.apache.xerces.util.XMLGrammarPoolImpl;
17 import org.apache.xerces.util.EntityResolverWrapper;
18 import org.apache.xerces.xni.XNIException;
19 import org.apache.xerces.xni.grammars.XMLGrammarDescription;
20 import org.apache.xerces.xni.grammars.XMLGrammarPool;
21 import org.apache.xerces.xni.parser.XMLInputSource;
22 import org.xml.sax.ErrorHandler JavaDoc;
23 import org.xml.sax.InputSource JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25 import org.xml.sax.EntityResolver JavaDoc;
26
27 import java.io.IOException JavaDoc;
28
29 class SchemaReaderImpl implements SchemaReader {
30   private static final PropertyId[] supportedPropertyIds = {
31     ValidateProperty.ERROR_HANDLER,
32     ValidateProperty.ENTITY_RESOLVER,
33   };
34   public Schema createSchema(InputSource in, PropertyMap properties)
35           throws IOException JavaDoc, SAXException JavaDoc, IncorrectSchemaException {
36     SymbolTable symbolTable = new SymbolTable();
37     XMLGrammarPreparser preparser = new XMLGrammarPreparser(symbolTable);
38     XMLGrammarPool grammarPool = new XMLGrammarPoolImpl();
39     preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
40     preparser.setGrammarPool(grammarPool);
41     ErrorHandler eh = ValidateProperty.ERROR_HANDLER.get(properties);
42     SAXXMLErrorHandler xeh = new SAXXMLErrorHandler(eh);
43     preparser.setErrorHandler(xeh);
44     EntityResolver JavaDoc er = ValidateProperty.ENTITY_RESOLVER.get(properties);
45     if (er != null)
46       preparser.setEntityResolver(new EntityResolverWrapper(er));
47     try {
48       preparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA, toXMLInputSource(in));
49     }
50     catch (XNIException e) {
51       throw ValidatorImpl.toSAXException(e);
52     }
53     if (xeh.getHadError())
54       throw new IncorrectSchemaException();
55     return new SchemaImpl(new SynchronizedSymbolTable(symbolTable),
56                           new CachingParserPool.SynchronizedGrammarPool(grammarPool),
57                           properties,
58                           supportedPropertyIds);
59   }
60
61   public Option getOption(String JavaDoc uri) {
62     return null;
63   }
64
65   private static XMLInputSource toXMLInputSource(InputSource in) {
66     XMLInputSource xin = new XMLInputSource(in.getPublicId(), in.getSystemId(), null);
67     xin.setByteStream(in.getByteStream());
68     xin.setCharacterStream(in.getCharacterStream());
69     xin.setEncoding(in.getEncoding());
70     return xin;
71   }
72 }
73
Popular Tags