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  * @version $Id: XSDDescription.java,v 1.6 2010-11-01 04:39:55 joehw Exp $
  38  */
  39 public class XSDDescription extends XMLResourceIdentifierImpl
  40                 implements XMLSchemaDescription {
  41     // used to indicate what triggered the call
  42     /**
  43      * Indicate that this description was just initialized.
  44      */
  45     public final static short CONTEXT_INITIALIZE = -1;
  46     /**
  47      * Indicate that the current schema document is <include>d by another
  48      * schema document.
  49      */
  50     public final static short CONTEXT_INCLUDE   = 0;
  51     /**
  52      * Indicate that the current schema document is <redefine>d by another
  53      * schema document.
  54      */
  55     public final static short CONTEXT_REDEFINE  = 1;
  56     /**
  57      * Indicate that the current schema document is <import>ed by another
  58      * schema document.
  59      */
  60     public final static short CONTEXT_IMPORT    = 2;
  61     /**
  62      * Indicate that the current schema document is being preparsed.
  63      */
  64     public final static short CONTEXT_PREPARSE  = 3;
  65     /**
  66      * Indicate that the parse of the current schema document is triggered
  67      * by xsi:schemaLocation/noNamespaceSchemaLocation attribute(s) in the
  68      * instance document. This value is only used if we don't defer the loading
  69      * of schema documents.
  70      */
  71     public final static short CONTEXT_INSTANCE  = 4;
  72     /**
  73      * Indicate that the parse of the current schema document is triggered by
  74      * the occurrence of an element whose namespace is the target namespace
  75      * of this schema document. This value is only used if we do defer the
  76      * loading of schema documents until a component from that namespace is
  77      * referenced from the instance.
  78      */
  79     public final static short CONTEXT_ELEMENT   = 5;
  80     /**
  81      * Indicate that the parse of the current schema document is triggered by
  82      * the occurrence of an attribute whose namespace is the target namespace
  83      * of this schema document. This value is only used if we do defer the
  84      * loading of schema documents until a component from that namespace is
  85      * referenced from the instance.
  86      */
  87     public final static short CONTEXT_ATTRIBUTE = 6;
  88     /**
  89      * Indicate that the parse of the current schema document is triggered by
  90      * the occurrence of an "xsi:type" attribute, whose value (a QName) has
  91      * the target namespace of this schema document as its namespace.
  92      * This value is only used if we do defer the loading of schema documents
  93      * until a component from that namespace is referenced from the instance.
  94      */
  95     public final static short CONTEXT_XSITYPE   = 7;
  96 
  97     // REVISIT: write description of these fields
  98     protected short fContextType;
  99     protected String [] fLocationHints ;
 100     protected QName fTriggeringComponent;
 101     protected QName fEnclosedElementName;
 102     protected XMLAttributes  fAttributes;
 103 
 104     /**
 105      * the type of the grammar (e.g., DTD or XSD);
 106      *
 107      * @see com.sun.org.apache.xerces.internal.xni.grammars.Grammar
 108      */
 109     public String getGrammarType() {
 110         return XMLGrammarDescription.XML_SCHEMA;
 111     }
 112 
 113     /**
 114      * Get the context. The returned value is one of the pre-defined
 115      * CONTEXT_xxx constants.
 116      *
 117      * @return  the value indicating the context
 118      */
 119     public short getContextType() {
 120         return fContextType ;
 121     }
 122 
 123     /**
 124      * If the context is "include" or "redefine", then return the target
 125      * namespace of the enclosing schema document; otherwise, the expected
 126      * target namespace of this document.
 127      *
 128      * @return  the expected/enclosing target namespace
 129      */
 130     public String getTargetNamespace() {
 131         return fNamespace;
 132     }
 133 
 134     /**
 135      * For import and references from the instance document, it's possible to
 136      * have multiple hints for one namespace. So this method returns an array,
 137      * which contains all location hints.
 138      *
 139      * @return  an array of all location hints associated to the expected
 140      *          target namespace
 141      */
 142     public String[] getLocationHints() {
 143         return fLocationHints ;
 144     }
 145 
 146     /**
 147      * If a call is triggered by an element/attribute/xsi:type in the instance,
 148      * this call returns the name of such triggering component: the name of
 149      * the element/attribute, or the value of the xsi:type.
 150      *
 151      * @return  the name of the triggering component
 152      */
 153     public QName getTriggeringComponent() {
 154         return fTriggeringComponent ;
 155     }
 156 
 157     /**
 158      * If a call is triggered by an attribute or xsi:type, then this mehtod
 159      * returns the enclosing element of such element.
 160      *
 161      * @return  the name of the enclosing element
 162      */
 163     public QName getEnclosingElementName() {
 164         return fEnclosedElementName ;
 165     }
 166 
 167     /**
 168      * If a call is triggered by an element/attribute/xsi:type in the instance,
 169      * this call returns all attribute of such element (or enclosing element).
 170      *
 171      * @return  all attributes of the tiggering/enclosing element
 172      */
 173     public XMLAttributes getAttributes() {
 174         return fAttributes;
 175     }
 176 
 177     public boolean fromInstance() {
 178         return fContextType == CONTEXT_ATTRIBUTE ||
 179                fContextType == CONTEXT_ELEMENT ||
 180                fContextType == CONTEXT_INSTANCE ||
 181                fContextType == CONTEXT_XSITYPE;
 182     }
 183 
 184     /**
 185      * @return true is the schema is external
 186      */
 187     public boolean isExternal() {
 188         return fContextType == CONTEXT_INCLUDE ||
 189                fContextType == CONTEXT_REDEFINE ||
 190                fContextType == CONTEXT_IMPORT ||
 191                fContextType == CONTEXT_ELEMENT ||
 192                fContextType == CONTEXT_ATTRIBUTE ||
 193                fContextType == CONTEXT_XSITYPE;
 194     }
 195     /**
 196      * Compares this grammar with the given grammar. Currently, we compare
 197      * the target namespaces.
 198      *
 199      * @param descObj The description of the grammar to be compared with
 200      * @return        True if they are equal, else false
 201      */
 202     public boolean equals(Object descObj) {
 203         if(!(descObj instanceof XMLSchemaDescription)) return false;
 204         XMLSchemaDescription desc = (XMLSchemaDescription)descObj;
 205         if (fNamespace != null)
 206             return fNamespace.equals(desc.getTargetNamespace());
 207         else // fNamespace == null
 208             return desc.getTargetNamespace() == null;
 209     }
 210 
 211     /**
 212      * Returns the hash code of this grammar
 213      *
 214      * @return The hash code
 215      */
 216     public int hashCode() {
 217          return (fNamespace == null) ? 0 : fNamespace.hashCode();
 218     }
 219 
 220     public void setContextType(short contextType){
 221         fContextType = contextType ;
 222     }
 223 
 224     public void setTargetNamespace(String targetNamespace){
 225         fNamespace = targetNamespace ;
 226     }
 227 
 228     public void setLocationHints(String [] locationHints){
 229         int length = locationHints.length ;
 230         fLocationHints  = new String[length];
 231         System.arraycopy(locationHints, 0, fLocationHints, 0, length);
 232         //fLocationHints = locationHints ;
 233     }
 234 
 235     public void setTriggeringComponent(QName triggeringComponent){
 236         fTriggeringComponent = triggeringComponent ;
 237     }
 238 
 239     public void setEnclosingElementName(QName enclosedElementName){
 240         fEnclosedElementName = enclosedElementName ;
 241     }
 242 
 243     public void setAttributes(XMLAttributes attributes){
 244         fAttributes = attributes ;
 245     }
 246 
 247     /**
 248      *  resets all the fields
 249      */
 250     public void reset(){
 251         super.clear();
 252         fContextType = CONTEXT_INITIALIZE;
 253         fLocationHints  = null ;
 254         fTriggeringComponent = null ;
 255         fEnclosedElementName = null ;
 256         fAttributes = null ;
 257     }
 258 
 259     public XSDDescription makeClone() {
 260         XSDDescription desc = new XSDDescription();
 261         desc.fAttributes = this.fAttributes;
 262         desc.fBaseSystemId = this.fBaseSystemId;
 263         desc.fContextType = this.fContextType;
 264         desc.fEnclosedElementName = this.fEnclosedElementName;
 265         desc.fExpandedSystemId = this.fExpandedSystemId;
 266         desc.fLiteralSystemId = this.fLiteralSystemId;
 267         desc.fLocationHints = this.fLocationHints;
 268         desc.fPublicId = this.fPublicId;
 269         desc.fNamespace = this.fNamespace;
 270         desc.fTriggeringComponent = this.fTriggeringComponent;
 271         return desc;
 272     }
 273 
 274 } // XSDDescription