001    package com.thaiopensource.relaxng.parse.sax;
002    
003    import com.thaiopensource.xml.sax.XMLReaderCreator;
004    import com.thaiopensource.relaxng.parse.BuildException;
005    import com.thaiopensource.relaxng.parse.IllegalSchemaException;
006    import com.thaiopensource.relaxng.parse.Parseable;
007    import com.thaiopensource.relaxng.parse.ParsedPattern;
008    import com.thaiopensource.relaxng.parse.SchemaBuilder;
009    import com.thaiopensource.relaxng.parse.Scope;
010    
011    import org.xml.sax.EntityResolver;
012    import org.xml.sax.ErrorHandler;
013    import org.xml.sax.InputSource;
014    import org.xml.sax.SAXException;
015    import org.xml.sax.XMLReader;
016    
017    import java.io.IOException;
018    
019    public class SAXParseable extends SAXSubParser implements Parseable {
020      private final InputSource in;
021    
022      public SAXParseable(XMLReaderCreator xrc, InputSource in, ErrorHandler eh, EntityResolver er) {
023        super(xrc, eh, er);
024        this.in = in;
025      }
026    
027      public ParsedPattern parse(SchemaBuilder schemaBuilder, Scope scope) throws BuildException, IllegalSchemaException {
028        try {
029          XMLReader xr = xrc.createXMLReader();
030          SchemaParser sp = new SchemaParser(xr, eh, er, schemaBuilder, null, scope);
031          xr.parse(in);
032          return sp.getParsedPattern();
033        }
034        catch (SAXException e) {
035          throw toBuildException(e);
036        }
037        catch (IOException e) {
038          throw new BuildException(e);
039        }
040      }
041    
042    }