1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /*
6  * Licensed to the Apache Software Foundation (ASF) under one or more
7  * contributor license agreements.  See the NOTICE file distributed with
8  * this work for additional information regarding copyright ownership.
9  * The ASF licenses this file to You under the Apache License, Version 2.0
10  * (the "License"); you may not use this file except in compliance with
11  * the License.  You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 package com.sun.org.apache.xerces.internal.impl.xs;
23 
24 import com.sun.org.apache.xerces.internal.util.NamespaceSupport;
25 
26 /**
27  * This class customizes the behaviour of the util.NamespaceSupport
28  * class in order to easily implement some features that we need for
29  * efficient schema handling.  It will not be generally useful.
30  *
31  * @xerces.internal
32  *
33  * @author Neil Graham, IBM
34  *
35  */
36 public class SchemaNamespaceSupport
37     extends NamespaceSupport {
38 
SchemaNamespaceSupport()39     public SchemaNamespaceSupport () {
40         super();
41     } // constructor
42 
43     // more effecient than NamespaceSupport(NamespaceContext)
SchemaNamespaceSupport(SchemaNamespaceSupport nSupport)44     public SchemaNamespaceSupport(SchemaNamespaceSupport nSupport) {
45         fNamespaceSize = nSupport.fNamespaceSize;
46         if (fNamespace.length < fNamespaceSize)
47             fNamespace = new String[fNamespaceSize];
48         System.arraycopy(nSupport.fNamespace, 0, fNamespace, 0, fNamespaceSize);
49         fCurrentContext = nSupport.fCurrentContext;
50         if (fContext.length <= fCurrentContext)
51             fContext = new int[fCurrentContext+1];
52         System.arraycopy(nSupport.fContext, 0, fContext, 0, fCurrentContext+1);
53     } // end constructor
54 
55     /**
56      * This method takes a set of Strings, as stored in a
57      * NamespaceSupport object, and "fools" the object into thinking
58      * that this is one unified context.  This is meant to be used in
59      * conjunction with things like local elements, whose declarations
60      * may be deeply nested but which for all practical purposes may
61      * be regarded as being one level below the global <schema>
62      * element--at least with regard to namespace declarations.
63      * It's worth noting that the context from which the strings are
64      * being imported had better be using the same SymbolTable.
65      */
setEffectiveContext(String [] namespaceDecls)66     public void setEffectiveContext (String [] namespaceDecls) {
67         if(namespaceDecls == null || namespaceDecls.length == 0) return;
68         pushContext();
69         int newSize = fNamespaceSize + namespaceDecls.length;
70         if (fNamespace.length < newSize) {
71             // expand namespace's size...
72             String[] tempNSArray = new String[newSize];
73             System.arraycopy(fNamespace, 0, tempNSArray, 0, fNamespace.length);
74             fNamespace = tempNSArray;
75         }
76         System.arraycopy(namespaceDecls, 0, fNamespace, fNamespaceSize,
77                          namespaceDecls.length);
78         fNamespaceSize = newSize;
79     } // setEffectiveContext(String):void
80 
81     /**
82      * This method returns an array of Strings, as would be stored in
83      * a NamespaceSupport object.  This array contains all
84      * declarations except those at the global level.
85      */
getEffectiveLocalContext()86     public String [] getEffectiveLocalContext() {
87         // the trick here is to recognize that all local contexts
88         // happen to start at fContext[3].
89         // context 1: empty
90         // context 2: decls for xml and xmlns;
91         // context 3: decls on <xs:schema>: the global ones
92         String[] returnVal = null;
93         if (fCurrentContext >= 3) {
94             int bottomLocalContext = fContext[3];
95             int copyCount = fNamespaceSize - bottomLocalContext;
96             if (copyCount > 0) {
97                 returnVal = new String[copyCount];
98                 System.arraycopy(fNamespace, bottomLocalContext, returnVal, 0,
99                                  copyCount);
100             }
101         }
102         return returnVal;
103     } // getEffectiveLocalContext():String
104 
105     // This method removes from this object all the namespaces
106     // returned by getEffectiveLocalContext.
makeGlobal()107     public void makeGlobal() {
108         if (fCurrentContext >= 3) {
109             fCurrentContext = 3;
110             fNamespaceSize = fContext[3];
111         }
112     } // makeGlobal
113 } // class NamespaceSupport
114