📄 literalelement.java
字号:
/* * Copyright 2001-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * $Id: LiteralElement.java,v 1.2.4.1 2005/09/13 12:38:33 pvedula Exp $ */package com.sun.org.apache.xalan.internal.xsltc.compiler;import java.util.Enumeration;import java.util.Hashtable;import java.util.Vector;import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;import com.sun.org.apache.bcel.internal.generic.InstructionList;import com.sun.org.apache.bcel.internal.generic.PUSH;import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;import com.sun.org.apache.xml.internal.serializer.ElemDesc;import com.sun.org.apache.xml.internal.serializer.ToHTMLStream;/** * @author Jacek Ambroziak * @author Santiago Pericas-Geertsen * @author Morten Jorgensen */final class LiteralElement extends Instruction { private String _name; private LiteralElement _literalElemParent = null; private Vector _attributeElements = null; private Hashtable _accessedPrefixes = null; // True if all attributes of this LRE are unique, i.e. they all have // different names. This flag is set to false if some attribute // names are not known at compile time. private boolean _allAttributesUnique = false; private final static String XMLNS_STRING = "xmlns"; /** * Returns the QName for this literal element */ public QName getName() { return _qname; } /** * Displays the contents of this literal element */ public void display(int indent) { indent(indent); Util.println("LiteralElement name = " + _name); displayContents(indent + IndentIncrement); } /** * Returns the namespace URI for which a prefix is pointing to */ private String accessedNamespace(String prefix) { if (_literalElemParent != null) { String result = _literalElemParent.accessedNamespace(prefix); if (result != null) { return result; } } return _accessedPrefixes != null ? (String) _accessedPrefixes.get(prefix) : null; } /** * Method used to keep track of what namespaces that are references by * this literal element and its attributes. The output must contain a * definition for each namespace, so we stuff them in a hashtable. */ public void registerNamespace(String prefix, String uri, SymbolTable stable, boolean declared) { // Check if the parent has a declaration for this namespace if (_literalElemParent != null) { final String parentUri = _literalElemParent.accessedNamespace(prefix); if (parentUri != null && parentUri.equals(uri)) { return; } } // Check if we have any declared namesaces if (_accessedPrefixes == null) { _accessedPrefixes = new Hashtable(); } else { if (!declared) { // Check if this node has a declaration for this namespace final String old = (String)_accessedPrefixes.get(prefix); if (old != null) { if (old.equals(uri)) return; else prefix = stable.generateNamespacePrefix(); } } } if (!prefix.equals("xml")) { _accessedPrefixes.put(prefix,uri); } } /** * Translates the prefix of a QName according to the rules set in * the attributes of xsl:stylesheet. Also registers a QName to assure * that the output element contains the necessary namespace declarations. */ private String translateQName(QName qname, SymbolTable stable) { // Break up the QName and get prefix:localname strings String localname = qname.getLocalPart(); String prefix = qname.getPrefix(); // Treat default namespace as "" and not null if (prefix == null) prefix = Constants.EMPTYSTRING; else if (prefix.equals(XMLNS_STRING)) return(XMLNS_STRING); // Check if we must translate the prefix final String alternative = stable.lookupPrefixAlias(prefix); if (alternative != null) { stable.excludeNamespaces(prefix); prefix = alternative; } // Get the namespace this prefix refers to String uri = lookupNamespace(prefix); if (uri == null) return(localname); // Register the namespace as accessed registerNamespace(prefix, uri, stable, false); // Construct the new name for the element (may be unchanged) if (prefix != Constants.EMPTYSTRING) return(prefix+":"+localname); else return(localname); } /** * Add an attribute to this element */ public void addAttribute(SyntaxTreeNode attribute) { if (_attributeElements == null) { _attributeElements = new Vector(2); } _attributeElements.add(attribute); } /** * Set the first attribute of this element */ public void setFirstAttribute(SyntaxTreeNode attribute) { if (_attributeElements == null) { _attributeElements = new Vector(2); } _attributeElements.insertElementAt(attribute,0); } /** * Type-check the contents of this element. The element itself does not * need any type checking as it leaves nothign on the JVM's stack. */ public Type typeCheck(SymbolTable stable) throws TypeCheckError { // Type-check all attributes if (_attributeElements != null) { final int count = _attributeElements.size(); for (int i = 0; i < count; i++) { SyntaxTreeNode node = (SyntaxTreeNode)_attributeElements.elementAt(i); node.typeCheck(stable); } } typeCheckContents(stable); return Type.Void; } /** * This method starts at a given node, traverses all namespace mappings, * and assembles a list of all prefixes that (for the given node) maps * to _ANY_ namespace URI. Used by literal result elements to determine */ public Enumeration getNamespaceScope(SyntaxTreeNode node) { Hashtable all = new Hashtable(); while (node != null) { Hashtable mapping = node.getPrefixMapping(); if (mapping != null) { Enumeration prefixes = mapping.keys(); while (prefixes.hasMoreElements()) { String prefix = (String)prefixes.nextElement(); if (!all.containsKey(prefix)) { all.put(prefix, mapping.get(prefix)); } } } node = node.getParent(); } return(all.keys()); } /** * Determines the final QName for the element and its attributes. * Registers all namespaces that are used by the element/attributes */ public void parseContents(Parser parser) { final SymbolTable stable = parser.getSymbolTable(); stable.setCurrentNode(this); // Check if in a literal element context SyntaxTreeNode parent = getParent(); if (parent != null && parent instanceof LiteralElement) { _literalElemParent = (LiteralElement) parent; } _name = translateQName(_qname, stable); // Process all attributes and register all namespaces they use final int count = _attributes.getLength(); for (int i = 0; i < count; i++) { final QName qname = parser.getQName(_attributes.getQName(i)); final String uri = qname.getNamespace(); final String val = _attributes.getValue(i); // Handle xsl:use-attribute-sets. Attribute sets are placed first // in the vector or attributes to make sure that later local // attributes can override an attributes in the set. if (qname == parser.getUseAttributeSets()) { if (!Util.isValidQNames(val)) { ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, val, this); parser.reportError(Constants.ERROR, err); } setFirstAttribute(new UseAttributeSets(val, parser)); } // Handle xsl:extension-element-prefixes else if (qname == parser.getExtensionElementPrefixes()) { stable.excludeNamespaces(val); } // Handle xsl:exclude-result-prefixes else if (qname == parser.getExcludeResultPrefixes()) { stable.excludeNamespaces(val); } else { // Ignore special attributes (e.g. xmlns:prefix and xmlns) final String prefix = qname.getPrefix(); if (prefix != null && prefix.equals(XMLNS_PREFIX) || prefix == null && qname.getLocalPart().equals("xmlns") || uri != null && uri.equals(XSLT_URI)) { continue; } // Handle all other literal attributes final String name = translateQName(qname, stable); LiteralAttribute attr = new LiteralAttribute(name, val, parser, this);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -