1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.backsource.utils.ant;
21
22 import java.io.File;
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.net.URL;
26
27 import org.xml.sax.EntityResolver;
28 import org.xml.sax.InputSource;
29 import org.xml.sax.SAXException;
30 import javax.xml.transform.URIResolver;
31 import javax.xml.transform.Source;
32 import javax.xml.transform.TransformerException;
33
34 import org.apache.tools.ant.types.XMLCatalog;
35 import org.apache.log4j.Category;
36 /***
37 * <p>Adapts the ant XMLCatalog, so that it is possible to lookup also systemid.
38 *<p>The current implementation of XMLCatalog only handles public id for dtd:s and enitiys. Often it is necesarry to be able to also look up a systemId at another location than the one specifyed, for example when only SYSTEM is specifyed in an XML file. eq <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">.
39 *
40 * <p>by specifying the last part of the systemid in the XMLCatalog data it is possible to lookup a systemid at another location than the one normaly resolved. for example, the following catalog definition would resolv the above SYSTEM to the log4j.jar file.
41 * * <pre>
42 * <xmlcatalog id="jbosscatalog">
43 * <classpath>
44 * <pathelement location="${lib.dir}/log4j.jar"/>
45 * </classpath>
46 * <entity
47 * publicId="log4j.dtd"
48 * location="org/apache/log4j/xml/log4j.dtd"/>
49 *
50 * </xmlcatalog>
51 * </pre>
52 *
53 * <p>Remember that this class is only possible to use from custom ant targets/helper classes.
54 *
55 * @author <a href="mailto:pra@tim.se">Peter Antman</a>
56 * @version $Revision: 1.1.1.1 $
57 */
58
59 public class XMLCatalogSystemIdAdapter implements EntityResolver,URIResolver{
60 private static final Category log = Category.getInstance(XMLCatalogSystemIdAdapter.class);
61 protected XMLCatalog catalog;
62
63 public XMLCatalogSystemIdAdapter (){
64
65 }
66
67 /***
68 * @param catalog the XMLCatalog to delegate to.
69 */
70 public XMLCatalogSystemIdAdapter (XMLCatalog catalog){
71 this.catalog = catalog;
72 }
73 public void setXMLCatalog(XMLCatalog catalog) {
74 this.catalog = catalog;
75 }
76
77 /***
78 * if publicId is null, translate systemId to a publicId thingy and call the real catalog.
79 */
80 public InputSource resolveEntity (String publicId, String systemId)
81 throws SAXException, IOException{
82 if ( publicId == null) {
83
84
85 log.debug("Resolving systemID : " + systemId);
86
87
88
89 File sysId = null;
90 try {
91 URL tmp = new URL(systemId);
92 sysId = new File(tmp.getFile());
93 }catch(java.net.MalformedURLException ex) {
94 sysId = new File(systemId);
95 }
96
97 publicId = sysId.getName();
98
99
100 }
101 InputSource is = null;
102 try {
103 is = catalog.resolveEntity(publicId,systemId);
104 } catch (IOException e) {
105 log.debug("Could not resolve " + publicId,e);
106 throw e;
107 } catch (SAXException e) {
108 log.debug("Could not resolve " + publicId,e);
109 throw e;
110 } catch (RuntimeException e) {
111 log.debug("Could not resolve " + publicId,e);
112 throw e;
113 }
114
115 return is;
116 }
117
118 /***
119 * Does nothing currently.
120 */
121 public Source resolve(String href, String base)
122 throws TransformerException {
123
124
125 return catalog.resolve(href,base);
126 }
127
128 }