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. package com.sun.org.apache.xalan.internal.xsltc.compiler;
  20. import java.util.Hashtable;
  21. import java.util.StringTokenizer;
  22. import java.util.Vector;
  23. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodType;
  24. /**
  25. * @author Jacek Ambroziak
  26. * @author Santiago Pericas-Geertsen
  27. * @author Morten Jorgensen
  28. */
  29. final class SymbolTable {
  30. // These hashtables are used for all stylesheets
  31. private final Hashtable _stylesheets = new Hashtable();
  32. private final Hashtable _primops = new Hashtable();
  33. // These hashtables are used for some stylesheets
  34. private Hashtable _variables = null;
  35. private Hashtable _templates = null;
  36. private Hashtable _attributeSets = null;
  37. private Hashtable _aliases = null;
  38. private Hashtable _excludedURI = null;
  39. private Hashtable _decimalFormats = null;
  40. public DecimalFormatting getDecimalFormatting(QName name) {
  41. if (_decimalFormats == null) return null;
  42. return((DecimalFormatting)_decimalFormats.get(name));
  43. }
  44. public void addDecimalFormatting(QName name, DecimalFormatting symbols) {
  45. if (_decimalFormats == null) _decimalFormats = new Hashtable();
  46. _decimalFormats.put(name, symbols);
  47. }
  48. public Stylesheet addStylesheet(QName name, Stylesheet node) {
  49. return (Stylesheet)_stylesheets.put(name, node);
  50. }
  51. public Stylesheet lookupStylesheet(QName name) {
  52. return (Stylesheet)_stylesheets.get(name);
  53. }
  54. public Template addTemplate(Template template) {
  55. final QName name = template.getName();
  56. if (_templates == null) _templates = new Hashtable();
  57. return (Template)_templates.put(name, template);
  58. }
  59. public Template lookupTemplate(QName name) {
  60. if (_templates == null) return null;
  61. return (Template)_templates.get(name);
  62. }
  63. public Variable addVariable(Variable variable) {
  64. if (_variables == null) _variables = new Hashtable();
  65. final String name = variable.getName().getStringRep();
  66. return (Variable)_variables.put(name, variable);
  67. }
  68. public Param addParam(Param parameter) {
  69. if (_variables == null) _variables = new Hashtable();
  70. final String name = parameter.getName().getStringRep();
  71. return (Param)_variables.put(name, parameter);
  72. }
  73. public Variable lookupVariable(QName qname) {
  74. if (_variables == null) return null;
  75. final String name = qname.getStringRep();
  76. final Object obj = _variables.get(name);
  77. return obj instanceof Variable ? (Variable)obj : null;
  78. }
  79. public Param lookupParam(QName qname) {
  80. if (_variables == null) return null;
  81. final String name = qname.getStringRep();
  82. final Object obj = _variables.get(name);
  83. return obj instanceof Param ? (Param)obj : null;
  84. }
  85. public SyntaxTreeNode lookupName(QName qname) {
  86. if (_variables == null) return null;
  87. final String name = qname.getStringRep();
  88. return (SyntaxTreeNode)_variables.get(name);
  89. }
  90. public AttributeSet addAttributeSet(AttributeSet atts) {
  91. if (_attributeSets == null) _attributeSets = new Hashtable();
  92. return (AttributeSet)_attributeSets.put(atts.getName(), atts);
  93. }
  94. public AttributeSet lookupAttributeSet(QName name) {
  95. if (_attributeSets == null) return null;
  96. return (AttributeSet)_attributeSets.get(name);
  97. }
  98. /**
  99. * Add a primitive operator or function to the symbol table. To avoid
  100. * name clashes with user-defined names, the prefix <tt>PrimopPrefix</tt>
  101. * is prepended.
  102. */
  103. public void addPrimop(String name, MethodType mtype) {
  104. Vector methods = (Vector)_primops.get(name);
  105. if (methods == null) {
  106. _primops.put(name, methods = new Vector());
  107. }
  108. methods.addElement(mtype);
  109. }
  110. /**
  111. * Lookup a primitive operator or function in the symbol table by
  112. * prepending the prefix <tt>PrimopPrefix</tt>.
  113. */
  114. public Vector lookupPrimop(String name) {
  115. return (Vector)_primops.get(name);
  116. }
  117. /**
  118. * This is used for xsl:attribute elements that have a "namespace"
  119. * attribute that is currently not defined using xmlns:
  120. */
  121. private int _nsCounter = 0;
  122. public String generateNamespacePrefix() {
  123. return(new String("ns"+(_nsCounter++)));
  124. }
  125. /**
  126. * Use a namespace prefix to lookup a namespace URI
  127. */
  128. private SyntaxTreeNode _current = null;
  129. public void setCurrentNode(SyntaxTreeNode node) {
  130. _current = node;
  131. }
  132. public String lookupNamespace(String prefix) {
  133. if (_current == null) return(Constants.EMPTYSTRING);
  134. return(_current.lookupNamespace(prefix));
  135. }
  136. /**
  137. * Adds an alias for a namespace prefix
  138. */
  139. public void addPrefixAlias(String prefix, String alias) {
  140. if (_aliases == null) _aliases = new Hashtable();
  141. _aliases.put(prefix,alias);
  142. }
  143. /**
  144. * Retrieves any alias for a given namespace prefix
  145. */
  146. public String lookupPrefixAlias(String prefix) {
  147. if (_aliases == null) return null;
  148. return (String)_aliases.get(prefix);
  149. }
  150. /**
  151. * Register a namespace URI so that it will not be declared in the output
  152. * unless it is actually referenced in the output.
  153. */
  154. public void excludeURI(String uri) {
  155. // The null-namespace cannot be excluded
  156. if (uri == null) return;
  157. // Create new hashtable of exlcuded URIs if none exists
  158. if (_excludedURI == null) _excludedURI = new Hashtable();
  159. // Register the namespace URI
  160. Integer refcnt = (Integer)_excludedURI.get(uri);
  161. if (refcnt == null)
  162. refcnt = new Integer(1);
  163. else
  164. refcnt = new Integer(refcnt.intValue() + 1);
  165. _excludedURI.put(uri,refcnt);
  166. }
  167. /**
  168. * Exclude a series of namespaces given by a list of whitespace
  169. * separated namespace prefixes.
  170. */
  171. public void excludeNamespaces(String prefixes) {
  172. if (prefixes != null) {
  173. StringTokenizer tokens = new StringTokenizer(prefixes);
  174. while (tokens.hasMoreTokens()) {
  175. final String prefix = tokens.nextToken();
  176. final String uri;
  177. if (prefix.equals("#default"))
  178. uri = lookupNamespace(Constants.EMPTYSTRING);
  179. else
  180. uri = lookupNamespace(prefix);
  181. if (uri != null) excludeURI(uri);
  182. }
  183. }
  184. }
  185. /**
  186. * Check if a namespace should not be declared in the output (unless used)
  187. */
  188. public boolean isExcludedNamespace(String uri) {
  189. if (uri != null && _excludedURI != null) {
  190. final Integer refcnt = (Integer)_excludedURI.get(uri);
  191. return (refcnt != null && refcnt.intValue() > 0);
  192. }
  193. return false;
  194. }
  195. /**
  196. * Turn of namespace declaration exclusion
  197. */
  198. public void unExcludeNamespaces(String prefixes) {
  199. if (_excludedURI == null) return;
  200. if (prefixes != null) {
  201. StringTokenizer tokens = new StringTokenizer(prefixes);
  202. while (tokens.hasMoreTokens()) {
  203. final String prefix = tokens.nextToken();
  204. final String uri;
  205. if (prefix.equals("#default"))
  206. uri = lookupNamespace(Constants.EMPTYSTRING);
  207. else
  208. uri = lookupNamespace(prefix);
  209. Integer refcnt = (Integer)_excludedURI.get(uri);
  210. if (refcnt != null)
  211. _excludedURI.put(uri, new Integer(refcnt.intValue() - 1));
  212. }
  213. }
  214. }
  215. }