public final class Result

  1. Object
  2. Result

An evaluator for a very small expression language to extract primitive types from structured information. This implementation is layered over the com.codename1.io.JSONParser and com.codename1.xml.XMLParser classes. This expression language allows applications to extract information from structured data returned by web services with minimal effort. You can read more about it here.

The expression language works a lot like a very small subset of XPath - the expression syntax uses the / character for sub-elements and square brackets for arrays.

Some sample expressions:

`Simple expression, get the title of the first photo element.

 /photos/photo[1]/title

 Globally find the first name of a person with a last name of 'Coolman'.

 //person[lastname='Coolman']/firstName

 Get the latitude value of the second last result element.

 /results[last()-1]/geometry/bounds/northeast/lat

 Get the names of players from Germany

 /tournament/player[@nationality='Germany']/name

 Get the purchase order numbers of any order with a lineitem worth over $5

 //order/lineitem[price > 5]/../@ponum
etc`

Fields

public static final String JSON = "json"
public static final String XML = "xml"
public static final char SEPARATOR = '/'
public static final char ARRAY_START = '['
public static final char ARRAY_END = ']'

Methods

public static Result fromContent(String content, String format) throws IllegalArgumentExceptionCreate an evaluator object from a structured content document (XML, JSON, etc) as a string.
public static Result fromContent(InputStream content, String format) throws IllegalArgumentException, IOExceptionCreate an evaluator object from a structured content document (XML, JSON, etc) input stream.
public static Result fromContent(Reader content, String format) throws IllegalArgumentException, IOExceptionCreate an evaluator object from a structured content document (XML, JSON, etc) input stream.
public static Result fromContent(Element content) throws IllegalArgumentExceptionCreate an evaluator object from a parsed XML DOM.
public static Result fromContent(Map content) throws IllegalArgumentExceptionCreate an evaluator object from parsed JSON content DOM.
public int hashCode()Returns a hashcode value for the object.
public boolean equals(Object other)Indicates whether some other object is “equal to” this one.
public String toString()Convert the object to a formatted structured content document.
public boolean getAsBoolean(String path) throws IllegalArgumentExceptionGet a boolean value from the requested path.
public int getAsInteger(String path) throws IllegalArgumentExceptionGet an integer value from the requested path.
public long getAsLong(String path) throws IllegalArgumentExceptionGet a long value from the requested path.
public double getAsDouble(String path) throws IllegalArgumentExceptionGet a double value from the requested path.
public String getAsString(String path) throws IllegalArgumentExceptionGet a string value from the requested path.
public Object get(String path) throws IllegalArgumentExceptionGet the object value from the requested path.
public int getSizeOfArray(String path) throws IllegalArgumentExceptionGet the size of an array at the requested path.
public String[] getAsStringArray(String path) throws IllegalArgumentExceptionGet an array of string values from the requested path.
public int[] getAsIntegerArray(String path) throws IllegalArgumentExceptionGet an array of values from the requested path.
public long[] getAsLongArray(String path) throws IllegalArgumentExceptionGet an array of values from the requested path.
public double[] getAsDoubleArray(String path) throws IllegalArgumentExceptionGet an array of values from the requested path.
public boolean[] getAsBooleanArray(String path) throws IllegalArgumentExceptionGet an array of values from the requested path.
public short[] getAsShortArray(String path) throws IllegalArgumentExceptionGet an array of short values from the requested path.
public float[] getAsFloatArray(String path) throws IllegalArgumentExceptionGet an array of float values from the requested path.
public byte[] getAsByteArray(String path) throws IllegalArgumentExceptionGet an array of byte values from the requested path.
public List getAsArray(String path) throws IllegalArgumentExceptionGet a List of values from the requested path.
public void mapNamespaceAlias(String namespaceURI, String alias)

Inherited methods

Field details

JSON

public static final String JSON = "json"

XML

public static final String XML = "xml"

SEPARATOR

public static final char SEPARATOR = '/'

ARRAY_START

public static final char ARRAY_START = '['

ARRAY_END

public static final char ARRAY_END = ']'

Method details

fromContent

public static Result fromContent(String content, String format) throws IllegalArgumentException
Create an evaluator object from a structured content document (XML, JSON, etc) as a string.

Parameters

content String
structured content document as a string.
format String
an identifier for the type of content passed (ie. xml, json, etc).

Returns

Result a result evaluator object

Throws

IllegalArgumentException
thrown if null content or format is passed.

fromContent

public static Result fromContent(InputStream content, String format) throws IllegalArgumentException, IOException

Create an evaluator object from a structured content document (XML, JSON, etc) input stream. Normally you would use this method within a content request implementation, for example:

ConnectionRequest request = new ConnectionRequest() {
    protected void readResponse(InputStream input) throws IOException {
        Result evaluator = Result.fromContent(input, Result.JSON);
        // ... evaluate the result here
   }
    // ... etc
};

Parameters

content InputStream
structured content document as a string.
format String
an identifier for the type of content passed (ie. xml, json, etc).

Returns

Result a result evaluator object

Throws

IllegalArgumentException
thrown if null content or format is passed.
IOException

fromContent

public static Result fromContent(Reader content, String format) throws IllegalArgumentException, IOException

Create an evaluator object from a structured content document (XML, JSON, etc) input stream. Normally you would use this method within a content request implementation, for example:

ConnectionRequest request = new ConnectionRequest() {
    protected void readResponse(InputStream input) throws IOException {
        Result evaluator = Result.fromContent(input, Result.JSON);
        // ... evaluate the result here
   }
    // ... etc
};

Parameters

content Reader
structured content document as a string.
format String
an identifier for the type of content passed (ie. xml, json, etc).

Returns

Result a result evaluator object

Throws

IllegalArgumentException
thrown if null content or format is passed.
IOException

fromContent

public static Result fromContent(Element content) throws IllegalArgumentException
Create an evaluator object from a parsed XML DOM.

Parameters

content Element
a parsed XML DOM.

Returns

Result a result evaluator object

Throws

IllegalArgumentException
thrown if null content is passed.

fromContent

public static Result fromContent(Map content) throws IllegalArgumentException
Create an evaluator object from parsed JSON content DOM.

Parameters

content Map
JSON content input stream

Returns

Result a result evaluator object

hashCode

public int hashCode()
Returns a hashcode value for the object.

equals

public boolean equals(Object other)
Indicates whether some other object is “equal to” this one.

toString

public String toString()
Convert the object to a formatted structured content document. For example, an XML or JSON document.

Returns

a structured content document as a string

getAsBoolean

public boolean getAsBoolean(String path) throws IllegalArgumentException

Get a boolean value from the requested path.

For example: JSON

{
"settings" : [
{
    "toggle" : "true",
    ... etc
}

Expression

boolean value = result.getAsBoolean("/settings[0]/toggle");

Parameters

path String
Path expression to evaluate

Returns

the value at the requested path

Throws

IllegalArgumentException
on error traversing the document, ie. traversing into an array without using subscripts.

getAsInteger

public int getAsInteger(String path) throws IllegalArgumentException

Get an integer value from the requested path.

For example: JSON

{
"settings"
{
    "connection"
    {
         "max_retries" : "20",
         ... etc
    }
}

Expression

int value = result.getAsInteger("//connection/max_retries");

Parameters

path String
Path expression to evaluate

Returns

the value at the requested path

Throws

IllegalException
on error traversing the document, ie. traversing into an array without using subscripts.
IllegalArgumentException

getAsLong

public long getAsLong(String path) throws IllegalArgumentException

Get a long value from the requested path.

For example: JSON

{
"settings"
{
    "connection"
    {
         "timeout_milliseconds" : "100000",
         ... etc
    }
}

Expression

long value = result.getAsLong("/settings/connection/timeout_milliseconds");

Parameters

path String
Path expression to evaluate

Returns

the value at the requested path

Throws

IllegalArgumentException
on error traversing the document, ie. traversing into an array without using subscripts.

getAsDouble

public double getAsDouble(String path) throws IllegalArgumentException

Get a double value from the requested path.

For example: JSON

{
 "geometry" : {
   "bounds" : {
     "northeast" : {
       "lat" : 42.94959820,
       "lng" : -81.24873959999999
      },
      "southwest" : {
        "lat" : 42.94830,
        "lng" : -81.24901740000001
      }
   },
   "location" : {
     "lat" : 42.94886990,
     "lng" : -81.24876030
   },
   "location_type" : "RANGE_INTERPOLATED",
   "viewport" : {
     "northeast" : {
        "lat" : 42.95029808029150,
        "lng" : -81.24752951970851
     },
     "southwest" : {
        "lat" : 42.94760011970850,
         "lng" : -81.25022748029151
     }
  }
  // etc

Expression

double neBoundsLat = result.getAsDouble("//bounds/northeast/lat");
double neBoundsLong = result.getAsDouble("//bounds/northeast/lng");
double swBoundsLat = result.getAsDouble("//bounds/southwest/lat");
double swBoundsLong = result.getAsDouble("//bounds/southwest/lng");

double memberDiscount = result.getAsDouble("pricing.members.members");

Parameters

path String
Path expression to evaluate

Returns

the value at the requested path

Throws

IllegalArgumentException
on error traversing the document, ie. traversing into an array without using subscripts.

getAsString

public String getAsString(String path) throws IllegalArgumentException

Get a string value from the requested path.

For example: JSON

{
"profile"
{
    "location"
    {
         "city" : "London",
         "region" : "Ontario",
         "country" : "Canada",
         ... etc
    },
}

Expression

String city = result.getAsDouble("//city");
String province = result.getAsDouble("//location//region");
String country = result.getAsDouble("profile//location//country");

Parameters

path String
Path expression to evaluate

Returns

the value at the requested path

Throws

IllegalArgumentException
on error traversing the document, ie. traversing into an array without using subscripts.

get

public Object get(String path) throws IllegalArgumentException
Get the object value from the requested path. This method may return a Map, List, String, or null.

Returns

the object at the given path, or null.

getSizeOfArray

public int getSizeOfArray(String path) throws IllegalArgumentException

Get the size of an array at the requested path.

For example: JSON

{
   "results" : [
      {
        "address_components" : [
          {
            "long_name" : "921-989",
            "short_name" : "921-989",
            "types" : [ "street_number" ]
          },
          {
            "long_name" : "Country Club Crescent",
            "short_name" : "Country Club Crescent",
            "types" : [ "route" ]
          },
          {
            "long_name" : "Ontario",
            "short_name" : "ON",
            "types" : [ "administrative_area_level_1", "political" ]
          },
          ... etc
      }
 }

Expression

int size = result.getSizeOfArray("/results[0]/address_components");
int size2 = result.getSizeOfArray("results");
int size3 = result.getSizeOfArray("/results[0]/address_components[2]/types");

Parameters

path String
Path expression to evaluate

Returns

the value at the requested path

Throws

IllegalArgumentException
on error traversing the document, ie. traversing into an array without using subscripts.

getAsStringArray

public String[] getAsStringArray(String path) throws IllegalArgumentException

Get an array of string values from the requested path.

For example: JSON

{
   "results" : [
      {
        "address_components" : [
          {
            "long_name" : "921-989",
            "short_name" : "921-989",
            "types" : [ "street_number" ]
          },
          {
            "long_name" : "Country Club Crescent",
            "short_name" : "Country Club Crescent",
            "types" : [ "route" ]
          },
          {
            "long_name" : "Ontario",
            "short_name" : "ON",
            "types" : [ "administrative_area_level_1", "political" ]
          },
          ... etc
      }
 }

Expression

String types[] = result
        .getAsStringArray("/results[0]/address_components[2]/types");

Parameters

path String
Path expression to evaluate

Returns

the value at the requested path

Throws

IllegalArgumentException
on error traversing the document, ie. traversing into an array without using subscripts.

getAsIntegerArray

public int[] getAsIntegerArray(String path) throws IllegalArgumentException

Get an array of values from the requested path.

For example: JSON

{
   "results" : [
      {
        "address_components" : [
          {
            "long_name" : "921-989",
            "short_name" : "921-989",
            "types" : [ "street_number" ]
          },
          {
            "long_name" : "Country Club Crescent",
            "short_name" : "Country Club Crescent",
            "types" : [ "route" ]
          },
          {
            "long_name" : "Ontario",
            "short_name" : "ON",
            "types" : [ "administrative_area_level_1", "political" ]
          },
          ... etc
      }
 }

Expression

String types[] = result
        .getAsStringArray("/results[0]/address_components[2]/types");

Parameters

path String
Path expression to evaluate

Returns

the value at the requested path

Throws

IllegalArgumentException
on error traversing the document, ie. traversing into an array without using subscripts.
NumberFormatException
if the value at path can not be converted to an integer.

getAsLongArray

public long[] getAsLongArray(String path) throws IllegalArgumentException

Get an array of values from the requested path.

String types[] = result
        .getAsStringArray("/results[0]/address_components[2]/types");

Parameters

path String
Path expression to evaluate

Returns

the value at the requested path

Throws

IllegalArgumentException
on error traversing the document, ie. traversing into an array without using subscripts.
NumberFormatException
if the value at path can not be converted to a long.

getAsDoubleArray

public double[] getAsDoubleArray(String path) throws IllegalArgumentException

Get an array of values from the requested path.

String types[] = result
        .getAsStringArray("/results[0]/address_components[2]/types");

Parameters

path String
Path expression to evaluate

Returns

the value at the requested path

Throws

IllegalArgumentException
on error traversing the document, ie. traversing into an array without using subscripts.
NumberFormatException
if the value at path can not be converted to a double.

getAsBooleanArray

public boolean[] getAsBooleanArray(String path) throws IllegalArgumentException

Get an array of values from the requested path.

String types[] = result
        .getAsStringArray("/results[0]/address_components[2]/types");

Parameters

path String
Path expression to evaluate

Returns

the value at the requested path

Throws

IllegalArgumentException
on error traversing the document, ie. traversing into an array without using subscripts.

getAsShortArray

public short[] getAsShortArray(String path) throws IllegalArgumentException

Get an array of short values from the requested path.

short values[] = result.getAsShortArray("//values");

Parameters

path String
Path expression to evaluate

Returns

the value at the requested path

Throws

IllegalArgumentException
on error traversing the document, ie. traversing into an array without using subscripts.
NumberFormatException
if the value at path can not be converted to a short.

getAsFloatArray

public float[] getAsFloatArray(String path) throws IllegalArgumentException

Get an array of float values from the requested path.

float values[] = result.getAsFloatArray("//values");

Parameters

path String
Path expression to evaluate

Returns

the value at the requested path

Throws

IllegalArgumentException
on error traversing the document, ie. traversing into an array without using subscripts.
NumberFormatException
if the value at path can not be converted to a float.

getAsByteArray

public byte[] getAsByteArray(String path) throws IllegalArgumentException

Get an array of byte values from the requested path.

byte values[] = result.getAsByteArray("//values");

Parameters

path String
Path expression to evaluate

Returns

the value at the requested path

Throws

IllegalArgumentException
on error traversing the document, ie. traversing into an array without using subscripts.
NumberFormatException
if the value at path can not be converted to a byte.

getAsArray

public List getAsArray(String path) throws IllegalArgumentException

Get a List of values from the requested path.

For example: JSON

{
   "results" : [
      {
        "address_components" : [
          {
            "long_name" : "921-989",
            "short_name" : "921-989",
            "types" : [ "street_number" ]
          },
          {
            "long_name" : "Country Club Crescent",
            "short_name" : "Country Club Crescent",
            "types" : [ "route" ]
          },
          ... etc
      }
 }

Expression

List addressComponents = result.getAsList("/results[0]/address_components");
result = Result.fromContent(addressComponents);
String longName = result.getAsString("[1]/long_name");

Parameters

path String
Path expression to evaluate

Returns

the value at the requested path

Throws

IllegalArgumentException
on error traversing the document, ie. traversing into an array without using subscripts.

mapNamespaceAlias

public void mapNamespaceAlias(String namespaceURI, String alias)