View Javadoc

1   /*
2    * Copyright (c) 2002 Peter Antman, Teknik i Media  <peter.antman@tim.se>
3    *
4    * $Id: XMLCatalogSystemIdAdapter.java,v 1.1.1.1 2004/05/19 12:07:30 pra Exp $
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version
10   * 
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 &lt;!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"&gt;.
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   * &lt;xmlcatalog id="jbosscatalog"&gt;
43   *   &lt;classpath&gt;
44   *     &lt;pathelement location="${lib.dir}/log4j.jar"/&gt;
45   *   &lt;/classpath&gt;
46   *   &lt;entity
47   *                   publicId="log4j.dtd"
48   *                   location="org/apache/log4j/xml/log4j.dtd"/&gt;
49   * 
50   *  &lt;/xmlcatalog&gt;
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           // Get last part of sysid
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       } // end of if ()
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       } // end of catch
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       //HM I am really not shure about this
125       return catalog.resolve(href,base);
126    }
127    
128 }// XMLCatalogSystemIdAdapter