1
package com.thaiopensource.relaxng.input.dtd;
3
import com.thaiopensource.resolver.Input;
4
import com.thaiopensource.resolver.Resolver;
5
import com.thaiopensource.resolver.ResolverException;
6
import com.thaiopensource.resolver.xml.ExternalEntityIdentifier;
7
import com.thaiopensource.xml.em.EntityManager;
8
import com.thaiopensource.xml.em.ExternalId;
9
import com.thaiopensource.xml.em.OpenEntity;
11
import java.io.IOException;
12
import java.io.InputStream;
13
import java.io.InputStreamReader;
14
import java.io.Reader;
19
public class ResolverEntityManager extends EntityManager {
20
private final Resolver resolver;
22
public ResolverEntityManager(Resolver resolver) {
23
this.resolver = resolver;
26
public OpenEntity open(String systemId) throws IOException {
27
Input input = new Input();
28
input.setUri(systemId);
32
catch (ResolverException e) {
33
throw toIOException(e);
37
public OpenEntity open(ExternalId xid, boolean isParameterEntity, String entityName) throws IOException {
38
Input input = new Input();
39
if (isParameterEntity)
40
entityName = "%" + entityName;
42
resolver.resolve(new ExternalEntityIdentifier(xid.getSystemId(),
49
catch (ResolverException e) {
50
throw toIOException(e);
54
private OpenEntity open(Input input) throws ResolverException, IOException {
57
throw new ResolverException("could not open input");
58
Reader reader = input.getCharacterStream();
59
String encoding = input.getEncoding();
60
String systemId = input.getUri();
63
encoding = "UTF-8"; // XXX not sure if it's safe to pass null here
64
return new OpenEntity(reader, systemId, systemId, encoding);
66
InputStream in = input.getByteStream();
68
return new OpenEntity(new InputStreamReader(in, encoding),
69
systemId, systemId, encoding);
70
return detectEncoding(in, systemId);
73
private static IOException toIOException(ResolverException e) {
74
String message = e.getMessage();
75
Throwable cause = e.getCause();
76
if (message == null) {
77
if (cause instanceof IOException)
78
return (IOException)cause;
79
// Avoid IOException(Throwable) because it's 1.6
80
return new IOException(cause.getMessage());
82
// Avoid IOException(String, Throwable) because it's 1.6
83
return new IOException(message);