📄 xmlcatalog.java
字号:
try { baseURL = FILE_UTILS.getFileURL(getProject().getBaseDir()); } catch (MalformedURLException ex) { throw new BuildException("Project basedir cannot be converted to a URL"); } } InputSource source = null; URL url = null; try { url = new URL(baseURL, uri); } catch (MalformedURLException ex) { // ignore } if (url != null) { try { InputStream is = url.openStream(); if (is != null) { source = new InputSource(is); String sysid = url.toExternalForm(); source.setSystemId(sysid); log("catalog entry matched as a URL: '" + sysid + "'", Project.MSG_DEBUG); } } catch (IOException ex) { // ignore } } return source; } /** * Interface implemented by both the InternalResolver strategy and * the ExternalResolver strategy. */ private interface CatalogResolver extends URIResolver, EntityResolver { InputSource resolveEntity(String publicId, String systemId); Source resolve(String href, String base) throws TransformerException; } /** * The InternalResolver strategy is used if the Apache resolver * library (Norm Walsh's library from xml-commons) is not * available. In this case, external catalog files will be * ignored. * */ private class InternalResolver implements CatalogResolver { public InternalResolver() { log("Apache resolver library not found, internal resolver will be used", Project.MSG_VERBOSE); } public InputSource resolveEntity(String publicId, String systemId) { InputSource result = null; ResourceLocation matchingEntry = findMatchingEntry(publicId); if (matchingEntry != null) { log("Matching catalog entry found for publicId: '" + matchingEntry.getPublicId() + "' location: '" + matchingEntry.getLocation() + "'", Project.MSG_DEBUG); result = filesystemLookup(matchingEntry); if (result == null) { result = classpathLookup(matchingEntry); } if (result == null) { result = urlLookup(matchingEntry); } } return result; } public Source resolve(String href, String base) throws TransformerException { SAXSource result = null; InputSource source = null; ResourceLocation matchingEntry = findMatchingEntry(href); if (matchingEntry != null) { log("Matching catalog entry found for uri: '" + matchingEntry.getPublicId() + "' location: '" + matchingEntry.getLocation() + "'", Project.MSG_DEBUG); // // Use the passed in base in preference to the base // from matchingEntry, which is either null or the // directory in which the external catalog file from // which it was obtained is located. We make a copy // so matchingEntry's original base is untouched. // // This is the standard behavior as per my reading of // the JAXP and XML Catalog specs. CKS 11/7/2002 // ResourceLocation entryCopy = matchingEntry; if (base != null) { try { URL baseURL = new URL(base); entryCopy = new ResourceLocation(); entryCopy.setBase(baseURL); } catch (MalformedURLException ex) { // ignore } } entryCopy.setPublicId(matchingEntry.getPublicId()); entryCopy.setLocation(matchingEntry.getLocation()); source = filesystemLookup(entryCopy); if (source == null) { source = classpathLookup(entryCopy); } if (source == null) { source = urlLookup(entryCopy); } if (source != null) { result = new SAXSource(source); } } return result; } } /** * The ExternalResolver strategy is used if the Apache resolver * library (Norm Walsh's library from xml-commons) is available in * the classpath. The ExternalResolver is a essentially a superset * of the InternalResolver. * */ private class ExternalResolver implements CatalogResolver { private Method setXMLCatalog = null; private Method parseCatalog = null; private Method resolveEntity = null; private Method resolve = null; /** The instance of the ApacheCatalogResolver bridge class */ private Object resolverImpl = null; private boolean externalCatalogsProcessed = false; public ExternalResolver(Class resolverImplClass, Object resolverImpl) { this.resolverImpl = resolverImpl; // // Get Method instances for each of the methods we need to // call on the resolverImpl using reflection. We can't // call them directly, because they require on the // xml-commons resolver library which may not be available // in the classpath. // try { setXMLCatalog = resolverImplClass.getMethod("setXMLCatalog", new Class[] {XMLCatalog.class}); parseCatalog = resolverImplClass.getMethod("parseCatalog", new Class[] {String.class}); resolveEntity = resolverImplClass.getMethod("resolveEntity", new Class[] {String.class, String.class}); resolve = resolverImplClass.getMethod("resolve", new Class[] {String.class, String.class}); } catch (NoSuchMethodException ex) { throw new BuildException(ex); } log("Apache resolver library found, xml-commons resolver will be used", Project.MSG_VERBOSE); } public InputSource resolveEntity(String publicId, String systemId) { InputSource result = null; processExternalCatalogs(); ResourceLocation matchingEntry = findMatchingEntry(publicId); if (matchingEntry != null) { log("Matching catalog entry found for publicId: '" + matchingEntry.getPublicId() + "' location: '" + matchingEntry.getLocation() + "'", Project.MSG_DEBUG); result = filesystemLookup(matchingEntry); if (result == null) { result = classpathLookup(matchingEntry); } if (result == null) { try { result = (InputSource) resolveEntity.invoke(resolverImpl, new Object[] {publicId, systemId}); } catch (Exception ex) { throw new BuildException(ex); } } } else { // // We didn't match a ResourceLocation, but since we // only support PUBLIC and URI entry types internally, // it is still possible that there is another entry in // an external catalog that will match. We call // Apache resolver's resolveEntity method to cover // this possibility. // try { result = (InputSource) resolveEntity.invoke(resolverImpl, new Object[] {publicId, systemId}); } catch (Exception ex) { throw new BuildException(ex); } } return result; } public Source resolve(String href, String base) throws TransformerException { SAXSource result = null; InputSource source = null; processExternalCatalogs(); ResourceLocation matchingEntry = findMatchingEntry(href); if (matchingEntry != null) { log("Matching catalog entry found for uri: '" + matchingEntry.getPublicId() + "' location: '" + matchingEntry.getLocation() + "'", Project.MSG_DEBUG); // // Use the passed in base in preference to the base // from matchingEntry, which is either null or the // directory in which the external catalog file from // which it was obtained is located. We make a copy // so matchingEntry's original base is untouched. Of // course, if there is no base, no need to make a // copy... // // This is the standard behavior as per my reading of // the JAXP and XML Catalog specs. CKS 11/7/2002 // ResourceLocation entryCopy = matchingEntry; if (base != null) { try { URL baseURL = new URL(base); entryCopy = new ResourceLocation(); entryCopy.setBase(baseURL); } catch (MalformedURLException ex) { // ignore } } entryCopy.setPublicId(matchingEntry.getPublicId()); entryCopy.setLocation(matchingEntry.getLocation()); source = filesystemLookup(entryCopy); if (source == null) { source = classpathLookup(entryCopy); } if (source != null) { result = new SAXSource(source); } else { try { result = (SAXSource) resolve.invoke(resolverImpl, new Object[] {href, base}); } catch (Exception ex) { throw new BuildException(ex); } } } else { // // We didn't match a ResourceLocation, but since we // only support PUBLIC and URI entry types internally, // it is still possible that there is another entry in // an external catalog that will match. We call // Apache resolver's resolveEntity method to cover // this possibility. // try { result = (SAXSource) resolve.invoke(resolverImpl, new Object[] {href, base}); } catch (Exception ex) { throw new BuildException(ex); } } return result; } /** * Process each external catalog file specified in a * <code><catalogpath></code>. It will be * parsed by the resolver library, and the individual elements * will be added back to us (that is, the controlling * XMLCatalog instance) via a callback mechanism. */ private void processExternalCatalogs() { if (!externalCatalogsProcessed) { try { setXMLCatalog.invoke(resolverImpl, new Object[] {XMLCatalog.this}); } catch (Exception ex) { throw new BuildException(ex); } // Parse each catalog listed in nested <catalogpath> elements Path catPath = getCatalogPath(); if (catPath != null) { log("Using catalogpath '" + getCatalogPath() + "'", Project.MSG_DEBUG); String[] catPathList = getCatalogPath().list(); for (int i = 0; i < catPathList.length; i++) { File catFile = new File(catPathList[i]); log("Parsing " + catFile, Project.MSG_DEBUG); try { parseCatalog.invoke(resolverImpl, new Object[] {catFile.getPath()}); } catch (Exception ex) { throw new BuildException(ex); } } } } externalCatalogsProcessed = true; } }} //-- XMLCatalog
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -