2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 * $Id: ElemTemplateElement.java 475981 2006-11-16 23:35:53Z minchau $
21 package org.apache.xalan.templates;
23 import java.io.Serializable;
24 import java.util.ArrayList;
25 import java.util.Enumeration;
26 import java.util.List;
28 import javax.xml.transform.SourceLocator;
29 import javax.xml.transform.TransformerException;
31 import org.apache.xalan.res.XSLMessages;
32 import org.apache.xalan.res.XSLTErrorResources;
33 import org.apache.xalan.transformer.TransformerImpl;
34 import org.apache.xml.serializer.SerializationHandler;
35 import org.apache.xml.utils.PrefixResolver;
36 import org.apache.xml.utils.UnImplNode;
37 import org.apache.xpath.ExpressionNode;
38 import org.apache.xpath.WhitespaceStrippingElementMatcher;
40 import org.w3c.dom.DOMException;
41 import org.w3c.dom.Document;
42 import org.w3c.dom.Node;
43 import org.w3c.dom.NodeList;
45 import org.xml.sax.helpers.NamespaceSupport;
48 * An instance of this class represents an element inside
49 * an xsl:template class. It has a single "execute" method
50 * which is expected to perform the given action on the
52 * This class acts like a Element node, and implements the
53 * Element interface, but is not a full implementation
54 * of that interface... it only implements enough for
55 * basic traversal of the tree.
60 public class ElemTemplateElement extends UnImplNode
61 implements PrefixResolver, Serializable, ExpressionNode,
62 WhitespaceStrippingElementMatcher, XSLTVisitable
64 static final long serialVersionUID = 4440018597841834447L;
67 * Construct a template element instance.
70 public ElemTemplateElement(){}
73 * Tell if this template is a compiled template.
75 * @return Boolean flag indicating whether this is a compiled template
77 public boolean isCompiledTemplate()
83 * Get an integer representation of the element type.
85 * @return An integer representation of the element, defined in the
87 * @see org.apache.xalan.templates.Constants
89 public int getXSLToken()
91 return Constants.ELEMNAME_UNDEFINED;
95 * Return the node name.
97 * @return An invalid node name
99 public String getNodeName()
101 return "Unknown XSLT Element";
105 * For now, just return the result of getNodeName(), which
108 * @return The result of getNodeName().
110 public String getLocalName()
113 return getNodeName();
118 * This function will be called on top-level elements
119 * only, just before the transform begins.
121 * @param transformer The XSLT TransformerFactory.
123 * @throws TransformerException
125 public void runtimeInit(TransformerImpl transformer) throws TransformerException{}
128 * Execute the element's primary function. Subclasses of this
129 * function may recursivly execute down the element tree.
131 * @param transformer The XSLT TransformerFactory.
133 * @throws TransformerException if any checked exception occurs.
136 TransformerImpl transformer)
137 throws TransformerException{}
140 * Get the owning "composed" stylesheet. This looks up the
141 * inheritance chain until it calls getStylesheetComposed
142 * on a Stylesheet object, which will Get the owning
143 * aggregated stylesheet, or that stylesheet if it is aggregated.
145 * @return the owning "composed" stylesheet.
147 public StylesheetComposed getStylesheetComposed()
149 return m_parentNode.getStylesheetComposed();
153 * Get the owning stylesheet. This looks up the
154 * inheritance chain until it calls getStylesheet
155 * on a Stylesheet object, which will return itself.
157 * @return the owning stylesheet
159 public Stylesheet getStylesheet()
161 return (null==m_parentNode) ? null : m_parentNode.getStylesheet();
165 * Get the owning root stylesheet. This looks up the
166 * inheritance chain until it calls StylesheetRoot
167 * on a Stylesheet object, which will return a reference
168 * to the root stylesheet.
170 * @return the owning root stylesheet
172 public StylesheetRoot getStylesheetRoot()
174 return m_parentNode.getStylesheetRoot();
178 * This function is called during recomposition to
179 * control how this element is composed.
181 public void recompose(StylesheetRoot root) throws TransformerException
186 * This function is called after everything else has been
187 * recomposed, and allows the template to set remaining
188 * values that may be based on some other property that
189 * depends on recomposition.
191 public void compose(StylesheetRoot sroot) throws TransformerException
193 resolvePrefixTables();
194 ElemTemplateElement t = getFirstChildElem();
195 m_hasTextLitOnly = ((t != null)
196 && (t.getXSLToken() == Constants.ELEMNAME_TEXTLITERALRESULT)
197 && (t.getNextSiblingElem() == null));
199 StylesheetRoot.ComposeState cstate = sroot.getComposeState();
200 cstate.pushStackMark();
204 * This after the template's children have been composed.
206 public void endCompose(StylesheetRoot sroot) throws TransformerException
208 StylesheetRoot.ComposeState cstate = sroot.getComposeState();
209 cstate.popStackMark();
213 * Throw a template element runtime error. (Note: should we throw a TransformerException instead?)
215 * @param msg key of the error that occured.
216 * @param args Arguments to be used in the message
218 public void error(String msg, Object[] args)
221 String themsg = XSLMessages.createMessage(msg, args);
223 throw new RuntimeException(XSLMessages.createMessage(
224 XSLTErrorResources.ER_ELEMTEMPLATEELEM_ERR,
225 new Object[]{ themsg }));
231 * @param msg Message key for the error
234 public void error(String msg)
240 // Implemented DOM Element methods.
242 * Add a child to the child list.
243 * NOTE: This presumes the child did not previously have a parent.
244 * Making that assumption makes this a less expensive operation -- but
245 * requires that if you *do* want to reparent a node, you use removeChild()
246 * first to remove it from its previous context. Failing to do so will
249 * @param newChild Child to be added to child list
251 * @return Child just added to the child list
252 * @throws DOMException
254 public Node appendChild(Node newChild) throws DOMException
257 if (null == newChild)
259 error(XSLTErrorResources.ER_NULL_CHILD, null); //"Trying to add a null child!");
262 ElemTemplateElement elem = (ElemTemplateElement) newChild;
264 if (null == m_firstChild)
270 ElemTemplateElement last = (ElemTemplateElement) getLastChild();
272 last.m_nextSibling = elem;
275 elem.m_parentNode = this;
281 * Add a child to the child list.
282 * NOTE: This presumes the child did not previously have a parent.
283 * Making that assumption makes this a less expensive operation -- but
284 * requires that if you *do* want to reparent a node, you use removeChild()
285 * first to remove it from its previous context. Failing to do so will
288 * @param elem Child to be added to child list
290 * @return Child just added to the child list
292 public ElemTemplateElement appendChild(ElemTemplateElement elem)
297 error(XSLTErrorResources.ER_NULL_CHILD, null); //"Trying to add a null child!");
300 if (null == m_firstChild)
306 ElemTemplateElement last = getLastChildElem();
308 last.m_nextSibling = elem;
311 elem.setParentElem(this);
318 * Tell if there are child nodes.
320 * @return True if there are child nodes
322 public boolean hasChildNodes()
324 return (null != m_firstChild);
328 * Get the type of the node.
330 * @return Constant for this node type
332 public short getNodeType()
334 return org.w3c.dom.Node.ELEMENT_NODE;
338 * Return the nodelist (same reference).
340 * @return The nodelist containing the child nodes (this)
342 public NodeList getChildNodes()
349 * ADDED 9/8/200 to support compilation.
350 * TODO: ***** Alternative is "removeMe() from my parent if any"
351 * ... which is less well checked, but more convenient in some cases.
352 * Given that we assume only experts are calling this class, it might
353 * be preferable. It's less DOMish, though.
355 * @param childETE The child to remove. This operation is a no-op
356 * if oldChild is not a child of this node.
358 * @return the removed child, or null if the specified
359 * node was not a child of this element.
361 public ElemTemplateElement removeChild(ElemTemplateElement childETE)
364 if (childETE == null || childETE.m_parentNode != this)
367 // Pointers to the child
368 if (childETE == m_firstChild)
369 m_firstChild = childETE.m_nextSibling;
372 ElemTemplateElement prev = childETE.getPreviousSiblingElem();
374 prev.m_nextSibling = childETE.m_nextSibling;
377 // Pointers from the child
378 childETE.m_parentNode = null;
379 childETE.m_nextSibling = null;
385 * Replace the old child with a new child.
387 * @param newChild New child to replace with
388 * @param oldChild Old child to be replaced
390 * @return The new child
392 * @throws DOMException
394 public Node replaceChild(Node newChild, Node oldChild) throws DOMException
397 if (oldChild == null || oldChild.getParentNode() != this)
400 ElemTemplateElement newChildElem = ((ElemTemplateElement) newChild);
401 ElemTemplateElement oldChildElem = ((ElemTemplateElement) oldChild);
403 // Fix up previous sibling.
404 ElemTemplateElement prev =
405 (ElemTemplateElement) oldChildElem.getPreviousSibling();
408 prev.m_nextSibling = newChildElem;
410 // Fix up parent (this)
411 if (m_firstChild == oldChildElem)
412 m_firstChild = newChildElem;
414 newChildElem.m_parentNode = this;
415 oldChildElem.m_parentNode = null;
416 newChildElem.m_nextSibling = oldChildElem.m_nextSibling;
417 oldChildElem.m_nextSibling = null;
419 // newChildElem.m_stylesheet = oldChildElem.m_stylesheet;
420 // oldChildElem.m_stylesheet = null;
425 * Unimplemented. See org.w3c.dom.Node
427 * @param newChild New child node to insert
428 * @param refChild Insert in front of this child
432 * @throws DOMException
434 public Node insertBefore(Node newChild, Node refChild) throws DOMException
438 appendChild(newChild);
442 if(newChild == refChild)
448 Node node = m_firstChild;
450 boolean foundit = false;
454 // If the newChild is already in the tree, it is first removed.
458 ((ElemTemplateElement)prev).m_nextSibling =
459 (ElemTemplateElement)node.getNextSibling();
461 m_firstChild = (ElemTemplateElement)node.getNextSibling();
462 node = node.getNextSibling();
463 continue; // prev remains the same.
469 ((ElemTemplateElement)prev).m_nextSibling = (ElemTemplateElement)newChild;
473 m_firstChild = (ElemTemplateElement)newChild;
475 ((ElemTemplateElement)newChild).m_nextSibling = (ElemTemplateElement)refChild;
476 ((ElemTemplateElement)newChild).setParentElem(this);
478 node = node.getNextSibling();
483 node = node.getNextSibling();
487 throw new DOMException(DOMException.NOT_FOUND_ERR,
488 "refChild was not found in insertBefore method!");
495 * Replace the old child with a new child.
497 * @param newChildElem New child to replace with
498 * @param oldChildElem Old child to be replaced
500 * @return The new child
502 * @throws DOMException
504 public ElemTemplateElement replaceChild(ElemTemplateElement newChildElem,
505 ElemTemplateElement oldChildElem)
508 if (oldChildElem == null || oldChildElem.getParentElem() != this)
511 // Fix up previous sibling.
512 ElemTemplateElement prev =
513 oldChildElem.getPreviousSiblingElem();
516 prev.m_nextSibling = newChildElem;
518 // Fix up parent (this)
519 if (m_firstChild == oldChildElem)
520 m_firstChild = newChildElem;
522 newChildElem.m_parentNode = this;
523 oldChildElem.m_parentNode = null;
524 newChildElem.m_nextSibling = oldChildElem.m_nextSibling;
525 oldChildElem.m_nextSibling = null;
527 // newChildElem.m_stylesheet = oldChildElem.m_stylesheet;
528 // oldChildElem.m_stylesheet = null;
533 * NodeList method: Count the immediate children of this node
535 * @return The count of children of this node
537 public int getLength()
540 // It is assumed that the getChildNodes call synchronized
541 // the children. Therefore, we can access the first child
542 // reference directly.
545 for (ElemTemplateElement node = m_firstChild; node != null;
546 node = node.m_nextSibling)
555 * NodeList method: Return the Nth immediate child of this node, or
556 * null if the index is out of bounds.
558 * @param index Index of child to find
559 * @return org.w3c.dom.Node: the child node at given index
561 public Node item(int index)
564 // It is assumed that the getChildNodes call synchronized
565 // the children. Therefore, we can access the first child
566 // reference directly.
567 ElemTemplateElement node = m_firstChild;
569 for (int i = 0; i < index && node != null; i++)
571 node = node.m_nextSibling;
578 * Get the stylesheet owner.
580 * @return The stylesheet owner
582 public Document getOwnerDocument()
584 return getStylesheet();
588 * Get the owning xsl:template element.
590 * @return The owning xsl:template element, this element if it is a xsl:template, or null if not found.
592 public ElemTemplate getOwnerXSLTemplate()
594 ElemTemplateElement el = this;
595 int type = el.getXSLToken();
596 while((null != el) && (type != Constants.ELEMNAME_TEMPLATE))
598 el = el.getParentElem();
600 type = el.getXSLToken();
602 return (ElemTemplate)el;
607 * Return the element name.
609 * @return The element name
611 public String getTagName()
613 return getNodeName();
617 * Tell if this element only has one text child, for optimization purposes.
618 * @return true of this element only has one text literal child.
620 public boolean hasTextLitOnly()
622 return m_hasTextLitOnly;
626 * Return the base identifier.
628 * @return The base identifier
630 public String getBaseIdentifier()
633 // Should this always be absolute?
634 return this.getSystemId();
637 /** line number where the current document event ends.
639 private int m_lineNumber;
641 /** line number where the current document event ends.
643 private int m_endLineNumber;
646 * Return the line number where the current document event ends.
647 * Note that this is the line position of the first character
648 * after the text associated with the document event.
649 * @return The line number, or -1 if none is available.
650 * @see #getColumnNumber
652 public int getEndLineNumber()
654 return m_endLineNumber;
658 * Return the line number where the current document event ends.
659 * Note that this is the line position of the first character
660 * after the text associated with the document event.
661 * @return The line number, or -1 if none is available.
662 * @see #getColumnNumber
664 public int getLineNumber()
669 /** the column number where the current document event ends.
671 private int m_columnNumber;
673 /** the column number where the current document event ends.
675 private int m_endColumnNumber;
678 * Return the column number where the current document event ends.
679 * Note that this is the column number of the first
680 * character after the text associated with the document
681 * event. The first column in a line is position 1.
682 * @return The column number, or -1 if none is available.
683 * @see #getLineNumber
685 public int getEndColumnNumber()
687 return m_endColumnNumber;
691 * Return the column number where the current document event ends.
692 * Note that this is the column number of the first
693 * character after the text associated with the document
694 * event. The first column in a line is position 1.
695 * @return The column number, or -1 if none is available.
696 * @see #getLineNumber
698 public int getColumnNumber()
700 return m_columnNumber;
704 * Return the public identifier for the current document event.
705 * <p>This will be the public identifier
706 * @return A string containing the public identifier, or
707 * null if none is available.
710 public String getPublicId()
712 return (null != m_parentNode) ? m_parentNode.getPublicId() : null;
716 * Return the system identifier for the current document event.
718 * <p>If the system identifier is a URL, the parser must resolve it
719 * fully before passing it to the application.</p>
721 * @return A string containing the system identifier, or null
722 * if none is available.
725 public String getSystemId()
727 Stylesheet sheet=getStylesheet();
728 return (sheet==null) ? null : sheet.getHref();
732 * Set the location information for this element.
734 * @param locator Source Locator with location information for this element
736 public void setLocaterInfo(SourceLocator locator)
738 m_lineNumber = locator.getLineNumber();
739 m_columnNumber = locator.getColumnNumber();
743 * Set the end location information for this element.
745 * @param locator Source Locator with location information for this element
747 public void setEndLocaterInfo(SourceLocator locator)
749 m_endLineNumber = locator.getLineNumber();
750 m_endColumnNumber = locator.getColumnNumber();
754 * Tell if this element has the default space handling
755 * turned off or on according to the xml:space attribute.
758 private boolean m_defaultSpace = true;
761 * Tell if this element only has one text child, for optimization purposes.
764 private boolean m_hasTextLitOnly = false;
767 * Tell if this element only has one text child, for optimization purposes.
770 protected boolean m_hasVariableDecl = false;
772 public boolean hasVariableDecl()
774 return m_hasVariableDecl;
778 * Set the "xml:space" attribute.
779 * A text node is preserved if an ancestor element of the text node
780 * has an xml:space attribute with a value of preserve, and
781 * no closer ancestor element has xml:space with a value of default.
782 * @see <a href="http://www.w3.org/TR/xslt#strip">strip in XSLT Specification</a>
783 * @see <a href="http://www.w3.org/TR/xslt#section-Creating-Text">section-Creating-Text in XSLT Specification</a>
785 * @param v Enumerated value, either Constants.ATTRVAL_PRESERVE
786 * or Constants.ATTRVAL_STRIP.
788 public void setXmlSpace(int v)
790 m_defaultSpace = ((Constants.ATTRVAL_STRIP == v) ? true : false);
794 * Get the "xml:space" attribute.
795 * A text node is preserved if an ancestor element of the text node
796 * has an xml:space attribute with a value of preserve, and
797 * no closer ancestor element has xml:space with a value of default.
798 * @see <a href="http://www.w3.org/TR/xslt#strip">strip in XSLT Specification</a>
799 * @see <a href="http://www.w3.org/TR/xslt#section-Creating-Text">section-Creating-Text in XSLT Specification</a>
801 * @return The value of the xml:space attribute
803 public boolean getXmlSpace()
805 return m_defaultSpace;
809 * The list of namespace declarations for this element only.
812 private List m_declaredPrefixes;
815 * Return a table that contains all prefixes available
816 * within this element context.
818 * @return Vector containing the prefixes available within this
821 public List getDeclaredPrefixes()
823 return m_declaredPrefixes;
827 * From the SAX2 helper class, set the namespace table for
828 * this element. Take care to call resolveInheritedNamespaceDecls.
829 * after all namespace declarations have been added.
831 * @param nsSupport non-null reference to NamespaceSupport from
832 * the ContentHandler.
834 * @throws TransformerException
836 public void setPrefixes(NamespaceSupport nsSupport) throws TransformerException
838 setPrefixes(nsSupport, false);
842 * Copy the namespace declarations from the NamespaceSupport object.
843 * Take care to call resolveInheritedNamespaceDecls.
844 * after all namespace declarations have been added.
846 * @param nsSupport non-null reference to NamespaceSupport from
847 * the ContentHandler.
848 * @param excludeXSLDecl true if XSLT namespaces should be ignored.
850 * @throws TransformerException
852 public void setPrefixes(NamespaceSupport nsSupport, boolean excludeXSLDecl)
853 throws TransformerException
856 Enumeration decls = nsSupport.getDeclaredPrefixes();
858 while (decls.hasMoreElements())
860 String prefix = (String) decls.nextElement();
862 if (null == m_declaredPrefixes)
863 m_declaredPrefixes = new ArrayList();
865 String uri = nsSupport.getURI(prefix);
867 if (excludeXSLDecl && uri.equals(Constants.S_XSLNAMESPACEURL))
870 // System.out.println("setPrefixes - "+prefix+", "+uri);
871 XMLNSDecl decl = new XMLNSDecl(prefix, uri, false);
873 m_declaredPrefixes.add(decl);
878 * Fullfill the PrefixResolver interface. Calling this for this class
879 * will throw an error.
881 * @param prefix The prefix to look up, which may be an empty string ("")
882 * for the default Namespace.
883 * @param context The node context from which to look up the URI.
885 * @return null if the error listener does not choose to throw an exception.
887 public String getNamespaceForPrefix(String prefix, org.w3c.dom.Node context)
889 this.error(XSLTErrorResources.ER_CANT_RESOLVE_NSPREFIX, null);
895 * Given a namespace, get the corrisponding prefix.
896 * 9/15/00: This had been iteratively examining the m_declaredPrefixes
897 * field for this node and its parents. That makes life difficult for
898 * the compilation experiment, which doesn't have a static vector of
899 * local declarations. Replaced a recursive solution, which permits
900 * easier subclassing/overriding.
902 * @param prefix non-null reference to prefix string, which should map
903 * to a namespace URL.
905 * @return The namespace URL that the prefix maps to, or null if no
906 * mapping can be found.
908 public String getNamespaceForPrefix(String prefix)
910 // if (null != prefix && prefix.equals("xmlns"))
912 // return Constants.S_XMLNAMESPACEURI;
915 List nsDecls = m_declaredPrefixes;
919 int n = nsDecls.size();
920 if(prefix.equals(Constants.ATTRVAL_DEFAULT_PREFIX))
925 for (int i = 0; i < n; i++)
927 XMLNSDecl decl = (XMLNSDecl) nsDecls.get(i);
929 if (prefix.equals(decl.getPrefix()))
930 return decl.getURI();
934 // Not found; ask our ancestors
935 if (null != m_parentNode)
936 return m_parentNode.getNamespaceForPrefix(prefix);
938 // JJK: No ancestors; try implicit
939 // %REVIEW% Are there literals somewhere that we should use instead?
940 // %REVIEW% Is this really the best place to patch?
941 if("xml".equals(prefix))
942 return "http://www.w3.org/XML/1998/namespace";
944 // No parent, so no definition
949 * The table of {@link XMLNSDecl}s for this element
950 * and all parent elements, screened for excluded prefixes.
953 private List m_prefixTable;
956 * Return a table that contains all prefixes available
957 * within this element context.
959 * @return reference to vector of {@link XMLNSDecl}s, which may be null.
961 List getPrefixTable()
963 return m_prefixTable;
966 void setPrefixTable(List list) {
967 m_prefixTable = list;
971 * Get whether or not the passed URL is contained flagged by
972 * the "extension-element-prefixes" property. This method is overridden
973 * by {@link ElemLiteralResult#containsExcludeResultPrefix}.
974 * @see <a href="http://www.w3.org/TR/xslt#extension-element">extension-element in XSLT Specification</a>
976 * @param prefix non-null reference to prefix that might be excluded.
978 * @return true if the prefix should normally be excluded.
980 public boolean containsExcludeResultPrefix(String prefix, String uri)
982 ElemTemplateElement parent = this.getParentElem();
984 return parent.containsExcludeResultPrefix(prefix, uri);
990 * Tell if the result namespace decl should be excluded. Should be called before
991 * namespace aliasing (I think).
993 * @param prefix non-null reference to prefix.
994 * @param uri reference to namespace that prefix maps to, which is protected
995 * for null, but should really never be passed as null.
997 * @return true if the given namespace should be excluded.
999 * @throws TransformerException
1001 private boolean excludeResultNSDecl(String prefix, String uri)
1002 throws TransformerException
1007 if (uri.equals(Constants.S_XSLNAMESPACEURL)
1008 || getStylesheet().containsExtensionElementURI(uri))
1011 if (containsExcludeResultPrefix(prefix, uri))
1019 * Combine the parent's namespaces with this namespace
1020 * for fast processing, taking care to reference the
1021 * parent's namespace if this namespace adds nothing new.
1022 * (Recursive method, walking the elements depth-first,
1023 * processing parents before children).
1024 * Note that this method builds m_prefixTable with aliased
1025 * namespaces, *not* the original namespaces.
1027 * @throws TransformerException
1029 public void resolvePrefixTables() throws TransformerException
1031 // Always start with a fresh prefix table!
1032 setPrefixTable(null);
1034 // If we have declared declarations, then we look for
1035 // a parent that has namespace decls, and add them
1036 // to this element's decls. Otherwise we just point
1037 // to the parent that has decls.
1038 if (null != this.m_declaredPrefixes)
1040 StylesheetRoot stylesheet = this.getStylesheetRoot();
1042 // Add this element's declared prefixes to the
1044 int n = m_declaredPrefixes.size();
1046 for (int i = 0; i < n; i++)
1048 XMLNSDecl decl = (XMLNSDecl) m_declaredPrefixes.get(i);
1049 String prefix = decl.getPrefix();
1050 String uri = decl.getURI();
1053 boolean shouldExclude = excludeResultNSDecl(prefix, uri);
1055 // Create a new prefix table if one has not already been created.
1056 if (null == m_prefixTable)
1057 setPrefixTable(new ArrayList());
1059 NamespaceAlias nsAlias = stylesheet.getNamespaceAliasComposed(uri);
1062 // Should I leave the non-aliased element in the table as
1063 // an excluded element?
1065 // The exclusion should apply to the non-aliased prefix, so
1066 // we don't calculate it here. -sb
1067 // Use stylesheet prefix, as per xsl WG
1068 decl = new XMLNSDecl(nsAlias.getStylesheetPrefix(),
1069 nsAlias.getResultNamespace(), shouldExclude);
1072 decl = new XMLNSDecl(prefix, uri, shouldExclude);
1074 m_prefixTable.add(decl);
1079 ElemTemplateElement parent = this.getParentNodeElem();
1084 // The prefix table of the parent should never be null!
1085 List prefixes = parent.m_prefixTable;
1087 if (null == m_prefixTable && !needToCheckExclude())
1090 // Nothing to combine, so just use parent's table!
1091 setPrefixTable(parent.m_prefixTable);
1096 // Add the prefixes from the parent's prefix table.
1097 int n = prefixes.size();
1099 for (int i = 0; i < n; i++)
1101 XMLNSDecl decl = (XMLNSDecl) prefixes.get(i);
1102 boolean shouldExclude = excludeResultNSDecl(decl.getPrefix(),
1105 if (shouldExclude != decl.getIsExcluded())
1107 decl = new XMLNSDecl(decl.getPrefix(), decl.getURI(),
1111 //m_prefixTable.addElement(decl);
1112 addOrReplaceDecls(decl);
1116 else if (null == m_prefixTable)
1119 // Must be stylesheet element without any result prefixes!
1120 setPrefixTable(new ArrayList());
1125 * Add or replace this namespace declaration in list
1126 * of namespaces in scope for this element.
1128 * @param newDecl namespace declaration to add to list
1130 void addOrReplaceDecls(XMLNSDecl newDecl)
1132 int n = m_prefixTable.size();
1134 for (int i = n - 1; i >= 0; i--)
1136 XMLNSDecl decl = (XMLNSDecl) m_prefixTable.get(i);
1138 if (decl.getPrefix().equals(newDecl.getPrefix()))
1143 m_prefixTable.add(newDecl);
1148 * Return whether we need to check namespace prefixes
1149 * against and exclude result prefixes list.
1151 boolean needToCheckExclude()
1157 * Send startPrefixMapping events to the result tree handler
1158 * for all declared prefix mappings in the stylesheet.
1160 * @param transformer non-null reference to the the current transform-time state.
1162 * @throws TransformerException
1164 void executeNSDecls(TransformerImpl transformer) throws TransformerException
1166 executeNSDecls(transformer, null);
1170 * Send startPrefixMapping events to the result tree handler
1171 * for all declared prefix mappings in the stylesheet.
1173 * @param transformer non-null reference to the the current transform-time state.
1174 * @param ignorePrefix string prefix to not startPrefixMapping
1176 * @throws TransformerException
1178 void executeNSDecls(TransformerImpl transformer, String ignorePrefix) throws TransformerException
1182 if (null != m_prefixTable)
1184 SerializationHandler rhandler = transformer.getResultTreeHandler();
1185 int n = m_prefixTable.size();
1187 for (int i = n - 1; i >= 0; i--)
1189 XMLNSDecl decl = (XMLNSDecl) m_prefixTable.get(i);
1191 if (!decl.getIsExcluded() && !(null != ignorePrefix && decl.getPrefix().equals(ignorePrefix)))
1193 rhandler.startPrefixMapping(decl.getPrefix(), decl.getURI(), true);
1198 catch(org.xml.sax.SAXException se)
1200 throw new TransformerException(se);
1205 * Send endPrefixMapping events to the result tree handler
1206 * for all declared prefix mappings in the stylesheet.
1208 * @param transformer non-null reference to the the current transform-time state.
1210 * @throws TransformerException
1212 void unexecuteNSDecls(TransformerImpl transformer) throws TransformerException
1214 unexecuteNSDecls(transformer, null);
1218 * Send endPrefixMapping events to the result tree handler
1219 * for all declared prefix mappings in the stylesheet.
1221 * @param transformer non-null reference to the the current transform-time state.
1222 * @param ignorePrefix string prefix to not endPrefixMapping
1224 * @throws TransformerException
1226 void unexecuteNSDecls(TransformerImpl transformer, String ignorePrefix) throws TransformerException
1231 if (null != m_prefixTable)
1233 SerializationHandler rhandler = transformer.getResultTreeHandler();
1234 int n = m_prefixTable.size();
1236 for (int i = 0; i < n; i++)
1238 XMLNSDecl decl = (XMLNSDecl) m_prefixTable.get(i);
1240 if (!decl.getIsExcluded() && !(null != ignorePrefix && decl.getPrefix().equals(ignorePrefix)))
1242 rhandler.endPrefixMapping(decl.getPrefix());
1247 catch(org.xml.sax.SAXException se)
1249 throw new TransformerException(se);
1253 /** The *relative* document order number of this element.
1255 protected int m_docOrderNumber = -1;
1258 * Set the UID (document order index).
1260 * @param i Index of this child.
1262 public void setUid(int i)
1264 m_docOrderNumber = i;
1268 * Get the UID (document order index).
1270 * @return Index of this child
1274 return m_docOrderNumber;
1282 protected ElemTemplateElement m_parentNode;
1285 * Get the parent as a Node.
1287 * @return This node's parent node
1289 public Node getParentNode()
1291 return m_parentNode;
1295 * Get the parent as an ElemTemplateElement.
1297 * @return This node's parent as an ElemTemplateElement
1299 public ElemTemplateElement getParentElem()
1301 return m_parentNode;
1305 * Set the parent as an ElemTemplateElement.
1307 * @param p This node's parent as an ElemTemplateElement
1309 public void setParentElem(ElemTemplateElement p)
1318 ElemTemplateElement m_nextSibling;
1321 * Get the next sibling (as a Node) or return null.
1323 * @return this node's next sibling or null
1325 public Node getNextSibling()
1327 return m_nextSibling;
1331 * Get the previous sibling (as a Node) or return null.
1332 * Note that this may be expensive if the parent has many kids;
1333 * we accept that price in exchange for avoiding the prev pointer
1334 * TODO: If we were sure parents and sibs are always ElemTemplateElements,
1335 * we could hit the fields directly rather than thru accessors.
1337 * @return This node's previous sibling or null
1339 public Node getPreviousSibling()
1342 Node walker = getParentNode(), prev = null;
1345 for (walker = walker.getFirstChild(); walker != null;
1346 prev = walker, walker = walker.getNextSibling())
1356 * Get the previous sibling (as a Node) or return null.
1357 * Note that this may be expensive if the parent has many kids;
1358 * we accept that price in exchange for avoiding the prev pointer
1359 * TODO: If we were sure parents and sibs are always ElemTemplateElements,
1360 * we could hit the fields directly rather than thru accessors.
1362 * @return This node's previous sibling or null
1364 public ElemTemplateElement getPreviousSiblingElem()
1367 ElemTemplateElement walker = getParentNodeElem();
1368 ElemTemplateElement prev = null;
1371 for (walker = walker.getFirstChildElem(); walker != null;
1372 prev = walker, walker = walker.getNextSiblingElem())
1383 * Get the next sibling (as a ElemTemplateElement) or return null.
1385 * @return This node's next sibling (as a ElemTemplateElement) or null
1387 public ElemTemplateElement getNextSiblingElem()
1389 return m_nextSibling;
1393 * Get the parent element.
1395 * @return This node's next parent (as a ElemTemplateElement) or null
1397 public ElemTemplateElement getParentNodeElem()
1399 return m_parentNode;
1407 ElemTemplateElement m_firstChild;
1410 * Get the first child as a Node.
1412 * @return This node's first child or null
1414 public Node getFirstChild()
1416 return m_firstChild;
1420 * Get the first child as a ElemTemplateElement.
1422 * @return This node's first child (as a ElemTemplateElement) or null
1424 public ElemTemplateElement getFirstChildElem()
1426 return m_firstChild;
1430 * Get the last child.
1432 * @return This node's last child
1434 public Node getLastChild()
1437 ElemTemplateElement lastChild = null;
1439 for (ElemTemplateElement node = m_firstChild; node != null;
1440 node = node.m_nextSibling)
1449 * Get the last child.
1451 * @return This node's last child
1453 public ElemTemplateElement getLastChildElem()
1456 ElemTemplateElement lastChild = null;
1458 for (ElemTemplateElement node = m_firstChild; node != null;
1459 node = node.m_nextSibling)
1468 /** DOM backpointer that this element originated from. */
1469 transient private org.w3c.dom.Node m_DOMBackPointer;
1472 * If this stylesheet was created from a DOM, get the
1473 * DOM backpointer that this element originated from.
1476 * @return DOM backpointer that this element originated from or null.
1478 public org.w3c.dom.Node getDOMBackPointer()
1480 return m_DOMBackPointer;
1484 * If this stylesheet was created from a DOM, set the
1485 * DOM backpointer that this element originated from.
1488 * @param n DOM backpointer that this element originated from.
1490 public void setDOMBackPointer(org.w3c.dom.Node n)
1492 m_DOMBackPointer = n;
1496 * Compares this object with the specified object for precedence order.
1497 * The order is determined by the getImportCountComposed() of the containing
1498 * composed stylesheet and the getUid() of this element.
1499 * Returns a negative integer, zero, or a positive integer as this
1500 * object is less than, equal to, or greater than the specified object.
1502 * @param o The object to be compared to this object
1503 * @return a negative integer, zero, or a positive integer as this object is
1504 * less than, equal to, or greater than the specified object.
1505 * @throws ClassCastException if the specified object's
1506 * type prevents it from being compared to this Object.
1508 public int compareTo(Object o) throws ClassCastException {
1510 ElemTemplateElement ro = (ElemTemplateElement) o;
1511 int roPrecedence = ro.getStylesheetComposed().getImportCountComposed();
1512 int myPrecedence = this.getStylesheetComposed().getImportCountComposed();
1514 if (myPrecedence < roPrecedence)
1516 else if (myPrecedence > roPrecedence)
1519 return this.getUid() - ro.getUid();
1523 * Get information about whether or not an element should strip whitespace.
1524 * @see <a href="http://www.w3.org/TR/xslt#strip">strip in XSLT Specification</a>
1526 * @param support The XPath runtime state.
1527 * @param targetElement Element to check
1529 * @return true if the whitespace should be stripped.
1531 * @throws TransformerException
1533 public boolean shouldStripWhiteSpace(
1534 org.apache.xpath.XPathContext support,
1535 org.w3c.dom.Element targetElement) throws TransformerException
1537 StylesheetRoot sroot = this.getStylesheetRoot();
1538 return (null != sroot) ? sroot.shouldStripWhiteSpace(support, targetElement) :false;
1542 * Get information about whether or not whitespace can be stripped.
1543 * @see <a href="http://www.w3.org/TR/xslt#strip">strip in XSLT Specification</a>
1545 * @return true if the whitespace can be stripped.
1547 public boolean canStripWhiteSpace()
1549 StylesheetRoot sroot = this.getStylesheetRoot();
1550 return (null != sroot) ? sroot.canStripWhiteSpace() : false;
1554 * Tell if this element can accept variable declarations.
1555 * @return true if the element can accept and process variable declarations.
1557 public boolean canAcceptVariables()
1562 //=============== ExpressionNode methods ================
1565 * Set the parent of this node.
1566 * @param n Must be a ElemTemplateElement.
1568 public void exprSetParent(ExpressionNode n)
1570 // This obviously requires that only a ElemTemplateElement can
1571 // parent a node of this type.
1572 setParentElem((ElemTemplateElement)n);
1576 * Get the ExpressionNode parent of this node.
1578 public ExpressionNode exprGetParent()
1580 return getParentElem();
1584 * This method tells the node to add its argument to the node's
1586 * @param n Must be a ElemTemplateElement.
1588 public void exprAddChild(ExpressionNode n, int i)
1590 appendChild((ElemTemplateElement)n);
1593 /** This method returns a child node. The children are numbered
1594 from zero, left to right. */
1595 public ExpressionNode exprGetChild(int i)
1597 return (ExpressionNode)item(i);
1600 /** Return the number of children the node has. */
1601 public int exprGetNumChildren()
1607 * Accept a visitor and call the appropriate method
1610 * @param visitor The visitor whose appropriate method will be called.
1611 * @return true if the children of the object should be visited.
1613 protected boolean accept(XSLTVisitor visitor)
1615 return visitor.visitInstruction(this);
1619 * @see XSLTVisitable#callVisitors(XSLTVisitor)
1621 public void callVisitors(XSLTVisitor visitor)
1625 callChildVisitors(visitor);
1630 * Call the children visitors.
1631 * @param visitor The visitor whose appropriate method will be called.
1633 protected void callChildVisitors(XSLTVisitor visitor, boolean callAttributes)
1635 for (ElemTemplateElement node = m_firstChild;
1637 node = node.m_nextSibling)
1639 node.callVisitors(visitor);
1644 * Call the children visitors.
1645 * @param visitor The visitor whose appropriate method will be called.
1647 protected void callChildVisitors(XSLTVisitor visitor)
1649 callChildVisitors(visitor, true);
1654 * @see PrefixResolver#handlesNullPrefixes()
1656 public boolean handlesNullPrefixes() {