KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > arooa > ArooaContext


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

58 package org.oddjob.arooa;
59
60 import java.util.ArrayList JavaDoc;
61 import java.util.HashMap JavaDoc;
62 import java.util.List JavaDoc;
63 import java.util.Map JavaDoc;
64
65 import org.xml.sax.Locator JavaDoc;
66
67 /**
68  * Context information for the processing.
69  * <p>
70  * Based on the original by <b>Costin Manolache</b>.
71  */

72 public class ArooaContext {
73
74     /**
75      * Locator for the configuration file parser.
76      * Used for giving locations of errors etc.
77      */

78     private Locator JavaDoc locator;
79
80     /** Keeps track of prefix -> uri mapping during parsing */
81     private Map JavaDoc prefixMapping = new HashMap JavaDoc();
82
83     private final Map JavaDoc values = new HashMap JavaDoc();
84     
85     private final ArooaContext parent;
86     
87     /**
88      * constructor
89      */

90     public ArooaContext() {
91         parent = null;
92     }
93
94     public ArooaContext(ArooaContext parent) {
95         this.parent = parent;
96     }
97     
98     public void set(String JavaDoc name, Object JavaDoc value) {
99         values.put(name, value);
100     }
101     
102     public Object JavaDoc get(String JavaDoc name) {
103         Object JavaDoc value = values.get(name);
104         if (value != null) {
105             return value;
106         }
107         if (parent != null) {
108             return parent.get(name);
109         }
110         return null;
111     }
112     
113     public Object JavaDoc getLocal(String JavaDoc name) {
114         return values.get(name);
115     }
116
117     public ArooaContext getParent() {
118         return parent;
119     }
120     
121     /**
122      * access the locator
123      * @return locator
124      */

125     public Locator JavaDoc getLocator() {
126         Locator JavaDoc locator = this.locator;
127         if (locator != null) {
128             return locator;
129         }
130         if (parent != null) {
131             return parent.getLocator();
132         }
133         return null;
134     }
135
136     /**
137      * sets the locator
138      * @param locator locator
139      */

140     public void setLocator(Locator JavaDoc locator) {
141         this.locator = locator;
142     }
143
144     /**
145      * Called during parsing, stores the prefix to uri mapping.
146      *
147      * @param prefix a namespace prefix
148      * @param uri a namespace uri
149      */

150     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) {
151         List JavaDoc list = (List JavaDoc) prefixMapping.get(prefix);
152         if (list == null) {
153             list = new ArrayList JavaDoc();
154             prefixMapping.put(prefix, list);
155         }
156         list.add(uri);
157     }
158
159     /**
160      * End of prefix to uri mapping.
161      *
162      * @param prefix the namespace prefix
163      */

164     public void endPrefixMapping(String JavaDoc prefix) {
165         List JavaDoc list = (List JavaDoc) prefixMapping.get(prefix);
166         if (list == null || list.size() == 0) {
167             return; // Should not happen
168
}
169         list.remove(list.size() - 1);
170     }
171
172     /**
173      * prefix to namespace uri mapping
174      *
175      * @param prefix the prefix to map
176      * @return the uri for this prefix, null if not present
177      */

178     public String JavaDoc getPrefixMapping(String JavaDoc prefix) {
179         List JavaDoc list = (List JavaDoc) prefixMapping.get(prefix);
180         if (list == null || list.size() == 0) {
181             return null;
182         }
183         return (String JavaDoc) list.get(list.size() - 1);
184     }
185     
186 }
187
188
189
Popular Tags