Merge from the pain train
[official-gcc.git] / libjava / gnu / xml / transform / Template.java
blob0e7c67a7e521c75c8ce4736cd5b79b25fa1723a8
1 /* Template.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)
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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 java.io.PrintStream;
41 import javax.xml.namespace.QName;
42 import javax.xml.transform.TransformerException;
43 import org.w3c.dom.Node;
44 import gnu.xml.xpath.Expr;
45 import gnu.xml.xpath.NameTest;
46 import gnu.xml.xpath.NodeTypeTest;
47 import gnu.xml.xpath.Pattern;
48 import gnu.xml.xpath.Selector;
49 import gnu.xml.xpath.Test;
51 /**
52 * A template in an XSL stylesheet.
54 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
56 class Template
57 implements Comparable
60 static final double DEFAULT_PRIORITY = 0.5d;
62 final Stylesheet stylesheet;
63 final QName name;
64 final Pattern match;
65 final TemplateNode node;
66 final double priority;
67 final int precedence;
68 final QName mode;
70 Template(Stylesheet stylesheet,
71 QName name, Pattern match, TemplateNode node,
72 int precedence, double priority, QName mode)
74 this.stylesheet = stylesheet;
75 this.name = name;
76 this.match = match;
77 this.node = node;
78 // adjust priority if necessary
79 // see XSLT section 5.5
80 Test test = getNodeTest(match);
81 if (test != null)
83 if (test instanceof NameTest)
85 NameTest nameTest = (NameTest) test;
86 if (nameTest.matchesAny() ||
87 nameTest.matchesAnyLocalName())
89 priority = -0.25d;
91 else
93 priority = 0.0d;
96 else
98 NodeTypeTest nodeTypeTest = (NodeTypeTest) test;
99 if (nodeTypeTest.getNodeType() ==
100 Node.PROCESSING_INSTRUCTION_NODE &&
101 nodeTypeTest.getData() != null)
103 priority = 0.0d;
105 else
107 priority = -0.5d;
111 this.precedence = precedence;
112 this.priority = priority;
113 this.mode = mode;
116 Template clone(Stylesheet stylesheet)
118 // FIXME by cloning we lose the imports() functionality, so
119 // apply-imports will be broken.
120 return new Template(stylesheet,
121 name,
122 (match == null) ? null :
123 (Pattern) match.clone(stylesheet),
124 (node == null) ? null : node.clone(stylesheet),
125 precedence,
126 priority,
127 mode);
130 public int compareTo(Object other)
132 if (other instanceof Template)
134 Template t = (Template) other;
135 int d = t.precedence - precedence;
136 if (d != 0)
138 return d;
140 double d2 = t.priority - priority;
141 if (d2 != 0.0d)
143 return (int) Math.round(d2 * 1000.0d);
146 return 0;
149 Test getNodeTest(Expr expr)
151 if (expr instanceof Selector)
153 Selector selector = (Selector) expr;
154 Test[] tests = selector.getTests();
155 if (tests.length > 0)
157 return tests[0];
160 return null;
163 boolean matches(QName mode, Node node)
165 if ((mode == null && this.mode != null) ||
166 (mode != null && !mode.equals(this.mode)))
168 return false;
170 if (match == null)
172 return false;
174 return match.matches(node);
177 boolean matches(QName name)
179 return name.equals(this.name);
182 boolean imports(Template other)
184 for (Stylesheet ctx = other.stylesheet.parent;
185 ctx != null;
186 ctx = ctx.parent)
188 if (ctx == stylesheet)
190 return true;
193 return false;
197 * @param stylesheet the stylesheet
198 * @param parent the parent of result nodes
199 * @param context the context node in the source document
200 * @param pos the context position
201 * @param len the context size
202 * @param nextSibling if non-null, add result nodes before this node
204 void apply(Stylesheet stylesheet, QName mode,
205 Node context, int pos, int len,
206 Node parent, Node nextSibling)
207 throws TransformerException
209 //System.err.println("...applying " + toString() + " to " + context);
210 if (node != null)
212 node.apply(stylesheet, mode,
213 context, pos, len,
214 parent, nextSibling);
218 public String toString()
220 StringBuffer buf = new StringBuffer(getClass().getName());
221 buf.append('[');
222 if (name != null)
224 buf.append("name=");
225 buf.append(name);
227 else if (match != null)
229 buf.append("match=");
230 buf.append(match);
232 if (mode != null)
234 buf.append(",mode=");
235 buf.append(mode);
237 buf.append(']');
238 return buf.toString();
240 //return (name != null) ? name.toString() : match.toString();
243 void list(PrintStream out)
245 out.println(toString());
246 if (node != null)
248 node.list(1, out, true);