01: // Copyright (c) 2003 Per M.A. Bothner.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.kawa.xml;
05:
06: import gnu.bytecode.*;
07: import gnu.mapping.*;
08: import gnu.expr.*;
09:
10: /** Sort argument nodes in document order.
11: * Uses the SortedNodes class to do the actual work. */
12:
13: public class SortNodes extends Procedure1 implements Inlineable {
14: public static final SortNodes sortNodes = new SortNodes();
15:
16: public Object apply1(Object values) {
17: SortedNodes nodes = new SortedNodes();
18: Values.writeValues(values, nodes);
19: if (nodes.count > 1)
20: return nodes;
21: else if (nodes.count == 0)
22: return Values.empty;
23: else
24: return nodes.get(0);
25: }
26:
27: public void compile(ApplyExp exp, Compilation comp, Target target) {
28: Expression[] args = exp.getArgs();
29: if (args.length != 1 || !comp.mustCompile)
30: ApplyExp.compile(exp, comp, target);
31: else {
32: Method resultMethod;
33: if (target instanceof ConsumerTarget
34: || (target instanceof StackTarget && target
35: .getType()
36: .isSubtype(Compilation.typeValues)))
37: resultMethod = null;
38: else
39: resultMethod = canonicalizeMethod;
40: ConsumerTarget.compileUsingConsumer(args[0], comp, target,
41: makeSortedNodesMethod, resultMethod);
42: }
43: }
44:
45: public Type getReturnType(Expression[] args) {
46: return Compilation.typeObject;
47: }
48:
49: public static final ClassType typeSortedNodes = ClassType
50: .make("gnu.kawa.xml.SortedNodes");
51: public static final Method makeSortedNodesMethod = typeSortedNodes
52: .getDeclaredMethod("<init>", 0);
53: public static final Method canonicalizeMethod = Compilation.typeValues
54: .getDeclaredMethod("canonicalize", 0);
55: }
|