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
.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
;
52 * A template in an XSL stylesheet.
54 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
60 static final double DEFAULT_PRIORITY
= 0.5d
;
62 final Stylesheet stylesheet
;
65 final TemplateNode node
;
66 final double priority
;
70 Template(Stylesheet stylesheet
,
71 QName name
, Pattern match
, TemplateNode node
,
72 int precedence
, double priority
, QName mode
)
74 this.stylesheet
= stylesheet
;
78 // adjust priority if necessary
79 // see XSLT section 5.5
80 Test test
= getNodeTest(match
);
83 if (test
instanceof NameTest
)
85 NameTest nameTest
= (NameTest
) test
;
86 if (nameTest
.matchesAny() ||
87 nameTest
.matchesAnyLocalName())
98 NodeTypeTest nodeTypeTest
= (NodeTypeTest
) test
;
99 if (nodeTypeTest
.getNodeType() ==
100 Node
.PROCESSING_INSTRUCTION_NODE
&&
101 nodeTypeTest
.getData() != null)
111 this.precedence
= precedence
;
112 this.priority
= priority
;
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
,
122 (match
== null) ?
null :
123 (Pattern
) match
.clone(stylesheet
),
124 (node
== null) ?
null : node
.clone(stylesheet
),
130 public int compareTo(Object other
)
132 if (other
instanceof Template
)
134 Template t
= (Template
) other
;
135 int d
= t
.precedence
- precedence
;
140 double d2
= t
.priority
- priority
;
143 return (int) Math
.round(d2
* 1000.0d
);
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)
163 boolean matches(QName mode
, Node node
)
165 if ((mode
== null && this.mode
!= null) ||
166 (mode
!= null && !mode
.equals(this.mode
)))
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
;
188 if (ctx
== stylesheet
)
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);
212 node
.apply(stylesheet
, mode
,
214 parent
, nextSibling
);
218 public String
toString()
220 StringBuffer buf
= new StringBuffer(getClass().getName());
227 else if (match
!= null)
229 buf
.append("match=");
234 buf
.append(",mode=");
238 return buf
.toString();
240 //return (name != null) ? name.toString() : match.toString();
243 void list(PrintStream out
)
245 out
.println(toString());
248 node
.list(1, out
, true);