1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xerces" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package com.sun.org.apache.xerces.internal.impl.dtd;
  58. import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription;
  59. import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
  60. import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
  61. import com.sun.org.apache.xerces.internal.util.XMLResourceIdentifierImpl;
  62. import java.util.Vector;
  63. /*
  64. * All information specific to dTD grammars.
  65. *
  66. * @author Neil Graham, IBM
  67. * @version $Id: XMLDTDDescription.java,v 1.7 2003/01/07 22:46:53 sandygao Exp $
  68. */
  69. public class XMLDTDDescription extends XMLResourceIdentifierImpl
  70. implements com.sun.org.apache.xerces.internal.xni.grammars.XMLDTDDescription {
  71. // Data
  72. // pieces of information needed to make this usable as a Grammar key
  73. // if we know the root of this grammar, here's its name:
  74. protected String fRootName = null;
  75. // if we don't know the root name, this stores all elements that
  76. // could serve; fPossibleRoots and fRootName cannot both be non-null
  77. protected Vector fPossibleRoots = null;
  78. // Constructors:
  79. public XMLDTDDescription(XMLResourceIdentifier id, String rootName) {
  80. this.setValues(id.getPublicId(), id.getLiteralSystemId(),
  81. id.getBaseSystemId(), id.getExpandedSystemId());
  82. this.fRootName = rootName;
  83. this.fPossibleRoots = null;
  84. } // init(XMLResourceIdentifier, String)
  85. public XMLDTDDescription(String publicId, String literalId,
  86. String baseId, String expandedId, String rootName) {
  87. this.setValues(publicId, literalId, baseId, expandedId);
  88. this.fRootName = rootName;
  89. this.fPossibleRoots = null;
  90. } // init(String, String, String, String, String)
  91. public XMLDTDDescription(XMLInputSource source) {
  92. this.setValues(source.getPublicId(), null,
  93. source.getBaseSystemId(), source.getSystemId());
  94. this.fRootName = null;
  95. this.fPossibleRoots = null;
  96. } // init(XMLInputSource)
  97. // XMLGrammarDescription methods
  98. public String getGrammarType () {
  99. return XMLGrammarDescription.XML_DTD;
  100. } // getGrammarType(): String
  101. // return the root name of this DTD
  102. // returns null if root name is unknown
  103. public String getRootName() {
  104. return fRootName;
  105. } // getRootName(): String
  106. // set the root name
  107. public void setRootName(String rootName) {
  108. fRootName = rootName;
  109. fPossibleRoots = null;
  110. }
  111. // set possible roots
  112. public void setPossibleRoots(Vector possibleRoots) {
  113. fPossibleRoots = possibleRoots;
  114. }
  115. /**
  116. * Compares this grammar with the given grammar. Currently, we compare
  117. * as follows:
  118. * - if grammar type not equal return false immediately
  119. * - try and find a common root name:
  120. * - if both have roots, use them
  121. * - else if one has a root, examine other's possible root's for a match;
  122. * - else try all combinations
  123. * - test fExpandedSystemId and fPublicId as above
  124. *
  125. * @param desc The description of the grammar to be compared with
  126. * @return True if they are equal, else false
  127. */
  128. public boolean equals(Object desc) {
  129. if(!(desc instanceof XMLGrammarDescription)) return false;
  130. if (!getGrammarType().equals(((XMLGrammarDescription)desc).getGrammarType())) {
  131. return false;
  132. }
  133. // assume it's a DTDDescription
  134. XMLDTDDescription dtdDesc = (XMLDTDDescription)desc;
  135. if(fRootName != null) {
  136. if((dtdDesc.fRootName) != null && !dtdDesc.fRootName.equals(fRootName)) {
  137. return false;
  138. } else if(dtdDesc.fPossibleRoots != null && !dtdDesc.fPossibleRoots.contains(fRootName)) {
  139. return false;
  140. }
  141. } else if(fPossibleRoots != null) {
  142. if(dtdDesc.fRootName != null) {
  143. if(!fPossibleRoots.contains(dtdDesc.fRootName)) {
  144. return false;
  145. }
  146. } else if(dtdDesc.fPossibleRoots == null) {
  147. return false;
  148. } else {
  149. boolean found = false;
  150. for(int i = 0; i<fPossibleRoots.size(); i++) {
  151. String root = (String)fPossibleRoots.elementAt(i);
  152. found = dtdDesc.fPossibleRoots.contains(root);
  153. if(found) break;
  154. }
  155. if(!found) return false;
  156. }
  157. }
  158. // if we got this far we've got a root match... try other two fields,
  159. // since so many different DTD's have roots in common:
  160. if(fExpandedSystemId != null) {
  161. if(!fExpandedSystemId.equals(dtdDesc.fExpandedSystemId))
  162. return false;
  163. }
  164. else if(dtdDesc.fExpandedSystemId != null)
  165. return false;
  166. if(fPublicId != null) {
  167. if(!fPublicId.equals(dtdDesc.fPublicId))
  168. return false;
  169. }
  170. else if(dtdDesc.fPublicId != null)
  171. return false;
  172. return true;
  173. }
  174. /**
  175. * Returns the hash code of this grammar
  176. * Because our .equals method is so complex, we just return a very
  177. * simple hash that might avoid calls to the equals method a bit...
  178. * @return The hash code
  179. */
  180. public int hashCode() {
  181. if(fExpandedSystemId != null)
  182. return fExpandedSystemId.hashCode();
  183. if(fPublicId != null)
  184. return fPublicId.hashCode();
  185. // give up; hope .equals can handle it:
  186. return 0;
  187. }
  188. } // class XMLDTDDescription