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