01: package uk.org.ponder.saxalizer;
02:
03: import org.xml.sax.InputSource;
04:
05: import uk.org.ponder.streamutil.StreamResolver;
06: import uk.org.ponder.util.Logger;
07:
08: import java.util.Hashtable;
09: import java.util.List;
10:
11: import java.io.InputStream;
12: import java.io.Reader;
13: import java.io.InputStreamReader;
14:
15: /** The <code>LocalEntityResolver</code> is used to map public DTD references
16: * onto locally cached copies of the files.
17: */
18:
19: public class EntityResolverStash {
20: public static final int PUBLIC_ID = 0;
21: public static final int SYSTEM_ID = 1;
22: private StreamResolver streamresolver;
23:
24: public void setStreamResolver(StreamResolver streamresolver) {
25: this .streamresolver = streamresolver;
26: }
27:
28: /** Adds the specified entityresolver into this stash.
29: * @param ler A LocalEntityResolver capable of resolving a single DTD entity
30: * reference. Aren't these things tedious.
31: */
32:
33: public void addEntityResolver(LocalEntityResolver ler) {
34: entityresolvers.put(ler.ID, ler);
35: }
36:
37: public void setEntityResolvers(List resolvers) {
38: for (int i = 0; i < resolvers.size(); ++i) {
39: addEntityResolver((LocalEntityResolver) resolvers.get(i));
40: }
41: }
42:
43: // This is a hashtable of id (String) to LocalEntityResolvers
44: private Hashtable entityresolvers = new Hashtable();
45:
46: /** Resolves the given public DTD ID onto an <code>InputSource</code>
47: * derived from a locally cached copy.
48: * @param publicID The public ID of the DTD.
49: * @return An <code>InputSource</code> with the contents of the DTD.
50: */
51: public InputSource resolve(String publicID) {
52: if (publicID == null)
53: return null;
54: LocalEntityResolver wer = (LocalEntityResolver) entityresolvers
55: .get(publicID);
56: if (wer == null) {
57: Logger.log.warn("Entity with public ID " + publicID
58: + " could not be resolved");
59: return null;
60: }
61: try {
62: InputStream is = streamresolver.openStream(wer.path);
63: Logger.log.info("Resolved " + publicID + " to " + wer.path);
64: Reader r = new InputStreamReader(is);
65: InputSource i = new InputSource(r);
66: i.setSystemId("Local entity resolver stashed page");
67: return i;
68: } catch (Exception e) {
69: e.printStackTrace();
70: return null;
71: }
72: }
73:
74: }
|