KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > xsltc > compiler > SymbolTable


1 /*
2  * Copyright 2001-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 /*
17  * $Id: SymbolTable.java,v 1.11 2004/02/16 22:25:10 minchau Exp $
18  */

19
20 package com.sun.org.apache.xalan.internal.xsltc.compiler;
21
22 import java.util.Hashtable JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodType;
27
28 /**
29  * @author Jacek Ambroziak
30  * @author Santiago Pericas-Geertsen
31  * @author Morten Jorgensen
32  */

33 final class SymbolTable {
34
35     // These hashtables are used for all stylesheets
36
private final Hashtable JavaDoc _stylesheets = new Hashtable JavaDoc();
37     private final Hashtable JavaDoc _primops = new Hashtable JavaDoc();
38
39     // These hashtables are used for some stylesheets
40
private Hashtable JavaDoc _variables = null;
41     private Hashtable JavaDoc _templates = null;
42     private Hashtable JavaDoc _attributeSets = null;
43     private Hashtable JavaDoc _aliases = null;
44     private Hashtable JavaDoc _excludedURI = null;
45     private Hashtable JavaDoc _decimalFormats = null;
46     private Hashtable JavaDoc _keys = null;
47
48     public Key getKey(QName name) {
49         if (_keys == null) return null;
50         return (Key) _keys.get(name);
51     }
52
53     public void addKey(QName name, Key key) {
54         if (_keys == null) _keys = new Hashtable JavaDoc();
55         _keys.put(name, key);
56     }
57
58     public DecimalFormatting getDecimalFormatting(QName name) {
59     if (_decimalFormats == null) return null;
60     return((DecimalFormatting)_decimalFormats.get(name));
61     }
62
63     public void addDecimalFormatting(QName name, DecimalFormatting symbols) {
64     if (_decimalFormats == null) _decimalFormats = new Hashtable JavaDoc();
65     _decimalFormats.put(name, symbols);
66     }
67
68     public Stylesheet addStylesheet(QName name, Stylesheet node) {
69     return (Stylesheet)_stylesheets.put(name, node);
70     }
71     
72     public Stylesheet lookupStylesheet(QName name) {
73     return (Stylesheet)_stylesheets.get(name);
74     }
75
76     public Template addTemplate(Template template) {
77     final QName name = template.getName();
78     if (_templates == null) _templates = new Hashtable JavaDoc();
79     return (Template)_templates.put(name, template);
80     }
81     
82     public Template lookupTemplate(QName name) {
83     if (_templates == null) return null;
84     return (Template)_templates.get(name);
85     }
86
87     public Variable addVariable(Variable variable) {
88     if (_variables == null) _variables = new Hashtable JavaDoc();
89     final String JavaDoc name = variable.getName().getStringRep();
90     return (Variable)_variables.put(name, variable);
91     }
92     
93     public Param addParam(Param parameter) {
94     if (_variables == null) _variables = new Hashtable JavaDoc();
95     final String JavaDoc name = parameter.getName().getStringRep();
96     return (Param)_variables.put(name, parameter);
97     }
98     
99     public Variable lookupVariable(QName qname) {
100     if (_variables == null) return null;
101     final String JavaDoc name = qname.getStringRep();
102     final Object JavaDoc obj = _variables.get(name);
103     return obj instanceof Variable ? (Variable)obj : null;
104     }
105
106     public Param lookupParam(QName qname) {
107     if (_variables == null) return null;
108     final String JavaDoc name = qname.getStringRep();
109     final Object JavaDoc obj = _variables.get(name);
110     return obj instanceof Param ? (Param)obj : null;
111     }
112     
113     public SyntaxTreeNode lookupName(QName qname) {
114     if (_variables == null) return null;
115     final String JavaDoc name = qname.getStringRep();
116     return (SyntaxTreeNode)_variables.get(name);
117     }
118
119     public AttributeSet addAttributeSet(AttributeSet atts) {
120     if (_attributeSets == null) _attributeSets = new Hashtable JavaDoc();
121     return (AttributeSet)_attributeSets.put(atts.getName(), atts);
122     }
123
124     public AttributeSet lookupAttributeSet(QName name) {
125     if (_attributeSets == null) return null;
126     return (AttributeSet)_attributeSets.get(name);
127     }
128
129     /**
130      * Add a primitive operator or function to the symbol table. To avoid
131      * name clashes with user-defined names, the prefix <tt>PrimopPrefix</tt>
132      * is prepended.
133      */

134     public void addPrimop(String JavaDoc name, MethodType mtype) {
135     Vector JavaDoc methods = (Vector JavaDoc)_primops.get(name);
136     if (methods == null) {
137         _primops.put(name, methods = new Vector JavaDoc());
138     }
139     methods.addElement(mtype);
140     }
141     
142     /**
143      * Lookup a primitive operator or function in the symbol table by
144      * prepending the prefix <tt>PrimopPrefix</tt>.
145      */

146     public Vector JavaDoc lookupPrimop(String JavaDoc name) {
147     return (Vector JavaDoc)_primops.get(name);
148     }
149
150     /**
151      * This is used for xsl:attribute elements that have a "namespace"
152      * attribute that is currently not defined using xmlns:
153      */

154     private int _nsCounter = 0;
155
156     public String JavaDoc generateNamespacePrefix() {
157     return(new String JavaDoc("ns"+(_nsCounter++)));
158     }
159
160     /**
161      * Use a namespace prefix to lookup a namespace URI
162      */

163     private SyntaxTreeNode _current = null;
164
165     public void setCurrentNode(SyntaxTreeNode node) {
166     _current = node;
167     }
168
169     public String JavaDoc lookupNamespace(String JavaDoc prefix) {
170     if (_current == null) return(Constants.EMPTYSTRING);
171     return(_current.lookupNamespace(prefix));
172     }
173
174     /**
175      * Adds an alias for a namespace prefix
176      */

177     public void addPrefixAlias(String JavaDoc prefix, String JavaDoc alias) {
178     if (_aliases == null) _aliases = new Hashtable JavaDoc();
179     _aliases.put(prefix,alias);
180     }
181
182     /**
183      * Retrieves any alias for a given namespace prefix
184      */

185     public String JavaDoc lookupPrefixAlias(String JavaDoc prefix) {
186     if (_aliases == null) return null;
187     return (String JavaDoc)_aliases.get(prefix);
188     }
189
190     /**
191      * Register a namespace URI so that it will not be declared in the output
192      * unless it is actually referenced in the output.
193      */

194     public void excludeURI(String JavaDoc uri) {
195     // The null-namespace cannot be excluded
196
if (uri == null) return;
197
198     // Create new hashtable of exlcuded URIs if none exists
199
if (_excludedURI == null) _excludedURI = new Hashtable JavaDoc();
200
201     // Register the namespace URI
202
Integer JavaDoc refcnt = (Integer JavaDoc)_excludedURI.get(uri);
203     if (refcnt == null)
204         refcnt = new Integer JavaDoc(1);
205     else
206         refcnt = new Integer JavaDoc(refcnt.intValue() + 1);
207     _excludedURI.put(uri,refcnt);
208     }
209
210     /**
211      * Exclude a series of namespaces given by a list of whitespace
212      * separated namespace prefixes.
213      */

214     public void excludeNamespaces(String JavaDoc prefixes) {
215     if (prefixes != null) {
216         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(prefixes);
217         while (tokens.hasMoreTokens()) {
218         final String JavaDoc prefix = tokens.nextToken();
219         final String JavaDoc uri;
220         if (prefix.equals("#default"))
221             uri = lookupNamespace(Constants.EMPTYSTRING);
222         else
223             uri = lookupNamespace(prefix);
224         if (uri != null) excludeURI(uri);
225         }
226     }
227     }
228
229     /**
230      * Check if a namespace should not be declared in the output (unless used)
231      */

232     public boolean isExcludedNamespace(String JavaDoc uri) {
233     if (uri != null && _excludedURI != null) {
234         final Integer JavaDoc refcnt = (Integer JavaDoc)_excludedURI.get(uri);
235         return (refcnt != null && refcnt.intValue() > 0);
236     }
237     return false;
238     }
239
240     /**
241      * Turn of namespace declaration exclusion
242      */

243     public void unExcludeNamespaces(String JavaDoc prefixes) {
244     if (_excludedURI == null) return;
245     if (prefixes != null) {
246         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(prefixes);
247         while (tokens.hasMoreTokens()) {
248         final String JavaDoc prefix = tokens.nextToken();
249         final String JavaDoc uri;
250         if (prefix.equals("#default"))
251             uri = lookupNamespace(Constants.EMPTYSTRING);
252         else
253             uri = lookupNamespace(prefix);
254         Integer JavaDoc refcnt = (Integer JavaDoc)_excludedURI.get(uri);
255         if (refcnt != null)
256             _excludedURI.put(uri, new Integer JavaDoc(refcnt.intValue() - 1));
257         }
258     }
259     }
260
261 }
262
263
Popular Tags