1. /*
  2. * Copyright 2002-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.types.resolver;
  18. import java.io.IOException;
  19. import java.net.MalformedURLException;
  20. import java.net.URL;
  21. import org.apache.tools.ant.BuildException;
  22. import org.apache.tools.ant.types.XMLCatalog;
  23. import org.apache.tools.ant.types.ResourceLocation;
  24. import org.apache.xml.resolver.Catalog;
  25. import org.apache.xml.resolver.CatalogManager;
  26. import org.apache.xml.resolver.tools.CatalogResolver;
  27. /**
  28. * <p>This class extends the CatalogResolver class provided by Norman
  29. * Walsh's resolver library in xml-commons. It provides the bridge
  30. * between the Ant XMLCatalog datatype and the xml-commons Catalog
  31. * class. XMLCatalog calls methods in this class using Reflection in
  32. * order to avoid requiring the xml-commons resolver library in the
  33. * path.</p>
  34. *
  35. * <p>The {@link org.apache.tools.ant.types.resolver.ApacheCatalog
  36. * ApacheCatalog} class is used to parse external catalog files, which
  37. * can be in either <a
  38. * href="http://oasis-open.org/committees/entity/background/9401.html">
  39. * plain text format</a> or <a
  40. * href="http://www.oasis-open.org/committees/entity/spec-2001-08-06.html">
  41. * XML format</a>.</p>
  42. *
  43. * <p>For each entry found in an external catalog file, if any, an
  44. * instance of {@link org.apache.tools.ant.types.ResourceLocation
  45. * ResourceLocation} is created and added to the controlling
  46. * XMLCatalog datatype. In this way, these entries will be included
  47. * in XMLCatalog's lookup algorithm. See XMLCatalog.java for more
  48. * details.</p>
  49. *
  50. * @see org.apache.tools.ant.types.XMLCatalog.CatalogResolver
  51. * @see org.apache.xml.resolver.CatalogManager
  52. * @version $Id: ApacheCatalogResolver.java,v 1.12.2.5 2004/03/09 17:01:56 peterreilly Exp $
  53. * @since Ant 1.6
  54. */
  55. public class ApacheCatalogResolver extends CatalogResolver {
  56. /** The XMLCatalog object to callback. */
  57. private XMLCatalog xmlCatalog = null;
  58. static {
  59. //
  60. // If you don't do this, you get all sorts of annoying
  61. // warnings about a missing properties file. However, it
  62. // seems to work just fine with default values. Ultimately,
  63. // we should probably include a "CatalogManager.properties"
  64. // file in the ant jarfile with some default property
  65. // settings. See CatalogManager.java for more details.
  66. //
  67. CatalogManager.getStaticManager().setIgnoreMissingProperties(true);
  68. //
  69. // Make sure CatalogResolver instantiates ApacheCatalog,
  70. // rather than a plain Catalog
  71. //
  72. System.getProperties().put("xml.catalog.className",
  73. ApacheCatalog.class.getName());
  74. CatalogManager.getStaticManager().setUseStaticCatalog(false);
  75. // debug
  76. // CatalogManager.getStaticManager().setVerbosity(4);
  77. }
  78. /** Set the XMLCatalog object to callback. */
  79. public void setXMLCatalog(XMLCatalog xmlCatalog) {
  80. this.xmlCatalog = xmlCatalog;
  81. }
  82. /**
  83. * XMLCatalog calls this to add an external catalog file for each
  84. * file within a <code><catalogfiles></code> fileset.
  85. */
  86. public void parseCatalog(String file) {
  87. Catalog _catalog = getCatalog();
  88. if (!(_catalog instanceof ApacheCatalog)) {
  89. throw new BuildException("Wrong catalog type found: " + _catalog.getClass().getName());
  90. }
  91. ApacheCatalog catalog = (ApacheCatalog) _catalog;
  92. // Pass in reference to ourselves so we can be called back.
  93. catalog.setResolver(this);
  94. try {
  95. catalog.parseCatalog(file);
  96. } catch (MalformedURLException ex) {
  97. throw new BuildException(ex);
  98. } catch (IOException ex) {
  99. throw new BuildException(ex);
  100. }
  101. }
  102. /**
  103. * <p>Add a PUBLIC catalog entry to the controlling XMLCatalog instance.
  104. * ApacheCatalog calls this for each PUBLIC entry found in an external
  105. * catalog file.</p>
  106. *
  107. * @param publicid The public ID of the resource
  108. * @param systemid The system ID (aka location) of the resource
  109. * @param base The base URL of the resource. If the systemid
  110. * specifies a relative URL/pathname, it is resolved using the
  111. * base. The default base for an external catalog file is the
  112. * directory in which the catalog is located.
  113. *
  114. */
  115. public void addPublicEntry(String publicid,
  116. String systemid,
  117. URL base) {
  118. ResourceLocation dtd = new ResourceLocation();
  119. dtd.setBase(base);
  120. dtd.setPublicId(publicid);
  121. dtd.setLocation(systemid);
  122. xmlCatalog.addDTD(dtd);
  123. }
  124. /**
  125. * <p>Add a URI catalog entry to the controlling XMLCatalog instance.
  126. * ApacheCatalog calls this for each URI entry found in an external
  127. * catalog file.</p>
  128. *
  129. * @param uri The URI of the resource
  130. * @param altURI The URI to which the resource should be mapped
  131. * (aka the location)
  132. * @param base The base URL of the resource. If the altURI
  133. * specifies a relative URL/pathname, it is resolved using the
  134. * base. The default base for an external catalog file is the
  135. * directory in which the catalog is located.
  136. *
  137. */
  138. public void addURIEntry(String uri,
  139. String altURI,
  140. URL base) {
  141. ResourceLocation entity = new ResourceLocation();
  142. entity.setBase(base);
  143. entity.setPublicId(uri);
  144. entity.setLocation(altURI);
  145. xmlCatalog.addEntity(entity);
  146. }
  147. } //-- ApacheCatalogResolver