Package org.geotools.mbstyle.parse
Class MBObjectParser
Object
MBObjectParser
Helper class used to perform JSON traversal of
JSONObject
and perform Expression and Filter conversions.
These utilities are used by the MBStyle to convert JSON to simple Java objects, process functions and perform common
JSON manipulation tasks.
Access methods
Example of transformation to Expression, using the fallback value if provided: MBObjectParser parse = new MBObjectParser( ff );
Expression fillOpacity = parse.percent( json, "fill-opacity", 1.0 );
Expression fillColor = parse.color( json, "fill-color", Color.BLACK );
Get Methods
Generic "get" methods are also available for safely accessing required fields. These methods will throw a validation error if the required tag was is not available. String id = parse.get("id");
String visibility = parse.getBoolean("visibility");
String source = parse.get("source");
Non generic "get" methods, like paint(JSONObject)
, are in position to provide an appropriate default value.
JSONObject paint = parse.paint( layer );
- Author:
- Torben Barsballe (Boundless)
-
Constructor Summary
ConstructorsConstructorDescriptionMBObjectParser
(Class<?> context) Parser used to in the provided context.MBObjectParser
(Class<MBFilter> context, MBObjectParser parse) Copy constructor allowing reuse of factories, while returning correctcontext
. -
Method Summary
Modifier and TypeMethodDescription<T> T[]
Lookup an array of the provided type in the provided JSONObject at 'tag', with a fallback if that tag is not found.double[]
Convert to double[]int[]
Convert to int[]Convert json to Expression boolean, or a function.Handles literal color definitions supplied as a string, returning aLiteral
.Convert json to GeoTools Expression color, or a function.Utility method used to convert enumerations to an appropriate GeoTools literal string.convertToColor
(String color) Converts color definitions supplied as a string to Color objects:displacement
(JSONObject json, String name, Displacement fallback) Maps a json value at 'tag' in the provided JSONObject to aDisplacement
.<T extends Enum<?>>
ExpressionenumToExpression
(JSONObject json, String name, Class<T> enumeration, T fallback) Parse a Mapbox enumeration property to a GeoTools Expression that evaluates to a GeoTools constant (supports the property being specified as a mapbox function).Convert the provided object to a font Expression (or function).get
(JSONArray json, int index) Quickly access required json index (as a String).Quickly access required json tag (as a String).Quickly access optional json tag.getBoolean
(JSONArray json, int index) Look up a Boolean in the providedJSONArray
at the provided index, or throw anMBFormatException
.getBoolean
(JSONObject json, String name) Look up a Boolean in the providedJSONObject
at the provided 'tag', or thrown anMBFormatException
.getBoolean
(JSONObject json, String name, Boolean fallback) Look up a Boolean in the providedJSONObject
at the provided 'tag', or thrown anMBFormatException
, with a fallback if the json is null or contains no such 'tag'.<T extends Enum<?>>
TConvert a String value to an enum value, ignoring case, with the provided fallback.Shared FilterFactoryJSONArray
getJSONArray
(JSONObject json, String name) Access json contains a JSONArray for the indicated tag.JSONArray
getJSONArray
(JSONObject json, String name, JSONArray fallback) Access a JSONArray at the provided tag in the provided JSONObject, with a fallback if no JSONArray is found at that tag.JSONObject
getJSONObject
(JSONObject json, String name) Access JSONObject for the indicated tag.JSONObject
getJSONObject
(JSONObject json, String name, JSONObject fallback) Access JSONObject for the indicated tag, with the provided fallback if the the json does not contain a JSONObject for that tag.double
getNumeric
(JSONArray json, int index) Look up a double in the providedJSONArray
at the provided index, or throw anMBFormatException
.getNumeric
(JSONObject json, String name) Look up a Double in the providedJSONObject
at the provided 'tag', or thrown anMBFormatException
.Shared StyleFactoryboolean
isArray
(JSONArray json, int index) True if array has an array element at the provided index, False otherwise.boolean
True if json has an array value for the provided name, False otherwise.boolean
isBoolean
(JSONArray json, int index) True if array has a boolean element at the provided index, False otherwise.boolean
True if json has a boolean value for the provided name, False otherwise.boolean
isDefined
(JSONArray json, int index) True if array has an element at the provided index, False otherwise.boolean
True if json has a value for the provided name, False otherwise.boolean
isNumeric
(JSONArray json, int index) True if array has a numeric element at the provided index, False otherwise.boolean
True if json has a numeric value for the provided name, False otherwise.boolean
isObject
(JSONArray json, int index) True if array has an object element at the provided index, False otherwise.boolean
True if json has an object value for the provided name, False otherwise.boolean
isPropertyDefined
(JSONObject json, String propertyName) Deprecated.boolean
isString
(JSONArray json, int index) True if array has a string element at the provided index, False otherwise.boolean
True if json has a string value for the provided name, False otherwise.JSONArray
Casts the provided obj to a JSONArray (otherwise throws anMBFormatException
).JSONArray
Casts the provided obj to a JSONArray (otherwise throws anMBFormatException
with the provided message).JSONObject
Casts the provided obj to a JSONObject (safely reporting format exception, with the provided message).JSONObject
jsonObject
(Object obj) Casts the provided obj to a JSONObject (safely reporting format exceptionJSONObject
layout
(JSONObject layer) Safely look up layout in provided layer json.number
(JSONArray json, int index) Convert the value at 'index' in the provided JSONArray to a numeric Expression or a Function.Convert the value in the provided JSONArray at index to a numeric Expression, or a function, with a fallback if the json is null.Convert the value in the provided JSONObject at 'tag' to a numeric GeoTools Expression, or a Function.Convert the value in the provided JSONObject at 'tag' to a numeric Expression, or a function, with a fallback if the json is null.<T> T
Optional lookup, will return fallback if not available.JSONObject
paint
(JSONObject layer) Safely look up paint in provided layer json.percentage
(JSONObject json, String name) Convert json to Expression number between 0 and 1, or a function.percentage
(JSONObject json, String name, Number fallback) Convert json to Expression number between 0 and 1, or a function.<T> T
Retrieve an object of the provided type in the JSONArray at this index, throwing anMBFormatException
if no object of that type is found at that index of the array.<T> T
Retrieve an object of the provided type in the JSONObject at this tag, throwing anMBFormatException
if no object of that type is found at that tag in the object.string
(JSONArray json, int index) Convert the value in the provided JSONArray at index to a string Literal, or a Function.Convert json to GeoTools Expression string, or a function.value
(JSONArray json, int index) Access a literal value (string, numeric, or boolean).Access a literal value (string, numeric, or boolean).
-
Constructor Details
-
MBObjectParser
Parser used to in the provided context.- Parameters:
context
- Context this parser is being used in (for better error reporting)
-
MBObjectParser
Copy constructor allowing reuse of factories, while returning correctcontext
.- Parameters:
context
- Context this parser is being used in (for better error reporting)parse
- Parent parser used to configure factories consistently
-
-
Method Details
-
getFilterFactory
Shared FilterFactory -
getStyleFactory
Shared StyleFactory -
paint
public JSONObject paint(JSONObject layer) Safely look up paint in provided layer json.Paint is optional, returning an empty JSONObject (to prevent the need for null checks).
- Returns:
- paint definition, optional so may be an empty JSONObject
- Throws:
MBFormatException
- If paint is provided as an invalid type (such as boolean).
-
layout
public JSONObject layout(JSONObject layer) Safely look up layout in provided layer json.Layout is optional, returning an empty JSONObject (to prevent the need for null checks).
- Returns:
- layout definition, optional so may be an empty JSONObject
- Throws:
MBFormatException
- If layout is provided as an invalid type (such as boolean).
-
getJSONObject
Access JSONObject for the indicated tag.Confirms json contains the provided tag as a JSONObject, correctly throwing
MBFormatException
if not available.- Parameters:
json
- The JSONObject in which to lookup the provided tag and return a JSONObjectname
- The tag to look up in the provided JSONObject- Returns:
- The JSONObject at the provided tag
- Throws:
MBFormatException
- If JSONObject not available for the provided tag
-
getJSONObject
Access JSONObject for the indicated tag, with the provided fallback if the the json does not contain a JSONObject for that tag.- Parameters:
json
- The JSONObject in which to lookup the provided tag and return a JSONObjectname
- The tag to look up in the provided JSONObjectfallback
- The JSONObject to return if the provided json does not contain a JSONObject for that tag.- Returns:
- The JSONObject at the provided tag, or the fallback object.
-
getJSONArray
Access json contains a JSONArray for the indicated tag.Confirms json contains the provided tag as a JSONArray, correctly throwing
MBFormatException
if not available.- Returns:
- JSONObject
- Throws:
MBFormatException
- If JSONObject not available for the provided tag
-
getJSONArray
Access a JSONArray at the provided tag in the provided JSONObject, with a fallback if no JSONArray is found at that tag.- Parameters:
json
- The JSONObject in which to lookup the provided tag and return a JSONArrayname
- The tag to look up in the provided JSONObjectfallback
- The JSONArray to return if the provided json does not contain a JSONArray for that tag.- Returns:
- The JSONArray at the provided tag, or the fallback JSONArray.
-
value
Access a literal value (string, numeric, or boolean).- Parameters:
json
- JSONArray object to parse.index
- position in the provided array for which to retrieve a value.- Returns:
- required string, numeric or boolean
- Throws:
MBFormatException
- if required index not available.
-
value
Access a literal value (string, numeric, or boolean).- Returns:
- required string, numeric or boolean
- Throws:
MBFormatException
- if required tag not available.
-
get
Quickly access required json index (as a String).- Returns:
- required string
- Throws:
MBFormatException
- if required index not available.
-
get
Quickly access required json tag (as a String).- Parameters:
json
- The object in which to look up the Stringname
- Tag to lookup- Returns:
- required string
- Throws:
MBFormatException
- if required tag not available.
-
get
Quickly access optional json tag.- Parameters:
json
- The object in which to look up the Stringname
- Tag to lookupfallback
- default string- Returns:
- required string, or fallback if unavailable
-
getNumeric
public double getNumeric(JSONArray json, int index) Look up a double in the providedJSONArray
at the provided index, or throw anMBFormatException
.- Parameters:
json
- The array in which to look up the doubleindex
- The index at which to look up the double- Returns:
- The double from the array at index.
-
getNumeric
Look up a Double in the providedJSONObject
at the provided 'tag', or thrown anMBFormatException
.- Parameters:
json
- The object in which to look up the Doublename
- The tag at which to look up the Double- Returns:
- The Double from the object at 'tag'
-
getBoolean
Look up a Boolean in the providedJSONArray
at the provided index, or throw anMBFormatException
.- Parameters:
json
- The array in which to look up the Booleanindex
- The index at which to look up the Boolean- Returns:
- The Boolean from the array at index.
-
getBoolean
Look up a Boolean in the providedJSONObject
at the provided 'tag', or thrown anMBFormatException
.- Parameters:
json
- The object in which to look up the Booleanname
- The tag at which to look up the Boolean- Returns:
- The Boolean from the object at 'tag'
-
getBoolean
Look up a Boolean in the providedJSONObject
at the provided 'tag', or thrown anMBFormatException
, with a fallback if the json is null or contains no such 'tag'.- Parameters:
json
- The object in which to look up the Booleanname
- The tag at which to look up the Booleanfallback
- The value to return if the json is null or contains no such 'tag'.- Returns:
- The Boolean from the object at 'tag', or the fallback value
-
require
Retrieve an object of the provided type in the JSONArray at this index, throwing anMBFormatException
if no object of that type is found at that index of the array.- Type Parameters:
T
- Class to return- Parameters:
type
- The type of the object to retrieve.json
- The JSONArray in which to retrieve the object.index
- The index in the JSONArray at which to retrieve the object.- Returns:
- The object of the required type in the array at index.
-
require
Retrieve an object of the provided type in the JSONObject at this tag, throwing anMBFormatException
if no object of that type is found at that tag in the object.- Type Parameters:
T
- Class to return- Parameters:
type
- The type of the object to retrieve.json
- The JSONObject in which to retrieve the object.name
- The index in the JSONObject at which to retrieve the object.- Returns:
- The object of the required type in the JSONObject at the tag.
-
optional
Optional lookup, will return fallback if not available.- Type Parameters:
T
- Class to return- Parameters:
type
- Type to lookupjson
- The JSONObject in which to lookup the valuename
- The tag at which to lookupthe value in the JSONObjectfallback
- The fallback value to use if the JSONObject is null or does not contain the provided tag- Returns:
- value for the provided tag, or fallback if not available
- Throws:
MBFormatException
- If value is found and is not the expected type
-
getEnum
public <T extends Enum<?>> T getEnum(JSONObject json, String name, Class<T> enumeration, T fallback) Convert a String value to an enum value, ignoring case, with the provided fallback.- Type Parameters:
T
- Enumeration type to return- Parameters:
json
- The json object to parse the value fromname
- The key used to retrieve the value from the json.enumeration
- The enum to convert the value to.fallback
- default value if json is null- Returns:
- The enum value from the string, or the fallback value.
- Throws:
MBFormatException
- if the value is not a String, or it is not a valid value for the enumeration.
-
enumToExpression
public <T extends Enum<?>> Expression enumToExpression(JSONObject json, String name, Class<T> enumeration, T fallback) Parse a Mapbox enumeration property to a GeoTools Expression that evaluates to a GeoTools constant (supports the property being specified as a mapbox function).For example, converts
LineMBLayer.LineJoin.BEVEL
to an expression that evaluates to the String value "bevel", orLineMBLayer.LineJoin.MITER
to an expression that evaluates to "mitre".- Parameters:
json
- The json object containing the propertyname
- The json key corresponding to the propertyenumeration
- The Mapbox enumeration that the value should be an instance offallback
- The fallback enumeration value, if the value is missing or invalid for the provided enumeration.- Returns:
- A GeoTools expression corresponding to the Mapbox enumeration value, evaluating to a GeoTools constant.
-
constant
Utility method used to convert enumerations to an appropriate GeoTools literal string.Any conversion between mapbox constants and geotools constants will be done here.
- Parameters:
value
- The value to be converted to the appropriate GeoTools literalenumeration
- The type of the mapbox enumeration- Returns:
- Literal, or null if unavailable
-
jsonObject
Casts the provided obj to a JSONObject (safely reporting format exception- Returns:
- JSONObject
- Throws:
MBFormatException
-
jsonObect
Casts the provided obj to a JSONObject (safely reporting format exception, with the provided message).- Parameters:
obj
- The object to castmessage
- The message for the exception of the object is not a JSONObject- Returns:
- The object, cast to JSONObject
- Throws:
MBFormatException
-
jsonArray
Casts the provided obj to a JSONArray (otherwise throws anMBFormatException
).- Parameters:
obj
- The object to cast- Returns:
- The object, cast to JSONArray
- Throws:
MBFormatException
-
jsonArray
Casts the provided obj to a JSONArray (otherwise throws anMBFormatException
with the provided message).- Parameters:
obj
- The object to castmessage
- The message for the exception of the object is not a JSONArray- Returns:
- The object, cast to JSONArray
- Throws:
MBFormatException
-
array
Lookup an array of the provided type in the provided JSONObject at 'tag', with a fallback if that tag is not found. Throws an exception if that tag is something other than an array, or if its contents cannot be cast to type.- Parameters:
type
- The type of the arrayjson
- The JSONObject in which to look up the arrayname
- The tag at which to look up the arrayfallback
- The fallback array- Returns:
- An array of the provided type, or the fallback array.
-
array
Convert to int[] -
array
Convert to double[] -
percentage
Convert json to Expression number between 0 and 1, or a function.- Parameters:
json
- json representation- Returns:
- Percentage GeoTools Expression based on provided json, or null if not provided
- Throws:
MBFormatException
-
percentage
public Expression percentage(JSONObject json, String name, Number fallback) throws MBFormatException Convert json to Expression number between 0 and 1, or a function.- Parameters:
json
- json representationname
- The tag at which to look up the percentagefallback
- default value if json is null- Returns:
- Percentage GeoTools Expression based on provided json, or literal if json was null.
- Throws:
MBFormatException
-
font
Convert the provided object to a font Expression (or function).- Parameters:
json
- The json context of the object, used for error messages.name
- Tag used to lookup font value- Returns:
- Font GeoTools Expression for the provided object
- Throws:
MBFormatException
-
number
Convert the value at 'index' in the provided JSONArray to a numeric Expression or a Function.- Parameters:
json
- The JSONArray in which to look up a valueindex
- The index in the JSONArray- Returns:
- Numeric GeoTools Expression based on provided json, or null.
- Throws:
MBFormatException
-
number
Convert the value in the provided JSONArray at index to a numeric Expression, or a function, with a fallback if the json is null.- Parameters:
json
- The JSONArray in which to look up the valueindex
- The index in the JSONArray at which to look up the valuefallback
- default value if json is null- Returns:
- Numeric GeoTools Expression based on provided json, or Literal if json was null.
- Throws:
MBFormatException
-
number
Convert the value in the provided JSONObject at 'tag' to a numeric GeoTools Expression, or a Function.- Parameters:
json
- The JSONObject in which to look up the valuename
- The tag in the JSONObject at which to look up the value- Returns:
- Numeric GeoTools Expression based on provided json, or null
- Throws:
MBFormatException
-
number
Convert the value in the provided JSONObject at 'tag' to a numeric Expression, or a function, with a fallback if the json is null.- Parameters:
json
- The JSONObject in which to look up the valuename
- The tag in the JSONObject at which to look up the valuefallback
- default value if the JSONObject is null- Returns:
- Numeric GeoTools Expression based on provided json, or fallback Literal if json was null.
- Throws:
MBFormatException
-
string
Convert the value in the provided JSONArray at index to a string Literal, or a Function.- Parameters:
json
- The JSONArray in which to look up the valueindex
- The index in the JSONArray at which to look up the value- Returns:
- String GeoTools Expression based on provided json, or literal if json was null.
-
string
Convert json to GeoTools Expression string, or a function.- Parameters:
json
- json representationfallback
- default value if json is null- Returns:
- String GeoTools Expression based on provided json, or literal if json was null.
- Throws:
MBFormatException
-
color
Convert json to GeoTools Expression color, or a function.- Parameters:
json
- json representationfallback
- default value (string representation of color) if json is null- Returns:
- String GeoTools Expression based on provided json, or literal if json was null.
- Throws:
MBFormatException
-
color
Handles literal color definitions supplied as a string, returning aLiteral
.-
{"line-color": "yellow"
named: a few have been put in pass test cases, prnding: plan to useHints.COLOR_DEFINITION
to allow for web colors. -
{"line-color": "#ffff00"}
hex: hex color conversion are supplied byColorConverterFactory
-
{"line-color": "#ff0"}
hex: we will need to special case this -
{"line-color": "rgb(255, 255, 0)"}
- we will need to special case this -
{"line-color": "rgba(255, 255, 0, 1)"}
- we will need to special case this -
{"line-color": "hsl(100, 50%, 50%)"}
- we will need to special case this -
{"line-color": "hsla(100, 50%, 50%, 1)"}
- we will need to special case this
Hints.COLOR_DEFINITION
"CSS" to support the use of web colors names.- Parameters:
color
- name of color (CSS or "web" colors)- Returns:
- appropriate java color, or null if not available.
-
-
convertToColor
Converts color definitions supplied as a string to Color objects:-
{"line-color": "yellow"
named: a few have been put in pass test cases, prnding: plan to useHints.COLOR_DEFINITION
to allow for web colors. -
{"line-color": "#ffff00"}
hex: hex color conversion are supplied byColorConverterFactory
-
{"line-color": "#ff0"}
hex: we will need to special case this -
{"line-color": "rgb(255, 255, 0)"}
- we will need to special case this -
{"line-color": "rgba(255, 255, 0, 1)"}
- we will need to special case this -
{"line-color": "hsl(100, 50%, 50%)"}
- we will need to special case this -
{"line-color": "hsla(100, 50%, 50%, 1)"}
- we will need to special case this
Hints.COLOR_DEFINITION
"CSS" to support the use of web colors names.- Parameters:
color
- name of color (CSS or "web" colors)- Returns:
- appropriate java color, or null if not available.
-
-
bool
Convert json to Expression boolean, or a function.- Parameters:
json
- json representationfallback
- default value if json is null- Returns:
- String GeoTools Expression based on provided json, or literal if json was null.
- Throws:
MBFormatException
-
displacement
Maps a json value at 'tag' in the provided JSONObject to aDisplacement
.- Parameters:
json
- The JSONObject in which to look up a displacement valuename
- The tag in the JSONObjectfallback
- The fallback displacement, if no value is found at that tag.- Returns:
- A displacement from the json
-
isPropertyDefined
@Deprecated public boolean isPropertyDefined(JSONObject json, String propertyName) throws MBFormatException Deprecated.- Returns:
- True if json has a value for the property, False otherwise.
- Throws:
MBFormatException
-
isDefined
True if json has a value for the provided name, False otherwise.- Parameters:
json
- JSON objectname
- Name used to look up a value- Returns:
- True if json has a value for the provided name, False otherwise.
- Throws:
MBFormatException
-
isDefined
True if array has an element at the provided index, False otherwise.- Parameters:
json
- JSON arrayindex
- Index of array element- Returns:
- True if array has an element at the provided index, False otherwise.
- Throws:
MBFormatException
-
isString
True if json has a string value for the provided name, False otherwise.- Parameters:
json
- JSON objectname
- Name used to look up a value- Returns:
- True if json has a string value for the provided name, False otherwise.
- Throws:
MBFormatException
-
isString
True if array has a string element at the provided index, False otherwise.- Parameters:
json
- JSON arrayindex
- Index of array element- Returns:
- True if array has a string element at the provided index, False otherwise.
- Throws:
MBFormatException
-
isNumeric
True if json has a numeric value for the provided name, False otherwise.- Parameters:
json
- JSON objectname
- Name used to look up a value- Returns:
- True if json has a numeric value for the provided name, False otherwise.
- Throws:
MBFormatException
-
isNumeric
True if array has a numeric element at the provided index, False otherwise.- Parameters:
json
- JSON arrayindex
- Index of array element- Returns:
- True if array has a numeric element at the provided index, False otherwise.
- Throws:
MBFormatException
-
isBoolean
True if json has a boolean value for the provided name, False otherwise.- Parameters:
json
- JSON objectname
- Name used to look up a value- Returns:
- True if json has a boolean value for the provided name, False otherwise.
- Throws:
MBFormatException
-
isBoolean
True if array has a boolean element at the provided index, False otherwise.- Parameters:
json
- JSON arrayindex
- Index of array element- Returns:
- True if array has a boolean element at the provided index, False otherwise.
- Throws:
MBFormatException
-
isArray
True if json has an array value for the provided name, False otherwise.- Parameters:
json
- JSON objectname
- Name used to look up a value- Returns:
- True if json has an array value for the provided name, False otherwise.
- Throws:
MBFormatException
-
isArray
True if array has an array element at the provided index, False otherwise.- Parameters:
json
- JSON arrayindex
- Index of array element- Returns:
- True if array has an array element at the provided index, False otherwise.
- Throws:
MBFormatException
-
isObject
True if json has an object value for the provided name, False otherwise.- Parameters:
json
- JSON objectname
- Name used to look up a value- Returns:
- True if json has an object value for the provided name, False otherwise.
- Throws:
MBFormatException
-
isObject
True if array has an object element at the provided index, False otherwise.- Parameters:
json
- JSON arrayindex
- Index of array element- Returns:
- True if array has an object element at the provided index, False otherwise.
- Throws:
MBFormatException
-