KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > 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 org.apache.xalan.xsltc.compiler;
21
22 import java.util.Hashtable JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 import org.apache.xalan.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
47     public DecimalFormatting getDecimalFormatting(QName name) {
48     if (_decimalFormats == null) return null;
49     return((DecimalFormatting)_decimalFormats.get(name));
50     }
51
52     public void addDecimalFormatting(QName name, DecimalFormatting symbols) {
53     if (_decimalFormats == null) _decimalFormats = new Hashtable JavaDoc();
54     _decimalFormats.put(name, symbols);
55     }
56
57     public Stylesheet addStylesheet(QName name, Stylesheet node) {
58     return (Stylesheet)_stylesheets.put(name, node);
59     }
60     
61     public Stylesheet lookupStylesheet(QName name) {
62     return (Stylesheet)_stylesheets.get(name);
63     }
64
65     public Template addTemplate(Template template) {
66     final QName name = template.getName();
67     if (_templates == null) _templates = new Hashtable JavaDoc();
68     return (Template)_templates.put(name, template);
69     }
70     
71     public Template lookupTemplate(QName name) {
72     if (_templates == null) return null;
73     return (Template)_templates.get(name);
74     }
75
76     public Variable addVariable(Variable variable) {
77     if (_variables == null) _variables = new Hashtable JavaDoc();
78     final String JavaDoc name = variable.getName().getStringRep();
79     return (Variable)_variables.put(name, variable);
80     }
81     
82     public Param addParam(Param parameter) {
83     if (_variables == null) _variables = new Hashtable JavaDoc();
84     final String JavaDoc name = parameter.getName().getStringRep();
85     return (Param)_variables.put(name, parameter);
86     }
87     
88     public Variable lookupVariable(QName qname) {
89     if (_variables == null) return null;
90     final String JavaDoc name = qname.getStringRep();
91     final Object JavaDoc obj = _variables.get(name);
92     return obj instanceof Variable ? (Variable)obj : null;
93     }
94
95     public Param lookupParam(QName qname) {
96     if (_variables == null) return null;
97     final String JavaDoc name = qname.getStringRep();
98     final Object JavaDoc obj = _variables.get(name);
99     return obj instanceof Param ? (Param)obj : null;
100     }
101     
102     public SyntaxTreeNode lookupName(QName qname) {
103     if (_variables == null) return null;
104     final String JavaDoc name = qname.getStringRep();
105     return (SyntaxTreeNode)_variables.get(name);
106     }
107
108     public AttributeSet addAttributeSet(AttributeSet atts) {
109     if (_attributeSets == null) _attributeSets = new Hashtable JavaDoc();
110     return (AttributeSet)_attributeSets.put(atts.getName(), atts);
111     }
112
113     public AttributeSet lookupAttributeSet(QName name) {
114     if (_attributeSets == null) return null;
115     return (AttributeSet)_attributeSets.get(name);
116     }
117
118     /**
119      * Add a primitive operator or function to the symbol table. To avoid
120      * name clashes with user-defined names, the prefix <tt>PrimopPrefix</tt>
121      * is prepended.
122      */

123     public void addPrimop(String JavaDoc name, MethodType mtype) {
124     Vector JavaDoc methods = (Vector JavaDoc)_primops.get(name);
125     if (methods == null) {
126         _primops.put(name, methods = new Vector JavaDoc());
127     }
128     methods.addElement(mtype);
129     }
130     
131     /**
132      * Lookup a primitive operator or function in the symbol table by
133      * prepending the prefix <tt>PrimopPrefix</tt>.
134      */

135     public Vector JavaDoc lookupPrimop(String JavaDoc name) {
136     return (Vector JavaDoc)_primops.get(name);
137     }
138
139     /**
140      * This is used for xsl:attribute elements that have a "namespace"
141      * attribute that is currently not defined using xmlns:
142      */

143     private int _nsCounter = 0;
144
145     public String JavaDoc generateNamespacePrefix() {
146     return(new String JavaDoc("ns"+(_nsCounter++)));
147     }
148
149     /**
150      * Use a namespace prefix to lookup a namespace URI
151      */

152     private SyntaxTreeNode _current = null;
153
154     public void setCurrentNode(SyntaxTreeNode node) {
155     _current = node;
156     }
157
158     public String JavaDoc lookupNamespace(String JavaDoc prefix) {
159     if (_current == null) return(Constants.EMPTYSTRING);
160     return(_current.lookupNamespace(prefix));
161     }
162
163     /**
164      * Adds an alias for a namespace prefix
165      */

166     public void addPrefixAlias(String JavaDoc prefix, String JavaDoc alias) {
167     if (_aliases == null) _aliases = new Hashtable JavaDoc();
168     _aliases.put(prefix,alias);
169     }
170
171     /**
172      * Retrieves any alias for a given namespace prefix
173      */

174     public String JavaDoc lookupPrefixAlias(String JavaDoc prefix) {
175     if (_aliases == null) return null;
176     return (String JavaDoc)_aliases.get(prefix);
177     }
178
179     /**
180      * Register a namespace URI so that it will not be declared in the output
181      * unless it is actually referenced in the output.
182      */

183     public void excludeURI(String JavaDoc uri) {
184     // The null-namespace cannot be excluded
185
if (uri == null) return;
186
187     // Create new hashtable of exlcuded URIs if none exists
188
if (_excludedURI == null) _excludedURI = new Hashtable JavaDoc();
189
190     // Register the namespace URI
191
Integer JavaDoc refcnt = (Integer JavaDoc)_excludedURI.get(uri);
192     if (refcnt == null)
193         refcnt = new Integer JavaDoc(1);
194     else
195         refcnt = new Integer JavaDoc(refcnt.intValue() + 1);
196     _excludedURI.put(uri,refcnt);
197     }
198
199     /**
200      * Exclude a series of namespaces given by a list of whitespace
201      * separated namespace prefixes.
202      */

203     public void excludeNamespaces(String JavaDoc prefixes) {
204     if (prefixes != null) {
205         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(prefixes);
206         while (tokens.hasMoreTokens()) {
207         final String JavaDoc prefix = tokens.nextToken();
208         final String JavaDoc uri;
209         if (prefix.equals("#default"))
210             uri = lookupNamespace(Constants.EMPTYSTRING);
211         else
212             uri = lookupNamespace(prefix);
213         if (uri != null) excludeURI(uri);
214         }
215     }
216     }
217
218     /**
219      * Check if a namespace should not be declared in the output (unless used)
220      */

221     public boolean isExcludedNamespace(String JavaDoc uri) {
222     if (uri != null && _excludedURI != null) {
223         final Integer JavaDoc refcnt = (Integer JavaDoc)_excludedURI.get(uri);
224         return (refcnt != null && refcnt.intValue() > 0);
225     }
226     return false;
227     }
228
229     /**
230      * Turn of namespace declaration exclusion
231      */

232     public void unExcludeNamespaces(String JavaDoc prefixes) {
233     if (_excludedURI == null) return;
234     if (prefixes != null) {
235         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(prefixes);
236         while (tokens.hasMoreTokens()) {
237         final String JavaDoc prefix = tokens.nextToken();
238         final String JavaDoc uri;
239         if (prefix.equals("#default"))
240             uri = lookupNamespace(Constants.EMPTYSTRING);
241         else
242             uri = lookupNamespace(prefix);
243         Integer JavaDoc refcnt = (Integer JavaDoc)_excludedURI.get(uri);
244         if (refcnt != null)
245             _excludedURI.put(uri, new Integer JavaDoc(refcnt.intValue() - 1));
246         }
247     }
248     }
249
250 }
251
252
Popular Tags