Class SimpleObjectParser<T>
setProperty(T, java.beans.PropertyDescriptor, java.lang.String)
.
This class supports parsing using a named group regular expression, which is pattern string
using normal Pattern
regular expression syntax with one additional grouping construct of the
form ({property}...)
, allowing the Java bean property name to be specified inside the
curly braces at the start of a grouped subexpression.
Instances of this class are immutable and thread-safe.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionGet the mapping from property name to property descriptor.Get the target class.Same asparse(Object, String, String, boolean)
but this method creates the target instance using the target type's default constructor.Same asparse(Object, String, Pattern, Map, boolean)
but this method creates the target instance using the target type's default constructor.Parse the given text using the provided named group regular expression.parse
(T target, String text, Pattern pattern, Map<Integer, String> patternMap, boolean allowSubstringMatch) Parse the given text using the provided pattern and mapping from pattern sub-group to Java bean property name.protected void
postProcess
(T obj) Post-process newly created instances.void
setProperty
(T obj, PropertyDescriptor property, String substring) Set a property value.void
setSimpleProperty
(T obj, PropertyDescriptor property, String substring) Set a primitive or string property value.
-
Constructor Details
-
SimpleObjectParser
Constructor.- Parameters:
targetClass
- type of target object we will be parsing
-
-
Method Details
-
getTargetClass
Get the target class.- Returns:
- target class for parse
-
parse
Same asparse(Object, String, String, boolean)
but this method creates the target instance using the target type's default constructor.- Parameters:
text
- string to parseregex
- named group regular expression containing object property namesallowSubstringMatch
- if false, entire text must match, otherwise only a (the first) substring need match- Returns:
- parsed object or null if parse fails
- Throws:
RuntimeException
- if a new target instance cannot be created using the default constructor- Since:
- 1.0.85
-
parse
Parse the given text using the provided named group regular expression.This method assumes the following about
regex
:- All instances of an opening parenthesis not preceded by a backslash are actual grouped sub-expressions
- In particular, all instances of substrings like
({foo}
are actual named group sub-expressions
- Parameters:
target
- target instancetext
- string to parseregex
- named group regular expression containing object property namesallowSubstringMatch
- if false, entire text must match, otherwise only a (the first) substring need match- Returns:
- parsed object or null if parse fails
- Throws:
PatternSyntaxException
- if the regular expression with the named group property names removed is invalidPatternSyntaxException
- if this method cannot successfully parse the regular expressionIllegalArgumentException
- if a named group specfies a property that is not a parseable property of this instance's target class- Since:
- 1.0.95
-
parse
public T parse(String text, Pattern pattern, Map<Integer, String> patternMap, boolean allowSubstringMatch) Same asparse(Object, String, Pattern, Map, boolean)
but this method creates the target instance using the target type's default constructor.- Parameters:
text
- string to parsepattern
- pattern with substring matching groups that match object propertiespatternMap
- mapping from pattern substring group index to object property nameallowSubstringMatch
- if false, entire text must match, otherwise only a (the first) substring need match- Returns:
- parsed object or null if parse fails
- Throws:
RuntimeException
- if a new target instance cannot be created using the default constructor- Since:
- 1.0.85
-
parse
public T parse(T target, String text, Pattern pattern, Map<Integer, String> patternMap, boolean allowSubstringMatch) Parse the given text using the provided pattern and mapping from pattern sub-group to Java bean property name.- Parameters:
target
- target instancetext
- string to parsepattern
- pattern with substring matching groups that match object propertiespatternMap
- mapping from pattern substring group index to object property nameallowSubstringMatch
- if false, entire text must match, otherwise only a (the first) substring need match- Returns:
- parsed object or null if parse fails
- Throws:
IllegalArgumentException
- if the map contains a property that is not a parseable property of this instance's target classIllegalArgumentException
- if a subgroup index key in patternMap is out of bounds- Since:
- 1.0.95
-
getPropertyMap
Get the mapping from property name to property descriptor.- Returns:
- property descriptor map with property setters
-
setProperty
Set a property value.The implementation in
SimpleObjectParser
simply invokessetSimpleProperty(T, java.beans.PropertyDescriptor, java.lang.String)
. Other property types can be handled by overriding this method.- Parameters:
obj
- newly created instanceproperty
- descriptor for the property being setsubstring
- matched substring- Throws:
IllegalArgumentException
- if substring cannot be successfully parsedIllegalArgumentException
- if an exception is thrown attempting to set the property
-
setSimpleProperty
Set a primitive or string property value.The implementation in
SimpleObjectParser
handles primitives using the correspondingvalueOf
method; String values are handled by setting the value directly.- Parameters:
obj
- object being parsedproperty
- property being decodedsubstring
- property value as a string- Throws:
IllegalArgumentException
- if property is not a primitive or String property.IllegalArgumentException
- if substring cannot be successfully parsed (if primitive)IllegalArgumentException
- if an exception is thrown attempting to set the property
-
postProcess
Post-process newly created instances. The instance's properties will have already been set by a successful parse.The implementation in
SimpleObjectParser
does nothing. Subclasses may override if needed.- Parameters:
obj
- newly created instance
-