~ubuntu-branches/debian/wheezy/jing-trang/wheezy

« back to all changes in this revision

Viewing changes to mod/convert-from-dtd/src/main/com/thaiopensource/relaxng/input/dtd/ResolverEntityManager.java

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Thibault
  • Date: 2009-09-01 15:53:03 UTC
  • Revision ID: james.westby@ubuntu.com-20090901155303-2kweef05h5v9j3ni
Tags: upstream-20090818
ImportĀ upstreamĀ versionĀ 20090818

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.thaiopensource.relaxng.input.dtd;
 
2
 
 
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;
 
10
 
 
11
import java.io.IOException;
 
12
import java.io.InputStream;
 
13
import java.io.InputStreamReader;
 
14
import java.io.Reader;
 
15
 
 
16
/**
 
17
 *
 
18
 */
 
19
public class ResolverEntityManager extends EntityManager {
 
20
  private final Resolver resolver;
 
21
 
 
22
  public ResolverEntityManager(Resolver resolver) {
 
23
    this.resolver = resolver;
 
24
  }
 
25
 
 
26
  public OpenEntity open(String systemId) throws IOException {
 
27
    Input input = new Input();
 
28
    input.setUri(systemId);
 
29
    try {
 
30
      return open(input);
 
31
    }
 
32
    catch (ResolverException e) {
 
33
      throw toIOException(e);
 
34
    }
 
35
  }
 
36
 
 
37
  public OpenEntity open(ExternalId xid, boolean isParameterEntity, String entityName) throws IOException {
 
38
    Input input = new Input();
 
39
    if (isParameterEntity)
 
40
      entityName = "%" + entityName;
 
41
    try {
 
42
      resolver.resolve(new ExternalEntityIdentifier(xid.getSystemId(),
 
43
                                                    xid.getBaseUri(),
 
44
                                                    xid.getPublicId(),
 
45
                                                    entityName),
 
46
                       input);
 
47
      return open(input);
 
48
    }
 
49
    catch (ResolverException e) {
 
50
      throw toIOException(e);
 
51
    }
 
52
  }
 
53
 
 
54
  private OpenEntity open(Input input) throws ResolverException, IOException {
 
55
    resolver.open(input);
 
56
    if (!input.isOpen())
 
57
      throw new ResolverException("could not open input");
 
58
    Reader reader = input.getCharacterStream();
 
59
    String encoding = input.getEncoding();
 
60
    String systemId = input.getUri();
 
61
    if (reader != null) {
 
62
      if (encoding == null)
 
63
        encoding = "UTF-8"; // XXX not sure if it's safe to pass null here
 
64
      return new OpenEntity(reader, systemId, systemId, encoding);
 
65
    }
 
66
    InputStream in = input.getByteStream();
 
67
    if (encoding != null)
 
68
      return new OpenEntity(new InputStreamReader(in, encoding),
 
69
                            systemId, systemId, encoding);
 
70
    return detectEncoding(in, systemId);
 
71
  }
 
72
 
 
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());
 
81
    }
 
82
    // Avoid IOException(String, Throwable) because it's 1.6
 
83
    return new IOException(message);
 
84
  }
 
85
}