libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / gnu / xml / transform / ForEachNode.java
blob8908114ccf4a6a7667f87f99791b16a98455eafd
1 /* ForEachNode.java --
2 Copyright (C) 2004,2006 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)
9 any later version.
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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
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
24 combination.
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 gnu.java.lang.CPStringBuilder;
42 import java.util.ArrayList;
43 import java.util.Collection;
44 import java.util.Collections;
45 import java.util.Iterator;
46 import java.util.List;
47 import javax.xml.namespace.QName;
48 import javax.xml.transform.TransformerException;
49 import org.w3c.dom.Node;
50 import gnu.xml.xpath.Expr;
52 /**
53 * A template node representing an XSLT <code>for-each</code> instruction.
55 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
57 final class ForEachNode
58 extends TemplateNode
61 final Expr select;
62 final List<SortKey> sortKeys;
64 ForEachNode(Expr select, List<SortKey> sortKeys)
66 this.select = select;
67 this.sortKeys = sortKeys;
70 TemplateNode clone(Stylesheet stylesheet)
72 int len = sortKeys.size();
73 List<SortKey> sortKeys2 = new ArrayList<SortKey>(len);
74 for (int i = 0; i < len; i++)
75 sortKeys2.add(sortKeys.get(i).clone(stylesheet));
76 TemplateNode ret = new ForEachNode(select.clone(stylesheet),
77 sortKeys2);
78 if (children != null)
79 ret.children = children.clone(stylesheet);
80 if (next != null)
81 ret.next = next.clone(stylesheet);
82 return ret;
85 @Override
86 void doApply(Stylesheet stylesheet, QName mode,
87 Node context, int pos, int len,
88 Node parent, Node nextSibling)
89 throws TransformerException
91 if (children != null)
93 // Set current template to null
94 Template saved = stylesheet.currentTemplate;
95 stylesheet.currentTemplate = null;
96 Object ret = select.evaluate(context, pos, len);
97 //System.err.println(toString() + ": " + context+" -> "+ret);
98 if (ret instanceof Collection)
100 /* Suppression is safe, as we know context produces Collection<Node> */
101 @SuppressWarnings("unchecked")
102 Collection<Node> ns = (Collection<Node>) ret;
103 List<Node> list = new ArrayList<Node>(ns);
104 if (!sortKeys.isEmpty())
106 for (SortKey sortKey : sortKeys)
108 sortKey.init(stylesheet, mode, context, pos, len, parent,
109 nextSibling);
111 Collections.sort(list, new XSLComparator(sortKeys));
113 else
114 Collections.sort(list, documentOrderComparator);
115 // Perform children for each node
116 int l = list.size();
117 int p = 1;
118 for (Node node : list)
120 stylesheet.current = node;
121 children.apply(stylesheet, mode,
122 node, p++, l,
123 parent, nextSibling);
126 // Restore current template
127 stylesheet.currentTemplate = saved;
129 if (next != null)
130 next.apply(stylesheet, mode,
131 context, pos, len,
132 parent, nextSibling);
135 @Override
136 public boolean references(QName var)
138 if (select != null && select.references(var))
139 return true;
140 for (Iterator<SortKey> i = sortKeys.iterator(); i.hasNext(); )
142 if (i.next().references(var))
143 return true;
145 return super.references(var);
148 @Override
149 public String toString()
151 CPStringBuilder buf = new CPStringBuilder("for-each");
152 buf.append('[');
153 buf.append("select=");
154 buf.append(select);
155 buf.append(']');
156 return buf.toString();