Debian Codegraph @

mod/rng-parse/src/main/com/thaiopensource/relaxng/parse/compact/CompactParseable.java

raw
1 package com.thaiopensource.relaxng.parse.compact;
2
3 import com.thaiopensource.relaxng.parse.Annotations;
4 import com.thaiopensource.relaxng.parse.BuildException;
5 import com.thaiopensource.relaxng.parse.CommentList;
6 import com.thaiopensource.relaxng.parse.IllegalSchemaException;
7 import com.thaiopensource.relaxng.parse.IncludedGrammar;
8 import com.thaiopensource.relaxng.parse.SchemaBuilder;
9 import com.thaiopensource.relaxng.parse.Scope;
10 import com.thaiopensource.relaxng.parse.SubParseable;
11 import com.thaiopensource.resolver.Identifier;
12 import com.thaiopensource.resolver.Input;
13 import com.thaiopensource.resolver.MediaTypedIdentifier;
14 import com.thaiopensource.resolver.Resolver;
15 import com.thaiopensource.resolver.ResolverException;
16 import com.thaiopensource.util.Uri;
17 import com.thaiopensource.xml.util.EncodingMap;
18 import org.xml.sax.ErrorHandler;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.io.PushbackInputStream;
24 import java.io.Reader;
25
26 public class CompactParseable<P, NC, L, EA, CL extends CommentList<L>, A extends Annotations<L, EA, CL>>
27 implements SubParseable<P, NC, L, EA, CL, A> {
28 private final Input in;
29 private final Resolver resolver;
30 private final ErrorHandler eh;
31 private static final String MEDIA_TYPE = "application/relax-ng-compact-syntax";
32
33 public CompactParseable(Input in, Resolver resolver, ErrorHandler eh) {
34 this.in = in;
35 this.resolver = resolver;
36 this.eh = eh;
37 }
38
39 public P parse(SchemaBuilder<P, NC, L, EA, CL, A> sb, Scope<P, L, EA, CL, A> scope) throws BuildException, IllegalSchemaException {
40 return new CompactSyntax<P, NC, L, EA, CL, A>(makeReader(in), in.getUri(), sb, eh).parse(scope);
41 }
42
43 public SubParseable<P, NC, L, EA, CL, A> createSubParseable(String href, String base) throws BuildException {
44 Identifier id = new MediaTypedIdentifier(href, base, MEDIA_TYPE);
45 Input input = new Input();
46 try {
47 resolver.resolve(id, input);
48 }
49 catch (ResolverException e) {
50 throw BuildException.fromResolverException(e);
51 }
52 catch (IOException e) {
53 throw new BuildException(e);
54 }
55 return new CompactParseable<P, NC, L, EA, CL, A>(input, resolver, eh);
56 }
57
58 public P parseAsInclude(SchemaBuilder<P, NC, L, EA, CL, A> sb, IncludedGrammar<P, L, EA, CL, A> g)
59 throws BuildException, IllegalSchemaException {
60 return new CompactSyntax<P, NC, L, EA, CL, A>(makeReader(in), in.getUri(), sb, eh).parseInclude(g);
61 }
62
63 public String getUri() {
64 String uri = in.getUri();
65 if (uri == null)
66 return null;
67 return Uri.escapeDisallowedChars(uri);
68 }
69
70 private static final String UTF8 = EncodingMap.getJavaName("UTF-8");
71 private static final String UTF16 = EncodingMap.getJavaName("UTF-16");
72
73 private Reader makeReader(Input in) throws BuildException {
74 try {
75 resolver.open(in);
76 Reader reader = in.getCharacterStream();
77 if (reader == null) {
78 InputStream byteStream = in.getByteStream();
79 if (byteStream == null)
80 throw new IllegalArgumentException("invalid input for CompactParseable");
81 String encoding = in.getEncoding();
82 if (encoding == null) {
83 PushbackInputStream pb = new PushbackInputStream(byteStream, 2);
84 encoding = detectEncoding(pb);
85 byteStream = pb;
86 }
87 reader = new InputStreamReader(byteStream, encoding);
88 }
89 return reader;
90 }
91 catch (ResolverException e) {
92 throw BuildException.fromResolverException(e);
93 }
94 catch (IOException e) {
95 throw new BuildException(e);
96 }
97 }
98
99 static private String detectEncoding(PushbackInputStream in) throws IOException {
100 String encoding = UTF8;
101 int b1 = in.read();
102 if (b1 != -1) {
103 int b2 = in.read();
104 if (b2 != -1) {
105 in.unread(b2);
106 if ((b1 == 0xFF && b2 == 0xFE) || (b1 == 0xFE && b2 == 0xFF))
107 encoding = UTF16;
108 }
109 in.unread(b1);
110 }
111 return encoding;
112 }
113 }