elemtemplateelement.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,705 行 · 第 1/4 页
JAVA
1,705 行
/* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xalan" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999, Lotus * Development Corporation., http://www.lotus.com. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */package org.apache.xalan.templates;import java.lang.InstantiationException;import java.io.Serializable;import java.util.Enumeration;import java.util.Vector;// Xalan importsimport org.apache.xml.utils.UnImplNode;import org.apache.xml.utils.NameSpace;import org.apache.xml.utils.PrefixResolver;import org.apache.xml.utils.QName;import org.apache.xml.utils.StringToStringTable;import org.apache.xalan.res.XSLTErrorResources;import org.apache.xalan.res.XSLMessages;import org.apache.xalan.transformer.TransformerImpl;import org.apache.xalan.transformer.ResultNameSpace;import org.apache.xalan.transformer.ResultTreeHandler;import org.apache.xpath.VariableStack;import org.apache.xpath.WhitespaceStrippingElementMatcher;import org.apache.xpath.ExpressionNode;// TRaX importsimport javax.xml.transform.Templates;import javax.xml.transform.SourceLocator;// DOM Importsimport org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.w3c.dom.DOMException;import org.w3c.dom.Document;import org.apache.xml.dtm.DTM;// SAX Importsimport org.xml.sax.Locator;import javax.xml.transform.TransformerException;import org.xml.sax.helpers.NamespaceSupport;import org.apache.xml.utils.NamespaceSupport2;/** * <meta name="usage" content="advanced"/> * An instance of this class represents an element inside * an xsl:template class. It has a single "execute" method * which is expected to perform the given action on the * result tree. * This class acts like a Element node, and implements the * Element interface, but is not a full implementation * of that interface... it only implements enough for * basic traversal of the tree. * * @see Stylesheet */public class ElemTemplateElement extends UnImplNode implements PrefixResolver, Serializable, ExpressionNode, WhitespaceStrippingElementMatcher, XSLTVisitable{ /** * Construct a template element instance. * * @param transformer The XSLT TransformerFactory. * @param stylesheetTree The owning stylesheet. * @param name The name of the element. * @param atts The element attributes. * @param lineNumber The line in the XSLT file that the element occurs on. * @param columnNumber The column index in the XSLT file that the element occurs on. */ public ElemTemplateElement(){} /** * Tell if this template is a compiled template. * * @return Boolean flag indicating whether this is a compiled template */ public boolean isCompiledTemplate() { return false; } /** * Get an integer representation of the element type. * * @return An integer representation of the element, defined in the * Constants class. * @see org.apache.xalan.templates.Constants */ public int getXSLToken() { return Constants.ELEMNAME_UNDEFINED; } /** * Return the node name. * * @return An invalid node name */ public String getNodeName() { return "Unknown XSLT Element"; } /** * For now, just return the result of getNodeName(), which * the local name. * * @return The result of getNodeName(). */ public String getLocalName() { return getNodeName(); } /** * This function will be called on top-level elements * only, just before the transform begins. * * @param transformer The XSLT TransformerFactory. * * @throws TransformerException */ public void runtimeInit(TransformerImpl transformer) throws TransformerException{} /** * Execute the element's primary function. Subclasses of this * function may recursivly execute down the element tree. * * @param transformer The XSLT TransformerFactory. * @param sourceNode The current context node. * @param mode The current mode. * * @throws TransformerException if any checked exception occurs. */ public void execute( TransformerImpl transformer) throws TransformerException{} /** * Get the owning "composed" stylesheet. This looks up the * inheritance chain until it calls getStylesheetComposed * on a Stylesheet object, which will Get the owning * aggregated stylesheet, or that stylesheet if it is aggregated. * * @return the owning "composed" stylesheet. */ public StylesheetComposed getStylesheetComposed() { return m_parentNode.getStylesheetComposed(); } /** * Get the owning stylesheet. This looks up the * inheritance chain until it calls getStylesheet * on a Stylesheet object, which will return itself. * * @return the owning stylesheet */ public Stylesheet getStylesheet() { return (null==m_parentNode) ? null : m_parentNode.getStylesheet(); } /** * Get the owning root stylesheet. This looks up the * inheritance chain until it calls StylesheetRoot * on a Stylesheet object, which will return a reference * to the root stylesheet. * * @return the owning root stylesheet */ public StylesheetRoot getStylesheetRoot() { return m_parentNode.getStylesheetRoot(); } /** * This function is called during recomposition to * control how this element is composed. */ public void recompose(StylesheetRoot root) throws TransformerException { } /** * This function is called after everything else has been * recomposed, and allows the template to set remaining * values that may be based on some other property that * depends on recomposition. */ public void compose(StylesheetRoot sroot) throws TransformerException { resolvePrefixTables(); ElemTemplateElement t = getFirstChildElem(); m_hasTextLitOnly = ((t != null) && (t.getXSLToken() == Constants.ELEMNAME_TEXTLITERALRESULT) && (t.getNextSiblingElem() == null)); StylesheetRoot.ComposeState cstate = sroot.getComposeState(); cstate.pushStackMark(); } /** * This after the template's children have been composed. */ public void endCompose(StylesheetRoot sroot) throws TransformerException { StylesheetRoot.ComposeState cstate = sroot.getComposeState(); cstate.popStackMark(); } /** * Validate that the string is an NCName. * * @param s The name in question. * @return True if the string is a valid NCName according to XML rules. * @see <a href="http://www.w3.org/TR/REC-xml-names#NT-NCName">XXX in XSLT Specification</a> */ protected boolean isValidNCName(String s) { int len = s.length(); char c = s.charAt(0); if (!(Character.isLetter(c) || (c == '_'))) return false; if (len > 0) { for (int i = 1; i < len; i++) { c = s.charAt(i); if (!(Character.isLetterOrDigit(c) || (c == '_') || (c == '-') || (c == '.'))) return false; } } return true; } /** * Throw a template element runtime error. (Note: should we throw a TransformerException instead?) * * @param msg key of the error that occured. * @param args Arguments to be used in the message */ public void error(String msg, Object[] args) { String themsg = XSLMessages.createMessage(msg, args); throw new RuntimeException(XSLMessages.createMessage( XSLTErrorResources.ER_ELEMTEMPLATEELEM_ERR, new Object[]{ themsg })); } /* * Throw an error. * * @param msg Message key for the error * */ public void error(String msg) { error(msg, null); } // Implemented DOM Element methods. /** * Add a child to the child list. * NOTE: This presumes the child did not previously have a parent. * Making that assumption makes this a less expensive operation -- but * requires that if you *do* want to reparent a node, you use removeChild() * first to remove it from its previous context. Failing to do so will * damage the tree. * * @param newChild Child to be added to child list * * @return Child just added to the child list * @throws DOMException */ public Node appendChild(Node newChild) throws DOMException { if (null == newChild) { error(XSLTErrorResources.ER_NULL_CHILD, null); //"Trying to add a null child!"); } ElemTemplateElement elem = (ElemTemplateElement) newChild; if (null == m_firstChild) { m_firstChild = elem; } else { ElemTemplateElement last = (ElemTemplateElement) getLastChild(); last.m_nextSibling = elem; } elem.m_parentNode = this; return newChild; } /** * Add a child to the child list. * NOTE: This presumes the child did not previously have a parent. * Making that assumption makes this a less expensive operation -- but * requires that if you *do* want to reparent a node, you use removeChild() * first to remove it from its previous context. Failing to do so will * damage the tree. * * @param newChild Child to be added to child list * * @return Child just added to the child list */ public ElemTemplateElement appendChild(ElemTemplateElement elem) { if (null == elem) { error(XSLTErrorResources.ER_NULL_CHILD, null); //"Trying to add a null child!"); } if (null == m_firstChild) { m_firstChild = elem; } else { ElemTemplateElement last = getLastChildElem(); last.m_nextSibling = elem; } elem.setParentElem(this); return elem; } /** * Tell if there are child nodes. * * @return True if there are child nodes */ public boolean hasChildNodes() { return (null != m_firstChild); } /** * Get the type of the node. * * @return Constant for this node type */ public short getNodeType() { return org.w3c.dom.Node.ELEMENT_NODE; } /**
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?