1 | /*
|
---|
2 | * Copyright 2002-2005 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.types.resolver;
|
---|
19 |
|
---|
20 | import org.apache.xml.resolver.Catalog;
|
---|
21 | import org.apache.xml.resolver.CatalogEntry;
|
---|
22 |
|
---|
23 | import org.apache.xml.resolver.helpers.PublicId;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * This class extends the Catalog class provided by Norman Walsh's
|
---|
27 | * resolver library in xml-commons in order to add classpath entity
|
---|
28 | * and URI resolution. Since XMLCatalog already does classpath
|
---|
29 | * resolution, we simply add all CatalogEntry instances back to the
|
---|
30 | * controlling XMLCatalog instance. This is done via a callback
|
---|
31 | * mechanism. ApacheCatalog is <em>only</em> used for external
|
---|
32 | * catalog files. Inline entries (currently <code><dtd></code>
|
---|
33 | * and <code><entity></code>) are not added to ApacheCatalog.
|
---|
34 | * See XMLCatalog.java for the details of the entity and URI
|
---|
35 | * resolution algorithms.
|
---|
36 | *
|
---|
37 | * @see org.apache.tools.ant.types.XMLCatalog.CatalogResolver
|
---|
38 | * @since Ant 1.6
|
---|
39 | */
|
---|
40 | public class ApacheCatalog extends Catalog {
|
---|
41 |
|
---|
42 | /** The resolver object to callback. */
|
---|
43 | private ApacheCatalogResolver resolver = null;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * <p>Create a new ApacheCatalog instance.</p>
|
---|
47 | *
|
---|
48 | * <p>This method overrides the superclass method of the same name
|
---|
49 | * in order to set the resolver object for callbacks. The reason
|
---|
50 | * we have to do this is that internally Catalog creates a new
|
---|
51 | * instance of itself for each external catalog file processed.
|
---|
52 | * That is, if two external catalog files are processed, there
|
---|
53 | * will be a total of two ApacheCatalog instances, and so on.</p>
|
---|
54 | */
|
---|
55 | protected Catalog newCatalog() {
|
---|
56 | ApacheCatalog cat = (ApacheCatalog) super.newCatalog();
|
---|
57 | cat.setResolver(resolver);
|
---|
58 | return cat;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /** Set the resolver object to callback. */
|
---|
62 | public void setResolver(ApacheCatalogResolver resolver) {
|
---|
63 | this.resolver = resolver;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * <p>This method overrides the superclass method of the same name
|
---|
68 | * in order to add catalog entries back to the controlling
|
---|
69 | * XMLCatalog instance. In this way, we can add classpath lookup
|
---|
70 | * for these entries.</p>
|
---|
71 | *
|
---|
72 | * <p>When we add an external catalog file, the entries inside it
|
---|
73 | * get parsed by this method. Therefore, we override it to add
|
---|
74 | * each of them back to the controlling XMLCatalog instance. This
|
---|
75 | * is done by performing a callback to the ApacheCatalogResolver,
|
---|
76 | * which in turn calls the XMLCatalog.</p>
|
---|
77 | *
|
---|
78 | * <p>XMLCatalog currently only understands <code>PUBLIC</code>
|
---|
79 | * and <code>URI</code> entry types, so we ignore the other types.</p>
|
---|
80 | *
|
---|
81 | * @param entry The CatalogEntry to process.
|
---|
82 | */
|
---|
83 | public void addEntry(CatalogEntry entry) {
|
---|
84 |
|
---|
85 | int type = entry.getEntryType();
|
---|
86 |
|
---|
87 | if (type == PUBLIC) {
|
---|
88 |
|
---|
89 | String publicid = PublicId.normalize(entry.getEntryArg(0));
|
---|
90 | String systemid = normalizeURI(entry.getEntryArg(1));
|
---|
91 |
|
---|
92 | if (resolver == null) {
|
---|
93 | catalogManager.debug
|
---|
94 | .message(1, "Internal Error: null ApacheCatalogResolver");
|
---|
95 | } else {
|
---|
96 | resolver.addPublicEntry(publicid, systemid, base);
|
---|
97 | }
|
---|
98 |
|
---|
99 | } else if (type == URI) {
|
---|
100 |
|
---|
101 | String uri = normalizeURI(entry.getEntryArg(0));
|
---|
102 | String altURI = normalizeURI(entry.getEntryArg(1));
|
---|
103 |
|
---|
104 | if (resolver == null) {
|
---|
105 | catalogManager.debug
|
---|
106 | .message(1, "Internal Error: null ApacheCatalogResolver");
|
---|
107 | } else {
|
---|
108 | resolver.addURIEntry(uri, altURI, base);
|
---|
109 | }
|
---|
110 |
|
---|
111 | }
|
---|
112 |
|
---|
113 | super.addEntry(entry);
|
---|
114 | }
|
---|
115 |
|
---|
116 | } //- ApacheCatalog
|
---|