    1   /*
    2    * Copyright 1999-2004 The Apache Software Foundation
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.jxpath.ri.compiler;
    17
    18  import java.util.ArrayList;
    19  import java.util.Collection;
    20  import java.util.Iterator;
    21  import java.util.Map;
    22
    23  import org.apache.commons.jxpath.BasicNodeSet;
    24  import org.apache.commons.jxpath.ExpressionContext;
    25  import org.apache.commons.jxpath.JXPathContext;
    26  import org.apache.commons.jxpath.NestedTestBean;
    27  import org.apache.commons.jxpath.Pointer;
    28  import org.apache.commons.jxpath.NodeSet;
    29
    30  /***
    31   * @author Dmitri Plotnikov
    32   * @version $Revision: 1.10 $ $Date: 2004/03/25 05:42:01 $
    33   */
    34  public class TestFunctions {
    35
    36      private int foo;
    37      private String bar;
    38
    39      public TestFunctions() {
    40      }
    41
    42      public TestFunctions(int foo, String bar) {
    43          this.foo = foo;
    44          this.bar = bar;
    45      }
    46
    47      public TestFunctions(ExpressionContext context, String bar) {
    48          this.foo =
    49              ((Number) context.getContextNodePointer().getValue()).intValue();
    50          this.bar = bar;
    51      }
    52
    53      public TestFunctions(int foo, Object object, boolean another) {
    54          this.foo = foo;
    55          bar = String.valueOf(object);
    56      }
    57
    58      public int getFoo() {
    59          return foo;
    60      }
    61
    62      public String getBar() {
    63          return bar;
    64      }
    65
    66      public void doit() {
    67      }
    68
    69      public TestFunctions setFooAndBar(int foo, String bar) {
    70          this.foo = foo;
    71          this.bar = bar;
    72          return this;
    73      }
    74
    75      public static TestFunctions build(int foo, String bar) {
    76          return new TestFunctions(foo, bar);
    77      }
    78
    79      public String toString() {
    80          return "foo=" + foo + "; bar=" + bar;
    81      }
    82
    83      public static String path(ExpressionContext context) {
    84          return context.getContextNodePointer().asPath();
    85      }
    86
    87      public String instancePath(ExpressionContext context) {
    88          return context.getContextNodePointer().asPath();
    89      }
    90
    91      public String pathWithSuffix(ExpressionContext context, String suffix) {
    92          return context.getContextNodePointer().asPath() + suffix;
    93      }
    94
    95      public String className(
    96          ExpressionContext context,
    97          ExpressionContext child)
    98      {
    99          return context.getContextNodePointer().asPath();
    100     }
    101
    102     /***
    103      * Returns true if the current node in the current context is a map
    104      */
    105     public static boolean isMap(ExpressionContext context) {
    106         Pointer ptr = context.getContextNodePointer();
    107         return ptr == null ? false : (ptr.getValue() instanceof Map);
    108     }
    109
    110     /***
    111      * Returns the number of nodes in the context that is passed as
    112      * the first argument.
    113      */
    114     public static int count(ExpressionContext context, Collection col) {
    115         for (Iterator iter = col.iterator(); iter.hasNext();) {
    116             Object element = iter.next();
    117             if (!(element instanceof String)) {
    118                 throw new RuntimeException("Invalid argument");
    119             }
    120         };
    121         return col.size();
    122     }
    123
    124     public static int countPointers(NodeSet nodeSet) {
    125         return nodeSet.getPointers().size();
    126     }
    127
    128     public static String string(String string) {
    129         return string;
    130     }
    131
    132     public static Collection collection() {
    133         ArrayList list = new ArrayList();
    134         list.add(new NestedTestBean("foo"));
    135         list.add(new NestedTestBean("bar"));
    136         return list;
    137     }
    138
    139     public static NodeSet nodeSet(ExpressionContext context) {
    140         JXPathContext jxpathCtx = context.getJXPathContext();
    141         BasicNodeSet set = new BasicNodeSet();
    142         set.add(jxpathCtx.getPointer("/beans[1]"));
    143         set.add(jxpathCtx.getPointer("/beans[2]"));
    144
    145         return set;
    146     }
    147
    148     public static Collection items(Collection arg) {
    149         return arg;
    150     }
    151 }