View Javadoc

1   /*
2    * Created on 28.7.2004
3    */
4   package net.sf.tomp.xtcl.resolver;
5   
6   import java.io.InputStream;
7   import java.net.URL;
8   
9   import net.sf.tomp.xtcl.Context;
10  
11  import org.apache.commons.logging.Log;
12  import org.apache.commons.logging.LogFactory;
13  import org.apache.xml.resolver.CatalogManager;
14  import org.apache.xml.resolver.tools.CatalogResolver;
15  import org.apache.xml.resolver.tools.ResolvingXMLReader;
16  import org.xml.sax.InputSource;
17  import org.xml.sax.XMLReader;
18  
19  /***
20   * Extension of Apache XML catalog entity resolver,
21   * enables to resolve also references to object stored 
22   * in the XTCL Context.<br/>
23   * 
24   * Does not do anything?
25   *  
26   * @author tomp
27   */
28  public class XTResolvingReader extends ResolvingXMLReader {
29  
30      /*** The manager for the underlying resolver. */
31      private CatalogManager catalogManager = CatalogManager.getStaticManager();
32  
33      /*** The underlying catalog resolver. */
34      private CatalogResolver catalogResolver = null;
35  
36      /*** A separate resolver for oasis-xml-pi catalogs. */
37      private CatalogResolver piCatalogResolver = null;
38  
39      private Context context;
40  
41      private static Log log = LogFactory.getLog(XTResolvingReader.class);
42  
43      /***
44       * 
45       */
46      public XTResolvingReader() {
47          super();
48          catalogResolver = new CatalogResolver(catalogManager);
49      }
50  
51      /***
52       * @param manager
53       */
54      public XTResolvingReader(CatalogManager manager) {
55          super(manager);
56          catalogManager = manager;
57          catalogResolver = new CatalogResolver(catalogManager);
58      }
59  
60      /*** Construct an XML filter with the specified parent. */
61      public XTResolvingReader(XMLReader parent, CatalogManager manager) {
62        super(manager);
63        catalogManager = manager;
64        catalogResolver = new CatalogResolver(catalogManager);
65        setParent(parent);
66      }
67  
68      /***
69       * @return Returns the context.
70       */
71      public Context getContext() {
72          return context;
73      }
74  
75      /***
76       * Implements the <code>resolveEntity</code> method for the SAX interface,
77       * using an underlying CatalogResolver to do the real work.
78       */
79      public InputSource resolveEntity(String publicId, String systemId) {
80          String resolved = catalogResolver.getResolvedEntity(publicId, systemId);
81  
82          if (resolved == null && piCatalogResolver != null) {
83              resolved = piCatalogResolver.getResolvedEntity(publicId, systemId);
84          }
85  
86          if (resolved != null) {
87              try {
88                  InputSource iSource = new InputSource(resolved);
89                  iSource.setPublicId(publicId);
90  
91                  // Ideally this method would not attempt to open the
92                  // InputStream, but there is a bug (in Xerces, at least)
93                  // that causes the parser to mistakenly open the wrong
94                  // system identifier if the returned InputSource does
95                  // not have a byteStream.
96                  //
97                  // It could be argued that we still shouldn't do this here,
98                  // but since the purpose of calling the entityResolver is
99                  // almost certainly to open the input stream, it seems to
100                 // do little harm.
101                 //
102                 URL url = new URL(resolved);
103                 InputStream iStream = url.openStream();
104                 iSource.setByteStream(iStream);
105 
106                 return iSource;
107             } catch (Exception e) {
108                 log.error("Failed to create InputSource", e);
109                 return null;
110             }
111         } else {
112             return null;
113         }
114     }
115 
116     /***
117      * @param context The context to set.
118      */
119     public void setContext(Context context) {
120         this.context = context;
121     }
122 }