1 |
769 |
jeremybenn |
/* SortKey.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., 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 javax.xml.namespace.QName;
|
41 |
|
|
import javax.xml.transform.TransformerException;
|
42 |
|
|
import org.w3c.dom.Document;
|
43 |
|
|
import org.w3c.dom.DocumentFragment;
|
44 |
|
|
import org.w3c.dom.Node;
|
45 |
|
|
import gnu.xml.xpath.Expr;
|
46 |
|
|
|
47 |
|
|
/**
|
48 |
|
|
* <p>
|
49 |
|
|
* An XSL sort key, as specified by section 10 of the XSL
|
50 |
|
|
* Transformations specification. This takes the form:
|
51 |
|
|
* </p>
|
52 |
|
|
* <pre>
|
53 |
|
|
* <xsl:sort
|
54 |
|
|
* select = string-expression
|
55 |
|
|
* lang = { nmtoken }
|
56 |
|
|
* data-type = { "text" | "number" | qname-but-not-ncname }
|
57 |
|
|
* order = { "ascending" | "descending" }
|
58 |
|
|
* case-order = { "upper-first" | "lower-first" } /&rt;
|
59 |
|
|
* </pre>
|
60 |
|
|
* <p>
|
61 |
|
|
* Note that all but the selection expression are optional,
|
62 |
|
|
* and so may be {@code null}.
|
63 |
|
|
* </p>
|
64 |
|
|
*
|
65 |
|
|
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
|
66 |
|
|
*/
|
67 |
|
|
final class SortKey
|
68 |
|
|
{
|
69 |
|
|
|
70 |
|
|
static final int DEFAULT = 0;
|
71 |
|
|
static final int UPPER_FIRST = 1;
|
72 |
|
|
static final int LOWER_FIRST = 2;
|
73 |
|
|
|
74 |
|
|
final Expr select;
|
75 |
|
|
final TemplateNode langTemplate;
|
76 |
|
|
final TemplateNode dataTypeTemplate;
|
77 |
|
|
final TemplateNode orderTemplate;
|
78 |
|
|
final TemplateNode caseOrderTemplate;
|
79 |
|
|
|
80 |
|
|
transient String lang;
|
81 |
|
|
transient String dataType;
|
82 |
|
|
transient boolean descending;
|
83 |
|
|
transient int caseOrder;
|
84 |
|
|
|
85 |
|
|
/**
|
86 |
|
|
* Constructs a new {@link SortKey} to represent an <xsl:sort&rt;
|
87 |
|
|
* tag.
|
88 |
|
|
*
|
89 |
|
|
* @param select the XPath expression which selects the nodes to be sorted.
|
90 |
|
|
* @param lang the language of the sort keys or {@code null} if unspecified.
|
91 |
|
|
* @param dataType the data type of the strings. May be "string", "number",
|
92 |
|
|
* a QName or {@code null} if unspecified.
|
93 |
|
|
* @param order the ordering of the nodes, which may be "ascending", "descending"
|
94 |
|
|
* or {@code null} if unspecified.
|
95 |
|
|
* @param caseOrder the treatment of case when the data type is a string. This
|
96 |
|
|
* may be "upper-first", "lower-first" or {@code null} if
|
97 |
|
|
* unspecified.
|
98 |
|
|
*/
|
99 |
|
|
SortKey(Expr select, TemplateNode lang, TemplateNode dataType,
|
100 |
|
|
TemplateNode order, TemplateNode caseOrder)
|
101 |
|
|
{
|
102 |
|
|
this.select = select;
|
103 |
|
|
this.langTemplate = lang;
|
104 |
|
|
this.dataTypeTemplate = dataType;
|
105 |
|
|
this.orderTemplate = order;
|
106 |
|
|
this.caseOrderTemplate = caseOrder;
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
String key(Node node)
|
110 |
|
|
{
|
111 |
|
|
Object ret = select.evaluate(node, 1, 1);
|
112 |
|
|
if (ret instanceof String)
|
113 |
|
|
{
|
114 |
|
|
return (String) ret;
|
115 |
|
|
}
|
116 |
|
|
else
|
117 |
|
|
{
|
118 |
|
|
return Expr._string(node, ret);
|
119 |
|
|
}
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
/**
|
123 |
|
|
* Prepare for a sort.
|
124 |
|
|
* This sets all transient variables from their AVTs.
|
125 |
|
|
*/
|
126 |
|
|
void init(Stylesheet stylesheet, QName mode,
|
127 |
|
|
Node context, int pos, int len,
|
128 |
|
|
Node parent, Node nextSibling)
|
129 |
|
|
throws TransformerException
|
130 |
|
|
{
|
131 |
|
|
Document doc = (context instanceof Document) ? (Document) context :
|
132 |
|
|
context.getOwnerDocument();
|
133 |
|
|
if (langTemplate == null)
|
134 |
|
|
{
|
135 |
|
|
lang = null;
|
136 |
|
|
}
|
137 |
|
|
else
|
138 |
|
|
{
|
139 |
|
|
DocumentFragment fragment = doc.createDocumentFragment();
|
140 |
|
|
langTemplate.apply(stylesheet, mode, context, pos, len,
|
141 |
|
|
fragment, null);
|
142 |
|
|
lang = Expr.stringValue(fragment);
|
143 |
|
|
}
|
144 |
|
|
if (dataTypeTemplate == null)
|
145 |
|
|
{
|
146 |
|
|
dataType = "text";
|
147 |
|
|
}
|
148 |
|
|
else
|
149 |
|
|
{
|
150 |
|
|
DocumentFragment fragment = doc.createDocumentFragment();
|
151 |
|
|
dataTypeTemplate.apply(stylesheet, mode, context, pos, len,
|
152 |
|
|
fragment, null);
|
153 |
|
|
dataType = Expr.stringValue(fragment);
|
154 |
|
|
}
|
155 |
|
|
if (orderTemplate == null)
|
156 |
|
|
{
|
157 |
|
|
descending = false;
|
158 |
|
|
}
|
159 |
|
|
else
|
160 |
|
|
{
|
161 |
|
|
DocumentFragment fragment = doc.createDocumentFragment();
|
162 |
|
|
orderTemplate.apply(stylesheet, mode, context, pos, len,
|
163 |
|
|
fragment, null);
|
164 |
|
|
String order = Expr.stringValue(fragment);
|
165 |
|
|
descending = "descending".equals(order);
|
166 |
|
|
}
|
167 |
|
|
if (caseOrderTemplate == null)
|
168 |
|
|
{
|
169 |
|
|
caseOrder = DEFAULT;
|
170 |
|
|
}
|
171 |
|
|
else
|
172 |
|
|
{
|
173 |
|
|
DocumentFragment fragment = doc.createDocumentFragment();
|
174 |
|
|
caseOrderTemplate.apply(stylesheet, mode, context, pos, len,
|
175 |
|
|
fragment, null);
|
176 |
|
|
String co = Expr.stringValue(fragment);
|
177 |
|
|
caseOrder = "upper-first".equals(co) ? UPPER_FIRST :
|
178 |
|
|
"lower-first".equals(co) ? LOWER_FIRST :
|
179 |
|
|
DEFAULT;
|
180 |
|
|
}
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
boolean references(QName var)
|
184 |
|
|
{
|
185 |
|
|
if (select != null && select.references(var))
|
186 |
|
|
{
|
187 |
|
|
return true;
|
188 |
|
|
}
|
189 |
|
|
if (langTemplate != null && langTemplate.references(var))
|
190 |
|
|
{
|
191 |
|
|
return true;
|
192 |
|
|
}
|
193 |
|
|
if (dataTypeTemplate != null && dataTypeTemplate.references(var))
|
194 |
|
|
{
|
195 |
|
|
return true;
|
196 |
|
|
}
|
197 |
|
|
if (orderTemplate != null && orderTemplate.references(var))
|
198 |
|
|
{
|
199 |
|
|
return true;
|
200 |
|
|
}
|
201 |
|
|
if (caseOrderTemplate != null && caseOrderTemplate.references(var))
|
202 |
|
|
{
|
203 |
|
|
return true;
|
204 |
|
|
}
|
205 |
|
|
return false;
|
206 |
|
|
}
|
207 |
|
|
|
208 |
|
|
/**
|
209 |
|
|
* Provides a clone of this {@link SortKey}, using the given
|
210 |
|
|
* stylesheet as a context.
|
211 |
|
|
*
|
212 |
|
|
* @param stylesheet the stylesheet which provides context for the cloning.
|
213 |
|
|
* @return a clone of this instance.
|
214 |
|
|
*/
|
215 |
|
|
SortKey clone(Stylesheet stylesheet)
|
216 |
|
|
{
|
217 |
|
|
return new SortKey(select.clone(stylesheet),
|
218 |
|
|
langTemplate == null ? null : cloneAttributeValueTemplate(langTemplate, stylesheet),
|
219 |
|
|
dataTypeTemplate == null ? null : cloneAttributeValueTemplate(dataTypeTemplate, stylesheet),
|
220 |
|
|
orderTemplate == null ? null : cloneAttributeValueTemplate(orderTemplate, stylesheet),
|
221 |
|
|
caseOrderTemplate == null ? null : cloneAttributeValueTemplate(caseOrderTemplate, stylesheet));
|
222 |
|
|
}
|
223 |
|
|
|
224 |
|
|
/**
|
225 |
|
|
* Clones an attribute value template as created by
|
226 |
|
|
* {@link Stylesheet#parseAttributeValueTemplate(String, Node)}.
|
227 |
|
|
* The node may either by a literal node or an xsl:value-of expression.
|
228 |
|
|
*
|
229 |
|
|
* @param node the node to clone.
|
230 |
|
|
* @param stylesheet the stylesheet which provides context for the cloning.
|
231 |
|
|
* @return the cloned node.
|
232 |
|
|
*/
|
233 |
|
|
private TemplateNode cloneAttributeValueTemplate(TemplateNode node, Stylesheet stylesheet)
|
234 |
|
|
{
|
235 |
|
|
if (node instanceof ValueOfNode)
|
236 |
|
|
return ((ValueOfNode) node).clone(stylesheet);
|
237 |
|
|
return ((LiteralNode) node).clone(stylesheet);
|
238 |
|
|
}
|
239 |
|
|
}
|