1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /*
6  * Copyright 2003-2005 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 package com.sun.org.apache.xerces.internal.xinclude;
21 
22 import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
23 
24 /**
25  * This is an implementation of NamespaceContext which is intended to be used for
26  * XInclude processing.  It enables each context to be marked as invalid, if necessary,
27  * to indicate that the namespaces recorded on those contexts won't be apparent in the
28  * resulting infoset.
29  *
30  * @author Peter McCracken, IBM
31  *
32  */
33 public class XIncludeNamespaceSupport extends MultipleScopeNamespaceSupport {
34 
35     /**
36      * This stores whether or not the context at the matching depth was valid.
37      */
38     private boolean[] fValidContext = new boolean[8];
39 
40     /**
41      *
42      */
XIncludeNamespaceSupport()43     public XIncludeNamespaceSupport() {
44         super();
45     }
46 
47     /**
48      * @param context
49      */
XIncludeNamespaceSupport(NamespaceContext context)50     public XIncludeNamespaceSupport(NamespaceContext context) {
51         super(context);
52     }
53 
54     /**
55      * Pushes a new context onto the stack.
56      */
pushContext()57     public void pushContext() {
58         super.pushContext();
59         if (fCurrentContext + 1 == fValidContext.length) {
60             boolean[] contextarray = new boolean[fValidContext.length * 2];
61             System.arraycopy(fValidContext, 0, contextarray, 0, fValidContext.length);
62             fValidContext = contextarray;
63         }
64 
65         fValidContext[fCurrentContext] = true;
66     }
67 
68     /**
69      * This method is used to set a context invalid for XInclude namespace processing.
70      * Any context defined by an <include> or <fallback> element is not
71      * valid for processing the include parent's [in-scope namespaces]. Thus, contexts
72      * defined by these elements are set to invalid by the XInclude processor using
73      * this method.
74      */
setContextInvalid()75     public void setContextInvalid() {
76         fValidContext[fCurrentContext] = false;
77     }
78 
79     /**
80      * This returns the namespace URI which was associated with the given pretext, in
81      * the context that existed at the include parent of the current element.  The
82      * include parent is the last element, before the current one, which was not set
83      * to an invalid context using setContextInvalid()
84      *
85      * @param prefix the prefix of the desired URI
86      * @return the URI corresponding to the prefix in the context of the include parent
87      */
getURIFromIncludeParent(String prefix)88     public String getURIFromIncludeParent(String prefix) {
89         int lastValidContext = fCurrentContext - 1;
90         while (lastValidContext > 0 && !fValidContext[lastValidContext]) {
91             lastValidContext--;
92         }
93         return getURI(prefix, lastValidContext);
94     }
95 }
96