KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > dtd > XMLDTDDescription


1 /*
2  * Copyright 2000-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.xerces.impl.dtd;
18
19 import org.apache.xerces.xni.grammars.XMLGrammarDescription;
20 import org.apache.xerces.xni.XMLResourceIdentifier;
21 import org.apache.xerces.xni.parser.XMLInputSource;
22
23 import org.apache.xerces.util.XMLResourceIdentifierImpl;
24 import java.util.Vector JavaDoc;
25
26 /**
27  * All information specific to DTD grammars.
28  *
29  * @xerces.internal
30  *
31  * @author Neil Graham, IBM
32  * @version $Id: XMLDTDDescription.java,v 1.9 2004/10/04 21:57:30 mrglavas Exp $
33  */

34 public class XMLDTDDescription extends XMLResourceIdentifierImpl
35         implements org.apache.xerces.xni.grammars.XMLDTDDescription {
36
37     // Data
38

39     // pieces of information needed to make this usable as a Grammar key
40
// if we know the root of this grammar, here's its name:
41
protected String JavaDoc fRootName = null;
42
43     // if we don't know the root name, this stores all elements that
44
// could serve; fPossibleRoots and fRootName cannot both be non-null
45
protected Vector JavaDoc fPossibleRoots = null;
46
47     // Constructors:
48
public XMLDTDDescription(XMLResourceIdentifier id, String JavaDoc rootName) {
49         this.setValues(id.getPublicId(), id.getLiteralSystemId(),
50                 id.getBaseSystemId(), id.getExpandedSystemId());
51         this.fRootName = rootName;
52         this.fPossibleRoots = null;
53     } // init(XMLResourceIdentifier, String)
54

55     public XMLDTDDescription(String JavaDoc publicId, String JavaDoc literalId,
56                 String JavaDoc baseId, String JavaDoc expandedId, String JavaDoc rootName) {
57         this.setValues(publicId, literalId, baseId, expandedId);
58         this.fRootName = rootName;
59         this.fPossibleRoots = null;
60     } // init(String, String, String, String, String)
61

62     public XMLDTDDescription(XMLInputSource source) {
63         this.setValues(source.getPublicId(), null,
64                 source.getBaseSystemId(), source.getSystemId());
65         this.fRootName = null;
66         this.fPossibleRoots = null;
67     } // init(XMLInputSource)
68

69     // XMLGrammarDescription methods
70

71     public String JavaDoc getGrammarType () {
72         return XMLGrammarDescription.XML_DTD;
73     } // getGrammarType(): String
74

75     /**
76      * @return the root name of this DTD or null if root name is unknown
77      */

78     public String JavaDoc getRootName() {
79         return fRootName;
80     } // getRootName(): String
81

82     /** Set the root name **/
83     public void setRootName(String JavaDoc rootName) {
84         fRootName = rootName;
85         fPossibleRoots = null;
86     }
87
88     /** Set possible roots **/
89     public void setPossibleRoots(Vector JavaDoc possibleRoots) {
90         fPossibleRoots = possibleRoots;
91     }
92
93     /**
94      * Compares this grammar with the given grammar. Currently, we compare
95      * as follows:
96      * - if grammar type not equal return false immediately
97      * - try and find a common root name:
98      * - if both have roots, use them
99      * - else if one has a root, examine other's possible root's for a match;
100      * - else try all combinations
101      * - test fExpandedSystemId and fPublicId as above
102      *
103      * @param desc The description of the grammar to be compared with
104      * @return True if they are equal, else false
105      */

106     public boolean equals(Object JavaDoc desc) {
107         if(!(desc instanceof XMLGrammarDescription)) return false;
108         if (!getGrammarType().equals(((XMLGrammarDescription)desc).getGrammarType())) {
109             return false;
110         }
111         // assume it's a DTDDescription
112
XMLDTDDescription dtdDesc = (XMLDTDDescription)desc;
113         if(fRootName != null) {
114             if((dtdDesc.fRootName) != null && !dtdDesc.fRootName.equals(fRootName)) {
115                 return false;
116             } else if(dtdDesc.fPossibleRoots != null && !dtdDesc.fPossibleRoots.contains(fRootName)) {
117                 return false;
118             }
119         } else if(fPossibleRoots != null) {
120             if(dtdDesc.fRootName != null) {
121                 if(!fPossibleRoots.contains(dtdDesc.fRootName)) {
122                     return false;
123                 }
124             } else if(dtdDesc.fPossibleRoots == null) {
125                 return false;
126             } else {
127                 boolean found = false;
128                 for(int i = 0; i<fPossibleRoots.size(); i++) {
129                     String JavaDoc root = (String JavaDoc)fPossibleRoots.elementAt(i);
130                     found = dtdDesc.fPossibleRoots.contains(root);
131                     if(found) break;
132                 }
133                 if(!found) return false;
134             }
135         }
136         // if we got this far we've got a root match... try other two fields,
137
// since so many different DTD's have roots in common:
138
if(fExpandedSystemId != null) {
139             if(!fExpandedSystemId.equals(dtdDesc.fExpandedSystemId))
140                 return false;
141         }
142         else if(dtdDesc.fExpandedSystemId != null)
143             return false;
144         if(fPublicId != null) {
145             if(!fPublicId.equals(dtdDesc.fPublicId))
146                 return false;
147         }
148         else if(dtdDesc.fPublicId != null)
149             return false;
150         return true;
151     }
152     
153     /**
154      * Returns the hash code of this grammar
155      * Because our .equals method is so complex, we just return a very
156      * simple hash that might avoid calls to the equals method a bit...
157      * @return The hash code
158      */

159     public int hashCode() {
160         if(fExpandedSystemId != null)
161             return fExpandedSystemId.hashCode();
162         if(fPublicId != null)
163             return fPublicId.hashCode();
164         // give up; hope .equals can handle it:
165
return 0;
166     }
167 } // class XMLDTDDescription
168

169
Popular Tags