KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > apache > xerces > utils > NamespacesScope


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.enhydra.apache.xerces.utils;
59
60 /**
61  * NamespacesScope provides a data structure for mapping namespace prefixes
62  * to their URI's. The mapping accurately reflects the scoping of namespaces
63  * at a particular instant in time.
64  */

65 public class NamespacesScope {
66     /**
67      * NamespacesHandler allows a client to be notified when namespace scopes change
68      */

69     public interface NamespacesHandler {
70         /**
71          * startNamespaceDeclScope is called when a new namespace scope is created
72          *
73          * @param prefix the StringPool handle of the namespace prefix being declared
74          * @param uri the StringPool handle of the namespace's URI
75          * @exception java.lang.Exception
76          */

77         public void startNamespaceDeclScope(int prefix, int uri) throws Exception JavaDoc;
78         /**
79          * endNamespaceDeclScope is called when a namespace scope ends
80          *
81          * @param prefix the StringPool handle of the namespace prefix going out of scope
82          * @exception java.lang.Exception
83          */

84         public void endNamespaceDeclScope(int prefix) throws Exception JavaDoc;
85     }
86     public NamespacesScope() {
87         this(new NamespacesHandler() {
88             public void startNamespaceDeclScope(int prefix, int uri) throws Exception JavaDoc {
89             }
90             public void endNamespaceDeclScope(int prefix) throws Exception JavaDoc {
91             }
92         });
93     }
94     public NamespacesScope(NamespacesHandler handler) {
95         fHandler = handler;
96         fNamespaceMappings[0] = new int[9];
97         fNamespaceMappings[0][0] = 1;
98     }
99     public NamespacesScope(NamespacesHandler handler, int elemDepth, int[][] map) {
100         fHandler = handler;
101         fElementDepth = elemDepth;
102         if (map == null) {
103             fNamespaceMappings = new int[8][];
104             return;
105         }
106         fNamespaceMappings = new int[map.length][];
107         for(int i = 0; i <= fElementDepth; i++) {
108             if(map[i]!= null) {
109                 fNamespaceMappings[i] = new int[map[i].length];
110                 System.arraycopy(map[i], 0, fNamespaceMappings[i], 0, map[i].length);
111             }
112         }
113     }
114     /**
115      * set the namespace URI for given prefix
116      *
117      * @param prefix the StringPool handler of the prefix
118      * @param namespace the StringPool handle of the namespace URI
119      */

120     public void setNamespaceForPrefix(int prefix, int namespace) throws Exception JavaDoc {
121         int offset = fNamespaceMappings[fElementDepth][0];
122         if (offset == fNamespaceMappings[fElementDepth].length) {
123             int[] newMappings = new int[offset + 8];
124             System.arraycopy(fNamespaceMappings[fElementDepth], 0, newMappings, 0, offset);
125             fNamespaceMappings[fElementDepth] = newMappings;
126         }
127         fNamespaceMappings[fElementDepth][offset++] = prefix;
128         fNamespaceMappings[fElementDepth][offset++] = namespace;
129         fNamespaceMappings[fElementDepth][0] = offset;
130         if (fElementDepth > 0)
131             fHandler.startNamespaceDeclScope(prefix, namespace);
132     }
133     /**
134      * retreive the namespace URI for a prefix
135      *
136      * @param prefix the StringPool handle of the prefix
137      */

138     public int getNamespaceForPrefix(int prefix) {
139         for (int depth = fElementDepth; depth >= 0; depth--) {
140             int offset = fNamespaceMappings[depth][0];
141             for (int i = 1; i < offset; i += 2) {
142                 if (prefix == fNamespaceMappings[depth][i]) {
143                     return fNamespaceMappings[depth][i+1];
144                 }
145             }
146         }
147         return StringPool.EMPTY_STRING;
148     }
149     /**
150      * Add a new namespace mapping
151      */

152     public void increaseDepth() throws Exception JavaDoc {
153         fElementDepth++;
154         if (fElementDepth == fNamespaceMappings.length) {
155             int[][] newMappings = new int[fElementDepth + 8][];
156             System.arraycopy(fNamespaceMappings, 0, newMappings, 0, fElementDepth);
157             fNamespaceMappings = newMappings;
158         }
159         if (fNamespaceMappings[fElementDepth] == null)
160             fNamespaceMappings[fElementDepth] = new int[9];
161         fNamespaceMappings[fElementDepth][0] = 1;
162     }
163     /**
164      * Remove a namespace mappng
165      */

166     public void decreaseDepth() throws Exception JavaDoc {
167         if (fElementDepth > 0) {
168             int offset = fNamespaceMappings[fElementDepth][0];
169             while (offset > 1) {
170                 offset -= 2;
171                 fHandler.endNamespaceDeclScope(fNamespaceMappings[fElementDepth][offset]);
172             }
173         }
174         fElementDepth--;
175     }
176
177     // Object methods:
178
public Object JavaDoc clone() {
179         return new NamespacesScope(fHandler, fElementDepth, fNamespaceMappings);
180     } // end clone()
181
private NamespacesHandler fHandler = null;
182     private int fElementDepth = 0;
183     private int[][] fNamespaceMappings = new int[8][];
184 }
185
Popular Tags