KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > relaxng > parse > compact > CompactParseable


1 package com.thaiopensource.relaxng.parse.compact;
2
3 import com.thaiopensource.relaxng.parse.BuildException;
4 import com.thaiopensource.relaxng.parse.IllegalSchemaException;
5 import com.thaiopensource.relaxng.parse.IncludedGrammar;
6 import com.thaiopensource.relaxng.parse.Parseable;
7 import com.thaiopensource.relaxng.parse.ParsedPattern;
8 import com.thaiopensource.relaxng.parse.SchemaBuilder;
9 import com.thaiopensource.relaxng.parse.Scope;
10 import com.thaiopensource.xml.util.EncodingMap;
11 import org.xml.sax.ErrorHandler JavaDoc;
12 import org.xml.sax.InputSource JavaDoc;
13
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.InputStreamReader JavaDoc;
17 import java.io.PushbackInputStream JavaDoc;
18 import java.io.Reader JavaDoc;
19 import java.net.URL JavaDoc;
20
21 public class CompactParseable implements Parseable {
22   private final InputSource JavaDoc in;
23   private final ErrorHandler JavaDoc eh;
24
25   public CompactParseable(InputSource JavaDoc in, ErrorHandler JavaDoc eh) {
26     this.in = in;
27     this.eh = eh;
28   }
29
30   public ParsedPattern parse(SchemaBuilder sb, Scope scope) throws BuildException, IllegalSchemaException {
31     return new CompactSyntax(makeReader(in), in.getSystemId(), sb, eh).parse(scope);
32   }
33
34   public ParsedPattern parseInclude(String JavaDoc uri, SchemaBuilder sb, IncludedGrammar g)
35           throws BuildException, IllegalSchemaException {
36     InputSource JavaDoc tem = new InputSource JavaDoc(uri);
37     tem.setEncoding(in.getEncoding());
38     return new CompactSyntax(makeReader(tem), uri, sb, eh).parseInclude(g);
39   }
40
41   public ParsedPattern parseExternal(String JavaDoc uri, SchemaBuilder sb, Scope scope)
42           throws BuildException, IllegalSchemaException {
43     InputSource JavaDoc tem = new InputSource JavaDoc(uri);
44     tem.setEncoding(in.getEncoding());
45     return new CompactSyntax(makeReader(tem), uri, sb, eh).parse(scope);
46   }
47
48   private static final String JavaDoc UTF8 = EncodingMap.getJavaName("UTF-8");
49   private static final String JavaDoc UTF16 = EncodingMap.getJavaName("UTF-16");
50
51   private static Reader JavaDoc makeReader(InputSource JavaDoc is) throws BuildException {
52     try {
53       Reader JavaDoc r = is.getCharacterStream();
54       if (r == null) {
55         InputStream JavaDoc in = is.getByteStream();
56         if (in == null) {
57           String JavaDoc systemId = is.getSystemId();
58           in = new URL JavaDoc(systemId).openStream();
59         }
60         String JavaDoc encoding = is.getEncoding();
61         if (encoding == null) {
62           PushbackInputStream JavaDoc pb = new PushbackInputStream JavaDoc(in, 2);
63           encoding = detectEncoding(pb);
64           in = pb;
65         }
66         r = new InputStreamReader JavaDoc(in, encoding);
67       }
68       return r;
69     }
70     catch (IOException JavaDoc e) {
71       throw new BuildException(e);
72     }
73   }
74
75   static private String JavaDoc detectEncoding(PushbackInputStream JavaDoc in) throws IOException JavaDoc {
76     String JavaDoc encoding = UTF8;
77     int b1 = in.read();
78     if (b1 != -1) {
79       int b2 = in.read();
80       if (b2 != -1) {
81         in.unread(b2);
82         if ((b1 == 0xFF && b2 == 0xFE) || (b1 == 0xFE && b2 == 0xFF))
83           encoding = UTF16;
84       }
85       in.unread(b1);
86     }
87     return encoding;
88   }
89 }
90
Popular Tags