View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2000-2002,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.NamespaceSupport;
24  
25  /**
26   * This class customizes the behaviour of the util.NamespaceSupport
27   * class in order to easily implement some features that we need for
28   * efficient schema handling.  It will not be generally useful.
29   *
30   * @xerces.internal
31   *
32   * @author Neil Graham, IBM
33   *
34   */
35  public class SchemaNamespaceSupport
36      extends NamespaceSupport {
37  
38      public SchemaNamespaceSupport () {
39          super();
40      } // constructor
41  
42      // more effecient than NamespaceSupport(NamespaceContext)
43      public SchemaNamespaceSupport(SchemaNamespaceSupport nSupport) {
44          fNamespaceSize = nSupport.fNamespaceSize;
45          if (fNamespace.length < fNamespaceSize)
46              fNamespace = new String[fNamespaceSize];
47          System.arraycopy(nSupport.fNamespace, 0, fNamespace, 0, fNamespaceSize);
48          fCurrentContext = nSupport.fCurrentContext;
49          if (fContext.length <= fCurrentContext)
50              fContext = new int[fCurrentContext+1];
51          System.arraycopy(nSupport.fContext, 0, fContext, 0, fCurrentContext+1);
52      } // end constructor
53  
54      /**
55       * This method takes a set of Strings, as stored in a
56       * NamespaceSupport object, and "fools" the object into thinking
57       * that this is one unified context.  This is meant to be used in
58       * conjunction with things like local elements, whose declarations
59       * may be deeply nested but which for all practical purposes may
60       * be regarded as being one level below the global <schema>
61       * element--at least with regard to namespace declarations.
62       * It's worth noting that the context from which the strings are
63       * being imported had better be using the same SymbolTable.
64       */
65      public void setEffectiveContext (String [] namespaceDecls) {
66          if(namespaceDecls == null || namespaceDecls.length == 0) return;
67          pushContext();
68          int newSize = fNamespaceSize + namespaceDecls.length;
69          if (fNamespace.length < newSize) {
70              // expand namespace's size...
71              String[] tempNSArray = new String[newSize];
72              System.arraycopy(fNamespace, 0, tempNSArray, 0, fNamespace.length);
73              fNamespace = tempNSArray;
74          }
75          System.arraycopy(namespaceDecls, 0, fNamespace, fNamespaceSize,
76                           namespaceDecls.length);
77          fNamespaceSize = newSize;
78      } // setEffectiveContext(String):void
79  
80      /**
81       * This method returns an array of Strings, as would be stored in
82       * a NamespaceSupport object.  This array contains all
83       * declarations except those at the global level.
84       */
85      public String [] getEffectiveLocalContext() {
86          // the trick here is to recognize that all local contexts
87          // happen to start at fContext[3].
88          // context 1: empty
89          // context 2: decls for xml and xmlns;
90          // context 3: decls on <xs:schema>: the global ones
91          String[] returnVal = null;
92          if (fCurrentContext >= 3) {
93              int bottomLocalContext = fContext[3];
94              int copyCount = fNamespaceSize - bottomLocalContext;
95              if (copyCount > 0) {
96                  returnVal = new String[copyCount];
97                  System.arraycopy(fNamespace, bottomLocalContext, returnVal, 0,
98                                   copyCount);
99              }
100         }
101         return returnVal;
102     } // getEffectiveLocalContext():String
103 
104     // This method removes from this object all the namespaces
105     // returned by getEffectiveLocalContext.
106     public void makeGlobal() {
107         if (fCurrentContext >= 3) {
108             fCurrentContext = 3;
109             fNamespaceSize = fContext[3];
110         }
111     } // makeGlobal
112 } // class NamespaceSupport