1 package com.thaiopensource.relaxng.parse.sax; 2 3 import com.thaiopensource.relaxng.parse.SubParser; 4 import com.thaiopensource.relaxng.parse.ParsedPattern; 5 import com.thaiopensource.relaxng.parse.SchemaBuilder; 6 import com.thaiopensource.relaxng.parse.IncludedGrammar; 7 import com.thaiopensource.relaxng.parse.BuildException; 8 import com.thaiopensource.relaxng.parse.IllegalSchemaException; 9 import com.thaiopensource.relaxng.parse.Scope; 10 import com.thaiopensource.xml.sax.XMLReaderCreator; 11 import org.xml.sax.XMLReader ; 12 import org.xml.sax.SAXException ; 13 import org.xml.sax.InputSource ; 14 import org.xml.sax.EntityResolver ; 15 import org.xml.sax.ErrorHandler ; 16 17 import java.io.IOException ; 18 19 public class SAXSubParser implements SubParser { 20 final XMLReaderCreator xrc; 21 final ErrorHandler eh; 22 23 SAXSubParser(XMLReaderCreator xrc, ErrorHandler eh) { 24 this.xrc = xrc; 25 this.eh = eh; 26 } 27 28 public ParsedPattern parseInclude(String uri, SchemaBuilder schemaBuilder, IncludedGrammar g) 29 throws BuildException, IllegalSchemaException { 30 try { 31 XMLReader xr = xrc.createXMLReader(); 32 SchemaParser sp = new SchemaParser(xr, eh, schemaBuilder, g, g); 33 xr.parse(makeInputSource(xr, uri)); 34 return sp.getParsedPattern(); 35 } 36 catch (SAXException e) { 37 throw SAXParseable.toBuildException(e); 38 } 39 catch (IOException e) { 40 throw new BuildException(e); 41 } 42 } 43 44 public ParsedPattern parseExternal(String uri, SchemaBuilder schemaBuilder, Scope s) 45 throws BuildException, IllegalSchemaException { 46 try { 47 XMLReader xr = xrc.createXMLReader(); 48 SchemaParser sp = new SchemaParser(xr, eh, schemaBuilder, null, s); 49 xr.parse(makeInputSource(xr, uri)); 50 return sp.getParsedPattern(); 51 } 52 catch (SAXException e) { 53 throw SAXParseable.toBuildException(e); 54 } 55 catch (IOException e) { 56 throw new BuildException(e); 57 } 58 } 59 60 private static InputSource makeInputSource(XMLReader xr, String systemId) throws IOException , SAXException { 61 EntityResolver er = xr.getEntityResolver(); 62 if (er != null) { 63 InputSource inputSource = er.resolveEntity(null, systemId); 64 if (inputSource != null) 65 return inputSource; 66 } 67 return new InputSource (systemId); 68 } 69 70 static BuildException toBuildException(SAXException e) { 71 Exception inner = e.getException(); 72 if (inner instanceof BuildException) 73 throw (BuildException)inner; 74 throw new BuildException(e); 75 } 76 } 77 | Popular Tags |