1 /* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * @LastModified: Oct 2017 4 */ 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.xerces.internal.impl.dtd; 23 24 import com.sun.org.apache.xerces.internal.util.XMLResourceIdentifierImpl; 25 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier; 26 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription; 27 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; 28 import java.util.List; 29 30 /** 31 * All information specific to DTD grammars. 32 * 33 * @xerces.internal 34 * 35 * @author Neil Graham, IBM 36 */ 37 public class XMLDTDDescription extends XMLResourceIdentifierImpl 38 implements com.sun.org.apache.xerces.internal.xni.grammars.XMLDTDDescription { 39 40 // Data 41 42 // pieces of information needed to make this usable as a Grammar key 43 // if we know the root of this grammar, here's its name: 44 protected String fRootName = null; 45 46 // if we don't know the root name, this stores all elements that 47 // could serve; fPossibleRoots and fRootName cannot both be non-null 48 protected List<String> fPossibleRoots = null; 49 50 // Constructors: 51 public XMLDTDDescription(XMLResourceIdentifier id, String rootName) { 52 this.setValues(id.getPublicId(), id.getLiteralSystemId(), 53 id.getBaseSystemId(), id.getExpandedSystemId()); 54 this.fRootName = rootName; 55 this.fPossibleRoots = null; 56 } // init(XMLResourceIdentifier, String) 57 58 public XMLDTDDescription(String publicId, String literalId, 59 String baseId, String expandedId, String rootName) { 60 this.setValues(publicId, literalId, baseId, expandedId); 61 this.fRootName = rootName; 62 this.fPossibleRoots = null; 63 } // init(String, String, String, String, String) 64 65 public XMLDTDDescription(XMLInputSource source) { 66 this.setValues(source.getPublicId(), null, 67 source.getBaseSystemId(), source.getSystemId()); 68 this.fRootName = null; 69 this.fPossibleRoots = null; 70 } // init(XMLInputSource) 71 72 // XMLGrammarDescription methods 73 74 public String getGrammarType () { 75 return XMLGrammarDescription.XML_DTD; 76 } // getGrammarType(): String 77 78 /** 79 * @return the root name of this DTD or null if root name is unknown 80 */ 81 public String getRootName() { 82 return fRootName; 83 } // getRootName(): String 84 85 /** Set the root name **/ 86 public void setRootName(String rootName) { 87 fRootName = rootName; 88 fPossibleRoots = null; 89 } 90 91 /** Set possible roots **/ 92 public void setPossibleRoots(List<String> possibleRoots) { 93 fPossibleRoots = possibleRoots; 94 } 95 96 /** 97 * Compares this grammar with the given grammar. Currently, we compare 98 * as follows: 99 * - if grammar type not equal return false immediately 100 * - try and find a common root name: 101 * - if both have roots, use them 102 * - else if one has a root, examine other's possible root's for a match; 103 * - else try all combinations 104 * - test fExpandedSystemId and fPublicId as above 105 * 106 * @param desc The description of the grammar to be compared with 107 * @return True if they are equal, else false 108 */ 109 public boolean equals(Object desc) { 110 if (!(desc instanceof XMLGrammarDescription)) return false; 111 if (!getGrammarType().equals(((XMLGrammarDescription)desc).getGrammarType())) { 112 return false; 113 } 114 // assume it's a DTDDescription 115 XMLDTDDescription dtdDesc = (XMLDTDDescription)desc; 116 if (fRootName != null) { 117 if ((dtdDesc.fRootName) != null && !dtdDesc.fRootName.equals(fRootName)) { 118 return false; 119 } 120 else if (dtdDesc.fPossibleRoots != null && !dtdDesc.fPossibleRoots.contains(fRootName)) { 121 return false; 122 } 123 } 124 else if (fPossibleRoots != null) { 125 if (dtdDesc.fRootName != null) { 126 if (!fPossibleRoots.contains(dtdDesc.fRootName)) { 127 return false; 128 } 129 } 130 else if (dtdDesc.fPossibleRoots == null) { 131 return false; 132 } 133 else { 134 boolean found = false; 135 for (String root : fPossibleRoots) { 136 found = dtdDesc.fPossibleRoots.contains(root); 137 if (found) break; 138 } 139 if (!found) return false; 140 } 141 } 142 // if we got this far we've got a root match... try other two fields, 143 // since so many different DTD's have roots in common: 144 if (fExpandedSystemId != null) { 145 if (!fExpandedSystemId.equals(dtdDesc.fExpandedSystemId)) { 146 return false; 147 } 148 } 149 else if (dtdDesc.fExpandedSystemId != null) { 150 return false; 151 } 152 if (fPublicId != null) { 153 if (!fPublicId.equals(dtdDesc.fPublicId)) { 154 return false; 155 } 156 } 157 else if (dtdDesc.fPublicId != null) { 158 return false; 159 } 160 return true; 161 } 162 163 /** 164 * Returns the hash code of this grammar 165 * Because our .equals method is so complex, we just return a very 166 * simple hash that might avoid calls to the equals method a bit... 167 * @return The hash code 168 */ 169 public int hashCode() { 170 if (fExpandedSystemId != null) { 171 return fExpandedSystemId.hashCode(); 172 } 173 if (fPublicId != null) { 174 return fPublicId.hashCode(); 175 } 176 // give up; hope .equals can handle it: 177 return 0; 178 } 179 } // class XMLDTDDescription