
us.monoid.web.jp.javacc.json.jj Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of resty Show documentation
Show all versions of resty Show documentation
Simple Java client to talk to RESTful backends
/*@bgen(jjtree) Generated By:JJTree: Do not edit this line. json.jj */
/*@egen*/options
{
STATIC = false;
DEBUG_PARSER = false;
ERROR_REPORTING = true;
}
PARSER_BEGIN(JSONPathCompiler)
package us.monoid.web.jp.javacc;
import us.monoid.json.*;
import java.util.*;
public class JSONPathCompiler/*@bgen(jjtree)*/implements JSONPathCompilerTreeConstants/*@egen*/ {/*@bgen(jjtree)*/
protected JJTJSONPathCompilerState jjtree = new JJTJSONPathCompilerState();
/*@egen*/
public static class JSONPathExpr extends AbstractJSONExpr {
JSONPathExpr(int i) {
super (i);
}
public void dump(String prefix) {
System.out.println(prefix + toString());
if (children != null && children.length > 0) {
System.out.println(prefix + " [");
for (int i = 0; children != null && i < children.length; ++i) {
System.out.print(prefix + " (" + (i + 1) + ")");
SimpleNode c = (SimpleNode) children [i];
if (c == null) {
System.out.println("null");
} else {
c.dump(prefix + " ");
}
}
System.out.println(prefix + " ]");
}
}
public Object eval(Object o) throws JSONException {
if (o == null) return null;
switch (this.id) {
case JJTIMAGE:
{
return this.value;
}
case JJTEXPRESSION: {
if (getChildrenCount() == 0) return null;
for (int i = 0; o != null && i < getChildrenCount(); ++i) {
o = at(i).eval(o);
}
return o;
}
case JJTARRAY: {
if (this.value instanceof Integer) {
int index = Integer.class.cast(this.value);
if (o instanceof JSONArray) {
JSONArray array = JSONArray.class.cast(o);
return array.get(index);
} else {
return null;
}
} else { // AST contains a selector expression. evaluate it against all objects in array
if (o instanceof JSONArray) {
JSONArray array = JSONArray.class.cast(o);
// match first strategy. only one item is returned
Object result = null;
match:
for (int i = 0, len = array.length(); i < len; i++) {
Object item = array.get(i);
boolean test = at(0).test(item);
if (test) {
result = item; // evaluation can continue on this item
break;
}
}
return result;
}
}
}
case JJTOBJECT: {
if (o instanceof JSONObject) {
JSONObject json = JSONObject.class.cast(o);
return json.get(this.value.toString());
} else if (o instanceof JSONArray)
{
JSONArray array = JSONArray.class.cast(o);
// return all items
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy