KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > relaxng > parse > sax > SAXSubParser


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 JavaDoc;
12 import org.xml.sax.SAXException JavaDoc;
13 import org.xml.sax.InputSource JavaDoc;
14 import org.xml.sax.EntityResolver JavaDoc;
15 import org.xml.sax.ErrorHandler JavaDoc;
16
17 import java.io.IOException JavaDoc;
18
19 public class SAXSubParser implements SubParser {
20   final XMLReaderCreator xrc;
21   final ErrorHandler JavaDoc eh;
22
23   SAXSubParser(XMLReaderCreator xrc, ErrorHandler JavaDoc eh) {
24     this.xrc = xrc;
25     this.eh = eh;
26   }
27
28   public ParsedPattern parseInclude(String JavaDoc uri, SchemaBuilder schemaBuilder, IncludedGrammar g)
29           throws BuildException, IllegalSchemaException {
30     try {
31       XMLReader JavaDoc 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 JavaDoc e) {
37      throw SAXParseable.toBuildException(e);
38     }
39     catch (IOException JavaDoc e) {
40      throw new BuildException(e);
41     }
42   }
43
44   public ParsedPattern parseExternal(String JavaDoc uri, SchemaBuilder schemaBuilder, Scope s)
45           throws BuildException, IllegalSchemaException {
46     try {
47       XMLReader JavaDoc 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 JavaDoc e) {
53       throw SAXParseable.toBuildException(e);
54     }
55     catch (IOException JavaDoc e) {
56       throw new BuildException(e);
57     }
58   }
59
60   private static InputSource JavaDoc makeInputSource(XMLReader JavaDoc xr, String JavaDoc systemId) throws IOException JavaDoc, SAXException JavaDoc {
61     EntityResolver JavaDoc er = xr.getEntityResolver();
62     if (er != null) {
63       InputSource JavaDoc inputSource = er.resolveEntity(null, systemId);
64       if (inputSource != null)
65     return inputSource;
66     }
67     return new InputSource JavaDoc(systemId);
68   }
69
70   static BuildException toBuildException(SAXException JavaDoc e) {
71     Exception JavaDoc inner = e.getException();
72     if (inner instanceof BuildException)
73       throw (BuildException)inner;
74     throw new BuildException(e);
75   }
76 }
77
Popular Tags