001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jxpath.ri.compiler;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Iterator;
021: import java.util.Map;
022:
023: import org.apache.commons.jxpath.BasicNodeSet;
024: import org.apache.commons.jxpath.ExpressionContext;
025: import org.apache.commons.jxpath.JXPathContext;
026: import org.apache.commons.jxpath.NestedTestBean;
027: import org.apache.commons.jxpath.Pointer;
028: import org.apache.commons.jxpath.NodeSet;
029:
030: /**
031: * @author Dmitri Plotnikov
032: * @version $Revision: 1.10 $ $Date: 2004/03/25 05:42:01 $
033: */
034: public class TestFunctions {
035:
036: private int foo;
037: private String bar;
038:
039: public TestFunctions() {
040: }
041:
042: public TestFunctions(int foo, String bar) {
043: this .foo = foo;
044: this .bar = bar;
045: }
046:
047: public TestFunctions(ExpressionContext context, String bar) {
048: this .foo = ((Number) context.getContextNodePointer().getValue())
049: .intValue();
050: this .bar = bar;
051: }
052:
053: public TestFunctions(int foo, Object object, boolean another) {
054: this .foo = foo;
055: bar = String.valueOf(object);
056: }
057:
058: public int getFoo() {
059: return foo;
060: }
061:
062: public String getBar() {
063: return bar;
064: }
065:
066: public void doit() {
067: }
068:
069: public TestFunctions setFooAndBar(int foo, String bar) {
070: this .foo = foo;
071: this .bar = bar;
072: return this ;
073: }
074:
075: public static TestFunctions build(int foo, String bar) {
076: return new TestFunctions(foo, bar);
077: }
078:
079: public String toString() {
080: return "foo=" + foo + "; bar=" + bar;
081: }
082:
083: public static String path(ExpressionContext context) {
084: return context.getContextNodePointer().asPath();
085: }
086:
087: public String instancePath(ExpressionContext context) {
088: return context.getContextNodePointer().asPath();
089: }
090:
091: public String pathWithSuffix(ExpressionContext context,
092: String suffix) {
093: return context.getContextNodePointer().asPath() + suffix;
094: }
095:
096: public String className(ExpressionContext context,
097: ExpressionContext child) {
098: return context.getContextNodePointer().asPath();
099: }
100:
101: /**
102: * Returns true if the current node in the current context is a map
103: */
104: public static boolean isMap(ExpressionContext context) {
105: Pointer ptr = context.getContextNodePointer();
106: return ptr == null ? false : (ptr.getValue() instanceof Map);
107: }
108:
109: /**
110: * Returns the number of nodes in the context that is passed as
111: * the first argument.
112: */
113: public static int count(ExpressionContext context, Collection col) {
114: for (Iterator iter = col.iterator(); iter.hasNext();) {
115: Object element = iter.next();
116: if (!(element instanceof String)) {
117: throw new RuntimeException("Invalid argument");
118: }
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: }
|