KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > xs > XSDDescription


1 /*
2  * Copyright 2002, 2003,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.xs;
18
19 import org.apache.xerces.util.XMLResourceIdentifierImpl;
20 import org.apache.xerces.xni.QName;
21 import org.apache.xerces.xni.XMLAttributes;
22 import org.apache.xerces.xni.grammars.XMLGrammarDescription;
23 import org.apache.xerces.xni.grammars.XMLSchemaDescription;
24
25 /**
26  * All information specific to XML Schema grammars.
27  *
28  * @xerces.internal
29  *
30  * @author Neil Graham, IBM
31  * @author Neeraj Bajaj, SUN Microsystems.
32  *
33  * @version $Id: XSDDescription.java,v 1.13 2004/10/14 03:49:32 mrglavas Exp $
34  */

35 public class XSDDescription extends XMLResourceIdentifierImpl
36                 implements XMLSchemaDescription {
37     // used to indicate what triggered the call
38
/**
39      * Indicate that this description was just initialized.
40      */

41     public final static short CONTEXT_INITIALIZE = -1;
42     /**
43      * Indicate that the current schema document is <include>d by another
44      * schema document.
45      */

46     public final static short CONTEXT_INCLUDE = 0;
47     /**
48      * Indicate that the current schema document is <redefine>d by another
49      * schema document.
50      */

51     public final static short CONTEXT_REDEFINE = 1;
52     /**
53      * Indicate that the current schema document is <import>ed by another
54      * schema document.
55      */

56     public final static short CONTEXT_IMPORT = 2;
57     /**
58      * Indicate that the current schema document is being preparsed.
59      */

60     public final static short CONTEXT_PREPARSE = 3;
61     /**
62      * Indicate that the parse of the current schema document is triggered
63      * by xsi:schemaLocation/noNamespaceSchemaLocation attribute(s) in the
64      * instance document. This value is only used if we don't defer the loading
65      * of schema documents.
66      */

67     public final static short CONTEXT_INSTANCE = 4;
68     /**
69      * Indicate that the parse of the current schema document is triggered by
70      * the occurrence of an element whose namespace is the target namespace
71      * of this schema document. This value is only used if we do defer the
72      * loading of schema documents until a component from that namespace is
73      * referenced from the instance.
74      */

75     public final static short CONTEXT_ELEMENT = 5;
76     /**
77      * Indicate that the parse of the current schema document is triggered by
78      * the occurrence of an attribute whose namespace is the target namespace
79      * of this schema document. This value is only used if we do defer the
80      * loading of schema documents until a component from that namespace is
81      * referenced from the instance.
82      */

83     public final static short CONTEXT_ATTRIBUTE = 6;
84     /**
85      * Indicate that the parse of the current schema document is triggered by
86      * the occurrence of an "xsi:type" attribute, whose value (a QName) has
87      * the target namespace of this schema document as its namespace.
88      * This value is only used if we do defer the loading of schema documents
89      * until a component from that namespace is referenced from the instance.
90      */

91     public final static short CONTEXT_XSITYPE = 7;
92
93     // REVISIT: write description of these fields
94
protected short fContextType;
95     protected String JavaDoc [] fLocationHints ;
96     protected QName fTriggeringComponent;
97     protected QName fEnclosedElementName;
98     protected XMLAttributes fAttributes;
99         
100     /**
101      * the type of the grammar (e.g., DTD or XSD);
102      *
103      * @see org.apache.xerces.xni.grammars.Grammar
104      */

105     public String JavaDoc getGrammarType() {
106         return XMLGrammarDescription.XML_SCHEMA;
107     }
108
109     /**
110      * Get the context. The returned value is one of the pre-defined
111      * CONTEXT_xxx constants.
112      *
113      * @return the value indicating the context
114      */

115     public short getContextType() {
116         return fContextType ;
117     }
118
119     /**
120      * If the context is "include" or "redefine", then return the target
121      * namespace of the enclosing schema document; otherwise, the expected
122      * target namespace of this document.
123      *
124      * @return the expected/enclosing target namespace
125      */

126     public String JavaDoc getTargetNamespace() {
127         return fNamespace;
128     }
129
130     /**
131      * For import and references from the instance document, it's possible to
132      * have multiple hints for one namespace. So this method returns an array,
133      * which contains all location hints.
134      *
135      * @return an array of all location hints associated to the expected
136      * target namespace
137      */

138     public String JavaDoc[] getLocationHints() {
139         return fLocationHints ;
140     }
141
142     /**
143      * If a call is triggered by an element/attribute/xsi:type in the instance,
144      * this call returns the name of such triggering component: the name of
145      * the element/attribute, or the value of the xsi:type.
146      *
147      * @return the name of the triggering component
148      */

149     public QName getTriggeringComponent() {
150         return fTriggeringComponent ;
151     }
152
153     /**
154      * If a call is triggered by an attribute or xsi:type, then this mehtod
155      * returns the enclosing element of such element.
156      *
157      * @return the name of the enclosing element
158      */

159     public QName getEnclosingElementName() {
160         return fEnclosedElementName ;
161     }
162     
163     /**
164      * If a call is triggered by an element/attribute/xsi:type in the instance,
165      * this call returns all attribute of such element (or enclosing element).
166      *
167      * @return all attributes of the tiggering/enclosing element
168      */

169     public XMLAttributes getAttributes() {
170         return fAttributes;
171     }
172     
173     public boolean fromInstance() {
174         return fContextType == CONTEXT_ATTRIBUTE ||
175                fContextType == CONTEXT_ELEMENT ||
176                fContextType == CONTEXT_INSTANCE ||
177                fContextType == CONTEXT_XSITYPE;
178     }
179     
180     /**
181      * Compares this grammar with the given grammar. Currently, we compare
182      * the target namespaces.
183      *
184      * @param descObj The description of the grammar to be compared with
185      * @return True if they are equal, else false
186      */

187     public boolean equals(Object JavaDoc descObj) {
188         if(!(descObj instanceof XMLSchemaDescription)) return false;
189         XMLSchemaDescription desc = (XMLSchemaDescription)descObj;
190         if (fNamespace != null)
191             return fNamespace.equals(desc.getTargetNamespace());
192         else // fNamespace == null
193
return desc.getTargetNamespace() == null;
194     }
195     
196     /**
197      * Returns the hash code of this grammar
198      *
199      * @return The hash code
200      */

201     public int hashCode() {
202          return (fNamespace == null) ? 0 : fNamespace.hashCode();
203     }
204     
205     public void setContextType(short contextType){
206         fContextType = contextType ;
207     }
208
209     public void setTargetNamespace(String JavaDoc targetNamespace){
210         fNamespace = targetNamespace ;
211     }
212
213     public void setLocationHints(String JavaDoc [] locationHints){
214         int length = locationHints.length ;
215         fLocationHints = new String JavaDoc[length];
216         System.arraycopy(locationHints, 0, fLocationHints, 0, length);
217         //fLocationHints = locationHints ;
218
}
219
220     public void setTriggeringComponent(QName triggeringComponent){
221         fTriggeringComponent = triggeringComponent ;
222     }
223
224     public void setEnclosingElementName(QName enclosedElementName){
225         fEnclosedElementName = enclosedElementName ;
226     }
227
228     public void setAttributes(XMLAttributes attributes){
229         fAttributes = attributes ;
230     }
231     
232     /**
233      * resets all the fields
234      */

235     public void reset(){
236         super.clear();
237         fContextType = CONTEXT_INITIALIZE;
238         fLocationHints = null ;
239         fTriggeringComponent = null ;
240         fEnclosedElementName = null ;
241         fAttributes = null ;
242     }
243     
244     public XSDDescription makeClone() {
245         XSDDescription desc = new XSDDescription();
246         desc.fAttributes = this.fAttributes;
247         desc.fBaseSystemId = this.fBaseSystemId;
248         desc.fContextType = this.fContextType;
249         desc.fEnclosedElementName = this.fEnclosedElementName;
250         desc.fExpandedSystemId = this.fExpandedSystemId;
251         desc.fLiteralSystemId = this.fLiteralSystemId;
252         desc.fLocationHints = this.fLocationHints;
253         desc.fPublicId = this.fPublicId;
254         desc.fNamespace = this.fNamespace;
255         desc.fTriggeringComponent = this.fTriggeringComponent;
256         return desc;
257     }
258     
259 } // XSDDescription
260
Popular Tags