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  
18  package org.apache.commons.jxpath.ri.compiler;
19  
20  import java.util.ArrayList;
21  import java.util.Collection;
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.NodeSet;
29  import org.apache.commons.jxpath.Pointer;
30  
31  /**
32   */
33  public class TestFunctions {
34  
35      public static TestFunctions build(final int foo, final String bar) {
36          return new TestFunctions(foo, bar);
37      }
38  
39      public static Collection collection() {
40          final ArrayList list = new ArrayList();
41          list.add(new NestedTestBean("foo"));
42          list.add(new NestedTestBean("bar"));
43          return list;
44      }
45  
46      /**
47       * Returns the number of nodes in the context that is passed as the first argument.
48       */
49      public static int count(final ExpressionContext context, final Collection col) {
50          for (final Object element : col) {
51              if (!(element instanceof String)) {
52                  throw new IllegalArgumentException("Invalid argument");
53              }
54          }
55          return col.size();
56      }
57  
58      public static int countPointers(final NodeSet nodeSet) {
59          return nodeSet.getPointers().size();
60      }
61  
62      public static Boolean isInstance(final Object o, final Class c) {
63          return c.isInstance(o) ? Boolean.TRUE : Boolean.FALSE;
64      }
65  
66      /**
67       * Returns true if the current node in the current context is a map
68       */
69      public static boolean isMap(final ExpressionContext context) {
70          final Pointer ptr = context.getContextNodePointer();
71          return ptr == null ? false : ptr.getValue() instanceof Map;
72      }
73  
74      public static Collection items(final Collection arg) {
75          return arg;
76      }
77  
78      public static NodeSet nodeSet(final ExpressionContext context) {
79          final JXPathContext jxpathCtx = context.getJXPathContext();
80          final BasicNodeSet set = new BasicNodeSet();
81          set.add(jxpathCtx.getPointer("/beans[1]"));
82          set.add(jxpathCtx.getPointer("/beans[2]"));
83          return set;
84      }
85  
86      public static String path(final ExpressionContext context) {
87          return context.getContextNodePointer().asPath();
88      }
89  
90      public static String string(final String string) {
91          return string;
92      }
93  
94      private int foo;
95      private String bar;
96  
97      public TestFunctions() {
98      }
99  
100     public TestFunctions(final ExpressionContext context, final String bar) {
101         this.foo = ((Number) context.getContextNodePointer().getValue()).intValue();
102         this.bar = bar;
103     }
104 
105     public TestFunctions(final int foo, final Object object, final boolean another) {
106         this.foo = foo;
107         bar = String.valueOf(object);
108     }
109 
110     public TestFunctions(final int foo, final String bar) {
111         this.foo = foo;
112         this.bar = bar;
113     }
114 
115     public String className(final ExpressionContext context, final ExpressionContext child) {
116         return context.getContextNodePointer().asPath();
117     }
118 
119     public void doit() {
120     }
121 
122     public String getBar() {
123         return bar;
124     }
125 
126     public int getFoo() {
127         return foo;
128     }
129 
130     public String instancePath(final ExpressionContext context) {
131         return context.getContextNodePointer().asPath();
132     }
133 
134     public String pathWithSuffix(final ExpressionContext context, final String suffix) {
135         return context.getContextNodePointer().asPath() + suffix;
136     }
137 
138     public TestFunctions setFooAndBar(final int foo, final String bar) {
139         this.foo = foo;
140         this.bar = bar;
141         return this;
142     }
143 
144     @Override
145     public String toString() {
146         return "foo=" + foo + "; bar=" + bar;
147     }
148 }