KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.xml.resolver.Catalog;
22 import org.apache.xml.resolver.CatalogEntry;
23
24 import org.apache.xml.resolver.helpers.PublicId;
25
26 /**
27  * This class extends the Catalog class provided by Norman Walsh's
28  * resolver library in xml-commons in order to add classpath entity
29  * and URI resolution. Since XMLCatalog already does classpath
30  * resolution, we simply add all CatalogEntry instances back to the
31  * controlling XMLCatalog instance. This is done via a callback
32  * mechanism. ApacheCatalog is <em>only</em> used for external
33  * catalog files. Inline entries (currently <code>&lt;dtd&gt;</code>
34  * and <code>&lt;entity&gt;</code>) are not added to ApacheCatalog.
35  * See XMLCatalog.java for the details of the entity and URI
36  * resolution algorithms.
37  *
38  * @see org.apache.tools.ant.types.XMLCatalog.CatalogResolver
39  * @since Ant 1.6
40  */

41 public class ApacheCatalog extends Catalog {
42
43     /** The resolver object to callback. */
44     private ApacheCatalogResolver resolver = null;
45
46     /**
47      * <p>Create a new ApacheCatalog instance.</p>
48      *
49      * <p>This method overrides the superclass method of the same name
50      * in order to set the resolver object for callbacks. The reason
51      * we have to do this is that internally Catalog creates a new
52      * instance of itself for each external catalog file processed.
53      * That is, if two external catalog files are processed, there
54      * will be a total of two ApacheCatalog instances, and so on.</p>
55      * @return the catalog.
56      */

57     protected Catalog newCatalog() {
58         ApacheCatalog cat = (ApacheCatalog) super.newCatalog();
59         cat.setResolver(resolver);
60         return cat;
61     }
62
63     /**
64      * Set the resolver object to callback.
65      * @param resolver the apache catalog resolver.
66      */

67     public void setResolver(ApacheCatalogResolver resolver) {
68         this.resolver = resolver;
69     }
70
71     /**
72      * <p>This method overrides the superclass method of the same name
73      * in order to add catalog entries back to the controlling
74      * XMLCatalog instance. In this way, we can add classpath lookup
75      * for these entries.</p>
76      *
77      * <p>When we add an external catalog file, the entries inside it
78      * get parsed by this method. Therefore, we override it to add
79      * each of them back to the controlling XMLCatalog instance. This
80      * is done by performing a callback to the ApacheCatalogResolver,
81      * which in turn calls the XMLCatalog.</p>
82      *
83      * <p>XMLCatalog currently only understands <code>PUBLIC</code>
84      * and <code>URI</code> entry types, so we ignore the other types.</p>
85      *
86      * @param entry The CatalogEntry to process.
87      */

88     public void addEntry(CatalogEntry entry) {
89
90         int type = entry.getEntryType();
91
92         if (type == PUBLIC) {
93
94             String JavaDoc publicid = PublicId.normalize(entry.getEntryArg(0));
95             String JavaDoc systemid = normalizeURI(entry.getEntryArg(1));
96
97             if (resolver == null) {
98                 catalogManager.debug
99                     .message(1, "Internal Error: null ApacheCatalogResolver");
100             } else {
101                 resolver.addPublicEntry(publicid, systemid, base);
102             }
103
104         } else if (type == URI) {
105
106             String JavaDoc uri = normalizeURI(entry.getEntryArg(0));
107             String JavaDoc altURI = normalizeURI(entry.getEntryArg(1));
108
109             if (resolver == null) {
110                 catalogManager.debug
111                     .message(1, "Internal Error: null ApacheCatalogResolver");
112             } else {
113                 resolver.addURIEntry(uri, altURI, base);
114             }
115
116         }
117
118         super.addEntry(entry);
119     }
120
121 } //- ApacheCatalog
122
Popular Tags