1 /* ApplyTemplatesNode.java -- 2 Copyright (C) 2004,2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 19 02110-1301 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 gnu.java.lang.CPStringBuilder; 41 42 import java.util.ArrayList; 43 import java.util.Collection; 44 import java.util.Collections; 45 import java.util.Iterator; 46 import java.util.LinkedList; 47 import java.util.List; 48 import javax.xml.namespace.QName; 49 import javax.xml.transform.TransformerException; 50 import org.w3c.dom.Node; 51 import gnu.xml.xpath.Expr; 52 53 /** 54 * A template node representing the XSL <code>apply-templates</code> 55 * instruction. 56 * 57 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> 58 */ 59 final class ApplyTemplatesNode 60 extends TemplateNode 61 { 62 63 final Expr select; 64 final QName mode; 65 final List sortKeys; 66 final List withParams; 67 final boolean isDefault; 68 ApplyTemplatesNode(Expr select, QName mode, List sortKeys, List withParams, boolean isDefault)69 ApplyTemplatesNode(Expr select, QName mode, 70 List sortKeys, List withParams, boolean isDefault) 71 { 72 this.select = select; 73 this.mode = mode; 74 this.sortKeys = sortKeys; 75 this.withParams = withParams; 76 this.isDefault = isDefault; 77 } 78 clone(Stylesheet stylesheet)79 TemplateNode clone(Stylesheet stylesheet) 80 { 81 int len = sortKeys != null ? sortKeys.size() : 0; 82 List sortKeys2 = new ArrayList(len); 83 for (int i = 0; i < len; i++) 84 sortKeys2.add(((Key) sortKeys.get(i)).clone(stylesheet)); 85 len = withParams != null ? withParams.size() : 0; 86 List withParams2 = new ArrayList(len); 87 for (int i = 0; i < len; i++) 88 withParams2.add(((WithParam) withParams.get(i)).clone(stylesheet)); 89 TemplateNode ret = new ApplyTemplatesNode(select.clone(stylesheet), 90 mode, sortKeys2, withParams2, 91 isDefault); 92 if (children != null) 93 ret.children = children.clone(stylesheet); 94 if (next != null) 95 ret.next = next.clone(stylesheet); 96 return ret; 97 } 98 doApply(Stylesheet stylesheet, QName mode, Node context, int pos, int len, Node parent, Node nextSibling)99 void doApply(Stylesheet stylesheet, QName mode, 100 Node context, int pos, int len, 101 Node parent, Node nextSibling) 102 throws TransformerException 103 { 104 Object ret = select.evaluate(context, pos, len); 105 if (ret != null && ret instanceof Collection) 106 { 107 if (withParams != null) 108 { 109 // compute the parameter values 110 LinkedList values = new LinkedList(); 111 for (Iterator i = withParams.iterator(); i.hasNext(); ) 112 { 113 WithParam p = (WithParam) i.next(); 114 Object value = p.getValue(stylesheet, mode, context, pos, len); 115 Object[] pair = new Object[2]; 116 pair[0] = p.name; 117 pair[1] = value; 118 values.add(pair); 119 } 120 // push the parameter context 121 stylesheet.bindings.push(Bindings.WITH_PARAM); 122 // set the parameters 123 for (Iterator i = values.iterator(); i.hasNext(); ) 124 { 125 Object[] pair = (Object[]) i.next(); 126 QName name = (QName) pair[0]; 127 Object value = pair[1]; 128 stylesheet.bindings.set(name, value, Bindings.WITH_PARAM); 129 } 130 } 131 Collection ns = (Collection) ret; 132 List nodes = new ArrayList(ns); 133 if (sortKeys != null) 134 { 135 for (Iterator i = sortKeys.iterator(); i.hasNext(); ) 136 { 137 SortKey sortKey = (SortKey) i.next(); 138 sortKey.init(stylesheet, mode, context, pos, len, parent, 139 nextSibling); 140 } 141 Collections.sort(nodes, new XSLComparator(sortKeys)); 142 } 143 else 144 Collections.sort(nodes, documentOrderComparator); 145 int l = nodes.size(); 146 QName effectiveMode = isDefault ? mode : this.mode; 147 for (int i = 0; i < l; i++) 148 { 149 Node node = (Node) nodes.get(i); 150 TemplateNode t = stylesheet.getTemplate(effectiveMode, node, 151 false); 152 if (t != null) 153 { 154 stylesheet.current = node; 155 t.apply(stylesheet, effectiveMode, node, i + 1, l, 156 parent, nextSibling); 157 } 158 } 159 if (withParams != null) 160 { 161 // pop the variable context 162 stylesheet.bindings.pop(Bindings.WITH_PARAM); 163 } 164 } 165 // apply-templates doesn't have processable children 166 if (next != null) 167 next.apply(stylesheet, mode, 168 context, pos, len, 169 parent, nextSibling); 170 } 171 references(QName var)172 public boolean references(QName var) 173 { 174 if (select != null && select.references(var)) 175 return true; 176 if (withParams != null) 177 { 178 for (Iterator i = withParams.iterator(); i.hasNext(); ) 179 { 180 if (((WithParam) i.next()).references(var)) 181 return true; 182 } 183 } 184 if (sortKeys != null) 185 { 186 for (Iterator i = sortKeys.iterator(); i.hasNext(); ) 187 { 188 if (((SortKey) i.next()).references(var)) 189 return true; 190 } 191 } 192 return super.references(var); 193 } 194 toString()195 public String toString() 196 { 197 CPStringBuilder buf = new CPStringBuilder("apply-templates"); 198 buf.append('['); 199 boolean o = false; 200 if (select != null) 201 { 202 buf.append("select="); 203 buf.append(select); 204 o = true; 205 } 206 if (mode != null) 207 { 208 if (o) 209 { 210 buf.append(','); 211 } 212 buf.append("mode="); 213 buf.append(mode); 214 } 215 buf.append(']'); 216 return buf.toString(); 217 } 218 219 } 220