1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 2002, 2003,2004 The Apache Software Foundation.
   7  *
   8  * Licensed under the Apache License, Version 2.0 (the "License");
   9  * you may not use this file except in compliance with the License.
  10  * You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.impl.xs;
  22 
  23 import com.sun.org.apache.xerces.internal.util.XMLResourceIdentifierImpl;
  24 import com.sun.org.apache.xerces.internal.xni.QName;
  25 import com.sun.org.apache.xerces.internal.xni.XMLAttributes;
  26 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription;
  27 import com.sun.org.apache.xerces.internal.xni.grammars.XMLSchemaDescription;
  28 
  29 /**
  30  * All information specific to XML Schema grammars.
  31  *
  32  * @xerces.internal
  33  *
  34  * @author Neil Graham, IBM
  35  * @author Neeraj Bajaj, SUN Microsystems.
  36  *
  37  */
  38 public class XSDDescription extends XMLResourceIdentifierImpl
  39                 implements XMLSchemaDescription {
  40     // used to indicate what triggered the call
  41     /**
  42      * Indicate that this description was just initialized.
  43      */
  44     public final static short CONTEXT_INITIALIZE = -1;
  45     /**
  46      * Indicate that the current schema document is <include>d by another
  47      * schema document.
  48      */
  49     public final static short CONTEXT_INCLUDE   = 0;
  50     /**
  51      * Indicate that the current schema document is <redefine>d by another
  52      * schema document.
  53      */
  54     public final static short CONTEXT_REDEFINE  = 1;
  55     /**
  56      * Indicate that the current schema document is <import>ed by another
  57      * schema document.
  58      */
  59     public final static short CONTEXT_IMPORT    = 2;
  60     /**
  61      * Indicate that the current schema document is being preparsed.
  62      */
  63     public final static short CONTEXT_PREPARSE  = 3;
  64     /**
  65      * Indicate that the parse of the current schema document is triggered
  66      * by xsi:schemaLocation/noNamespaceSchemaLocation attribute(s) in the
  67      * instance document. This value is only used if we don't defer the loading
  68      * of schema documents.
  69      */
  70     public final static short CONTEXT_INSTANCE  = 4;
  71     /**
  72      * Indicate that the parse of the current schema document is triggered by
  73      * the occurrence of an element whose namespace is the target namespace
  74      * of this schema document. This value is only used if we do defer the
  75      * loading of schema documents until a component from that namespace is
  76      * referenced from the instance.
  77      */
  78     public final static short CONTEXT_ELEMENT   = 5;
  79     /**
  80      * Indicate that the parse of the current schema document is triggered by
  81      * the occurrence of an attribute whose namespace is the target namespace
  82      * of this schema document. This value is only used if we do defer the
  83      * loading of schema documents until a component from that namespace is
  84      * referenced from the instance.
  85      */
  86     public final static short CONTEXT_ATTRIBUTE = 6;
  87     /**
  88      * Indicate that the parse of the current schema document is triggered by
  89      * the occurrence of an "xsi:type" attribute, whose value (a QName) has
  90      * the target namespace of this schema document as its namespace.
  91      * This value is only used if we do defer the loading of schema documents
  92      * until a component from that namespace is referenced from the instance.
  93      */
  94     public final static short CONTEXT_XSITYPE   = 7;
  95 
  96     // REVISIT: write description of these fields
  97     protected short fContextType;
  98     protected String [] fLocationHints ;
  99     protected QName fTriggeringComponent;
 100     protected QName fEnclosedElementName;
 101     protected XMLAttributes  fAttributes;
 102 
 103     /**
 104      * the type of the grammar (e.g., DTD or XSD);
 105      *
 106      * @see com.sun.org.apache.xerces.internal.xni.grammars.Grammar
 107      */
 108     public String getGrammarType() {
 109         return XMLGrammarDescription.XML_SCHEMA;
 110     }
 111 
 112     /**
 113      * Get the context. The returned value is one of the pre-defined
 114      * CONTEXT_xxx constants.
 115      *
 116      * @return  the value indicating the context
 117      */
 118     public short getContextType() {
 119         return fContextType ;
 120     }
 121 
 122     /**
 123      * If the context is "include" or "redefine", then return the target
 124      * namespace of the enclosing schema document; otherwise, the expected
 125      * target namespace of this document.
 126      *
 127      * @return  the expected/enclosing target namespace
 128      */
 129     public String getTargetNamespace() {
 130         return fNamespace;
 131     }
 132 
 133     /**
 134      * For import and references from the instance document, it's possible to
 135      * have multiple hints for one namespace. So this method returns an array,
 136      * which contains all location hints.
 137      *
 138      * @return  an array of all location hints associated to the expected
 139      *          target namespace
 140      */
 141     public String[] getLocationHints() {
 142         return fLocationHints ;
 143     }
 144 
 145     /**
 146      * If a call is triggered by an element/attribute/xsi:type in the instance,
 147      * this call returns the name of such triggering component: the name of
 148      * the element/attribute, or the value of the xsi:type.
 149      *
 150      * @return  the name of the triggering component
 151      */
 152     public QName getTriggeringComponent() {
 153         return fTriggeringComponent ;
 154     }
 155 
 156     /**
 157      * If a call is triggered by an attribute or xsi:type, then this mehtod
 158      * returns the enclosing element of such element.
 159      *
 160      * @return  the name of the enclosing element
 161      */
 162     public QName getEnclosingElementName() {
 163         return fEnclosedElementName ;
 164     }
 165 
 166     /**
 167      * If a call is triggered by an element/attribute/xsi:type in the instance,
 168      * this call returns all attribute of such element (or enclosing element).
 169      *
 170      * @return  all attributes of the tiggering/enclosing element
 171      */
 172     public XMLAttributes getAttributes() {
 173         return fAttributes;
 174     }
 175 
 176     public boolean fromInstance() {
 177         return fContextType == CONTEXT_ATTRIBUTE ||
 178                fContextType == CONTEXT_ELEMENT ||
 179                fContextType == CONTEXT_INSTANCE ||
 180                fContextType == CONTEXT_XSITYPE;
 181     }
 182 
 183     /**
 184      * @return true is the schema is external
 185      */
 186     public boolean isExternal() {
 187         return fContextType == CONTEXT_INCLUDE ||
 188                fContextType == CONTEXT_REDEFINE ||
 189                fContextType == CONTEXT_IMPORT ||
 190                fContextType == CONTEXT_ELEMENT ||
 191                fContextType == CONTEXT_ATTRIBUTE ||
 192                fContextType == CONTEXT_XSITYPE;
 193     }
 194     /**
 195      * Compares this grammar with the given grammar. Currently, we compare
 196      * the target namespaces.
 197      *
 198      * @param descObj The description of the grammar to be compared with
 199      * @return        True if they are equal, else false
 200      */
 201     public boolean equals(Object descObj) {
 202         if(!(descObj instanceof XMLSchemaDescription)) return false;
 203         XMLSchemaDescription desc = (XMLSchemaDescription)descObj;
 204         if (fNamespace != null)
 205             return fNamespace.equals(desc.getTargetNamespace());
 206         else // fNamespace == null
 207             return desc.getTargetNamespace() == null;
 208     }
 209 
 210     /**
 211      * Returns the hash code of this grammar
 212      *
 213      * @return The hash code
 214      */
 215     public int hashCode() {
 216          return (fNamespace == null) ? 0 : fNamespace.hashCode();
 217     }
 218 
 219     public void setContextType(short contextType){
 220         fContextType = contextType ;
 221     }
 222 
 223     public void setTargetNamespace(String targetNamespace){
 224         fNamespace = targetNamespace ;
 225     }
 226 
 227     public void setLocationHints(String [] locationHints){
 228         int length = locationHints.length ;
 229         fLocationHints  = new String[length];
 230         System.arraycopy(locationHints, 0, fLocationHints, 0, length);
 231         //fLocationHints = locationHints ;
 232     }
 233 
 234     public void setTriggeringComponent(QName triggeringComponent){
 235         fTriggeringComponent = triggeringComponent ;
 236     }
 237 
 238     public void setEnclosingElementName(QName enclosedElementName){
 239         fEnclosedElementName = enclosedElementName ;
 240     }
 241 
 242     public void setAttributes(XMLAttributes attributes){
 243         fAttributes = attributes ;
 244     }
 245 
 246     /**
 247      *  resets all the fields
 248      */
 249     public void reset(){
 250         super.clear();
 251         fContextType = CONTEXT_INITIALIZE;
 252         fLocationHints  = null ;
 253         fTriggeringComponent = null ;
 254         fEnclosedElementName = null ;
 255         fAttributes = null ;
 256     }
 257 
 258     public XSDDescription makeClone() {
 259         XSDDescription desc = new XSDDescription();
 260         desc.fAttributes = this.fAttributes;
 261         desc.fBaseSystemId = this.fBaseSystemId;
 262         desc.fContextType = this.fContextType;
 263         desc.fEnclosedElementName = this.fEnclosedElementName;
 264         desc.fExpandedSystemId = this.fExpandedSystemId;
 265         desc.fLiteralSystemId = this.fLiteralSystemId;
 266         desc.fLocationHints = this.fLocationHints;
 267         desc.fPublicId = this.fPublicId;
 268         desc.fNamespace = this.fNamespace;
 269         desc.fTriggeringComponent = this.fTriggeringComponent;
 270         return desc;
 271     }
 272 
 273 } // XSDDescription