public interface Context
Context
object can be compared to the
XPath syntax, which is used to navigate
the elements and attribute of an XML document. Only difference here is that this API is not married to any specific
format.
All the operations provided by this interface are read-only, which essentially renders the Context
object
immutable after its creation.
Created by QuijadaJ on 5/3/2017.Modifier and Type | Field and Description |
---|---|
static String |
FOUND_ELEM_VAL_IS_REGEX |
static String |
IGNORE_INCOMPATIBLE_SEARCH_PATH_PROVIDED_ERROR |
static String |
IGNORE_INCOMPATIBLE_TARGET_ELEMENT_PROVIDED_ERROR |
static String |
PARTIAL_REGEX_MATCH |
Modifier and Type | Method and Description |
---|---|
boolean |
arrayContains(String pVal)
When the
Context is an array, checks if the value is contained in it. |
List<Context> |
asArray()
|
boolean |
containsElement(String pElemName)
To be used only when the underlying data is complex, returns true if underlying data contains the
pElemName given |
Context |
entryFromArray(int pIdx)
If the underlying data is array-like, will retrieve entry at index
pIdx |
Set<Map.Entry<String,Context>> |
entrySet()
To be used only when the underlying data is complex, returns a
Set of Map.Entry s
to represent the name/value pairs contained int he complex data structure. |
SearchResult |
findElement(SearchPath pSearchPath,
Filter pFilter,
TargetElements pTargetElements,
Map<String,String> pExtraParams)
Represents the entry point to begin searching the underlying data structure.
|
SearchResult |
findElement(SelectionCriteria pSelectCriteria,
Map<String,String> pExtraParams)
Works similar to
findElement(SearchPath, Filter, TargetElements, Map) , except that the first three
arguments are replaced with a SelectionCriteria element. |
boolean |
isArray()
Implementing classes use this method to tell if underlying data is a type of array or list-like.
|
boolean |
isPrimitive()
Implementing classes use this method to tell if underlying data is a primitive (I.e.
|
boolean |
isRecursible()
Implementing classes use this method to tell if underlying data is complex (I.e.
|
Context |
memberValue(String pMemberName)
To be used only when the underlying data is complex, returns the value of the specified
pMemberName |
SearchPath |
startSearchPath()
Gives the starting search path of this
Context . |
String |
stringRepresentation()
If the underlying data provides implementation aside from the toString() to return the data as a string,
then this method is expected to wrap such an implmentation.
|
List<String> |
topLevelElementNames()
|
static <T extends String> |
transformArgumentToListObject(T pArg)
Utility method which attempts to transform passed in argument to a
List . |
static final String FOUND_ELEM_VAL_IS_REGEX
static final String PARTIAL_REGEX_MATCH
static final String IGNORE_INCOMPATIBLE_SEARCH_PATH_PROVIDED_ERROR
static final String IGNORE_INCOMPATIBLE_TARGET_ELEMENT_PROVIDED_ERROR
SearchResult findElement(SearchPath pSearchPath, Filter pFilter, TargetElements pTargetElements, Map<String,String> pExtraParams) throws IllegalArgumentException
SearchPath
.
The Context
object off of which this method can be invoked was previously obtained via a call to factory
method ContextFactory.obtainContext(Object)
.
To further refine the results use the pFilter and pTargetElements arguments.
TODO: Add more examples of usagepSearchPath
- - The path that in the underlying data structure that this method has been instructed to find. For more
information on how to build a SearchPath
object to then pass it here, read the documentation
of SearchPath.valueOf(String)
.
Basically the last node of the SearchPath
is what gets returned to the caller. This can be
a primitive, and array (of primitives, or other complex structures, or a combination thereof), or a complex
data structure. The type of the found element determines how the pFilter and pTargetElements
arguments behave. Read respective description of these arguments for more details.
If an array element will be
encountered somewhere in the search path, then the corresponding node should contain square
brackets like this: someElemName[0]. If an array element is encountered yet the path
did not tell this method to expect an array at this point (by appending "[N]" to the
path node), IllegalArgumentException is thrown, *unless* the array element happens to be the
last node of the search path.pFilter
- - Use this argument to further refine the search rules. Build a filter by specifying a semi-colon
separated list of name/value pairs separated by equals sign, and pass that string to
factory method Filter.valueOf(String)
, like this:
Filter.valueOf("name1=val1;name2=val2")
The keys specified should correspond to keys found in the last node of the pSearchPath
.
This assumes that developer is intimately familiar with the data structure he's working with, and that
he'll know exactly what result pSearchPath
will yield.
How pFilter
gets applied depends on the type of data found in the last node of the
pSearchPath
. In all cases, the members of the search results are checked to see if
a corresponding key is found in the Filter
specified, and if so that member value
is compared against the filter value.
The filter values can have wildcards too. There can be a wildcard at either the beginning, the end
or both.
Below lists the possible types of data that can be found at end of search path, and how the filter
gets applied in each case:
primitive: simply compare the corresponding filter value against the primitive, and if there's a match
that primitive will be included in the search result
array: Primitive entries are handled the same way single primitive results are found. Complex
structures in the array have their members checked against the filter the same way
single complex structures are handled as described below.
complex structure: Each member of the complex structure is checked to see if there's a filter
value provided. If there's a match, then this complex structure will be included in the results.
A Filter
key can be a search path also, in which case the value for the filter
search path is found relative to the last node of the search path passed to this method. An error
is thrown if the filter search path is not found, otherwise the node will be included in the
search results if the filter value matches the value found in the underlying Context
.
An example of a filter that has a search path as key is:
SearchPath: top_node.inner_node.member2
Filter: key=sub_member5.key=1234
Context (assuming underlying data is in JSON format):
{
"top_node":{
"inner_node":{
"member1":1110,
"member2":[
{
"sub_member_1":1110,
"sub_member_2":15199,
"sub_member_3":13135,
"sub_member_4":7184441216,
"sub_member5": {
"key": "abcd"
}
},
{
"sub_member_1":1110,
"sub_member_2":15199,
"sub_member_3":24099,
"sub_member_4":7184441216,
"sub_member5": {
"key": "1234"
}
}
],
"member3":1073,
"member4":7184441216,
"member5":19555
}
}
}
Based on the filter, the second entry of the "member2" array could be selected.
TODO: Filter key values also support passing in a comma separated list of valuespTargetElements
- - Use it to further refine the search results by specifying which elements to return in the search
results when the last node of the pSearchPath yields a complex data structure, and you're only
interested in a sub-set of the members of the found complex data structure.pExtraParams
- - Implementing classes can use this Map
to provide arbitrary list of name/value
pairs to provide features/behavior that this interface does not already plan for.
TODO: Clearly document *all* keys supported; they're defined as constants at top of this classSearchResult
which is nothing more than a Map
that maps keys to Context
objects. Each Context
object in the SearchResult
can be a primitive, or an array, or
another complex object. Refer to the other methods of this class for the available operations.IllegalArgumentException
- - Thrown if parameter pSearchPath
is determined to be invalid
for whatever reason.SearchResult findElement(SelectionCriteria pSelectCriteria, Map<String,String> pExtraParams) throws IllegalArgumentException
findElement(SearchPath, Filter, TargetElements, Map)
, except that the first three
arguments are replaced with a SelectionCriteria
element.pSelectCriteria
- - pSelectCriteriapExtraParams
- - pExtraParamsIllegalArgumentException
- - TODOboolean isPrimitive()
String
, etc...boolean isRecursible()
boolean isArray()
List<Context> asArray() throws IllegalStateException
IllegalStateException
- - TODOContext entryFromArray(int pIdx) throws IllegalStateException
pIdx
pIdx
- - pIdxIllegalStateException
- - TODOString stringRepresentation()
boolean containsElement(String pElemName) throws IllegalStateException
pElemName
givenpElemName
- - pElemNameIllegalStateException
- - TODOSet<Map.Entry<String,Context>> entrySet() throws IllegalStateException
Set
of Map.Entry
s
to represent the name/value pairs contained int he complex data structure.IllegalStateException
- - TODOContext memberValue(String pMemberName) throws IllegalStateException
pMemberName
pMemberName
- - pMemberNameIllegalStateException
- - TODOboolean arrayContains(String pVal) throws IllegalStateException
Context
is an array, checks if the value is contained in it.pVal
- - pValIllegalStateException
- - TODOSearchPath startSearchPath()
Context
. This works only when the data is recursible
(isRecursible()
yields true
), and there's only one outer most element, else
this method returns null. This was added mainly as a convenience so that calling code does not need to be
calculating this value over and over again. There's no reason the Context
object itself can't provide
this information.SearchPath
if there's just a single outermost element, null
otherwise.static <T extends String> List<T> transformArgumentToListObject(T pArg)
List
. Argument must be a String
object or a sub-type, and must be a comma-separated list of values, or a value that
ContextFactory.obtainContext(Object)
can transform to an array of primitives (for example a string
like '[a,b,c,d]' is recognized as JSON, hence can be transformed to Context by ContextFactory.obtainContext(Object)
because JSON is one the formats recognized by that factory).T
- - TpArg
- - What will get transformed to a List
of String
's, if possible.List
of String
's produced, if possible, null
otherwiseCopyright © 2019. All rights reserved.