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 java.io.IOException;
|
---|
21 |
|
---|
22 | import java.net.MalformedURLException;
|
---|
23 | import java.net.URL;
|
---|
24 |
|
---|
25 | import org.apache.tools.ant.BuildException;
|
---|
26 |
|
---|
27 | import org.apache.tools.ant.types.XMLCatalog;
|
---|
28 | import org.apache.tools.ant.types.ResourceLocation;
|
---|
29 |
|
---|
30 | import org.apache.xml.resolver.Catalog;
|
---|
31 | import org.apache.xml.resolver.CatalogManager;
|
---|
32 |
|
---|
33 | import org.apache.xml.resolver.tools.CatalogResolver;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * <p>This class extends the CatalogResolver class provided by Norman
|
---|
37 | * Walsh's resolver library in xml-commons. It provides the bridge
|
---|
38 | * between the Ant XMLCatalog datatype and the xml-commons Catalog
|
---|
39 | * class. XMLCatalog calls methods in this class using Reflection in
|
---|
40 | * order to avoid requiring the xml-commons resolver library in the
|
---|
41 | * path.</p>
|
---|
42 | *
|
---|
43 | * <p>The {@link org.apache.tools.ant.types.resolver.ApacheCatalog
|
---|
44 | * ApacheCatalog} class is used to parse external catalog files, which
|
---|
45 | * can be in either <a
|
---|
46 | * href="http://oasis-open.org/committees/entity/background/9401.html">
|
---|
47 | * plain text format</a> or <a
|
---|
48 | * href="http://www.oasis-open.org/committees/entity/spec-2001-08-06.html">
|
---|
49 | * XML format</a>.</p>
|
---|
50 | *
|
---|
51 | * <p>For each entry found in an external catalog file, if any, an
|
---|
52 | * instance of {@link org.apache.tools.ant.types.ResourceLocation
|
---|
53 | * ResourceLocation} is created and added to the controlling
|
---|
54 | * XMLCatalog datatype. In this way, these entries will be included
|
---|
55 | * in XMLCatalog's lookup algorithm. See XMLCatalog.java for more
|
---|
56 | * details.</p>
|
---|
57 | *
|
---|
58 | * @see org.apache.tools.ant.types.XMLCatalog.CatalogResolver
|
---|
59 | * @see org.apache.xml.resolver.CatalogManager
|
---|
60 | * @since Ant 1.6
|
---|
61 | */
|
---|
62 |
|
---|
63 | public class ApacheCatalogResolver extends CatalogResolver {
|
---|
64 |
|
---|
65 | /** The XMLCatalog object to callback. */
|
---|
66 | private XMLCatalog xmlCatalog = null;
|
---|
67 |
|
---|
68 | static {
|
---|
69 | //
|
---|
70 | // If you don't do this, you get all sorts of annoying
|
---|
71 | // warnings about a missing properties file. However, it
|
---|
72 | // seems to work just fine with default values. Ultimately,
|
---|
73 | // we should probably include a "CatalogManager.properties"
|
---|
74 | // file in the ant jarfile with some default property
|
---|
75 | // settings. See CatalogManager.java for more details.
|
---|
76 | //
|
---|
77 | CatalogManager.getStaticManager().setIgnoreMissingProperties(true);
|
---|
78 |
|
---|
79 | //
|
---|
80 | // Make sure CatalogResolver instantiates ApacheCatalog,
|
---|
81 | // rather than a plain Catalog
|
---|
82 | //
|
---|
83 | System.getProperties().put("xml.catalog.className",
|
---|
84 | ApacheCatalog.class.getName());
|
---|
85 |
|
---|
86 | CatalogManager.getStaticManager().setUseStaticCatalog(false);
|
---|
87 |
|
---|
88 | // debug
|
---|
89 | // CatalogManager.getStaticManager().setVerbosity(4);
|
---|
90 | }
|
---|
91 |
|
---|
92 | /** Set the XMLCatalog object to callback. */
|
---|
93 | public void setXMLCatalog(XMLCatalog xmlCatalog) {
|
---|
94 | this.xmlCatalog = xmlCatalog;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * XMLCatalog calls this to add an external catalog file for each
|
---|
99 | * file within a <code><catalogfiles></code> fileset.
|
---|
100 | */
|
---|
101 | public void parseCatalog(String file) {
|
---|
102 |
|
---|
103 | Catalog _catalog = getCatalog();
|
---|
104 | if (!(_catalog instanceof ApacheCatalog)) {
|
---|
105 | throw new BuildException("Wrong catalog type found: " + _catalog.getClass().getName());
|
---|
106 | }
|
---|
107 | ApacheCatalog catalog = (ApacheCatalog) _catalog;
|
---|
108 |
|
---|
109 | // Pass in reference to ourselves so we can be called back.
|
---|
110 | catalog.setResolver(this);
|
---|
111 |
|
---|
112 | try {
|
---|
113 | catalog.parseCatalog(file);
|
---|
114 | } catch (MalformedURLException ex) {
|
---|
115 | throw new BuildException(ex);
|
---|
116 | } catch (IOException ex) {
|
---|
117 | throw new BuildException(ex);
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * <p>Add a PUBLIC catalog entry to the controlling XMLCatalog instance.
|
---|
123 | * ApacheCatalog calls this for each PUBLIC entry found in an external
|
---|
124 | * catalog file.</p>
|
---|
125 | *
|
---|
126 | * @param publicid The public ID of the resource
|
---|
127 | * @param systemid The system ID (aka location) of the resource
|
---|
128 | * @param base The base URL of the resource. If the systemid
|
---|
129 | * specifies a relative URL/pathname, it is resolved using the
|
---|
130 | * base. The default base for an external catalog file is the
|
---|
131 | * directory in which the catalog is located.
|
---|
132 | *
|
---|
133 | */
|
---|
134 | public void addPublicEntry(String publicid,
|
---|
135 | String systemid,
|
---|
136 | URL base) {
|
---|
137 |
|
---|
138 | ResourceLocation dtd = new ResourceLocation();
|
---|
139 | dtd.setBase(base);
|
---|
140 | dtd.setPublicId(publicid);
|
---|
141 | dtd.setLocation(systemid);
|
---|
142 |
|
---|
143 | xmlCatalog.addDTD(dtd);
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * <p>Add a URI catalog entry to the controlling XMLCatalog instance.
|
---|
148 | * ApacheCatalog calls this for each URI entry found in an external
|
---|
149 | * catalog file.</p>
|
---|
150 | *
|
---|
151 | * @param uri The URI of the resource
|
---|
152 | * @param altURI The URI to which the resource should be mapped
|
---|
153 | * (aka the location)
|
---|
154 | * @param base The base URL of the resource. If the altURI
|
---|
155 | * specifies a relative URL/pathname, it is resolved using the
|
---|
156 | * base. The default base for an external catalog file is the
|
---|
157 | * directory in which the catalog is located.
|
---|
158 | *
|
---|
159 | */
|
---|
160 | public void addURIEntry(String uri,
|
---|
161 | String altURI,
|
---|
162 | URL base) {
|
---|
163 |
|
---|
164 | ResourceLocation entity = new ResourceLocation();
|
---|
165 | entity.setBase(base);
|
---|
166 | entity.setPublicId(uri);
|
---|
167 | entity.setLocation(altURI);
|
---|
168 |
|
---|
169 | xmlCatalog.addEntity(entity);
|
---|
170 | }
|
---|
171 |
|
---|
172 | } //-- ApacheCatalogResolver
|
---|