001: package org.kohsuke.rngom.parse.xml;
002:
003: import java.io.IOException;
004:
005: import org.kohsuke.rngom.ast.builder.BuildException;
006: import org.kohsuke.rngom.ast.builder.IncludedGrammar;
007: import org.kohsuke.rngom.ast.builder.SchemaBuilder;
008: import org.kohsuke.rngom.ast.builder.Scope;
009: import org.kohsuke.rngom.ast.om.ParsedPattern;
010: import org.kohsuke.rngom.parse.IllegalSchemaException;
011: import org.kohsuke.rngom.parse.Parseable;
012: import org.kohsuke.rngom.xml.sax.JAXPXMLReaderCreator;
013: import org.kohsuke.rngom.xml.sax.XMLReaderCreator;
014: import org.xml.sax.EntityResolver;
015: import org.xml.sax.ErrorHandler;
016: import org.xml.sax.InputSource;
017: import org.xml.sax.SAXException;
018: import org.xml.sax.XMLReader;
019:
020: /**
021: * RELAX NG schema in the XML syntax.
022: *
023: */
024: public class SAXParseable implements Parseable {
025: private final InputSource in;
026:
027: final XMLReaderCreator xrc;
028: final ErrorHandler eh;
029:
030: public SAXParseable(InputSource in, ErrorHandler eh,
031: XMLReaderCreator xrc) {
032: this .xrc = xrc;
033: this .eh = eh;
034: this .in = in;
035: }
036:
037: public SAXParseable(InputSource in, ErrorHandler eh) {
038: this (in, eh, new JAXPXMLReaderCreator());
039: }
040:
041: public ParsedPattern parse(SchemaBuilder schemaBuilder)
042: throws BuildException, IllegalSchemaException {
043: try {
044: XMLReader xr = xrc.createXMLReader();
045: SchemaParser sp = new SchemaParser(this , xr, eh,
046: schemaBuilder, null, null, "");
047: xr.parse(in);
048: ParsedPattern p = sp.getParsedPattern();
049: return schemaBuilder.expandPattern(p);
050: } catch (SAXException e) {
051: throw toBuildException(e);
052: } catch (IOException e) {
053: throw new BuildException(e);
054: }
055: }
056:
057: public ParsedPattern parseInclude(String uri,
058: SchemaBuilder schemaBuilder, IncludedGrammar g,
059: String inheritedNs) throws BuildException,
060: IllegalSchemaException {
061: try {
062: XMLReader xr = xrc.createXMLReader();
063: SchemaParser sp = new SchemaParser(this , xr, eh,
064: schemaBuilder, g, g, inheritedNs);
065: xr.parse(makeInputSource(xr, uri));
066: return sp.getParsedPattern();
067: } catch (SAXException e) {
068: throw SAXParseable.toBuildException(e);
069: } catch (IOException e) {
070: throw new BuildException(e);
071: }
072: }
073:
074: public ParsedPattern parseExternal(String uri,
075: SchemaBuilder schemaBuilder, Scope s, String inheritedNs)
076: throws BuildException, IllegalSchemaException {
077: try {
078: XMLReader xr = xrc.createXMLReader();
079: SchemaParser sp = new SchemaParser(this , xr, eh,
080: schemaBuilder, null, s, inheritedNs);
081: xr.parse(makeInputSource(xr, uri));
082: return sp.getParsedPattern();
083: } catch (SAXException e) {
084: throw SAXParseable.toBuildException(e);
085: } catch (IOException e) {
086: throw new BuildException(e);
087: }
088: }
089:
090: private static InputSource makeInputSource(XMLReader xr,
091: String systemId) throws IOException, SAXException {
092: EntityResolver er = xr.getEntityResolver();
093: if (er != null) {
094: InputSource inputSource = er.resolveEntity(null, systemId);
095: if (inputSource != null)
096: return inputSource;
097: }
098: return new InputSource(systemId);
099: }
100:
101: static BuildException toBuildException(SAXException e) {
102: Exception inner = e.getException();
103: if (inner instanceof BuildException)
104: throw (BuildException) inner;
105: throw new BuildException(e);
106: }
107: }
|