33349
|
1 |
/*
|
|
2 |
* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
*/
|
12005
|
4 |
/*
|
26516
|
5 |
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
6 |
* contributor license agreements. See the NOTICE file distributed with
|
|
7 |
* this work for additional information regarding copyright ownership.
|
|
8 |
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
9 |
* (the "License"); you may not use this file except in compliance with
|
|
10 |
* the License. You may obtain a copy of the License at
|
12005
|
11 |
*
|
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13 |
*
|
|
14 |
* Unless required by applicable law or agreed to in writing, software
|
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17 |
* See the License for the specific language governing permissions and
|
|
18 |
* limitations under the License.
|
|
19 |
*/
|
|
20 |
|
|
21 |
package com.sun.org.apache.xml.internal.resolver;
|
|
22 |
|
12458
|
23 |
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
|
16953
|
24 |
import com.sun.org.apache.xerces.internal.utils.SecuritySupport;
|
26516
|
25 |
import com.sun.org.apache.xml.internal.resolver.helpers.FileURL;
|
|
26 |
import com.sun.org.apache.xml.internal.resolver.helpers.PublicId;
|
|
27 |
import com.sun.org.apache.xml.internal.resolver.readers.CatalogReader;
|
|
28 |
import com.sun.org.apache.xml.internal.resolver.readers.OASISXMLCatalogReader;
|
|
29 |
import com.sun.org.apache.xml.internal.resolver.readers.SAXCatalogReader;
|
|
30 |
import com.sun.org.apache.xml.internal.resolver.readers.TR9401CatalogReader;
|
|
31 |
import java.io.DataInputStream;
|
|
32 |
import java.io.FileNotFoundException;
|
12005
|
33 |
import java.io.IOException;
|
|
34 |
import java.io.InputStream;
|
|
35 |
import java.io.UnsupportedEncodingException;
|
26516
|
36 |
import java.net.MalformedURLException;
|
|
37 |
import java.net.URL;
|
12005
|
38 |
import java.util.Enumeration;
|
33349
|
39 |
import java.util.HashMap;
|
26516
|
40 |
import java.util.Locale;
|
33349
|
41 |
import java.util.Map;
|
12005
|
42 |
import java.util.Vector;
|
|
43 |
import javax.xml.parsers.SAXParserFactory;
|
|
44 |
|
|
45 |
/**
|
|
46 |
* Represents OASIS Open Catalog files.
|
|
47 |
*
|
|
48 |
* <p>This class implements the semantics of OASIS Open Catalog files
|
|
49 |
* (defined by
|
|
50 |
* <a href="http://www.oasis-open.org/html/a401.htm">OASIS Technical
|
|
51 |
* Resolution 9401:1997 (Amendment 2 to TR 9401)</a>).</p>
|
|
52 |
*
|
|
53 |
* <p>The primary purpose of the Catalog is to associate resources in the
|
|
54 |
* document with local system identifiers. Some entities
|
|
55 |
* (document types, XML entities, and notations) have names and all of them
|
|
56 |
* can have either public or system identifiers or both. (In XML, only a
|
|
57 |
* notation can have a public identifier without a system identifier, but
|
|
58 |
* the methods implemented in this class obey the Catalog semantics
|
|
59 |
* from the SGML
|
|
60 |
* days when system identifiers were optional.)</p>
|
|
61 |
*
|
|
62 |
* <p>The system identifiers returned by the resolution methods in this
|
|
63 |
* class are valid, i.e. usable by, and in fact constructed by, the
|
|
64 |
* <tt>java.net.URL</tt> class. Unfortunately, this class seems to behave in
|
|
65 |
* somewhat non-standard ways and the system identifiers returned may
|
|
66 |
* not be directly usable in a browser or filesystem context.
|
|
67 |
*
|
|
68 |
* <p>This class recognizes all of the Catalog entries defined in
|
|
69 |
* TR9401:1997:</p>
|
|
70 |
*
|
|
71 |
* <ul>
|
|
72 |
* <li><b>BASE</b>
|
|
73 |
* changes the base URI for resolving relative system identifiers. The
|
|
74 |
* initial base URI is the URI of the location of the catalog (which is,
|
|
75 |
* in turn, relative to the location of the current working directory
|
|
76 |
* at startup, as returned by the <tt>user.dir</tt> system property).</li>
|
|
77 |
* <li><b>CATALOG</b>
|
|
78 |
* processes other catalog files. An included catalog occurs logically
|
|
79 |
* at the end of the including catalog.</li>
|
|
80 |
* <li><b>DELEGATE_PUBLIC</b>
|
|
81 |
* specifies alternate catalogs for some public identifiers. The delegated
|
|
82 |
* catalogs are not loaded until they are needed, but they are cached
|
|
83 |
* once loaded.</li>
|
|
84 |
* <li><b>DELEGATE_SYSTEM</b>
|
|
85 |
* specifies alternate catalogs for some system identifiers. The delegated
|
|
86 |
* catalogs are not loaded until they are needed, but they are cached
|
|
87 |
* once loaded.</li>
|
|
88 |
* <li><b>DELEGATE_URI</b>
|
|
89 |
* specifies alternate catalogs for some URIs. The delegated
|
|
90 |
* catalogs are not loaded until they are needed, but they are cached
|
|
91 |
* once loaded.</li>
|
|
92 |
* <li><b>REWRITE_SYSTEM</b>
|
|
93 |
* specifies alternate prefix for a system identifier.</li>
|
|
94 |
* <li><b>REWRITE_URI</b>
|
|
95 |
* specifies alternate prefix for a URI.</li>
|
|
96 |
* <li><b>SYSTEM_SUFFIX</b>
|
|
97 |
* maps any system identifier that ends with a particular suffix to another
|
|
98 |
* system identifier.</li>
|
|
99 |
* <li><b>URI_SUFFIX</b>
|
|
100 |
* maps any URI that ends with a particular suffix to another URI.</li>
|
|
101 |
* <li><b>DOCTYPE</b>
|
|
102 |
* associates the names of root elements with URIs. (In other words, an XML
|
|
103 |
* processor might infer the doctype of an XML document that does not include
|
|
104 |
* a doctype declaration by looking for the DOCTYPE entry in the
|
|
105 |
* catalog which matches the name of the root element of the document.)</li>
|
|
106 |
* <li><b>DOCUMENT</b>
|
|
107 |
* provides a default document.</li>
|
|
108 |
* <li><b>DTDDECL</b>
|
|
109 |
* recognized and silently ignored. Not relevant for XML.</li>
|
|
110 |
* <li><b>ENTITY</b>
|
|
111 |
* associates entity names with URIs.</li>
|
|
112 |
* <li><b>LINKTYPE</b>
|
|
113 |
* recognized and silently ignored. Not relevant for XML.</li>
|
|
114 |
* <li><b>NOTATION</b>
|
|
115 |
* associates notation names with URIs.</li>
|
|
116 |
* <li><b>OVERRIDE</b>
|
|
117 |
* changes the override behavior. Initial behavior is set by the
|
|
118 |
* system property <tt>xml.catalog.override</tt>. The default initial
|
|
119 |
* behavior is 'YES', that is, entries in the catalog override
|
|
120 |
* system identifiers specified in the document.</li>
|
|
121 |
* <li><b>PUBLIC</b>
|
|
122 |
* maps a public identifier to a system identifier.</li>
|
|
123 |
* <li><b>SGMLDECL</b>
|
|
124 |
* recognized and silently ignored. Not relevant for XML.</li>
|
|
125 |
* <li><b>SYSTEM</b>
|
|
126 |
* maps a system identifier to another system identifier.</li>
|
|
127 |
* <li><b>URI</b>
|
|
128 |
* maps a URI to another URI.</li>
|
|
129 |
* </ul>
|
|
130 |
*
|
|
131 |
* <p>Note that BASE entries are treated as described by RFC2396. In
|
|
132 |
* particular, this has the counter-intuitive property that after a BASE
|
|
133 |
* entry identifing "http://example.com/a/b/c" as the base URI,
|
|
134 |
* the relative URI "foo" is resolved to the absolute URI
|
|
135 |
* "http://example.com/a/b/foo". You must provide the trailing slash if
|
|
136 |
* you do not want the final component of the path to be discarded as a
|
|
137 |
* filename would in a URI for a resource: "http://example.com/a/b/c/".
|
|
138 |
* </p>
|
|
139 |
*
|
|
140 |
* <p>Note that subordinate catalogs (all catalogs except the first,
|
|
141 |
* including CATALOG and DELEGATE* catalogs) are only loaded if and when
|
|
142 |
* they are required.</p>
|
|
143 |
*
|
|
144 |
* <p>This class relies on classes which implement the CatalogReader
|
|
145 |
* interface to actually load catalog files. This allows the catalog
|
|
146 |
* semantics to be implemented for TR9401 text-based catalogs, XML
|
|
147 |
* catalogs, or any number of other storage formats.</p>
|
|
148 |
*
|
|
149 |
* <p>Additional catalogs may also be loaded with the
|
|
150 |
* {@link #parseCatalog} method.</p>
|
|
151 |
*
|
|
152 |
* <p><b>Change Log:</b></p>
|
|
153 |
* <dl>
|
|
154 |
* <dt>2.0</dt>
|
|
155 |
* <dd><p>Rewrite to use CatalogReaders.</p></dd>
|
|
156 |
* <dt>1.1</dt>
|
|
157 |
* <dd><p>Allow quoted components in <tt>xml.catalog.files</tt>
|
|
158 |
* so that URLs containing colons can be used on Unix.
|
|
159 |
* The string passed to <tt>xml.catalog.files</tt> can now have the form:</p>
|
|
160 |
* <pre>
|
|
161 |
* unquoted-path-with-no-sep-chars:"double-quoted path with or without sep chars":'single-quoted path with or without sep chars'
|
|
162 |
* </pre>
|
|
163 |
* <p>(Where ":" is the separater character in this example.)</p>
|
|
164 |
* <p>If an unquoted path contains an embedded double or single quote
|
|
165 |
* character, no special processig is performed on that character. No
|
|
166 |
* path can contain separater characters, double, and single quotes
|
|
167 |
* simultaneously.</p>
|
|
168 |
* <p>Fix bug in calculation of BASE entries: if
|
|
169 |
* a catalog contains multiple BASE entries, each is relative to the preceding
|
|
170 |
* base, not the default base URI of the catalog.</p>
|
|
171 |
* </dd>
|
|
172 |
* <dt>1.0.1</dt>
|
|
173 |
* <dd><p>Fixed a bug in the calculation of the list of subordinate catalogs.
|
|
174 |
* This bug caused an infinite loop where parsing would alternately process
|
|
175 |
* two catalogs indefinitely.</p>
|
|
176 |
* </dd>
|
|
177 |
* </dl>
|
|
178 |
*
|
|
179 |
* @see CatalogReader
|
|
180 |
* @see CatalogEntry
|
|
181 |
*
|
|
182 |
* @author Norman Walsh
|
|
183 |
* <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
|
|
184 |
*
|
|
185 |
* @version 1.0
|
|
186 |
*
|
|
187 |
* <p>Derived from public domain code originally published by Arbortext,
|
|
188 |
* Inc.</p>
|
|
189 |
*/
|
|
190 |
public class Catalog {
|
|
191 |
/** The BASE Catalog Entry type. */
|
|
192 |
public static final int BASE = CatalogEntry.addEntryType("BASE", 1);
|
|
193 |
|
|
194 |
/** The CATALOG Catalog Entry type. */
|
|
195 |
public static final int CATALOG = CatalogEntry.addEntryType("CATALOG", 1);
|
|
196 |
|
|
197 |
/** The DOCUMENT Catalog Entry type. */
|
|
198 |
public static final int DOCUMENT = CatalogEntry.addEntryType("DOCUMENT", 1);
|
|
199 |
|
|
200 |
/** The OVERRIDE Catalog Entry type. */
|
|
201 |
public static final int OVERRIDE = CatalogEntry.addEntryType("OVERRIDE", 1);
|
|
202 |
|
|
203 |
/** The SGMLDECL Catalog Entry type. */
|
|
204 |
public static final int SGMLDECL = CatalogEntry.addEntryType("SGMLDECL", 1);
|
|
205 |
|
|
206 |
/** The DELEGATE_PUBLIC Catalog Entry type. */
|
|
207 |
public static final int DELEGATE_PUBLIC = CatalogEntry.addEntryType("DELEGATE_PUBLIC", 2);
|
|
208 |
|
|
209 |
/** The DELEGATE_SYSTEM Catalog Entry type. */
|
|
210 |
public static final int DELEGATE_SYSTEM = CatalogEntry.addEntryType("DELEGATE_SYSTEM", 2);
|
|
211 |
|
|
212 |
/** The DELEGATE_URI Catalog Entry type. */
|
|
213 |
public static final int DELEGATE_URI = CatalogEntry.addEntryType("DELEGATE_URI", 2);
|
|
214 |
|
|
215 |
/** The DOCTYPE Catalog Entry type. */
|
|
216 |
public static final int DOCTYPE = CatalogEntry.addEntryType("DOCTYPE", 2);
|
|
217 |
|
|
218 |
/** The DTDDECL Catalog Entry type. */
|
|
219 |
public static final int DTDDECL = CatalogEntry.addEntryType("DTDDECL", 2);
|
|
220 |
|
|
221 |
/** The ENTITY Catalog Entry type. */
|
|
222 |
public static final int ENTITY = CatalogEntry.addEntryType("ENTITY", 2);
|
|
223 |
|
|
224 |
/** The LINKTYPE Catalog Entry type. */
|
|
225 |
public static final int LINKTYPE = CatalogEntry.addEntryType("LINKTYPE", 2);
|
|
226 |
|
|
227 |
/** The NOTATION Catalog Entry type. */
|
|
228 |
public static final int NOTATION = CatalogEntry.addEntryType("NOTATION", 2);
|
|
229 |
|
|
230 |
/** The PUBLIC Catalog Entry type. */
|
|
231 |
public static final int PUBLIC = CatalogEntry.addEntryType("PUBLIC", 2);
|
|
232 |
|
|
233 |
/** The SYSTEM Catalog Entry type. */
|
|
234 |
public static final int SYSTEM = CatalogEntry.addEntryType("SYSTEM", 2);
|
|
235 |
|
|
236 |
/** The URI Catalog Entry type. */
|
|
237 |
public static final int URI = CatalogEntry.addEntryType("URI", 2);
|
|
238 |
|
|
239 |
/** The REWRITE_SYSTEM Catalog Entry type. */
|
|
240 |
public static final int REWRITE_SYSTEM = CatalogEntry.addEntryType("REWRITE_SYSTEM", 2);
|
|
241 |
|
|
242 |
/** The REWRITE_URI Catalog Entry type. */
|
|
243 |
public static final int REWRITE_URI = CatalogEntry.addEntryType("REWRITE_URI", 2);
|
|
244 |
/** The SYSTEM_SUFFIX Catalog Entry type. */
|
|
245 |
public static final int SYSTEM_SUFFIX = CatalogEntry.addEntryType("SYSTEM_SUFFIX", 2);
|
|
246 |
/** The URI_SUFFIX Catalog Entry type. */
|
|
247 |
public static final int URI_SUFFIX = CatalogEntry.addEntryType("URI_SUFFIX", 2);
|
|
248 |
|
|
249 |
/**
|
|
250 |
* The base URI for relative system identifiers in the catalog.
|
|
251 |
* This may be changed by BASE entries in the catalog.
|
|
252 |
*/
|
|
253 |
protected URL base;
|
|
254 |
|
|
255 |
/** The base URI of the Catalog file currently being parsed. */
|
|
256 |
protected URL catalogCwd;
|
|
257 |
|
|
258 |
/** The catalog entries currently known to the system. */
|
|
259 |
protected Vector catalogEntries = new Vector();
|
|
260 |
|
|
261 |
/** The default initial override setting. */
|
|
262 |
protected boolean default_override = true;
|
|
263 |
|
|
264 |
/** The catalog manager in use for this instance. */
|
|
265 |
protected CatalogManager catalogManager = CatalogManager.getStaticManager();
|
|
266 |
|
|
267 |
/**
|
|
268 |
* A vector of catalog files to be loaded.
|
|
269 |
*
|
|
270 |
* <p>This list is initially established by
|
|
271 |
* <code>loadSystemCatalogs</code> when
|
|
272 |
* it parses the system catalog list, but CATALOG entries may
|
|
273 |
* contribute to it during the course of parsing.</p>
|
|
274 |
*
|
|
275 |
* @see #loadSystemCatalogs
|
|
276 |
* @see #localCatalogFiles
|
|
277 |
*/
|
|
278 |
protected Vector catalogFiles = new Vector();
|
|
279 |
|
|
280 |
/**
|
|
281 |
* A vector of catalog files constructed during processing of
|
|
282 |
* CATALOG entries in the current catalog.
|
|
283 |
*
|
|
284 |
* <p>This two-level system is actually necessary to correctly implement
|
|
285 |
* the semantics of the CATALOG entry. If one catalog file includes
|
|
286 |
* another with a CATALOG entry, the included catalog logically
|
|
287 |
* occurs <i>at the end</i> of the including catalog, and after any
|
|
288 |
* preceding CATALOG entries. In other words, the CATALOG entry
|
|
289 |
* cannot insert anything into the middle of a catalog file.</p>
|
|
290 |
*
|
|
291 |
* <p>When processing reaches the end of each catalog files, any
|
|
292 |
* elements on this vector are added to the front of the
|
|
293 |
* <code>catalogFiles</code> vector.</p>
|
|
294 |
*
|
|
295 |
* @see #catalogFiles
|
|
296 |
*/
|
|
297 |
protected Vector localCatalogFiles = new Vector();
|
|
298 |
|
|
299 |
/**
|
|
300 |
* A vector of Catalogs.
|
|
301 |
*
|
|
302 |
* <p>The semantics of Catalog resolution are such that each
|
|
303 |
* catalog is effectively a list of Catalogs (in other words,
|
|
304 |
* a recursive list of Catalog instances).</p>
|
|
305 |
*
|
|
306 |
* <p>Catalogs that are processed as the result of CATALOG or
|
|
307 |
* DELEGATE* entries are subordinate to the catalog that contained
|
|
308 |
* them, but they may in turn have subordinate catalogs.</p>
|
|
309 |
*
|
|
310 |
* <p>Catalogs are only loaded when they are needed, so this vector
|
|
311 |
* initially contains a list of Catalog filenames (URLs). If, during
|
|
312 |
* processing, one of these catalogs has to be loaded, the resulting
|
|
313 |
* Catalog object is placed in the vector, effectively caching it
|
|
314 |
* for the next query.</p>
|
|
315 |
*/
|
|
316 |
protected Vector catalogs = new Vector();
|
|
317 |
|
|
318 |
/**
|
|
319 |
* A vector of DELEGATE* Catalog entries constructed during
|
|
320 |
* processing of the Catalog.
|
|
321 |
*
|
|
322 |
* <p>This two-level system has two purposes; first, it allows
|
|
323 |
* us to sort the DELEGATE* entries by the length of the partial
|
|
324 |
* public identifier so that a linear search encounters them in
|
|
325 |
* the correct order and second, it puts them all at the end of
|
|
326 |
* the Catalog.</p>
|
|
327 |
*
|
|
328 |
* <p>When processing reaches the end of each catalog file, any
|
|
329 |
* elements on this vector are added to the end of the
|
|
330 |
* <code>catalogEntries</code> vector. This assures that matching
|
|
331 |
* PUBLIC keywords are encountered before DELEGATE* entries.</p>
|
|
332 |
*/
|
|
333 |
protected Vector localDelegate = new Vector();
|
|
334 |
|
|
335 |
/**
|
|
336 |
* A hash of CatalogReaders.
|
|
337 |
*
|
|
338 |
* <p>This hash maps MIME types to elements in the readerArr
|
|
339 |
* vector. This allows the Catalog to quickly locate the reader
|
|
340 |
* for a particular MIME type.</p>
|
|
341 |
*/
|
33349
|
342 |
protected Map<String, Integer> readerMap = new HashMap<>();
|
12005
|
343 |
|
|
344 |
/**
|
|
345 |
* A vector of CatalogReaders.
|
|
346 |
*
|
|
347 |
* <p>This vector contains all of the readers in the order that they
|
|
348 |
* were added. In the event that a catalog is read from a file, where
|
|
349 |
* the MIME type is unknown, each reader is attempted in turn until
|
|
350 |
* one succeeds.</p>
|
|
351 |
*/
|
|
352 |
protected Vector readerArr = new Vector();
|
|
353 |
|
|
354 |
/**
|
|
355 |
* Constructs an empty Catalog.
|
|
356 |
*
|
|
357 |
* <p>The constructor interrogates the relevant system properties
|
|
358 |
* using the default (static) CatalogManager
|
|
359 |
* and initializes the catalog data structures.</p>
|
|
360 |
*/
|
|
361 |
public Catalog() {
|
|
362 |
// nop;
|
|
363 |
}
|
|
364 |
|
|
365 |
/**
|
|
366 |
* Constructs an empty Catalog with a specific CatalogManager.
|
|
367 |
*
|
|
368 |
* <p>The constructor interrogates the relevant system properties
|
|
369 |
* using the specified Catalog Manager
|
|
370 |
* and initializes the catalog data structures.</p>
|
|
371 |
*/
|
|
372 |
public Catalog(CatalogManager manager) {
|
|
373 |
catalogManager = manager;
|
|
374 |
}
|
|
375 |
|
|
376 |
/**
|
|
377 |
* Return the CatalogManager used by this catalog.
|
|
378 |
*
|
|
379 |
*/
|
|
380 |
public CatalogManager getCatalogManager() {
|
|
381 |
return catalogManager;
|
|
382 |
}
|
|
383 |
|
|
384 |
/**
|
|
385 |
* Establish the CatalogManager used by this catalog.
|
|
386 |
*
|
|
387 |
*/
|
|
388 |
public void setCatalogManager(CatalogManager manager) {
|
|
389 |
catalogManager = manager;
|
|
390 |
}
|
|
391 |
|
|
392 |
/**
|
|
393 |
* Setup readers.
|
|
394 |
*/
|
|
395 |
public void setupReaders() {
|
12458
|
396 |
SAXParserFactory spf = catalogManager.useServicesMechanism() ?
|
|
397 |
SAXParserFactory.newInstance() : new SAXParserFactoryImpl();
|
12005
|
398 |
spf.setNamespaceAware(true);
|
|
399 |
spf.setValidating(false);
|
|
400 |
|
|
401 |
SAXCatalogReader saxReader = new SAXCatalogReader(spf);
|
|
402 |
|
|
403 |
saxReader.setCatalogParser(null, "XMLCatalog",
|
|
404 |
"com.sun.org.apache.xml.internal.resolver.readers.XCatalogReader");
|
|
405 |
|
|
406 |
saxReader.setCatalogParser(OASISXMLCatalogReader.namespaceName,
|
|
407 |
"catalog",
|
|
408 |
"com.sun.org.apache.xml.internal.resolver.readers.OASISXMLCatalogReader");
|
|
409 |
|
|
410 |
addReader("application/xml", saxReader);
|
|
411 |
|
|
412 |
TR9401CatalogReader textReader = new TR9401CatalogReader();
|
|
413 |
addReader("text/plain", textReader);
|
|
414 |
}
|
|
415 |
|
|
416 |
/**
|
|
417 |
* Add a new CatalogReader to the Catalog.
|
|
418 |
*
|
|
419 |
* <p>This method allows you to add a new CatalogReader to the
|
|
420 |
* catalog. The reader will be associated with the specified mimeType.
|
|
421 |
* You can only have one reader per mimeType.</p>
|
|
422 |
*
|
|
423 |
* <p>In the absence of a mimeType (e.g., when reading a catalog
|
|
424 |
* directly from a file on the local system), the readers are attempted
|
|
425 |
* in the order that you add them to the Catalog.</p>
|
|
426 |
*
|
|
427 |
* <p>Note that subordinate catalogs (created by CATALOG or
|
|
428 |
* DELEGATE* entries) get a copy of the set of readers present in
|
|
429 |
* the primary catalog when they are created. Readers added subsequently
|
|
430 |
* will not be available. For this reason, it is best to add all
|
|
431 |
* of the readers before the first call to parse a catalog.</p>
|
|
432 |
*
|
|
433 |
* @param mimeType The MIME type associated with this reader.
|
|
434 |
* @param reader The CatalogReader to use.
|
|
435 |
*/
|
|
436 |
public void addReader(String mimeType, CatalogReader reader) {
|
|
437 |
if (readerMap.containsKey(mimeType)) {
|
33349
|
438 |
Integer pos = readerMap.get(mimeType);
|
|
439 |
readerArr.set(pos, reader);
|
12005
|
440 |
} else {
|
|
441 |
readerArr.add(reader);
|
33349
|
442 |
Integer pos = readerArr.size()-1;
|
12005
|
443 |
readerMap.put(mimeType, pos);
|
|
444 |
}
|
|
445 |
}
|
|
446 |
|
|
447 |
/**
|
|
448 |
* Copies the reader list from the current Catalog to a new Catalog.
|
|
449 |
*
|
|
450 |
* <p>This method is used internally when constructing a new catalog.
|
|
451 |
* It copies the current reader associations over to the new catalog.
|
|
452 |
* </p>
|
|
453 |
*
|
|
454 |
* @param newCatalog The new Catalog.
|
|
455 |
*/
|
|
456 |
protected void copyReaders(Catalog newCatalog) {
|
|
457 |
// Have to copy the readers in the right order...convert hash to arr
|
|
458 |
Vector mapArr = new Vector(readerMap.size());
|
|
459 |
|
|
460 |
// Pad the mapArr out to the right length
|
|
461 |
for (int count = 0; count < readerMap.size(); count++) {
|
|
462 |
mapArr.add(null);
|
|
463 |
}
|
|
464 |
|
33349
|
465 |
for (Map.Entry<String, Integer> entry : readerMap.entrySet()) {
|
|
466 |
mapArr.set(entry.getValue().intValue(), entry.getKey());
|
12005
|
467 |
}
|
|
468 |
|
|
469 |
for (int count = 0; count < mapArr.size(); count++) {
|
|
470 |
String mimeType = (String) mapArr.get(count);
|
33349
|
471 |
Integer pos = readerMap.get(mimeType);
|
12005
|
472 |
newCatalog.addReader(mimeType,
|
|
473 |
(CatalogReader)
|
|
474 |
readerArr.get(pos.intValue()));
|
|
475 |
}
|
|
476 |
}
|
|
477 |
|
|
478 |
/**
|
|
479 |
* Create a new Catalog object.
|
|
480 |
*
|
|
481 |
* <p>This method constructs a new instance of the running Catalog
|
|
482 |
* class (which might be a subtype of com.sun.org.apache.xml.internal.resolver.Catalog).
|
|
483 |
* All new catalogs are managed by the same CatalogManager.
|
|
484 |
* </p>
|
|
485 |
*
|
|
486 |
* <p>N.B. All Catalog subtypes should call newCatalog() to construct
|
|
487 |
* a new Catalog. Do not simply use "new Subclass()" since that will
|
|
488 |
* confuse future subclasses.</p>
|
|
489 |
*/
|
|
490 |
protected Catalog newCatalog() {
|
|
491 |
String catalogClass = this.getClass().getName();
|
|
492 |
|
|
493 |
try {
|
|
494 |
Catalog c = (Catalog) (Class.forName(catalogClass).newInstance());
|
|
495 |
c.setCatalogManager(catalogManager);
|
|
496 |
copyReaders(c);
|
|
497 |
return c;
|
|
498 |
} catch (ClassNotFoundException cnfe) {
|
|
499 |
catalogManager.debug.message(1, "Class Not Found Exception: " + catalogClass);
|
|
500 |
} catch (IllegalAccessException iae) {
|
|
501 |
catalogManager.debug.message(1, "Illegal Access Exception: " + catalogClass);
|
|
502 |
} catch (InstantiationException ie) {
|
|
503 |
catalogManager.debug.message(1, "Instantiation Exception: " + catalogClass);
|
|
504 |
} catch (ClassCastException cce) {
|
|
505 |
catalogManager.debug.message(1, "Class Cast Exception: " + catalogClass);
|
|
506 |
} catch (Exception e) {
|
|
507 |
catalogManager.debug.message(1, "Other Exception: " + catalogClass);
|
|
508 |
}
|
|
509 |
|
|
510 |
Catalog c = new Catalog();
|
|
511 |
c.setCatalogManager(catalogManager);
|
|
512 |
copyReaders(c);
|
|
513 |
return c;
|
|
514 |
}
|
|
515 |
|
|
516 |
/**
|
|
517 |
* Returns the current base URI.
|
|
518 |
*/
|
|
519 |
public String getCurrentBase() {
|
|
520 |
return base.toString();
|
|
521 |
}
|
|
522 |
|
|
523 |
/**
|
|
524 |
* Returns the default override setting associated with this
|
|
525 |
* catalog.
|
|
526 |
*
|
|
527 |
* <p>All catalog files loaded by this catalog will have the
|
|
528 |
* initial override setting specified by this default.</p>
|
|
529 |
*/
|
|
530 |
public String getDefaultOverride() {
|
|
531 |
if (default_override) {
|
|
532 |
return "yes";
|
|
533 |
} else {
|
|
534 |
return "no";
|
|
535 |
}
|
|
536 |
}
|
|
537 |
|
|
538 |
/**
|
|
539 |
* Load the system catalog files.
|
|
540 |
*
|
|
541 |
* <p>The method adds all of the
|
|
542 |
* catalogs specified in the <tt>xml.catalog.files</tt> property
|
|
543 |
* to the Catalog list.</p>
|
|
544 |
*
|
|
545 |
* @throws MalformedURLException One of the system catalogs is
|
|
546 |
* identified with a filename that is not a valid URL.
|
|
547 |
* @throws IOException One of the system catalogs cannot be read.
|
|
548 |
*/
|
|
549 |
public void loadSystemCatalogs()
|
|
550 |
throws MalformedURLException, IOException {
|
|
551 |
|
|
552 |
Vector catalogs = catalogManager.getCatalogFiles();
|
|
553 |
if (catalogs != null) {
|
|
554 |
for (int count = 0; count < catalogs.size(); count++) {
|
|
555 |
catalogFiles.addElement(catalogs.elementAt(count));
|
|
556 |
}
|
|
557 |
}
|
|
558 |
|
|
559 |
if (catalogFiles.size() > 0) {
|
|
560 |
// This is a little odd. The parseCatalog() method expects
|
|
561 |
// a filename, but it adds that name to the end of the
|
|
562 |
// catalogFiles vector, and then processes that vector.
|
|
563 |
// This allows the system to handle CATALOG entries
|
|
564 |
// correctly.
|
|
565 |
//
|
|
566 |
// In this init case, we take the last element off the
|
|
567 |
// catalogFiles vector and pass it to parseCatalog. This
|
|
568 |
// will "do the right thing" in the init case, and allow
|
|
569 |
// parseCatalog() to do the right thing in the non-init
|
|
570 |
// case. Honest.
|
|
571 |
//
|
|
572 |
String catfile = (String) catalogFiles.lastElement();
|
|
573 |
catalogFiles.removeElement(catfile);
|
|
574 |
parseCatalog(catfile);
|
|
575 |
}
|
|
576 |
}
|
|
577 |
|
|
578 |
/**
|
|
579 |
* Parse a catalog file, augmenting internal data structures.
|
|
580 |
*
|
|
581 |
* @param fileName The filename of the catalog file to process
|
|
582 |
*
|
|
583 |
* @throws MalformedURLException The fileName cannot be turned into
|
|
584 |
* a valid URL.
|
|
585 |
* @throws IOException Error reading catalog file.
|
|
586 |
*/
|
|
587 |
public synchronized void parseCatalog(String fileName)
|
|
588 |
throws MalformedURLException, IOException {
|
|
589 |
|
|
590 |
default_override = catalogManager.getPreferPublic();
|
|
591 |
catalogManager.debug.message(4, "Parse catalog: " + fileName);
|
|
592 |
|
|
593 |
// Put the file into the list of catalogs to process...
|
|
594 |
// In all cases except the case when initCatalog() is the
|
|
595 |
// caller, this will be the only catalog initially in the list...
|
|
596 |
catalogFiles.addElement(fileName);
|
|
597 |
|
|
598 |
// Now process all the pending catalogs...
|
|
599 |
parsePendingCatalogs();
|
|
600 |
}
|
|
601 |
|
|
602 |
/**
|
|
603 |
* Parse a catalog file, augmenting internal data structures.
|
|
604 |
*
|
|
605 |
* <p>Catalogs retrieved over the net may have an associated MIME type.
|
|
606 |
* The MIME type can be used to select an appropriate reader.</p>
|
|
607 |
*
|
|
608 |
* @param mimeType The MIME type of the catalog file.
|
|
609 |
* @param is The InputStream from which the catalog should be read
|
|
610 |
*
|
|
611 |
* @throws CatalogException Failed to load catalog
|
|
612 |
* mimeType.
|
|
613 |
* @throws IOException Error reading catalog file.
|
|
614 |
*/
|
|
615 |
public synchronized void parseCatalog(String mimeType, InputStream is)
|
|
616 |
throws IOException, CatalogException {
|
|
617 |
|
|
618 |
default_override = catalogManager.getPreferPublic();
|
|
619 |
catalogManager.debug.message(4, "Parse " + mimeType + " catalog on input stream");
|
|
620 |
|
|
621 |
CatalogReader reader = null;
|
|
622 |
|
|
623 |
if (readerMap.containsKey(mimeType)) {
|
|
624 |
int arrayPos = ((Integer) readerMap.get(mimeType)).intValue();
|
|
625 |
reader = (CatalogReader) readerArr.get(arrayPos);
|
|
626 |
}
|
|
627 |
|
|
628 |
if (reader == null) {
|
|
629 |
String msg = "No CatalogReader for MIME type: " + mimeType;
|
|
630 |
catalogManager.debug.message(2, msg);
|
|
631 |
throw new CatalogException(CatalogException.UNPARSEABLE, msg);
|
|
632 |
}
|
|
633 |
|
|
634 |
reader.readCatalog(this, is);
|
|
635 |
|
|
636 |
// Now process all the pending catalogs...
|
|
637 |
parsePendingCatalogs();
|
|
638 |
}
|
|
639 |
|
|
640 |
/**
|
|
641 |
* Parse a catalog document, augmenting internal data structures.
|
|
642 |
*
|
|
643 |
* <p>This method supports catalog files stored in jar files: e.g.,
|
|
644 |
* jar:file:///path/to/filename.jar!/path/to/catalog.xml". That URI
|
|
645 |
* doesn't survive transmogrification through the URI processing that
|
|
646 |
* the parseCatalog(String) performs and passing it as an input stream
|
|
647 |
* doesn't set the base URI appropriately.</p>
|
|
648 |
*
|
|
649 |
* <p>Written by Stefan Wachter (2002-09-26)</p>
|
|
650 |
*
|
|
651 |
* @param aUrl The URL of the catalog document to process
|
|
652 |
*
|
|
653 |
* @throws IOException Error reading catalog file.
|
|
654 |
*/
|
|
655 |
public synchronized void parseCatalog(URL aUrl) throws IOException {
|
|
656 |
catalogCwd = aUrl;
|
|
657 |
base = aUrl;
|
|
658 |
|
|
659 |
default_override = catalogManager.getPreferPublic();
|
|
660 |
catalogManager.debug.message(4, "Parse catalog: " + aUrl.toString());
|
|
661 |
|
|
662 |
DataInputStream inStream = null;
|
|
663 |
boolean parsed = false;
|
|
664 |
|
|
665 |
for (int count = 0; !parsed && count < readerArr.size(); count++) {
|
|
666 |
CatalogReader reader = (CatalogReader) readerArr.get(count);
|
|
667 |
|
|
668 |
try {
|
|
669 |
inStream = new DataInputStream(aUrl.openStream());
|
|
670 |
} catch (FileNotFoundException fnfe) {
|
|
671 |
// No catalog; give up!
|
|
672 |
break;
|
|
673 |
}
|
|
674 |
|
|
675 |
try {
|
|
676 |
reader.readCatalog(this, inStream);
|
|
677 |
parsed=true;
|
|
678 |
} catch (CatalogException ce) {
|
|
679 |
if (ce.getExceptionType() == CatalogException.PARSE_FAILED) {
|
|
680 |
// give up!
|
|
681 |
break;
|
|
682 |
} else {
|
|
683 |
// try again!
|
|
684 |
}
|
|
685 |
}
|
|
686 |
|
|
687 |
try {
|
|
688 |
inStream.close();
|
|
689 |
} catch (IOException e) {
|
|
690 |
//nop
|
|
691 |
}
|
|
692 |
}
|
|
693 |
|
|
694 |
if (parsed) parsePendingCatalogs();
|
|
695 |
}
|
|
696 |
|
|
697 |
/**
|
|
698 |
* Parse all of the pending catalogs.
|
|
699 |
*
|
|
700 |
* <p>Catalogs may refer to other catalogs, this method parses
|
|
701 |
* all of the currently pending catalog files.</p>
|
|
702 |
*/
|
|
703 |
protected synchronized void parsePendingCatalogs()
|
|
704 |
throws MalformedURLException, IOException {
|
|
705 |
|
|
706 |
if (!localCatalogFiles.isEmpty()) {
|
|
707 |
// Move all the localCatalogFiles into the front of
|
|
708 |
// the catalogFiles queue
|
|
709 |
Vector newQueue = new Vector();
|
|
710 |
Enumeration q = localCatalogFiles.elements();
|
|
711 |
while (q.hasMoreElements()) {
|
|
712 |
newQueue.addElement(q.nextElement());
|
|
713 |
}
|
|
714 |
|
|
715 |
// Put the rest of the catalogs on the end of the new list
|
|
716 |
for (int curCat = 0; curCat < catalogFiles.size(); curCat++) {
|
|
717 |
String catfile = (String) catalogFiles.elementAt(curCat);
|
|
718 |
newQueue.addElement(catfile);
|
|
719 |
}
|
|
720 |
|
|
721 |
catalogFiles = newQueue;
|
|
722 |
localCatalogFiles.clear();
|
|
723 |
}
|
|
724 |
|
|
725 |
// Suppose there are no catalog files to process, but the
|
|
726 |
// single catalog already parsed included some delegate
|
|
727 |
// entries? Make sure they don't get lost.
|
|
728 |
if (catalogFiles.isEmpty() && !localDelegate.isEmpty()) {
|
|
729 |
Enumeration e = localDelegate.elements();
|
|
730 |
while (e.hasMoreElements()) {
|
|
731 |
catalogEntries.addElement(e.nextElement());
|
|
732 |
}
|
|
733 |
localDelegate.clear();
|
|
734 |
}
|
|
735 |
|
|
736 |
// Now process all the files on the catalogFiles vector. This
|
|
737 |
// vector can grow during processing if CATALOG entries are
|
|
738 |
// encountered in the catalog
|
|
739 |
while (!catalogFiles.isEmpty()) {
|
|
740 |
String catfile = (String) catalogFiles.elementAt(0);
|
|
741 |
try {
|
|
742 |
catalogFiles.remove(0);
|
|
743 |
} catch (ArrayIndexOutOfBoundsException e) {
|
|
744 |
// can't happen
|
|
745 |
}
|
|
746 |
|
|
747 |
if (catalogEntries.size() == 0 && catalogs.size() == 0) {
|
|
748 |
// We haven't parsed any catalogs yet, let this
|
|
749 |
// catalog be the first...
|
|
750 |
try {
|
|
751 |
parseCatalogFile(catfile);
|
|
752 |
} catch (CatalogException ce) {
|
|
753 |
System.out.println("FIXME: " + ce.toString());
|
|
754 |
}
|
|
755 |
} else {
|
|
756 |
// This is a subordinate catalog. We save its name,
|
|
757 |
// but don't bother to load it unless it's necessary.
|
|
758 |
catalogs.addElement(catfile);
|
|
759 |
}
|
|
760 |
|
|
761 |
if (!localCatalogFiles.isEmpty()) {
|
|
762 |
// Move all the localCatalogFiles into the front of
|
|
763 |
// the catalogFiles queue
|
|
764 |
Vector newQueue = new Vector();
|
|
765 |
Enumeration q = localCatalogFiles.elements();
|
|
766 |
while (q.hasMoreElements()) {
|
|
767 |
newQueue.addElement(q.nextElement());
|
|
768 |
}
|
|
769 |
|
|
770 |
// Put the rest of the catalogs on the end of the new list
|
|
771 |
for (int curCat = 0; curCat < catalogFiles.size(); curCat++) {
|
|
772 |
catfile = (String) catalogFiles.elementAt(curCat);
|
|
773 |
newQueue.addElement(catfile);
|
|
774 |
}
|
|
775 |
|
|
776 |
catalogFiles = newQueue;
|
|
777 |
localCatalogFiles.clear();
|
|
778 |
}
|
|
779 |
|
|
780 |
if (!localDelegate.isEmpty()) {
|
|
781 |
Enumeration e = localDelegate.elements();
|
|
782 |
while (e.hasMoreElements()) {
|
|
783 |
catalogEntries.addElement(e.nextElement());
|
|
784 |
}
|
|
785 |
localDelegate.clear();
|
|
786 |
}
|
|
787 |
}
|
|
788 |
|
|
789 |
// We've parsed them all, reinit the vector...
|
|
790 |
catalogFiles.clear();
|
|
791 |
}
|
|
792 |
|
|
793 |
/**
|
|
794 |
* Parse a single catalog file, augmenting internal data structures.
|
|
795 |
*
|
|
796 |
* @param fileName The filename of the catalog file to process
|
|
797 |
*
|
|
798 |
* @throws MalformedURLException The fileName cannot be turned into
|
|
799 |
* a valid URL.
|
|
800 |
* @throws IOException Error reading catalog file.
|
|
801 |
*/
|
|
802 |
protected synchronized void parseCatalogFile(String fileName)
|
|
803 |
throws MalformedURLException, IOException, CatalogException {
|
|
804 |
|
|
805 |
CatalogEntry entry;
|
|
806 |
|
|
807 |
// The base-base is the cwd. If the catalog file is specified
|
|
808 |
// with a relative path, this assures that it gets resolved
|
|
809 |
// properly...
|
|
810 |
try {
|
|
811 |
// tack on a basename because URLs point to files not dirs
|
|
812 |
catalogCwd = FileURL.makeURL("basename");
|
|
813 |
} catch (MalformedURLException e) {
|
16953
|
814 |
String userdir = SecuritySupport.getSystemProperty("user.dir");
|
26516
|
815 |
userdir = userdir.replace('\\', '/');
|
12005
|
816 |
catalogManager.debug.message(1, "Malformed URL on cwd", userdir);
|
|
817 |
catalogCwd = null;
|
|
818 |
}
|
|
819 |
|
|
820 |
// The initial base URI is the location of the catalog file
|
|
821 |
try {
|
|
822 |
base = new URL(catalogCwd, fixSlashes(fileName));
|
|
823 |
} catch (MalformedURLException e) {
|
|
824 |
try {
|
|
825 |
base = new URL("file:" + fixSlashes(fileName));
|
|
826 |
} catch (MalformedURLException e2) {
|
|
827 |
catalogManager.debug.message(1, "Malformed URL on catalog filename",
|
|
828 |
fixSlashes(fileName));
|
|
829 |
base = null;
|
|
830 |
}
|
|
831 |
}
|
|
832 |
|
|
833 |
catalogManager.debug.message(2, "Loading catalog", fileName);
|
|
834 |
catalogManager.debug.message(4, "Default BASE", base.toString());
|
|
835 |
|
|
836 |
fileName = base.toString();
|
|
837 |
|
|
838 |
DataInputStream inStream = null;
|
|
839 |
boolean parsed = false;
|
|
840 |
boolean notFound = false;
|
|
841 |
|
|
842 |
for (int count = 0; !parsed && count < readerArr.size(); count++) {
|
|
843 |
CatalogReader reader = (CatalogReader) readerArr.get(count);
|
|
844 |
|
|
845 |
try {
|
|
846 |
notFound = false;
|
|
847 |
inStream = new DataInputStream(base.openStream());
|
|
848 |
} catch (FileNotFoundException fnfe) {
|
|
849 |
// No catalog; give up!
|
|
850 |
notFound = true;
|
|
851 |
break;
|
|
852 |
}
|
|
853 |
|
|
854 |
try {
|
|
855 |
reader.readCatalog(this, inStream);
|
|
856 |
parsed = true;
|
|
857 |
} catch (CatalogException ce) {
|
|
858 |
if (ce.getExceptionType() == CatalogException.PARSE_FAILED) {
|
|
859 |
// give up!
|
|
860 |
break;
|
|
861 |
} else {
|
|
862 |
// try again!
|
|
863 |
}
|
|
864 |
}
|
|
865 |
|
|
866 |
try {
|
|
867 |
inStream.close();
|
|
868 |
} catch (IOException e) {
|
|
869 |
//nop
|
|
870 |
}
|
|
871 |
}
|
|
872 |
|
|
873 |
if (!parsed) {
|
|
874 |
if (notFound) {
|
|
875 |
catalogManager.debug.message(3, "Catalog does not exist", fileName);
|
|
876 |
} else {
|
|
877 |
catalogManager.debug.message(1, "Failed to parse catalog", fileName);
|
|
878 |
}
|
|
879 |
}
|
|
880 |
}
|
|
881 |
|
|
882 |
/**
|
|
883 |
* Cleanup and process a Catalog entry.
|
|
884 |
*
|
|
885 |
* <p>This method processes each Catalog entry, changing mapped
|
|
886 |
* relative system identifiers into absolute ones (based on the current
|
|
887 |
* base URI), and maintaining other information about the current
|
|
888 |
* catalog.</p>
|
|
889 |
*
|
|
890 |
* @param entry The CatalogEntry to process.
|
|
891 |
*/
|
|
892 |
public void addEntry(CatalogEntry entry) {
|
|
893 |
int type = entry.getEntryType();
|
|
894 |
|
|
895 |
if (type == BASE) {
|
|
896 |
String value = entry.getEntryArg(0);
|
|
897 |
URL newbase = null;
|
|
898 |
|
|
899 |
if (base == null) {
|
|
900 |
catalogManager.debug.message(5, "BASE CUR", "null");
|
|
901 |
} else {
|
|
902 |
catalogManager.debug.message(5, "BASE CUR", base.toString());
|
|
903 |
}
|
|
904 |
catalogManager.debug.message(4, "BASE STR", value);
|
|
905 |
|
|
906 |
try {
|
|
907 |
value = fixSlashes(value);
|
|
908 |
newbase = new URL(base, value);
|
|
909 |
} catch (MalformedURLException e) {
|
|
910 |
try {
|
|
911 |
newbase = new URL("file:" + value);
|
|
912 |
} catch (MalformedURLException e2) {
|
|
913 |
catalogManager.debug.message(1, "Malformed URL on base", value);
|
|
914 |
newbase = null;
|
|
915 |
}
|
|
916 |
}
|
|
917 |
|
|
918 |
if (newbase != null) {
|
|
919 |
base = newbase;
|
|
920 |
}
|
|
921 |
|
|
922 |
catalogManager.debug.message(5, "BASE NEW", base.toString());
|
|
923 |
} else if (type == CATALOG) {
|
|
924 |
String fsi = makeAbsolute(entry.getEntryArg(0));
|
|
925 |
|
|
926 |
catalogManager.debug.message(4, "CATALOG", fsi);
|
|
927 |
|
|
928 |
localCatalogFiles.addElement(fsi);
|
|
929 |
} else if (type == PUBLIC) {
|
|
930 |
String publicid = PublicId.normalize(entry.getEntryArg(0));
|
|
931 |
String systemid = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
|
|
932 |
|
|
933 |
entry.setEntryArg(0, publicid);
|
|
934 |
entry.setEntryArg(1, systemid);
|
|
935 |
|
|
936 |
catalogManager.debug.message(4, "PUBLIC", publicid, systemid);
|
|
937 |
|
|
938 |
catalogEntries.addElement(entry);
|
|
939 |
} else if (type == SYSTEM) {
|
|
940 |
String systemid = normalizeURI(entry.getEntryArg(0));
|
|
941 |
String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
|
|
942 |
|
|
943 |
entry.setEntryArg(1, fsi);
|
|
944 |
|
|
945 |
catalogManager.debug.message(4, "SYSTEM", systemid, fsi);
|
|
946 |
|
|
947 |
catalogEntries.addElement(entry);
|
|
948 |
} else if (type == URI) {
|
|
949 |
String uri = normalizeURI(entry.getEntryArg(0));
|
|
950 |
String altURI = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
|
|
951 |
|
|
952 |
entry.setEntryArg(1, altURI);
|
|
953 |
|
|
954 |
catalogManager.debug.message(4, "URI", uri, altURI);
|
|
955 |
|
|
956 |
catalogEntries.addElement(entry);
|
|
957 |
} else if (type == DOCUMENT) {
|
|
958 |
String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(0)));
|
|
959 |
entry.setEntryArg(0, fsi);
|
|
960 |
|
|
961 |
catalogManager.debug.message(4, "DOCUMENT", fsi);
|
|
962 |
|
|
963 |
catalogEntries.addElement(entry);
|
|
964 |
} else if (type == OVERRIDE) {
|
|
965 |
catalogManager.debug.message(4, "OVERRIDE", entry.getEntryArg(0));
|
|
966 |
|
|
967 |
catalogEntries.addElement(entry);
|
|
968 |
} else if (type == SGMLDECL) {
|
|
969 |
// meaningless in XML
|
|
970 |
String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(0)));
|
|
971 |
entry.setEntryArg(0, fsi);
|
|
972 |
|
|
973 |
catalogManager.debug.message(4, "SGMLDECL", fsi);
|
|
974 |
|
|
975 |
catalogEntries.addElement(entry);
|
|
976 |
} else if (type == DELEGATE_PUBLIC) {
|
|
977 |
String ppi = PublicId.normalize(entry.getEntryArg(0));
|
|
978 |
String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
|
|
979 |
|
|
980 |
entry.setEntryArg(0, ppi);
|
|
981 |
entry.setEntryArg(1, fsi);
|
|
982 |
|
|
983 |
catalogManager.debug.message(4, "DELEGATE_PUBLIC", ppi, fsi);
|
|
984 |
|
|
985 |
addDelegate(entry);
|
|
986 |
} else if (type == DELEGATE_SYSTEM) {
|
|
987 |
String psi = normalizeURI(entry.getEntryArg(0));
|
|
988 |
String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
|
|
989 |
|
|
990 |
entry.setEntryArg(0, psi);
|
|
991 |
entry.setEntryArg(1, fsi);
|
|
992 |
|
|
993 |
catalogManager.debug.message(4, "DELEGATE_SYSTEM", psi, fsi);
|
|
994 |
|
|
995 |
addDelegate(entry);
|
|
996 |
} else if (type == DELEGATE_URI) {
|
|
997 |
String pui = normalizeURI(entry.getEntryArg(0));
|
|
998 |
String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
|
|
999 |
|
|
1000 |
entry.setEntryArg(0, pui);
|
|
1001 |
entry.setEntryArg(1, fsi);
|
|
1002 |
|
|
1003 |
catalogManager.debug.message(4, "DELEGATE_URI", pui, fsi);
|
|
1004 |
|
|
1005 |
addDelegate(entry);
|
|
1006 |
} else if (type == REWRITE_SYSTEM) {
|
|
1007 |
String psi = normalizeURI(entry.getEntryArg(0));
|
|
1008 |
String rpx = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
|
|
1009 |
|
|
1010 |
entry.setEntryArg(0, psi);
|
|
1011 |
entry.setEntryArg(1, rpx);
|
|
1012 |
|
|
1013 |
catalogManager.debug.message(4, "REWRITE_SYSTEM", psi, rpx);
|
|
1014 |
|
|
1015 |
catalogEntries.addElement(entry);
|
|
1016 |
} else if (type == REWRITE_URI) {
|
|
1017 |
String pui = normalizeURI(entry.getEntryArg(0));
|
|
1018 |
String upx = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
|
|
1019 |
|
|
1020 |
entry.setEntryArg(0, pui);
|
|
1021 |
entry.setEntryArg(1, upx);
|
|
1022 |
|
|
1023 |
catalogManager.debug.message(4, "REWRITE_URI", pui, upx);
|
|
1024 |
|
|
1025 |
catalogEntries.addElement(entry);
|
|
1026 |
} else if (type == SYSTEM_SUFFIX) {
|
|
1027 |
String pui = normalizeURI(entry.getEntryArg(0));
|
|
1028 |
String upx = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
|
|
1029 |
|
|
1030 |
entry.setEntryArg(0, pui);
|
|
1031 |
entry.setEntryArg(1, upx);
|
|
1032 |
|
|
1033 |
catalogManager.debug.message(4, "SYSTEM_SUFFIX", pui, upx);
|
|
1034 |
|
|
1035 |
catalogEntries.addElement(entry);
|
|
1036 |
} else if (type == URI_SUFFIX) {
|
|
1037 |
String pui = normalizeURI(entry.getEntryArg(0));
|
|
1038 |
String upx = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
|
|
1039 |
|
|
1040 |
entry.setEntryArg(0, pui);
|
|
1041 |
entry.setEntryArg(1, upx);
|
|
1042 |
|
|
1043 |
catalogManager.debug.message(4, "URI_SUFFIX", pui, upx);
|
|
1044 |
|
|
1045 |
catalogEntries.addElement(entry);
|
|
1046 |
} else if (type == DOCTYPE) {
|
|
1047 |
String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
|
|
1048 |
entry.setEntryArg(1, fsi);
|
|
1049 |
|
|
1050 |
catalogManager.debug.message(4, "DOCTYPE", entry.getEntryArg(0), fsi);
|
|
1051 |
|
|
1052 |
catalogEntries.addElement(entry);
|
|
1053 |
} else if (type == DTDDECL) {
|
|
1054 |
// meaningless in XML
|
|
1055 |
String fpi = PublicId.normalize(entry.getEntryArg(0));
|
|
1056 |
entry.setEntryArg(0, fpi);
|
|
1057 |
String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
|
|
1058 |
entry.setEntryArg(1, fsi);
|
|
1059 |
|
|
1060 |
catalogManager.debug.message(4, "DTDDECL", fpi, fsi);
|
|
1061 |
|
|
1062 |
catalogEntries.addElement(entry);
|
|
1063 |
} else if (type == ENTITY) {
|
|
1064 |
String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
|
|
1065 |
entry.setEntryArg(1, fsi);
|
|
1066 |
|
|
1067 |
catalogManager.debug.message(4, "ENTITY", entry.getEntryArg(0), fsi);
|
|
1068 |
|
|
1069 |
catalogEntries.addElement(entry);
|
|
1070 |
} else if (type == LINKTYPE) {
|
|
1071 |
// meaningless in XML
|
|
1072 |
String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
|
|
1073 |
entry.setEntryArg(1, fsi);
|
|
1074 |
|
|
1075 |
catalogManager.debug.message(4, "LINKTYPE", entry.getEntryArg(0), fsi);
|
|
1076 |
|
|
1077 |
catalogEntries.addElement(entry);
|
|
1078 |
} else if (type == NOTATION) {
|
|
1079 |
String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));
|
|
1080 |
entry.setEntryArg(1, fsi);
|
|
1081 |
|
|
1082 |
catalogManager.debug.message(4, "NOTATION", entry.getEntryArg(0), fsi);
|
|
1083 |
|
|
1084 |
catalogEntries.addElement(entry);
|
|
1085 |
} else {
|
|
1086 |
catalogEntries.addElement(entry);
|
|
1087 |
}
|
|
1088 |
}
|
|
1089 |
|
|
1090 |
/**
|
|
1091 |
* Handle unknown CatalogEntry types.
|
|
1092 |
*
|
|
1093 |
* <p>This method exists to allow subclasses to deal with unknown
|
|
1094 |
* entry types.</p>
|
|
1095 |
*/
|
|
1096 |
public void unknownEntry(Vector strings) {
|
|
1097 |
if (strings != null && strings.size() > 0) {
|
|
1098 |
String keyword = (String) strings.elementAt(0);
|
|
1099 |
catalogManager.debug.message(2, "Unrecognized token parsing catalog", keyword);
|
|
1100 |
}
|
|
1101 |
}
|
|
1102 |
|
|
1103 |
/**
|
|
1104 |
* Parse all subordinate catalogs.
|
|
1105 |
*
|
|
1106 |
* <p>This method recursively parses all of the subordinate catalogs.
|
|
1107 |
* If this method does not throw an exception, you can be confident that
|
|
1108 |
* no subsequent call to any resolve*() method will either, with two
|
|
1109 |
* possible exceptions:</p>
|
|
1110 |
*
|
|
1111 |
* <ol>
|
|
1112 |
* <li><p>Delegated catalogs are re-parsed each time they are needed
|
|
1113 |
* (because a variable list of them may be needed in each case,
|
|
1114 |
* depending on the length of the matching partial public identifier).</p>
|
|
1115 |
* <p>But they are parsed by this method, so as long as they don't
|
|
1116 |
* change or disappear while the program is running, they shouldn't
|
|
1117 |
* generate errors later if they don't generate errors now.</p>
|
|
1118 |
* <li><p>If you add new catalogs with <code>parseCatalog</code>, they
|
|
1119 |
* won't be loaded until they are needed or until you call
|
|
1120 |
* <code>parseAllCatalogs</code> again.</p>
|
|
1121 |
* </ol>
|
|
1122 |
*
|
|
1123 |
* <p>On the other hand, if you don't call this method, you may
|
|
1124 |
* successfully parse documents without having to load all possible
|
|
1125 |
* catalogs.</p>
|
|
1126 |
*
|
|
1127 |
* @throws MalformedURLException The filename (URL) for a
|
|
1128 |
* subordinate or delegated catalog is not a valid URL.
|
|
1129 |
* @throws IOException Error reading some subordinate or delegated
|
|
1130 |
* catalog file.
|
|
1131 |
*/
|
|
1132 |
public void parseAllCatalogs()
|
|
1133 |
throws MalformedURLException, IOException {
|
|
1134 |
|
|
1135 |
// Parse all the subordinate catalogs
|
|
1136 |
for (int catPos = 0; catPos < catalogs.size(); catPos++) {
|
|
1137 |
Catalog c = null;
|
|
1138 |
|
|
1139 |
try {
|
|
1140 |
c = (Catalog) catalogs.elementAt(catPos);
|
|
1141 |
} catch (ClassCastException e) {
|
|
1142 |
String catfile = (String) catalogs.elementAt(catPos);
|
|
1143 |
c = newCatalog();
|
|
1144 |
|
|
1145 |
c.parseCatalog(catfile);
|
|
1146 |
catalogs.setElementAt(c, catPos);
|
|
1147 |
c.parseAllCatalogs();
|
|
1148 |
}
|
|
1149 |
}
|
|
1150 |
|
|
1151 |
// Parse all the DELEGATE catalogs
|
|
1152 |
Enumeration en = catalogEntries.elements();
|
|
1153 |
while (en.hasMoreElements()) {
|
|
1154 |
CatalogEntry e = (CatalogEntry) en.nextElement();
|
|
1155 |
if (e.getEntryType() == DELEGATE_PUBLIC
|
|
1156 |
|| e.getEntryType() == DELEGATE_SYSTEM
|
|
1157 |
|| e.getEntryType() == DELEGATE_URI) {
|
|
1158 |
Catalog dcat = newCatalog();
|
|
1159 |
dcat.parseCatalog(e.getEntryArg(1));
|
|
1160 |
}
|
|
1161 |
}
|
|
1162 |
}
|
|
1163 |
|
|
1164 |
|
|
1165 |
/**
|
|
1166 |
* Return the applicable DOCTYPE system identifier.
|
|
1167 |
*
|
|
1168 |
* @param entityName The name of the entity (element) for which
|
|
1169 |
* a doctype is required.
|
|
1170 |
* @param publicId The nominal public identifier for the doctype
|
|
1171 |
* (as provided in the source document).
|
|
1172 |
* @param systemId The nominal system identifier for the doctype
|
|
1173 |
* (as provided in the source document).
|
|
1174 |
*
|
|
1175 |
* @return The system identifier to use for the doctype.
|
|
1176 |
*
|
|
1177 |
* @throws MalformedURLException The formal system identifier of a
|
|
1178 |
* subordinate catalog cannot be turned into a valid URL.
|
|
1179 |
* @throws IOException Error reading subordinate catalog file.
|
|
1180 |
*/
|
|
1181 |
public String resolveDoctype(String entityName,
|
|
1182 |
String publicId,
|
|
1183 |
String systemId)
|
|
1184 |
throws MalformedURLException, IOException {
|
|
1185 |
String resolved = null;
|
|
1186 |
|
|
1187 |
catalogManager.debug.message(3, "resolveDoctype("
|
|
1188 |
+entityName+","+publicId+","+systemId+")");
|
|
1189 |
|
|
1190 |
systemId = normalizeURI(systemId);
|
|
1191 |
|
|
1192 |
if (publicId != null && publicId.startsWith("urn:publicid:")) {
|
|
1193 |
publicId = PublicId.decodeURN(publicId);
|
|
1194 |
}
|
|
1195 |
|
|
1196 |
if (systemId != null && systemId.startsWith("urn:publicid:")) {
|
|
1197 |
systemId = PublicId.decodeURN(systemId);
|
|
1198 |
if (publicId != null && !publicId.equals(systemId)) {
|
|
1199 |
catalogManager.debug.message(1, "urn:publicid: system identifier differs from public identifier; using public identifier");
|
|
1200 |
systemId = null;
|
|
1201 |
} else {
|
|
1202 |
publicId = systemId;
|
|
1203 |
systemId = null;
|
|
1204 |
}
|
|
1205 |
}
|
|
1206 |
|
|
1207 |
if (systemId != null) {
|
|
1208 |
// If there's a SYSTEM entry in this catalog, use it
|
|
1209 |
resolved = resolveLocalSystem(systemId);
|
|
1210 |
if (resolved != null) {
|
|
1211 |
return resolved;
|
|
1212 |
}
|
|
1213 |
}
|
|
1214 |
|
|
1215 |
if (publicId != null) {
|
|
1216 |
// If there's a PUBLIC entry in this catalog, use it
|
|
1217 |
resolved = resolveLocalPublic(DOCTYPE,
|
|
1218 |
entityName,
|
|
1219 |
publicId,
|
|
1220 |
systemId);
|
|
1221 |
if (resolved != null) {
|
|
1222 |
return resolved;
|
|
1223 |
}
|
|
1224 |
}
|
|
1225 |
|
|
1226 |
// If there's a DOCTYPE entry in this catalog, use it
|
|
1227 |
boolean over = default_override;
|
|
1228 |
Enumeration en = catalogEntries.elements();
|
|
1229 |
while (en.hasMoreElements()) {
|
|
1230 |
CatalogEntry e = (CatalogEntry) en.nextElement();
|
|
1231 |
if (e.getEntryType() == OVERRIDE) {
|
|
1232 |
over = e.getEntryArg(0).equalsIgnoreCase("YES");
|
|
1233 |
continue;
|
|
1234 |
}
|
|
1235 |
|
|
1236 |
if (e.getEntryType() == DOCTYPE
|
|
1237 |
&& e.getEntryArg(0).equals(entityName)) {
|
|
1238 |
if (over || systemId == null) {
|
|
1239 |
return e.getEntryArg(1);
|
|
1240 |
}
|
|
1241 |
}
|
|
1242 |
}
|
|
1243 |
|
|
1244 |
// Otherwise, look in the subordinate catalogs
|
|
1245 |
return resolveSubordinateCatalogs(DOCTYPE,
|
|
1246 |
entityName,
|
|
1247 |
publicId,
|
|
1248 |
systemId);
|
|
1249 |
}
|
|
1250 |
|
|
1251 |
/**
|
|
1252 |
* Return the applicable DOCUMENT entry.
|
|
1253 |
*
|
|
1254 |
* @return The system identifier to use for the doctype.
|
|
1255 |
*
|
|
1256 |
* @throws MalformedURLException The formal system identifier of a
|
|
1257 |
* subordinate catalog cannot be turned into a valid URL.
|
|
1258 |
* @throws IOException Error reading subordinate catalog file.
|
|
1259 |
*/
|
|
1260 |
public String resolveDocument()
|
|
1261 |
throws MalformedURLException, IOException {
|
|
1262 |
// If there's a DOCUMENT entry, return it
|
|
1263 |
|
|
1264 |
catalogManager.debug.message(3, "resolveDocument");
|
|
1265 |
|
|
1266 |
Enumeration en = catalogEntries.elements();
|
|
1267 |
while (en.hasMoreElements()) {
|
|
1268 |
CatalogEntry e = (CatalogEntry) en.nextElement();
|
|
1269 |
if (e.getEntryType() == DOCUMENT) {
|
|
1270 |
return e.getEntryArg(0);
|
|
1271 |
}
|
|
1272 |
}
|
|
1273 |
|
|
1274 |
return resolveSubordinateCatalogs(DOCUMENT,
|
|
1275 |
null, null, null);
|
|
1276 |
}
|
|
1277 |
|
|
1278 |
/**
|
|
1279 |
* Return the applicable ENTITY system identifier.
|
|
1280 |
*
|
|
1281 |
* @param entityName The name of the entity for which
|
|
1282 |
* a system identifier is required.
|
|
1283 |
* @param publicId The nominal public identifier for the entity
|
|
1284 |
* (as provided in the source document).
|
|
1285 |
* @param systemId The nominal system identifier for the entity
|
|
1286 |
* (as provided in the source document).
|
|
1287 |
*
|
|
1288 |
* @return The system identifier to use for the entity.
|
|
1289 |
*
|
|
1290 |
* @throws MalformedURLException The formal system identifier of a
|
|
1291 |
* subordinate catalog cannot be turned into a valid URL.
|
|
1292 |
* @throws IOException Error reading subordinate catalog file.
|
|
1293 |
*/
|
|
1294 |
public String resolveEntity(String entityName,
|
|
1295 |
String publicId,
|
|
1296 |
String systemId)
|
|
1297 |
throws MalformedURLException, IOException {
|
|
1298 |
String resolved = null;
|
|
1299 |
|
|
1300 |
catalogManager.debug.message(3, "resolveEntity("
|
|
1301 |
+entityName+","+publicId+","+systemId+")");
|
|
1302 |
|
|
1303 |
systemId = normalizeURI(systemId);
|
|
1304 |
|
|
1305 |
if (publicId != null && publicId.startsWith("urn:publicid:")) {
|
|
1306 |
publicId = PublicId.decodeURN(publicId);
|
|
1307 |
}
|
|
1308 |
|
|
1309 |
if (systemId != null && systemId.startsWith("urn:publicid:")) {
|
|
1310 |
systemId = PublicId.decodeURN(systemId);
|
|
1311 |
if (publicId != null && !publicId.equals(systemId)) {
|
|
1312 |
catalogManager.debug.message(1, "urn:publicid: system identifier differs from public identifier; using public identifier");
|
|
1313 |
systemId = null;
|
|
1314 |
} else {
|
|
1315 |
publicId = systemId;
|
|
1316 |
systemId = null;
|
|
1317 |
}
|
|
1318 |
}
|
|
1319 |
|
|
1320 |
if (systemId != null) {
|
|
1321 |
// If there's a SYSTEM entry in this catalog, use it
|
|
1322 |
resolved = resolveLocalSystem(systemId);
|
|
1323 |
if (resolved != null) {
|
|
1324 |
return resolved;
|
|
1325 |
}
|
|
1326 |
}
|
|
1327 |
|
|
1328 |
if (publicId != null) {
|
|
1329 |
// If there's a PUBLIC entry in this catalog, use it
|
|
1330 |
resolved = resolveLocalPublic(ENTITY,
|
|
1331 |
entityName,
|
|
1332 |
publicId,
|
|
1333 |
systemId);
|
|
1334 |
if (resolved != null) {
|
|
1335 |
return resolved;
|
|
1336 |
}
|
|
1337 |
}
|
|
1338 |
|
|
1339 |
// If there's a ENTITY entry in this catalog, use it
|
|
1340 |
boolean over = default_override;
|
|
1341 |
Enumeration en = catalogEntries.elements();
|
|
1342 |
while (en.hasMoreElements()) {
|
|
1343 |
CatalogEntry e = (CatalogEntry) en.nextElement();
|
|
1344 |
if (e.getEntryType() == OVERRIDE) {
|
|
1345 |
over = e.getEntryArg(0).equalsIgnoreCase("YES");
|
|
1346 |
continue;
|
|
1347 |
}
|
|
1348 |
|
|
1349 |
if (e.getEntryType() == ENTITY
|
|
1350 |
&& e.getEntryArg(0).equals(entityName)) {
|
|
1351 |
if (over || systemId == null) {
|
|
1352 |
return e.getEntryArg(1);
|
|
1353 |
}
|
|
1354 |
}
|
|
1355 |
}
|
|
1356 |
|
|
1357 |
// Otherwise, look in the subordinate catalogs
|
|
1358 |
return resolveSubordinateCatalogs(ENTITY,
|
|
1359 |
entityName,
|
|
1360 |
publicId,
|
|
1361 |
systemId);
|
|
1362 |
}
|
|
1363 |
|
|
1364 |
/**
|
|
1365 |
* Return the applicable NOTATION system identifier.
|
|
1366 |
*
|
|
1367 |
* @param notationName The name of the notation for which
|
|
1368 |
* a doctype is required.
|
|
1369 |
* @param publicId The nominal public identifier for the notation
|
|
1370 |
* (as provided in the source document).
|
|
1371 |
* @param systemId The nominal system identifier for the notation
|
|
1372 |
* (as provided in the source document).
|
|
1373 |
*
|
|
1374 |
* @return The system identifier to use for the notation.
|
|
1375 |
*
|
|
1376 |
* @throws MalformedURLException The formal system identifier of a
|
|
1377 |
* subordinate catalog cannot be turned into a valid URL.
|
|
1378 |
* @throws IOException Error reading subordinate catalog file.
|
|
1379 |
*/
|
|
1380 |
public String resolveNotation(String notationName,
|
|
1381 |
String publicId,
|
|
1382 |
String systemId)
|
|
1383 |
throws MalformedURLException, IOException {
|
|
1384 |
String resolved = null;
|
|
1385 |
|
|
1386 |
catalogManager.debug.message(3, "resolveNotation("
|
|
1387 |
+notationName+","+publicId+","+systemId+")");
|
|
1388 |
|
|
1389 |
systemId = normalizeURI(systemId);
|
|
1390 |
|
|
1391 |
if (publicId != null && publicId.startsWith("urn:publicid:")) {
|
|
1392 |
publicId = PublicId.decodeURN(publicId);
|
|
1393 |
}
|
|
1394 |
|
|
1395 |
if (systemId != null && systemId.startsWith("urn:publicid:")) {
|
|
1396 |
systemId = PublicId.decodeURN(systemId);
|
|
1397 |
if (publicId != null && !publicId.equals(systemId)) {
|
|
1398 |
catalogManager.debug.message(1, "urn:publicid: system identifier differs from public identifier; using public identifier");
|
|
1399 |
systemId = null;
|
|
1400 |
} else {
|
|
1401 |
publicId = systemId;
|
|
1402 |
systemId = null;
|
|
1403 |
}
|
|
1404 |
}
|
|
1405 |
|
|
1406 |
if (systemId != null) {
|
|
1407 |
// If there's a SYSTEM entry in this catalog, use it
|
|
1408 |
resolved = resolveLocalSystem(systemId);
|
|
1409 |
if (resolved != null) {
|
|
1410 |
return resolved;
|
|
1411 |
}
|
|
1412 |
}
|
|
1413 |
|
|
1414 |
if (publicId != null) {
|
|
1415 |
// If there's a PUBLIC entry in this catalog, use it
|
|
1416 |
resolved = resolveLocalPublic(NOTATION,
|
|
1417 |
notationName,
|
|
1418 |
publicId,
|
|
1419 |
systemId);
|
|
1420 |
if (resolved != null) {
|
|
1421 |
return resolved;
|
|
1422 |
}
|
|
1423 |
}
|
|
1424 |
|
|
1425 |
// If there's a NOTATION entry in this catalog, use it
|
|
1426 |
boolean over = default_override;
|
|
1427 |
Enumeration en = catalogEntries.elements();
|
|
1428 |
while (en.hasMoreElements()) {
|
|
1429 |
CatalogEntry e = (CatalogEntry) en.nextElement();
|
|
1430 |
if (e.getEntryType() == OVERRIDE) {
|
|
1431 |
over = e.getEntryArg(0).equalsIgnoreCase("YES");
|
|
1432 |
continue;
|
|
1433 |
}
|
|
1434 |
|
|
1435 |
if (e.getEntryType() == NOTATION
|
|
1436 |
&& e.getEntryArg(0).equals(notationName)) {
|
|
1437 |
if (over || systemId == null) {
|
|
1438 |
return e.getEntryArg(1);
|
|
1439 |
}
|
|
1440 |
}
|
|
1441 |
}
|
|
1442 |
|
|
1443 |
// Otherwise, look in the subordinate catalogs
|
|
1444 |
return resolveSubordinateCatalogs(NOTATION,
|
|
1445 |
notationName,
|
|
1446 |
publicId,
|
|
1447 |
systemId);
|
|
1448 |
}
|
|
1449 |
|
|
1450 |
/**
|
|
1451 |
* Return the applicable PUBLIC or SYSTEM identifier.
|
|
1452 |
*
|
|
1453 |
* <p>This method searches the Catalog and returns the system
|
|
1454 |
* identifier specified for the given system or
|
|
1455 |
* public identifiers. If
|
|
1456 |
* no appropriate PUBLIC or SYSTEM entry is found in the Catalog,
|
|
1457 |
* null is returned.</p>
|
|
1458 |
*
|
|
1459 |
* @param publicId The public identifier to locate in the catalog.
|
|
1460 |
* Public identifiers are normalized before comparison.
|
|
1461 |
* @param systemId The nominal system identifier for the entity
|
|
1462 |
* in question (as provided in the source document).
|
|
1463 |
*
|
|
1464 |
* @throws MalformedURLException The formal system identifier of a
|
|
1465 |
* subordinate catalog cannot be turned into a valid URL.
|
|
1466 |
* @throws IOException Error reading subordinate catalog file.
|
|
1467 |
*
|
|
1468 |
* @return The system identifier to use.
|
|
1469 |
* Note that the nominal system identifier is not returned if a
|
|
1470 |
* match is not found in the catalog, instead null is returned
|
|
1471 |
* to indicate that no match was found.
|
|
1472 |
*/
|
|
1473 |
public String resolvePublic(String publicId, String systemId)
|
|
1474 |
throws MalformedURLException, IOException {
|
|
1475 |
|
|
1476 |
catalogManager.debug.message(3, "resolvePublic("+publicId+","+systemId+")");
|
|
1477 |
|
|
1478 |
systemId = normalizeURI(systemId);
|
|
1479 |
|
|
1480 |
if (publicId != null && publicId.startsWith("urn:publicid:")) {
|
|
1481 |
publicId = PublicId.decodeURN(publicId);
|
|
1482 |
}
|
|
1483 |
|
|
1484 |
if (systemId != null && systemId.startsWith("urn:publicid:")) {
|
|
1485 |
systemId = PublicId.decodeURN(systemId);
|
|
1486 |
if (publicId != null && !publicId.equals(systemId)) {
|
|
1487 |
catalogManager.debug.message(1, "urn:publicid: system identifier differs from public identifier; using public identifier");
|
|
1488 |
systemId = null;
|
|
1489 |
} else {
|
|
1490 |
publicId = systemId;
|
|
1491 |
systemId = null;
|
|
1492 |
}
|
|
1493 |
}
|
|
1494 |
|
|
1495 |
// If there's a SYSTEM entry in this catalog, use it
|
|
1496 |
if (systemId != null) {
|
|
1497 |
String resolved = resolveLocalSystem(systemId);
|
|
1498 |
if (resolved != null) {
|
|
1499 |
return resolved;
|
|
1500 |
}
|
|
1501 |
}
|
|
1502 |
|
|
1503 |
// If there's a PUBLIC entry in this catalog, use it
|
|
1504 |
String resolved = resolveLocalPublic(PUBLIC,
|
|
1505 |
null,
|
|
1506 |
publicId,
|
|
1507 |
systemId);
|
|
1508 |
if (resolved != null) {
|
|
1509 |
return resolved;
|
|
1510 |
}
|
|
1511 |
|
|
1512 |
// Otherwise, look in the subordinate catalogs
|
|
1513 |
return resolveSubordinateCatalogs(PUBLIC,
|
|
1514 |
null,
|
|
1515 |
publicId,
|
|
1516 |
systemId);
|
|
1517 |
}
|
|
1518 |
|
|
1519 |
/**
|
|
1520 |
* Return the applicable PUBLIC or SYSTEM identifier.
|
|
1521 |
*
|
|
1522 |
* <p>This method searches the Catalog and returns the system
|
|
1523 |
* identifier specified for the given system or public identifiers.
|
|
1524 |
* If no appropriate PUBLIC or SYSTEM entry is found in the Catalog,
|
|
1525 |
* delegated Catalogs are interrogated.</p>
|
|
1526 |
*
|
|
1527 |
* <p>There are four possible cases:</p>
|
|
1528 |
*
|
|
1529 |
* <ul>
|
|
1530 |
* <li>If the system identifier provided matches a SYSTEM entry
|
|
1531 |
* in the current catalog, the SYSTEM entry is returned.
|
|
1532 |
* <li>If the system identifier is not null, the PUBLIC entries
|
|
1533 |
* that were encountered when OVERRIDE YES was in effect are
|
|
1534 |
* interrogated and the first matching entry is returned.</li>
|
|
1535 |
* <li>If the system identifier is null, then all of the PUBLIC
|
|
1536 |
* entries are interrogated and the first matching entry
|
|
1537 |
* is returned. This may not be the same as the preceding case, if
|
|
1538 |
* some PUBLIC entries are encountered when OVERRIDE NO is in effect. In
|
|
1539 |
* XML, the only place where a public identifier may occur without
|
|
1540 |
* a system identifier is in a notation declaration.</li>
|
|
1541 |
* <li>Finally, if the public identifier matches one of the partial
|
|
1542 |
* public identifiers specified in a DELEGATE* entry in
|
|
1543 |
* the Catalog, the delegated catalog is interrogated. The first
|
|
1544 |
* time that the delegated catalog is required, it will be
|
|
1545 |
* retrieved and parsed. It is subsequently cached.
|
|
1546 |
* </li>
|
|
1547 |
* </ul>
|
|
1548 |
*
|
|
1549 |
* @param entityType The CatalogEntry type for which this query is
|
|
1550 |
* being conducted. This is necessary in order to do the approprate
|
|
1551 |
* query on a delegated catalog.
|
|
1552 |
* @param entityName The name of the entity being searched for, if
|
|
1553 |
* appropriate.
|
|
1554 |
* @param publicId The public identifier of the entity in question.
|
|
1555 |
* @param systemId The nominal system identifier for the entity
|
|
1556 |
* in question (as provided in the source document).
|
|
1557 |
*
|
|
1558 |
* @throws MalformedURLException The formal system identifier of a
|
|
1559 |
* delegated catalog cannot be turned into a valid URL.
|
|
1560 |
* @throws IOException Error reading delegated catalog file.
|
|
1561 |
*
|
|
1562 |
* @return The system identifier to use.
|
|
1563 |
* Note that the nominal system identifier is not returned if a
|
|
1564 |
* match is not found in the catalog, instead null is returned
|
|
1565 |
* to indicate that no match was found.
|
|
1566 |
*/
|
|
1567 |
protected synchronized String resolveLocalPublic(int entityType,
|
|
1568 |
String entityName,
|
|
1569 |
String publicId,
|
|
1570 |
String systemId)
|
|
1571 |
throws MalformedURLException, IOException {
|
|
1572 |
|
|
1573 |
// Always normalize the public identifier before attempting a match
|
|
1574 |
publicId = PublicId.normalize(publicId);
|
|
1575 |
|
|
1576 |
// If there's a SYSTEM entry in this catalog, use it
|
|
1577 |
if (systemId != null) {
|
|
1578 |
String resolved = resolveLocalSystem(systemId);
|
|
1579 |
if (resolved != null) {
|
|
1580 |
return resolved;
|
|
1581 |
}
|
|
1582 |
}
|
|
1583 |
|
|
1584 |
// If there's a PUBLIC entry in this catalog, use it
|
|
1585 |
boolean over = default_override;
|
|
1586 |
Enumeration en = catalogEntries.elements();
|
|
1587 |
while (en.hasMoreElements()) {
|
|
1588 |
CatalogEntry e = (CatalogEntry) en.nextElement();
|
|
1589 |
if (e.getEntryType() == OVERRIDE) {
|
|
1590 |
over = e.getEntryArg(0).equalsIgnoreCase("YES");
|
|
1591 |
continue;
|
|
1592 |
}
|
|
1593 |
|
|
1594 |
if (e.getEntryType() == PUBLIC
|
|
1595 |
&& e.getEntryArg(0).equals(publicId)) {
|
|
1596 |
if (over || systemId == null) {
|
|
1597 |
return e.getEntryArg(1);
|
|
1598 |
}
|
|
1599 |
}
|
|
1600 |
}
|
|
1601 |
|
|
1602 |
// If there's a DELEGATE_PUBLIC entry in this catalog, use it
|
|
1603 |
over = default_override;
|
|
1604 |
en = catalogEntries.elements();
|
|
1605 |
Vector delCats = new Vector();
|
|
1606 |
while (en.hasMoreElements()) {
|
|
1607 |
CatalogEntry e = (CatalogEntry) en.nextElement();
|
|
1608 |
if (e.getEntryType() == OVERRIDE) {
|
|
1609 |
over = e.getEntryArg(0).equalsIgnoreCase("YES");
|
|
1610 |
continue;
|
|
1611 |
}
|
|
1612 |
|
|
1613 |
if (e.getEntryType() == DELEGATE_PUBLIC
|
|
1614 |
&& (over || systemId == null)) {
|
|
1615 |
String p = (String) e.getEntryArg(0);
|
|
1616 |
if (p.length() <= publicId.length()
|
|
1617 |
&& p.equals(publicId.substring(0, p.length()))) {
|
|
1618 |
// delegate this match to the other catalog
|
|
1619 |
|
|
1620 |
delCats.addElement(e.getEntryArg(1));
|
|
1621 |
}
|
|
1622 |
}
|
|
1623 |
}
|
|
1624 |
|
|
1625 |
if (delCats.size() > 0) {
|
|
1626 |
Enumeration enCats = delCats.elements();
|
|
1627 |
|
|
1628 |
if (catalogManager.debug.getDebug() > 1) {
|
|
1629 |
catalogManager.debug.message(2, "Switching to delegated catalog(s):");
|
|
1630 |
while (enCats.hasMoreElements()) {
|
|
1631 |
String delegatedCatalog = (String) enCats.nextElement();
|
|
1632 |
catalogManager.debug.message(2, "\t" + delegatedCatalog);
|
|
1633 |
}
|
|
1634 |
}
|
|
1635 |
|
|
1636 |
Catalog dcat = newCatalog();
|
|
1637 |
|
|
1638 |
enCats = delCats.elements();
|
|
1639 |
while (enCats.hasMoreElements()) {
|
|
1640 |
String delegatedCatalog = (String) enCats.nextElement();
|
|
1641 |
dcat.parseCatalog(delegatedCatalog);
|
|
1642 |
}
|
|
1643 |
|
|
1644 |
return dcat.resolvePublic(publicId, null);
|
|
1645 |
}
|
|
1646 |
|
|
1647 |
// Nada!
|
|
1648 |
return null;
|
|
1649 |
}
|
|
1650 |
|
|
1651 |
/**
|
|
1652 |
* Return the applicable SYSTEM system identifier.
|
|
1653 |
*
|
|
1654 |
* <p>If a SYSTEM entry exists in the Catalog
|
|
1655 |
* for the system ID specified, return the mapped value.</p>
|
|
1656 |
*
|
|
1657 |
* <p>On Windows-based operating systems, the comparison between
|
|
1658 |
* the system identifier provided and the SYSTEM entries in the
|
|
1659 |
* Catalog is case-insensitive.</p>
|
|
1660 |
*
|
|
1661 |
* @param systemId The system ID to locate in the catalog.
|
|
1662 |
*
|
|
1663 |
* @return The resolved system identifier.
|
|
1664 |
*
|
|
1665 |
* @throws MalformedURLException The formal system identifier of a
|
|
1666 |
* subordinate catalog cannot be turned into a valid URL.
|
|
1667 |
* @throws IOException Error reading subordinate catalog file.
|
|
1668 |
*/
|
|
1669 |
public String resolveSystem(String systemId)
|
|
1670 |
throws MalformedURLException, IOException {
|
|
1671 |
|
|
1672 |
catalogManager.debug.message(3, "resolveSystem("+systemId+")");
|
|
1673 |
|
|
1674 |
systemId = normalizeURI(systemId);
|
|
1675 |
|
|
1676 |
if (systemId != null && systemId.startsWith("urn:publicid:")) {
|
|
1677 |
systemId = PublicId.decodeURN(systemId);
|
|
1678 |
return resolvePublic(systemId, null);
|
|
1679 |
}
|
|
1680 |
|
|
1681 |
// If there's a SYSTEM entry in this catalog, use it
|
|
1682 |
if (systemId != null) {
|
|
1683 |
String resolved = resolveLocalSystem(systemId);
|
|
1684 |
if (resolved != null) {
|
|
1685 |
return resolved;
|
|
1686 |
}
|
|
1687 |
}
|
|
1688 |
|
|
1689 |
// Otherwise, look in the subordinate catalogs
|
|
1690 |
return resolveSubordinateCatalogs(SYSTEM,
|
|
1691 |
null,
|
|
1692 |
null,
|
|
1693 |
systemId);
|
|
1694 |
}
|
|
1695 |
|
|
1696 |
/**
|
|
1697 |
* Return the applicable SYSTEM system identifier in this
|
|
1698 |
* catalog.
|
|
1699 |
*
|
|
1700 |
* <p>If a SYSTEM entry exists in the catalog file
|
|
1701 |
* for the system ID specified, return the mapped value.</p>
|
|
1702 |
*
|
|
1703 |
* @param systemId The system ID to locate in the catalog
|
|
1704 |
*
|
|
1705 |
* @return The mapped system identifier or null
|
|
1706 |
*/
|
|
1707 |
protected String resolveLocalSystem(String systemId)
|
|
1708 |
throws MalformedURLException, IOException {
|
|
1709 |
|
16953
|
1710 |
String osname = SecuritySupport.getSystemProperty("os.name");
|
12005
|
1711 |
boolean windows = (osname.indexOf("Windows") >= 0);
|
|
1712 |
Enumeration en = catalogEntries.elements();
|
|
1713 |
while (en.hasMoreElements()) {
|
|
1714 |
CatalogEntry e = (CatalogEntry) en.nextElement();
|
|
1715 |
if (e.getEntryType() == SYSTEM
|
|
1716 |
&& (e.getEntryArg(0).equals(systemId)
|
|
1717 |
|| (windows
|
|
1718 |
&& e.getEntryArg(0).equalsIgnoreCase(systemId)))) {
|
|
1719 |
return e.getEntryArg(1);
|
|
1720 |
}
|
|
1721 |
}
|
|
1722 |
|
|
1723 |
// If there's a REWRITE_SYSTEM entry in this catalog, use it
|
|
1724 |
en = catalogEntries.elements();
|
|
1725 |
String startString = null;
|
|
1726 |
String prefix = null;
|
|
1727 |
while (en.hasMoreElements()) {
|
|
1728 |
CatalogEntry e = (CatalogEntry) en.nextElement();
|
|
1729 |
|
|
1730 |
if (e.getEntryType() == REWRITE_SYSTEM) {
|
|
1731 |
String p = (String) e.getEntryArg(0);
|
|
1732 |
if (p.length() <= systemId.length()
|
|
1733 |
&& p.equals(systemId.substring(0, p.length()))) {
|
|
1734 |
// Is this the longest prefix?
|
|
1735 |
if (startString == null
|
|
1736 |
|| p.length() > startString.length()) {
|
|
1737 |
startString = p;
|
|
1738 |
prefix = e.getEntryArg(1);
|
|
1739 |
}
|
|
1740 |
}
|
|
1741 |
}
|
|
1742 |
}
|
|
1743 |
|
|
1744 |
if (prefix != null) {
|
|
1745 |
// return the systemId with the new prefix
|
|
1746 |
return prefix + systemId.substring(startString.length());
|
|
1747 |
}
|
|
1748 |
|
|
1749 |
// If there's a SYSTEM_SUFFIX entry in this catalog, use it
|
|
1750 |
en = catalogEntries.elements();
|
|
1751 |
String suffixString = null;
|
|
1752 |
String suffixURI = null;
|
|
1753 |
while (en.hasMoreElements()) {
|
|
1754 |
CatalogEntry e = (CatalogEntry) en.nextElement();
|
|
1755 |
|
|
1756 |
if (e.getEntryType() == SYSTEM_SUFFIX) {
|
|
1757 |
String p = (String) e.getEntryArg(0);
|
|
1758 |
if (p.length() <= systemId.length()
|
|
1759 |
&& systemId.endsWith(p)) {
|
|
1760 |
// Is this the longest prefix?
|
|
1761 |
if (suffixString == null
|
|
1762 |
|| p.length() > suffixString.length()) {
|
|
1763 |
suffixString = p;
|
|
1764 |
suffixURI = e.getEntryArg(1);
|
|
1765 |
}
|
|
1766 |
}
|
|
1767 |
}
|
|
1768 |
}
|
|
1769 |
|
|
1770 |
if (suffixURI != null) {
|
|
1771 |
// return the systemId for the suffix
|
|
1772 |
return suffixURI;
|
|
1773 |
}
|
|
1774 |
|
|
1775 |
// If there's a DELEGATE_SYSTEM entry in this catalog, use it
|
|
1776 |
en = catalogEntries.elements();
|
|
1777 |
Vector delCats = new Vector();
|
|
1778 |
while (en.hasMoreElements()) {
|
|
1779 |
CatalogEntry e = (CatalogEntry) en.nextElement();
|
|
1780 |
|
|
1781 |
if (e.getEntryType() == DELEGATE_SYSTEM) {
|
|
1782 |
String p = (String) e.getEntryArg(0);
|
|
1783 |
if (p.length() <= systemId.length()
|
|
1784 |
&& p.equals(systemId.substring(0, p.length()))) {
|
|
1785 |
// delegate this match to the other catalog
|
|
1786 |
|
|
1787 |
delCats.addElement(e.getEntryArg(1));
|
|
1788 |
}
|
|
1789 |
}
|
|
1790 |
}
|
|
1791 |
|
|
1792 |
if (delCats.size() > 0) {
|
|
1793 |
Enumeration enCats = delCats.elements();
|
|
1794 |
|
|
1795 |
if (catalogManager.debug.getDebug() > 1) {
|
|
1796 |
catalogManager.debug.message(2, "Switching to delegated catalog(s):");
|
|
1797 |
while (enCats.hasMoreElements()) {
|
|
1798 |
String delegatedCatalog = (String) enCats.nextElement();
|
|
1799 |
catalogManager.debug.message(2, "\t" + delegatedCatalog);
|
|
1800 |
}
|
|
1801 |
}
|
|
1802 |
|
|
1803 |
Catalog dcat = newCatalog();
|
|
1804 |
|
|
1805 |
enCats = delCats.elements();
|
|
1806 |
while (enCats.hasMoreElements()) {
|
|
1807 |
String delegatedCatalog = (String) enCats.nextElement();
|
|
1808 |
dcat.parseCatalog(delegatedCatalog);
|
|
1809 |
}
|
|
1810 |
|
|
1811 |
return dcat.resolveSystem(systemId);
|
|
1812 |
}
|
|
1813 |
|
|
1814 |
return null;
|
|
1815 |
}
|
|
1816 |
|
|
1817 |
/**
|
|
1818 |
* Return the applicable URI.
|
|
1819 |
*
|
|
1820 |
* <p>If a URI entry exists in the Catalog
|
|
1821 |
* for the URI specified, return the mapped value.</p>
|
|
1822 |
*
|
|
1823 |
* <p>URI comparison is case sensitive.</p>
|
|
1824 |
*
|
|
1825 |
* @param uri The URI to locate in the catalog.
|
|
1826 |
*
|
|
1827 |
* @return The resolved URI.
|
|
1828 |
*
|
|
1829 |
* @throws MalformedURLException The system identifier of a
|
|
1830 |
* subordinate catalog cannot be turned into a valid URL.
|
|
1831 |
* @throws IOException Error reading subordinate catalog file.
|
|
1832 |
*/
|
|
1833 |
public String resolveURI(String uri)
|
|
1834 |
throws MalformedURLException, IOException {
|
|
1835 |
|
|
1836 |
catalogManager.debug.message(3, "resolveURI("+uri+")");
|
|
1837 |
|
|
1838 |
uri = normalizeURI(uri);
|
|
1839 |
|
|
1840 |
if (uri != null && uri.startsWith("urn:publicid:")) {
|
|
1841 |
uri = PublicId.decodeURN(uri);
|
|
1842 |
return resolvePublic(uri, null);
|
|
1843 |
}
|
|
1844 |
|
|
1845 |
// If there's a URI entry in this catalog, use it
|
|
1846 |
if (uri != null) {
|
|
1847 |
String resolved = resolveLocalURI(uri);
|
|
1848 |
if (resolved != null) {
|
|
1849 |
return resolved;
|
|
1850 |
}
|
|
1851 |
}
|
|
1852 |
|
|
1853 |
// Otherwise, look in the subordinate catalogs
|
|
1854 |
return resolveSubordinateCatalogs(URI,
|
|
1855 |
null,
|
|
1856 |
null,
|
|
1857 |
uri);
|
|
1858 |
}
|
|
1859 |
|
|
1860 |
/**
|
|
1861 |
* Return the applicable URI in this catalog.
|
|
1862 |
*
|
|
1863 |
* <p>If a URI entry exists in the catalog file
|
|
1864 |
* for the URI specified, return the mapped value.</p>
|
|
1865 |
*
|
|
1866 |
* @param uri The URI to locate in the catalog
|
|
1867 |
*
|
|
1868 |
* @return The mapped URI or null
|
|
1869 |
*/
|
|
1870 |
protected String resolveLocalURI(String uri)
|
|
1871 |
throws MalformedURLException, IOException {
|
|
1872 |
Enumeration en = catalogEntries.elements();
|
|
1873 |
while (en.hasMoreElements()) {
|
|
1874 |
CatalogEntry e = (CatalogEntry) en.nextElement();
|
|
1875 |
if (e.getEntryType() == URI
|
|
1876 |
&& (e.getEntryArg(0).equals(uri))) {
|
|
1877 |
return e.getEntryArg(1);
|
|
1878 |
}
|
|
1879 |
}
|
|
1880 |
|
|
1881 |
// If there's a REWRITE_URI entry in this catalog, use it
|
|
1882 |
en = catalogEntries.elements();
|
|
1883 |
String startString = null;
|
|
1884 |
String prefix = null;
|
|
1885 |
while (en.hasMoreElements()) {
|
|
1886 |
CatalogEntry e = (CatalogEntry) en.nextElement();
|
|
1887 |
|
|
1888 |
if (e.getEntryType() == REWRITE_URI) {
|
|
1889 |
String p = (String) e.getEntryArg(0);
|
|
1890 |
if (p.length() <= uri.length()
|
|
1891 |
&& p.equals(uri.substring(0, p.length()))) {
|
|
1892 |
// Is this the longest prefix?
|
|
1893 |
if (startString == null
|
|
1894 |
|| p.length() > startString.length()) {
|
|
1895 |
startString = p;
|
|
1896 |
prefix = e.getEntryArg(1);
|
|
1897 |
}
|
|
1898 |
}
|
|
1899 |
}
|
|
1900 |
}
|
|
1901 |
|
|
1902 |
if (prefix != null) {
|
|
1903 |
// return the uri with the new prefix
|
|
1904 |
return prefix + uri.substring(startString.length());
|
|
1905 |
}
|
|
1906 |
|
|
1907 |
// If there's a URI_SUFFIX entry in this catalog, use it
|
|
1908 |
en = catalogEntries.elements();
|
|
1909 |
String suffixString = null;
|
|
1910 |
String suffixURI = null;
|
|
1911 |
while (en.hasMoreElements()) {
|
|
1912 |
CatalogEntry e = (CatalogEntry) en.nextElement();
|
|
1913 |
|
|
1914 |
if (e.getEntryType() == URI_SUFFIX) {
|
|
1915 |
String p = (String) e.getEntryArg(0);
|
|
1916 |
if (p.length() <= uri.length()
|
|
1917 |
&& uri.endsWith(p)) {
|
|
1918 |
// Is this the longest prefix?
|
|
1919 |
if (suffixString == null
|
|
1920 |
|| p.length() > suffixString.length()) {
|
|
1921 |
suffixString = p;
|
|
1922 |
suffixURI = e.getEntryArg(1);
|
|
1923 |
}
|
|
1924 |
}
|
|
1925 |
}
|
|
1926 |
}
|
|
1927 |
|
|
1928 |
if (suffixURI != null) {
|
|
1929 |
// return the uri for the suffix
|
|
1930 |
return suffixURI;
|
|
1931 |
}
|
|
1932 |
|
|
1933 |
// If there's a DELEGATE_URI entry in this catalog, use it
|
|
1934 |
en = catalogEntries.elements();
|
|
1935 |
Vector delCats = new Vector();
|
|
1936 |
while (en.hasMoreElements()) {
|
|
1937 |
CatalogEntry e = (CatalogEntry) en.nextElement();
|
|
1938 |
|
|
1939 |
if (e.getEntryType() == DELEGATE_URI) {
|
|
1940 |
String p = (String) e.getEntryArg(0);
|
|
1941 |
if (p.length() <= uri.length()
|
|
1942 |
&& p.equals(uri.substring(0, p.length()))) {
|
|
1943 |
// delegate this match to the other catalog
|
|
1944 |
|
|
1945 |
delCats.addElement(e.getEntryArg(1));
|
|
1946 |
}
|
|
1947 |
}
|
|
1948 |
}
|
|
1949 |
|
|
1950 |
if (delCats.size() > 0) {
|
|
1951 |
Enumeration enCats = delCats.elements();
|
|
1952 |
|
|
1953 |
if (catalogManager.debug.getDebug() > 1) {
|
|
1954 |
catalogManager.debug.message(2, "Switching to delegated catalog(s):");
|
|
1955 |
while (enCats.hasMoreElements()) {
|
|
1956 |
String delegatedCatalog = (String) enCats.nextElement();
|
|
1957 |
catalogManager.debug.message(2, "\t" + delegatedCatalog);
|
|
1958 |
}
|
|
1959 |
}
|
|
1960 |
|
|
1961 |
Catalog dcat = newCatalog();
|
|
1962 |
|
|
1963 |
enCats = delCats.elements();
|
|
1964 |
while (enCats.hasMoreElements()) {
|
|
1965 |
String delegatedCatalog = (String) enCats.nextElement();
|
|
1966 |
dcat.parseCatalog(delegatedCatalog);
|
|
1967 |
}
|
|
1968 |
|
|
1969 |
return dcat.resolveURI(uri);
|
|
1970 |
}
|
|
1971 |
|
|
1972 |
return null;
|
|
1973 |
}
|
|
1974 |
|
|
1975 |
/**
|
|
1976 |
* Search the subordinate catalogs, in order, looking for a match.
|
|
1977 |
*
|
|
1978 |
* <p>This method searches the Catalog and returns the system
|
|
1979 |
* identifier specified for the given entity type with the given
|
|
1980 |
* name, public, and system identifiers. In some contexts, these
|
|
1981 |
* may be null.</p>
|
|
1982 |
*
|
|
1983 |
* @param entityType The CatalogEntry type for which this query is
|
|
1984 |
* being conducted. This is necessary in order to do the approprate
|
|
1985 |
* query on a subordinate catalog.
|
|
1986 |
* @param entityName The name of the entity being searched for, if
|
|
1987 |
* appropriate.
|
|
1988 |
* @param publicId The public identifier of the entity in question
|
|
1989 |
* (as provided in the source document).
|
|
1990 |
* @param systemId The nominal system identifier for the entity
|
|
1991 |
* in question (as provided in the source document). This parameter is
|
|
1992 |
* overloaded for the URI entry type.
|
|
1993 |
*
|
|
1994 |
* @throws MalformedURLException The formal system identifier of a
|
|
1995 |
* delegated catalog cannot be turned into a valid URL.
|
|
1996 |
* @throws IOException Error reading delegated catalog file.
|
|
1997 |
*
|
|
1998 |
* @return The system identifier to use.
|
|
1999 |
* Note that the nominal system identifier is not returned if a
|
|
2000 |
* match is not found in the catalog, instead null is returned
|
|
2001 |
* to indicate that no match was found.
|
|
2002 |
*/
|
|
2003 |
protected synchronized String resolveSubordinateCatalogs(int entityType,
|
|
2004 |
String entityName,
|
|
2005 |
String publicId,
|
|
2006 |
String systemId)
|
|
2007 |
throws MalformedURLException, IOException {
|
|
2008 |
|
|
2009 |
for (int catPos = 0; catPos < catalogs.size(); catPos++) {
|
|
2010 |
Catalog c = null;
|
|
2011 |
|
|
2012 |
try {
|
|
2013 |
c = (Catalog) catalogs.elementAt(catPos);
|
|
2014 |
} catch (ClassCastException e) {
|
|
2015 |
String catfile = (String) catalogs.elementAt(catPos);
|
|
2016 |
c = newCatalog();
|
|
2017 |
|
|
2018 |
try {
|
|
2019 |
c.parseCatalog(catfile);
|
|
2020 |
} catch (MalformedURLException mue) {
|
|
2021 |
catalogManager.debug.message(1, "Malformed Catalog URL", catfile);
|
|
2022 |
} catch (FileNotFoundException fnfe) {
|
|
2023 |
catalogManager.debug.message(1, "Failed to load catalog, file not found",
|
|
2024 |
catfile);
|
|
2025 |
} catch (IOException ioe) {
|
|
2026 |
catalogManager.debug.message(1, "Failed to load catalog, I/O error", catfile);
|
|
2027 |
}
|
|
2028 |
|
|
2029 |
catalogs.setElementAt(c, catPos);
|
|
2030 |
}
|
|
2031 |
|
|
2032 |
String resolved = null;
|
|
2033 |
|
|
2034 |
// Ok, now what are we supposed to call here?
|
|
2035 |
if (entityType == DOCTYPE) {
|
|
2036 |
resolved = c.resolveDoctype(entityName,
|
|
2037 |
publicId,
|
|
2038 |
systemId);
|
|
2039 |
} else if (entityType == DOCUMENT) {
|
|
2040 |
resolved = c.resolveDocument();
|
|
2041 |
} else if (entityType == ENTITY) {
|
|
2042 |
resolved = c.resolveEntity(entityName,
|
|
2043 |
publicId,
|
|
2044 |
systemId);
|
|
2045 |
} else if (entityType == NOTATION) {
|
|
2046 |
resolved = c.resolveNotation(entityName,
|
|
2047 |
publicId,
|
|
2048 |
systemId);
|
|
2049 |
} else if (entityType == PUBLIC) {
|
|
2050 |
resolved = c.resolvePublic(publicId, systemId);
|
|
2051 |
} else if (entityType == SYSTEM) {
|
|
2052 |
resolved = c.resolveSystem(systemId);
|
|
2053 |
} else if (entityType == URI) {
|
|
2054 |
resolved = c.resolveURI(systemId);
|
|
2055 |
}
|
|
2056 |
|
|
2057 |
if (resolved != null) {
|
|
2058 |
return resolved;
|
|
2059 |
}
|
|
2060 |
}
|
|
2061 |
|
|
2062 |
return null;
|
|
2063 |
}
|
|
2064 |
|
|
2065 |
// -----------------------------------------------------------------
|
|
2066 |
|
|
2067 |
/**
|
|
2068 |
* Replace backslashes with forward slashes. (URLs always use
|
|
2069 |
* forward slashes.)
|
|
2070 |
*
|
|
2071 |
* @param sysid The input system identifier.
|
|
2072 |
* @return The same system identifier with backslashes turned into
|
|
2073 |
* forward slashes.
|
|
2074 |
*/
|
|
2075 |
protected String fixSlashes (String sysid) {
|
|
2076 |
return sysid.replace('\\', '/');
|
|
2077 |
}
|
|
2078 |
|
|
2079 |
/**
|
|
2080 |
* Construct an absolute URI from a relative one, using the current
|
|
2081 |
* base URI.
|
|
2082 |
*
|
|
2083 |
* @param sysid The (possibly relative) system identifier
|
|
2084 |
* @return The system identifier made absolute with respect to the
|
|
2085 |
* current {@link #base}.
|
|
2086 |
*/
|
|
2087 |
protected String makeAbsolute(String sysid) {
|
|
2088 |
URL local = null;
|
|
2089 |
|
|
2090 |
sysid = fixSlashes(sysid);
|
|
2091 |
|
|
2092 |
try {
|
|
2093 |
local = new URL(base, sysid);
|
|
2094 |
} catch (MalformedURLException e) {
|
|
2095 |
catalogManager.debug.message(1, "Malformed URL on system identifier", sysid);
|
|
2096 |
}
|
|
2097 |
|
|
2098 |
if (local != null) {
|
|
2099 |
return local.toString();
|
|
2100 |
} else {
|
|
2101 |
return sysid;
|
|
2102 |
}
|
|
2103 |
}
|
|
2104 |
|
|
2105 |
|
26516
|
2106 |
/**
|
|
2107 |
* Perform character normalization on a URI reference.
|
|
2108 |
*
|
|
2109 |
* @param uriref The URI reference
|
|
2110 |
* @return The normalized URI reference.
|
|
2111 |
*/
|
|
2112 |
protected String normalizeURI(String uriref) {
|
|
2113 |
if (uriref == null) {
|
|
2114 |
return null;
|
|
2115 |
}
|
|
2116 |
final int length = uriref.length();
|
|
2117 |
for (int i = 0; i < length; ++i) {
|
|
2118 |
char c = uriref.charAt(i);
|
|
2119 |
if ((c <= 0x20) // ctrl
|
|
2120 |
|| (c > 0x7F) // high ascii
|
|
2121 |
|| (c == 0x22) // "
|
|
2122 |
|| (c == 0x3C) // <
|
|
2123 |
|| (c == 0x3E) // >
|
|
2124 |
|| (c == 0x5C) // \
|
|
2125 |
|| (c == 0x5E) // ^
|
|
2126 |
|| (c == 0x60) // `
|
|
2127 |
|| (c == 0x7B) // {
|
|
2128 |
|| (c == 0x7C) // |
|
|
2129 |
|| (c == 0x7D) // }
|
|
2130 |
|| (c == 0x7F)) {
|
|
2131 |
return normalizeURI(uriref, i);
|
|
2132 |
}
|
|
2133 |
}
|
|
2134 |
return uriref;
|
12005
|
2135 |
}
|
|
2136 |
|
26516
|
2137 |
/**
|
|
2138 |
* Perform character normalization on a URI reference.
|
|
2139 |
*
|
|
2140 |
* @param uriref The URI reference
|
|
2141 |
* @param index The index of the first character which requires escaping.
|
|
2142 |
* @return The normalized URI reference.
|
|
2143 |
*/
|
|
2144 |
private String normalizeURI(String uriref, int index) {
|
|
2145 |
final StringBuilder buffer = new StringBuilder();
|
|
2146 |
for (int i = 0; i < index; ++i) {
|
|
2147 |
buffer.append(uriref.charAt(i));
|
|
2148 |
}
|
|
2149 |
final byte[] bytes;
|
|
2150 |
try {
|
|
2151 |
bytes = uriref.substring(index).getBytes("UTF-8");
|
|
2152 |
}
|
|
2153 |
catch (UnsupportedEncodingException uee) {
|
|
2154 |
// this can't happen
|
|
2155 |
catalogManager.debug.message(1, "UTF-8 is an unsupported encoding!?");
|
|
2156 |
return uriref;
|
|
2157 |
}
|
|
2158 |
for (int count = 0; count < bytes.length; ++count) {
|
|
2159 |
int ch = bytes[count] & 0xFF;
|
|
2160 |
if ((ch <= 0x20) // ctrl
|
|
2161 |
|| (ch > 0x7F) // high ascii
|
|
2162 |
|| (ch == 0x22) // "
|
|
2163 |
|| (ch == 0x3C) // <
|
|
2164 |
|| (ch == 0x3E) // >
|
|
2165 |
|| (ch == 0x5C) // \
|
|
2166 |
|| (ch == 0x5E) // ^
|
|
2167 |
|| (ch == 0x60) // `
|
|
2168 |
|| (ch == 0x7B) // {
|
|
2169 |
|| (ch == 0x7C) // |
|
|
2170 |
|| (ch == 0x7D) // }
|
|
2171 |
|| (ch == 0x7F)) {
|
|
2172 |
writeEncodedByte(ch, buffer);
|
|
2173 |
}
|
|
2174 |
else {
|
|
2175 |
buffer.append((char) bytes[count]);
|
|
2176 |
}
|
|
2177 |
}
|
|
2178 |
return buffer.toString();
|
12005
|
2179 |
}
|
|
2180 |
|
26516
|
2181 |
/**
|
|
2182 |
* Perform %-encoding on a single byte.
|
|
2183 |
*
|
|
2184 |
* @param b The 8-bit integer that represents the byte. (Bytes are signed
|
|
2185 |
* but encoding needs to look at the bytes unsigned.)
|
|
2186 |
* @return The %-encoded string for the byte in question.
|
|
2187 |
*/
|
|
2188 |
protected String encodedByte(int b) {
|
|
2189 |
StringBuilder buffer = new StringBuilder(3);
|
|
2190 |
writeEncodedByte(b, buffer);
|
|
2191 |
return buffer.toString();
|
|
2192 |
}
|
12005
|
2193 |
|
26516
|
2194 |
/**
|
|
2195 |
* Perform %-encoding on a single byte.
|
|
2196 |
*
|
|
2197 |
* @param b The 8-bit integer that represents the byte. (Bytes are signed
|
|
2198 |
* but encoding needs to look at the bytes unsigned.)
|
|
2199 |
* @param buffer The target for the %-encoded string for the byte in question.
|
|
2200 |
*/
|
|
2201 |
private void writeEncodedByte(int b, StringBuilder buffer) {
|
|
2202 |
String hex = Integer.toHexString(b).toUpperCase(Locale.ENGLISH);
|
|
2203 |
if (hex.length() < 2) {
|
|
2204 |
buffer.append("%0");
|
|
2205 |
buffer.append(hex);
|
|
2206 |
}
|
|
2207 |
else {
|
|
2208 |
buffer.append('%');
|
|
2209 |
buffer.append(hex);
|
|
2210 |
}
|
12005
|
2211 |
}
|
|
2212 |
|
|
2213 |
// -----------------------------------------------------------------
|
|
2214 |
|
|
2215 |
/**
|
|
2216 |
* Add to the current list of delegated catalogs.
|
|
2217 |
*
|
|
2218 |
* <p>This method always constructs the {@link #localDelegate}
|
|
2219 |
* vector so that it is ordered by length of partial
|
|
2220 |
* public identifier.</p>
|
|
2221 |
*
|
|
2222 |
* @param entry The DELEGATE catalog entry
|
|
2223 |
*/
|
|
2224 |
protected void addDelegate(CatalogEntry entry) {
|
|
2225 |
int pos = 0;
|
|
2226 |
String partial = entry.getEntryArg(0);
|
|
2227 |
|
|
2228 |
Enumeration local = localDelegate.elements();
|
|
2229 |
while (local.hasMoreElements()) {
|
|
2230 |
CatalogEntry dpe = (CatalogEntry) local.nextElement();
|
|
2231 |
String dp = dpe.getEntryArg(0);
|
|
2232 |
if (dp.equals(partial)) {
|
|
2233 |
// we already have this prefix
|
|
2234 |
return;
|
|
2235 |
}
|
|
2236 |
if (dp.length() > partial.length()) {
|
|
2237 |
pos++;
|
|
2238 |
}
|
|
2239 |
if (dp.length() < partial.length()) {
|
|
2240 |
break;
|
|
2241 |
}
|
|
2242 |
}
|
|
2243 |
|
|
2244 |
// now insert partial into the vector at [pos]
|
|
2245 |
if (localDelegate.size() == 0) {
|
|
2246 |
localDelegate.addElement(entry);
|
|
2247 |
} else {
|
|
2248 |
localDelegate.insertElementAt(entry, pos);
|
|
2249 |
}
|
|
2250 |
}
|
|
2251 |
}
|