1 /* ApplyTemplatesNode.java --
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.xml.transform;
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.Collections;
43 import java.util.Iterator;
44 import java.util.List;
45 import javax.xml.namespace.QName;
46 import javax.xml.transform.TransformerException;
47 import org.w3c.dom.Node;
48 import gnu.xml.xpath.Expr;
51 * A template node representing the XSL <code>apply-templates</code>
54 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
56 final class ApplyTemplatesNode
63 final List withParams;
64 final boolean isDefault;
66 ApplyTemplatesNode(TemplateNode children, TemplateNode next,
67 Expr select, QName mode,
68 List sortKeys, List withParams, boolean isDefault)
70 super(children, next);
73 this.sortKeys = sortKeys;
74 this.withParams = withParams;
75 this.isDefault = isDefault;
78 TemplateNode clone(Stylesheet stylesheet)
80 int len = sortKeys.size();
81 List sortKeys2 = new ArrayList(len);
82 for (int i = 0; i < len; i++)
84 sortKeys2.add(((Key) sortKeys.get(i)).clone(stylesheet));
86 len = withParams.size();
87 List withParams2 = new ArrayList(len);
88 for (int i = 0; i < len; i++)
90 withParams2.add(((WithParam) withParams.get(i)).clone(stylesheet));
92 return new ApplyTemplatesNode((children == null) ? null :
93 children.clone(stylesheet),
94 (next == null) ? null :
95 next.clone(stylesheet),
96 select.clone(stylesheet),
97 mode, sortKeys2, withParams2, isDefault);
100 void doApply(Stylesheet stylesheet, QName mode,
101 Node context, int pos, int len,
102 Node parent, Node nextSibling)
103 throws TransformerException
105 Object ret = select.evaluate(context, pos, len);
106 if (ret != null && ret instanceof Collection)
108 if (withParams != null)
110 // push the parameter context
111 stylesheet.bindings.push(false);
112 // set the parameters
113 for (Iterator i = withParams.iterator(); i.hasNext(); )
115 WithParam p = (WithParam) i.next();
116 Object value = p.getValue(stylesheet, mode, context, pos, len);
117 stylesheet.bindings.set(p.name, value, false);
120 Collection ns = (Collection) ret;
121 List nodes = new ArrayList(ns);
122 if (sortKeys != null)
124 for (Iterator i = sortKeys.iterator(); i.hasNext(); )
126 SortKey sortKey = (SortKey) i.next();
127 sortKey.init(stylesheet, mode, context, pos, len, parent,
130 Collections.sort(nodes, new XSLComparator(sortKeys));
134 Collections.sort(nodes, documentOrderComparator);
136 int l = nodes.size();
137 QName effectiveMode = isDefault ? mode : this.mode;
138 for (int i = 0; i < l; i++)
140 Node node = (Node) nodes.get(i);
141 TemplateNode t = stylesheet.getTemplate(effectiveMode, node,
145 if (stylesheet.debug)
147 System.err.println("Applying " + t);
149 stylesheet.current = node;
150 t.apply(stylesheet, effectiveMode, node, i + 1, l,
151 parent, nextSibling);
154 if (withParams != null)
156 // pop the variable context
157 stylesheet.bindings.pop(false);
160 // apply-templates doesn't have processable children
163 next.apply(stylesheet, mode,
165 parent, nextSibling);
169 public String toString()
171 StringBuffer buf = new StringBuffer(getClass().getName());
176 buf.append("select=");
190 return buf.toString();