View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * @author Dmitri Plotnikov
33   * @version $Revision: 1523201 $ $Date: 2013-09-14 11:48:41 +0200 (Sa, 14 Sep 2013) $
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      * Returns true if the current node in the current context is a map
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      * Returns the number of nodes in the context that is passed as
113      * the first argument.
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 }