001    package com.thaiopensource.relaxng.parse.sax;
002    
003    import com.thaiopensource.relaxng.parse.SubParser;
004    import com.thaiopensource.relaxng.parse.ParsedPattern;
005    import com.thaiopensource.relaxng.parse.SchemaBuilder;
006    import com.thaiopensource.relaxng.parse.IncludedGrammar;
007    import com.thaiopensource.relaxng.parse.BuildException;
008    import com.thaiopensource.relaxng.parse.IllegalSchemaException;
009    import com.thaiopensource.relaxng.parse.Scope;
010    import com.thaiopensource.xml.sax.XMLReaderCreator;
011    import org.xml.sax.XMLReader;
012    import org.xml.sax.SAXException;
013    import org.xml.sax.InputSource;
014    import org.xml.sax.EntityResolver;
015    import org.xml.sax.ErrorHandler;
016    
017    import java.io.IOException;
018    
019    public class SAXSubParser implements SubParser {
020      final XMLReaderCreator xrc;
021      final ErrorHandler eh;
022      final EntityResolver er;
023    
024      SAXSubParser(XMLReaderCreator xrc, ErrorHandler eh, EntityResolver er) {
025        this.xrc = xrc;
026        this.eh = eh;
027        this.er = er;
028      }
029    
030      public ParsedPattern parseInclude(String uri, SchemaBuilder schemaBuilder, IncludedGrammar g)
031              throws BuildException, IllegalSchemaException {
032        try {
033          XMLReader xr = xrc.createXMLReader();
034          SchemaParser sp = new SchemaParser(xr, eh, er, schemaBuilder, g, g);
035          xr.parse(makeInputSource(xr, uri));
036          return sp.getParsedPattern();
037        }
038        catch (SAXException e) {
039         throw SAXParseable.toBuildException(e);
040        }
041        catch (IOException e) {
042         throw new BuildException(e);
043        }
044      }
045    
046      public ParsedPattern parseExternal(String uri, SchemaBuilder schemaBuilder, Scope s)
047              throws BuildException, IllegalSchemaException {
048        try {
049          XMLReader xr = xrc.createXMLReader();
050          SchemaParser sp = new SchemaParser(xr, eh, er, schemaBuilder, null, s);
051          xr.parse(makeInputSource(xr, uri));
052          return sp.getParsedPattern();
053        }
054        catch (SAXException e) {
055          throw SAXParseable.toBuildException(e);
056        }
057        catch (IOException e) {
058          throw new BuildException(e);
059        }
060      }
061    
062      private static InputSource makeInputSource(XMLReader xr, String systemId) throws IOException, SAXException {
063        EntityResolver er = xr.getEntityResolver();
064        if (er != null) {
065          InputSource inputSource = er.resolveEntity(null, systemId);
066          if (inputSource != null)
067            return inputSource;
068        }
069        return new InputSource(systemId);
070      }
071    
072      static BuildException toBuildException(SAXException e) {
073        Exception inner = e.getException();
074        if (inner instanceof BuildException)
075          throw (BuildException)inner;
076        throw new BuildException(e);
077      }
078    }