KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > serializers > util > Namespaces


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.serializers.util;
17
18 import org.xml.sax.SAXException JavaDoc;
19
20 /**
21  * The <code>Namespaces</code> class is an utility class implementing a
22  * stack for XML namespaces declarations.
23  *
24  * @author <a HREF="mailto:pier@apache.org">Pier Fumagalli</a>, February 2003
25  * @version CVS $Id: Namespaces.java 54845 2004-10-15 10:52:09Z cziegeler $
26  */

27 public class Namespaces {
28     /** The array of all URIs in this stack. */
29     private String JavaDoc uri[] = new String JavaDoc[512];
30     /** The array of all prefixes in this stack. */
31     private String JavaDoc pre[] = new String JavaDoc[512];
32     /** The number of URI/prefix mappings in this stack. */
33     private int depth = 0;
34     /** The last "committed" namespace. */
35     private int last = 0;
36
37     /** The index of the namespace prefix in for <code>commit()</code>. */
38     public static final int NAMESPACE_PREFIX = 0;
39     /** The index of the namespace uri in for <code>commit()</code>. */
40     public static final int NAMESPACE_URI = 1;
41
42     /**
43      * Create a new <code>Namespaces</code> instance.
44      */

45     public Namespaces() {
46         super();
47
48         this.push("", "");
49         this.push("xml", "http://www.w3.org/XML/1998/namespace");
50         this.last = this.depth;
51     }
52
53     /**
54      * Push a new namespace declaration into this stack.
55      *
56      * @param prefix The prefix to associate with the specified URI.
57      * @param uri The URI associated with the namespace.
58      */

59     public synchronized void push(String JavaDoc prefix, String JavaDoc uri) {
60         if (this.depth == this.uri.length) {
61             int newDepth = this.uri.length + (this.uri.length >> 1);
62             String JavaDoc newUri[] = new String JavaDoc[newDepth];
63             String JavaDoc newPre[] = new String JavaDoc[newDepth];
64             System.arraycopy(this.uri, 0, newUri, 0, this.depth);
65             System.arraycopy(this.pre, 0, newPre, 0, this.depth);
66             this.uri = newUri;
67             this.pre = newPre;
68         }
69
70         this.uri[this.depth] = uri;
71         this.pre[this.depth] = prefix;
72         this.depth ++;
73     }
74
75     /**
76      * Pop a new namespace declaration out of this stack.
77      * <br />
78      * If more than one namespace is associated with the specified namespace,
79      * only the last pushed namespace will be popped out.
80      *
81      * @param prefix The prefix to associate with the specified URI.
82      * @throws SAXException If the prefix was not mapped in this stack.
83      */

84     public synchronized void pop(String JavaDoc prefix)
85     throws SAXException JavaDoc {
86         for (int x = this.position(prefix, pre); x < this.depth; x++) {
87             int k = (x + 1);
88             this.pre[x] = this.pre[k];
89             this.uri[x] = this.uri[k];
90         }
91         //this.pre[this.depth] = null;
92
//this.uri[this.depth] = null;
93
this.last--;
94         this.depth--;
95     }
96
97     /**
98      * Qualify an XML name.
99      * <br />
100      * Given a URI, local name and qualified name as passed to the SAX
101      * <code>ContentHandler</code> interface in the <code>startElement()</code>
102      * method, this method will always return a valid XML name token usable
103      * for serialization (checking namespaces URIs and prefixes).
104      *
105      * @param nsuri The Namespace URI, or the empty string if the element has
106      * no namespace URI or if namespace processing is not being
107      * performed.
108      * @param local The local name (without prefix), or the empty string if
109      * namespace processing is not being performed.
110      * @param qualified The qualified name (with prefix), or the empty string
111      * if qualified names are not available.
112      * @throws SAXException If the specified URI is not mapped with a prefix.
113      */

114     public String JavaDoc qualify(String JavaDoc nsuri, String JavaDoc local, String JavaDoc qualified)
115     throws SAXException JavaDoc {
116         if (nsuri == null) nsuri = "";
117         if (local == null) local = "";
118         if (qualified == null) qualified = "";
119
120         /** No namespaces processing. */
121         if ((nsuri.length() == 0 ) && (local.length() == 0)) return(qualified);
122
123         /*
124          * Get the prefix for the given namespace and return the qualified
125          * name: "prefix:local" if prefix is not empty, "local" otherwise.
126          */

127         int position = position(nsuri, this.uri);
128         if (this.pre[position].length() > 0) {
129             return(this.pre[position] + ':' + local);
130         }
131         return(local);
132     }
133
134     /**
135      * Checkpoint this stack, returning the list of all namespaces added since
136      * the last <code>commit()</code> or <code>pop(...)</code> call.
137      */

138     public String JavaDoc[][] commit() {
139         int size = this.depth - this.last;
140         String JavaDoc result[][] = new String JavaDoc[size][2];
141         int k = 0;
142         for (int x = this.last; x < this.depth; x++) {
143             result[k][NAMESPACE_PREFIX] = this.pre[x];
144             result[k][NAMESPACE_URI] = this.uri[x];
145             k++;
146         }
147         this.last = this.depth;
148         return(result);
149     }
150
151     /**
152      * Return the namespace URI associated with the specified prefix.
153      *
154      * @throws SAXException If the prefix cannot be mapped.
155      */

156     public String JavaDoc getUri(String JavaDoc prefix)
157     throws SAXException JavaDoc {
158         return(this.uri[this.position(prefix, this.pre)]);
159     }
160
161     /**
162      * Return the namespace prefix associated with the specified URI.
163      *
164      * @throws SAXException If the URI cannot be mapped.
165      */

166     public String JavaDoc getPrefix(String JavaDoc nsuri)
167     throws SAXException JavaDoc {
168         return(this.pre[this.position(nsuri, this.uri)]);
169     }
170
171     /**
172      * Return the position of the given check <code>String</code> in the
173      * specified <code>String</code> array.
174      */

175     private int position(String JavaDoc check, String JavaDoc array[])
176     throws SAXException JavaDoc {
177         int x = this.depth;
178         while (true) {
179             if (check.equals(array[--x])) return(x);
180             if (x == 0) break;
181         }
182         throw new SAXException JavaDoc("Unable to map \"" + check + "\"");
183     }
184
185 }
186
Popular Tags