KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > resolver > ApacheCatalogResolver


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.types.resolver;
20
21 import java.io.IOException JavaDoc;
22
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25
26 import org.apache.tools.ant.BuildException;
27
28 import org.apache.tools.ant.types.XMLCatalog;
29 import org.apache.tools.ant.types.ResourceLocation;
30
31 import org.apache.xml.resolver.Catalog;
32 import org.apache.xml.resolver.CatalogManager;
33
34 import org.apache.xml.resolver.tools.CatalogResolver;
35
36 /**
37  * <p>This class extends the CatalogResolver class provided by Norman
38  * Walsh's resolver library in xml-commons. It provides the bridge
39  * between the Ant XMLCatalog datatype and the xml-commons Catalog
40  * class. XMLCatalog calls methods in this class using Reflection in
41  * order to avoid requiring the xml-commons resolver library in the
42  * path.</p>
43  *
44  * <p>The {@link org.apache.tools.ant.types.resolver.ApacheCatalog
45  * ApacheCatalog} class is used to parse external catalog files, which
46  * can be in either <a
47  * HREF="http://oasis-open.org/committees/entity/background/9401.html">
48  * plain text format</a> or <a
49  * HREF="http://www.oasis-open.org/committees/entity/spec-2001-08-06.html">
50  * XML format</a>.</p>
51  *
52  * <p>For each entry found in an external catalog file, if any, an
53  * instance of {@link org.apache.tools.ant.types.ResourceLocation
54  * ResourceLocation} is created and added to the controlling
55  * XMLCatalog datatype. In this way, these entries will be included
56  * in XMLCatalog's lookup algorithm. See XMLCatalog.java for more
57  * details.</p>
58  *
59  * @see org.apache.tools.ant.types.XMLCatalog.CatalogResolver
60  * @see org.apache.xml.resolver.CatalogManager
61  * @since Ant 1.6
62  */

63
64 public class ApacheCatalogResolver extends CatalogResolver {
65
66     /** The XMLCatalog object to callback. */
67     private XMLCatalog xmlCatalog = null;
68
69     static {
70         //
71
// If you don't do this, you get all sorts of annoying
72
// warnings about a missing properties file. However, it
73
// seems to work just fine with default values. Ultimately,
74
// we should probably include a "CatalogManager.properties"
75
// file in the ant jarfile with some default property
76
// settings. See CatalogManager.java for more details.
77
//
78
CatalogManager.getStaticManager().setIgnoreMissingProperties(true);
79
80         //
81
// Make sure CatalogResolver instantiates ApacheCatalog,
82
// rather than a plain Catalog
83
//
84
System.getProperties().put("xml.catalog.className",
85                                    ApacheCatalog.class.getName());
86
87         CatalogManager.getStaticManager().setUseStaticCatalog(false);
88
89         // debug
90
// CatalogManager.getStaticManager().setVerbosity(4);
91
}
92
93     /**
94      * Set the XMLCatalog object to callback.
95      * @param xmlCatalog the XMLCatalog to use.
96      */

97     public void setXMLCatalog(XMLCatalog xmlCatalog) {
98         this.xmlCatalog = xmlCatalog;
99     }
100
101     /**
102      * XMLCatalog calls this to add an external catalog file for each
103      * file within a <code>&lt;catalogfiles&gt;</code> fileset.
104      * @param file the external catalog file.
105      */

106     public void parseCatalog(String JavaDoc file) {
107
108         Catalog catalog = getCatalog();
109         if (!(catalog instanceof ApacheCatalog)) {
110             throw new BuildException("Wrong catalog type found: " + catalog.getClass().getName());
111         }
112         ApacheCatalog apacheCatalog = (ApacheCatalog) catalog;
113
114         // Pass in reference to ourselves so we can be called back.
115
apacheCatalog.setResolver(this);
116
117         try {
118             apacheCatalog.parseCatalog(file);
119         } catch (MalformedURLException JavaDoc ex) {
120             throw new BuildException(ex);
121         } catch (IOException JavaDoc ex) {
122             throw new BuildException(ex);
123         }
124     }
125
126     /**
127      * <p>Add a PUBLIC catalog entry to the controlling XMLCatalog instance.
128      * ApacheCatalog calls this for each PUBLIC entry found in an external
129      * catalog file.</p>
130      *
131      * @param publicid The public ID of the resource
132      * @param systemid The system ID (aka location) of the resource
133      * @param base The base URL of the resource. If the systemid
134      * specifies a relative URL/pathname, it is resolved using the
135      * base. The default base for an external catalog file is the
136      * directory in which the catalog is located.
137      *
138      */

139     public void addPublicEntry(String JavaDoc publicid,
140                                String JavaDoc systemid,
141                                URL JavaDoc base) {
142
143         ResourceLocation dtd = new ResourceLocation();
144         dtd.setBase(base);
145         dtd.setPublicId(publicid);
146         dtd.setLocation(systemid);
147
148         xmlCatalog.addDTD(dtd);
149     }
150
151     /**
152      * <p>Add a URI catalog entry to the controlling XMLCatalog instance.
153      * ApacheCatalog calls this for each URI entry found in an external
154      * catalog file.</p>
155      *
156      * @param uri The URI of the resource
157      * @param altURI The URI to which the resource should be mapped
158      * (aka the location)
159      * @param base The base URL of the resource. If the altURI
160      * specifies a relative URL/pathname, it is resolved using the
161      * base. The default base for an external catalog file is the
162      * directory in which the catalog is located.
163      *
164      */

165     public void addURIEntry(String JavaDoc uri,
166                             String JavaDoc altURI,
167                             URL JavaDoc base) {
168
169         ResourceLocation entity = new ResourceLocation();
170         entity.setBase(base);
171         entity.setPublicId(uri);
172         entity.setLocation(altURI);
173
174         xmlCatalog.addEntity(entity);
175     }
176
177 } //-- ApacheCatalogResolver
178
Popular Tags