KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > impl > dtd > XMLDTDDescription


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
58 package com.sun.org.apache.xerces.internal.impl.dtd;
59
60 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription;
61 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
62 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
63
64 import com.sun.org.apache.xerces.internal.util.XMLResourceIdentifierImpl;
65 import java.util.Vector JavaDoc;
66
67 /*
68  * All information specific to dTD grammars.
69  *
70  * @author Neil Graham, IBM
71  * @version $Id: XMLDTDDescription.java,v 1.7 2003/01/07 22:46:53 sandygao Exp $
72  */

73
74 public class XMLDTDDescription extends XMLResourceIdentifierImpl
75         implements com.sun.org.apache.xerces.internal.xni.grammars.XMLDTDDescription {
76
77     // Data
78

79     // pieces of information needed to make this usable as a Grammar key
80
// if we know the root of this grammar, here's its name:
81
protected String JavaDoc fRootName = null;
82
83     // if we don't know the root name, this stores all elements that
84
// could serve; fPossibleRoots and fRootName cannot both be non-null
85
protected Vector JavaDoc fPossibleRoots = null;
86
87     // Constructors:
88
public XMLDTDDescription(XMLResourceIdentifier id, String JavaDoc rootName) {
89         this.setValues(id.getPublicId(), id.getLiteralSystemId(),
90                 id.getBaseSystemId(), id.getExpandedSystemId());
91         this.fRootName = rootName;
92         this.fPossibleRoots = null;
93     } // init(XMLResourceIdentifier, String)
94

95     public XMLDTDDescription(String JavaDoc publicId, String JavaDoc literalId,
96                 String JavaDoc baseId, String JavaDoc expandedId, String JavaDoc rootName) {
97         this.setValues(publicId, literalId, baseId, expandedId);
98         this.fRootName = rootName;
99         this.fPossibleRoots = null;
100     } // init(String, String, String, String, String)
101

102     public XMLDTDDescription(XMLInputSource source) {
103         this.setValues(source.getPublicId(), null,
104                 source.getBaseSystemId(), source.getSystemId());
105         this.fRootName = null;
106         this.fPossibleRoots = null;
107     } // init(XMLInputSource)
108

109     // XMLGrammarDescription methods
110

111     public String JavaDoc getGrammarType () {
112         return XMLGrammarDescription.XML_DTD;
113     } // getGrammarType(): String
114

115     // return the root name of this DTD
116
// returns null if root name is unknown
117
public String JavaDoc getRootName() {
118         return fRootName;
119     } // getRootName(): String
120

121     // set the root name
122
public void setRootName(String JavaDoc rootName) {
123         fRootName = rootName;
124         fPossibleRoots = null;
125     }
126
127     // set possible roots
128
public void setPossibleRoots(Vector JavaDoc possibleRoots) {
129         fPossibleRoots = possibleRoots;
130     }
131
132     /**
133      * Compares this grammar with the given grammar. Currently, we compare
134      * as follows:
135      * - if grammar type not equal return false immediately
136      * - try and find a common root name:
137      * - if both have roots, use them
138      * - else if one has a root, examine other's possible root's for a match;
139      * - else try all combinations
140      * - test fExpandedSystemId and fPublicId as above
141      *
142      * @param desc The description of the grammar to be compared with
143      * @return True if they are equal, else false
144      */

145     public boolean equals(Object JavaDoc desc) {
146         if(!(desc instanceof XMLGrammarDescription)) return false;
147         if (!getGrammarType().equals(((XMLGrammarDescription)desc).getGrammarType())) {
148             return false;
149         }
150         // assume it's a DTDDescription
151
XMLDTDDescription dtdDesc = (XMLDTDDescription)desc;
152         if(fRootName != null) {
153             if((dtdDesc.fRootName) != null && !dtdDesc.fRootName.equals(fRootName)) {
154                 return false;
155             } else if(dtdDesc.fPossibleRoots != null && !dtdDesc.fPossibleRoots.contains(fRootName)) {
156                 return false;
157             }
158         } else if(fPossibleRoots != null) {
159             if(dtdDesc.fRootName != null) {
160                 if(!fPossibleRoots.contains(dtdDesc.fRootName)) {
161                     return false;
162                 }
163             } else if(dtdDesc.fPossibleRoots == null) {
164                 return false;
165             } else {
166                 boolean found = false;
167                 for(int i = 0; i<fPossibleRoots.size(); i++) {
168                     String JavaDoc root = (String JavaDoc)fPossibleRoots.elementAt(i);
169                     found = dtdDesc.fPossibleRoots.contains(root);
170                     if(found) break;
171                 }
172                 if(!found) return false;
173             }
174         }
175         // if we got this far we've got a root match... try other two fields,
176
// since so many different DTD's have roots in common:
177
if(fExpandedSystemId != null) {
178             if(!fExpandedSystemId.equals(dtdDesc.fExpandedSystemId))
179                 return false;
180         }
181         else if(dtdDesc.fExpandedSystemId != null)
182             return false;
183         if(fPublicId != null) {
184             if(!fPublicId.equals(dtdDesc.fPublicId))
185                 return false;
186         }
187         else if(dtdDesc.fPublicId != null)
188             return false;
189         return true;
190     }
191     
192     /**
193      * Returns the hash code of this grammar
194      * Because our .equals method is so complex, we just return a very
195      * simple hash that might avoid calls to the equals method a bit...
196      * @return The hash code
197      */

198     public int hashCode() {
199         if(fExpandedSystemId != null)
200             return fExpandedSystemId.hashCode();
201         if(fPublicId != null)
202             return fPublicId.hashCode();
203         // give up; hope .equals can handle it:
204
return 0;
205     }
206 } // class XMLDTDDescription
207

208
Popular Tags