1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jxpath.ri.compiler;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Iterator;
22 import java.util.Map;
23
24 import org.apache.commons.jxpath.BasicNodeSet;
25 import org.apache.commons.jxpath.ExpressionContext;
26 import org.apache.commons.jxpath.JXPathContext;
27 import org.apache.commons.jxpath.NestedTestBean;
28 import org.apache.commons.jxpath.Pointer;
29 import org.apache.commons.jxpath.NodeSet;
30
31
32
33
34
35 public class TestFunctions {
36
37 private int foo;
38 private String bar;
39
40 public TestFunctions() {
41 }
42
43 public TestFunctions(int foo, String bar) {
44 this.foo = foo;
45 this.bar = bar;
46 }
47
48 public TestFunctions(ExpressionContext context, String bar) {
49 this.foo =
50 ((Number) context.getContextNodePointer().getValue()).intValue();
51 this.bar = bar;
52 }
53
54 public TestFunctions(int foo, Object object, boolean another) {
55 this.foo = foo;
56 bar = String.valueOf(object);
57 }
58
59 public int getFoo() {
60 return foo;
61 }
62
63 public String getBar() {
64 return bar;
65 }
66
67 public void doit() {
68 }
69
70 public TestFunctions setFooAndBar(int foo, String bar) {
71 this.foo = foo;
72 this.bar = bar;
73 return this;
74 }
75
76 public static TestFunctions build(int foo, String bar) {
77 return new TestFunctions(foo, bar);
78 }
79
80 public String toString() {
81 return "foo=" + foo + "; bar=" + bar;
82 }
83
84 public static String path(ExpressionContext context) {
85 return context.getContextNodePointer().asPath();
86 }
87
88 public String instancePath(ExpressionContext context) {
89 return context.getContextNodePointer().asPath();
90 }
91
92 public String pathWithSuffix(ExpressionContext context, String suffix) {
93 return context.getContextNodePointer().asPath() + suffix;
94 }
95
96 public String className(
97 ExpressionContext context,
98 ExpressionContext child)
99 {
100 return context.getContextNodePointer().asPath();
101 }
102
103
104
105
106 public static boolean isMap(ExpressionContext context) {
107 Pointer ptr = context.getContextNodePointer();
108 return ptr == null ? false : (ptr.getValue() instanceof Map);
109 }
110
111
112
113
114
115 public static int count(ExpressionContext context, Collection col) {
116 for (Iterator iter = col.iterator(); iter.hasNext();) {
117 Object element = iter.next();
118 if (!(element instanceof String)) {
119 throw new RuntimeException("Invalid argument");
120 }
121 }
122 return col.size();
123 }
124
125 public static int countPointers(NodeSet nodeSet) {
126 return nodeSet.getPointers().size();
127 }
128
129 public static String string(String string) {
130 return string;
131 }
132
133 public static Collection collection() {
134 ArrayList list = new ArrayList();
135 list.add(new NestedTestBean("foo"));
136 list.add(new NestedTestBean("bar"));
137 return list;
138 }
139
140 public static NodeSet nodeSet(ExpressionContext context) {
141 JXPathContext jxpathCtx = context.getJXPathContext();
142 BasicNodeSet set = new BasicNodeSet();
143 set.add(jxpathCtx.getPointer("/beans[1]"));
144 set.add(jxpathCtx.getPointer("/beans[2]"));
145
146 return set;
147 }
148
149 public static Collection items(Collection arg) {
150 return arg;
151 }
152
153 public static Boolean isInstance(Object o, Class c) {
154 return c.isInstance(o) ? Boolean.TRUE : Boolean.FALSE;
155 }
156
157 }