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 org.apache.xml.resolver.Catalog;
  19. import org.apache.xml.resolver.CatalogEntry;
  20. import org.apache.xml.resolver.helpers.PublicId;
  21. /**
  22. * This class extends the Catalog class provided by Norman Walsh's
  23. * resolver library in xml-commons in order to add classpath entity
  24. * and URI resolution. Since XMLCatalog already does classpath
  25. * resolution, we simply add all CatalogEntry instances back to the
  26. * controlling XMLCatalog instance. This is done via a callback
  27. * mechanism. ApacheCatalog is <em>only</em> used for external
  28. * catalog files. Inline entries (currently <code><dtd></code>
  29. * and <code><entity></code>) are not added to ApacheCatalog.
  30. * See XMLCatalog.java for the details of the entity and URI
  31. * resolution algorithms.
  32. *
  33. * @see org.apache.tools.ant.types.XMLCatalog.CatalogResolver
  34. * @version $Id: ApacheCatalog.java,v 1.9.2.4 2004/03/09 17:01:56 peterreilly Exp $
  35. * @since Ant 1.6
  36. */
  37. public class ApacheCatalog extends Catalog {
  38. /** The resolver object to callback. */
  39. private ApacheCatalogResolver resolver = null;
  40. /**
  41. * <p>Create a new ApacheCatalog instance.</p>
  42. *
  43. * <p>This method overrides the superclass method of the same name
  44. * in order to set the resolver object for callbacks. The reason
  45. * we have to do this is that internally Catalog creates a new
  46. * instance of itself for each external catalog file processed.
  47. * That is, if two external catalog files are processed, there
  48. * will be a total of two ApacheCatalog instances, and so on.</p>
  49. */
  50. protected Catalog newCatalog() {
  51. ApacheCatalog cat = (ApacheCatalog) super.newCatalog();
  52. cat.setResolver(resolver);
  53. return cat;
  54. }
  55. /** Set the resolver object to callback. */
  56. public void setResolver(ApacheCatalogResolver resolver) {
  57. this.resolver = resolver;
  58. }
  59. /**
  60. * <p>This method overrides the superclass method of the same name
  61. * in order to add catalog entries back to the controlling
  62. * XMLCatalog instance. In this way, we can add classpath lookup
  63. * for these entries.</p>
  64. *
  65. * <p>When we add an external catalog file, the entries inside it
  66. * get parsed by this method. Therefore, we override it to add
  67. * each of them back to the controlling XMLCatalog instance. This
  68. * is done by performing a callback to the ApacheCatalogResolver,
  69. * which in turn calls the XMLCatalog.</p>
  70. *
  71. * <p>XMLCatalog currently only understands <code>PUBLIC</code>
  72. * and <code>URI</code> entry types, so we ignore the other types.</p>
  73. *
  74. * @param entry The CatalogEntry to process.
  75. */
  76. public void addEntry(CatalogEntry entry) {
  77. int type = entry.getEntryType();
  78. if (type == PUBLIC) {
  79. String publicid = PublicId.normalize(entry.getEntryArg(0));
  80. String systemid = normalizeURI(entry.getEntryArg(1));
  81. if (resolver == null) {
  82. catalogManager.debug
  83. .message(1, "Internal Error: null ApacheCatalogResolver");
  84. } else {
  85. resolver.addPublicEntry(publicid, systemid, base);
  86. }
  87. } else if (type == URI) {
  88. String uri = normalizeURI(entry.getEntryArg(0));
  89. String altURI = normalizeURI(entry.getEntryArg(1));
  90. if (resolver == null) {
  91. catalogManager.debug
  92. .message(1, "Internal Error: null ApacheCatalogResolver");
  93. } else {
  94. resolver.addURIEntry(uri, altURI, base);
  95. }
  96. }
  97. super.addEntry(entry);
  98. }
  99. } //- ApacheCatalog