Class Result

java.lang.Object
com.codename1.processing.Result

public final class Result extends Object

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`
  • Field Details

  • 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: structured content document as a string.

      • format: @param format an identifier for the type of content passed (ie. xml, json, etc).

      Returns

      Result a result evaluator object

      Throws
      • IllegalArgumentException: @throws IllegalArgumentException thrown if null content or format is passed.
      Throws:
      IllegalArgumentException
    • 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: structured content document as a string.

      • format: @param format an identifier for the type of content passed (ie. xml, json, etc).

      Returns

      Result a result evaluator object

      Throws
      • IllegalArgumentException: @throws IllegalArgumentException thrown if null content or format is passed.
      Throws:
      IllegalArgumentException
      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: structured content document as a string.

      • format: @param format an identifier for the type of content passed (ie. xml, json, etc).

      Returns

      Result a result evaluator object

      Throws
      • IllegalArgumentException: @throws IllegalArgumentException thrown if null content or format is passed.
      Throws:
      IllegalArgumentException
      IOException
    • fromContent

      public static Result fromContent(Element content) throws IllegalArgumentException

      Create an evaluator object from a parsed XML DOM.

      Parameters
      • content: a parsed XML DOM.
      Returns

      Result a result evaluator object

      Throws
      • IllegalArgumentException: thrown if null content is passed.
      Throws:
      IllegalArgumentException
    • fromContent

      public static Result fromContent(Map content) throws IllegalArgumentException

      Create an evaluator object from parsed JSON content DOM.

      Parameters
      • content: JSON content input stream
      Returns

      Result a result evaluator object

      Throws:
      IllegalArgumentException
    • hashCode

      public int hashCode()

      Returns a hashcode value for the object.

      See also
      • Object#hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object other)

      Indicates whether some other object is "equal to" this one.

      See also
      • Object#equals(Object)
      Overrides:
      equals in class Object
    • 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

      Overrides:
      toString in class Object
    • 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: Path expression to evaluate
      Returns

      the value at the requested path

      Throws
      • IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts.
      Throws:
      IllegalArgumentException
    • 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: Path expression to evaluate
      Returns

      the value at the requested path

      Throws
      • IllegalException: @throws IllegalException on error traversing the document, ie. traversing into an array without using subscripts.
      Throws:
      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: Path expression to evaluate
      Returns

      the value at the requested path

      Throws
      • IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts.
      Throws:
      IllegalArgumentException
    • 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: Path expression to evaluate
      Returns

      the value at the requested path

      Throws
      • IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts.
      Throws:
      IllegalArgumentException
    • 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: Path expression to evaluate
      Returns

      the value at the requested path

      Throws
      • IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts.
      Throws:
      IllegalArgumentException
    • 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.

      Parameters
      • path
      Returns

      the object at the given path, or null.

      Throws
      • IllegalArgumentException
      Throws:
      IllegalArgumentException
    • 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: Path expression to evaluate
      Returns

      the value at the requested path

      Throws
      • IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts.
      Throws:
      IllegalArgumentException
    • 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: Path expression to evaluate
      Returns

      the value at the requested path

      Throws
      • IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts.
      Throws:
      IllegalArgumentException
    • 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: Path expression to evaluate
      Returns

      the value at the requested path

      Throws
      • IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts.

      • NumberFormatException: @throws NumberFormatException if the value at path can not be converted to an integer.

      Throws:
      IllegalArgumentException
    • 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: Path expression to evaluate
      Returns

      the value at the requested path

      Throws
      • IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts.

      • NumberFormatException: @throws NumberFormatException if the value at path can not be converted to a long.

      Throws:
      IllegalArgumentException
    • 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: Path expression to evaluate
      Returns

      the value at the requested path

      Throws
      • IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts.

      • NumberFormatException: @throws NumberFormatException if the value at path can not be converted to a double.

      Throws:
      IllegalArgumentException
    • 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: Path expression to evaluate
      Returns

      the value at the requested path

      Throws
      • IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts.
      Throws:
      IllegalArgumentException
    • 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: Path expression to evaluate
      Returns

      the value at the requested path

      Throws
      • IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts.

      • NumberFormatException: @throws NumberFormatException if the value at path can not be converted to a short.

      Throws:
      IllegalArgumentException
    • 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: Path expression to evaluate
      Returns

      the value at the requested path

      Throws
      • IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts.

      • NumberFormatException: @throws NumberFormatException if the value at path can not be converted to a float.

      Throws:
      IllegalArgumentException
    • 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: Path expression to evaluate
      Returns

      the value at the requested path

      Throws
      • IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts.

      • NumberFormatException: @throws NumberFormatException if the value at path can not be converted to a byte.

      Throws:
      IllegalArgumentException
    • 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: Path expression to evaluate
      Returns

      the value at the requested path

      Throws
      • IllegalArgumentException: @throws IllegalArgumentException on error traversing the document, ie. traversing into an array without using subscripts.
      Throws:
      IllegalArgumentException
    • mapNamespaceAlias

      public void mapNamespaceAlias(String namespaceURI, String alias)