OSDN Git Service

* external/w3c_dom/Makefile.am: New file.
[pf3gnuchains/gcc-fork.git] / libjava / gnu / xml / transform / ApplyTemplatesNode.java
1 /* ApplyTemplatesNode.java -- 
2    Copyright (C) 2004 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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.
10
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.
15
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
19 02111-1307 USA.
20
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.
25
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. */
37
38 package gnu.xml.transform;
39
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;
49
50 /**
51  * A template node representing the XSL <code>apply-templates</code>
52  * instruction.
53  *
54  * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
55  */
56 final class ApplyTemplatesNode
57   extends TemplateNode
58 {
59
60   final Expr select;
61   final QName mode;
62   final List sortKeys;
63   final List withParams;
64   final boolean isDefault;
65
66   ApplyTemplatesNode(TemplateNode children, TemplateNode next,
67                      Expr select, QName mode,
68                      List sortKeys, List withParams, boolean isDefault)
69   {
70     super(children, next);
71     this.select = select;
72     this.mode = mode;
73     this.sortKeys = sortKeys;
74     this.withParams = withParams;
75     this.isDefault = isDefault;
76   }
77
78   TemplateNode clone(Stylesheet stylesheet)
79   {
80     int len = sortKeys.size();
81     List sortKeys2 = new ArrayList(len);
82     for (int i = 0; i < len; i++)
83       {
84         sortKeys2.add(((Key) sortKeys.get(i)).clone(stylesheet));
85       }
86     len = withParams.size();
87     List withParams2 = new ArrayList(len);
88     for (int i = 0; i < len; i++)
89       {
90         withParams2.add(((WithParam) withParams.get(i)).clone(stylesheet));
91       }
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);
98   }
99
100   void doApply(Stylesheet stylesheet, QName mode,
101                Node context, int pos, int len,
102                Node parent, Node nextSibling)
103     throws TransformerException
104   {
105     Object ret = select.evaluate(context, pos, len);
106     if (ret != null && ret instanceof Collection)
107       {
108         if (withParams != null)
109           {
110             // push the parameter context
111             stylesheet.bindings.push(false);
112             // set the parameters
113             for (Iterator i = withParams.iterator(); i.hasNext(); )
114               {
115                 WithParam p = (WithParam) i.next();
116                 Object value = p.getValue(stylesheet, mode, context, pos, len);
117                 stylesheet.bindings.set(p.name, value, false);
118               }
119           }
120         Collection ns = (Collection) ret;
121         List nodes = new ArrayList(ns);
122         if (sortKeys != null)
123           {
124             for (Iterator i = sortKeys.iterator(); i.hasNext(); )
125               {
126                 SortKey sortKey = (SortKey) i.next();
127                 sortKey.init(stylesheet, mode, context, pos, len, parent,
128                              nextSibling);
129               }
130             Collections.sort(nodes, new XSLComparator(sortKeys));
131           }
132         else
133           {
134             Collections.sort(nodes, documentOrderComparator);
135           }
136         int l = nodes.size();
137         QName effectiveMode = isDefault ? mode : this.mode;
138         for (int i = 0; i < l; i++)
139           {
140             Node node = (Node) nodes.get(i);
141             TemplateNode t = stylesheet.getTemplate(effectiveMode, node,
142                                                     false);
143             if (t != null)
144               {
145                 if (stylesheet.debug)
146                   {
147                     System.err.println("Applying " + t);
148                   }
149                 stylesheet.current = node;
150                 t.apply(stylesheet, effectiveMode, node, i + 1, l,
151                         parent, nextSibling);
152               }
153           }
154         if (withParams != null)
155           {
156             // pop the variable context
157             stylesheet.bindings.pop(false);
158           }
159       }
160     // apply-templates doesn't have processable children
161     if (next != null)
162       {
163         next.apply(stylesheet, mode,
164                    context, pos, len,
165                    parent, nextSibling);
166       }
167   }
168   
169   public String toString()
170   {
171     StringBuffer buf = new StringBuffer(getClass().getName());
172     buf.append('[');
173     boolean o = false;
174     if (select != null)
175       {
176         buf.append("select=");
177         buf.append(select);
178         o = true;
179       }
180     if (mode != null)
181       {
182         if (o)
183           {
184             buf.append(',');
185           }
186         buf.append("mode=");
187         buf.append(mode);
188       }
189     buf.append(']');
190     return buf.toString();
191   }
192   
193 }