|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.dellroad.stuff.string.SimpleObjectParser<T>
public class SimpleObjectParser<T>
Parses strings using regular expressions into new instances of some class by parsing substrings.
Primitive and String values are handled automatically. Other property types can be handled by
overriding 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 | |
|---|---|
SimpleObjectParser(Class<T> targetClass)
Constructor. |
|
| Method Summary | |
|---|---|
Map<String,PropertyDescriptor> |
getPropertyMap()
Get the mapping from property name to setter method. |
Class<T> |
getTargetClass()
Get the target class. |
T |
parse(String text,
Pattern pattern,
Map<Integer,String> patternMap,
boolean allowSubstringMatch)
Same as parse(Object, String, Pattern, Map, boolean) but this method creates the target instance using
the target type's default constructor. |
T |
parse(String text,
String regex,
boolean allowSubstringMatch)
Same as parse(Object, String, String, boolean) but this method creates the target instance using
the target type's default constructor. |
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. |
T |
parse(T target,
String text,
String regex,
boolean allowSubstringMatch)
Parse the given text using the provided named group regular expression. |
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. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public SimpleObjectParser(Class<T> targetClass)
targetClass - type of target object we will be parsing| Method Detail |
|---|
public Class<T> getTargetClass()
public T parse(String text,
String regex,
boolean allowSubstringMatch)
parse(Object, String, String, boolean) but this method creates the target instance using
the target type's default constructor.
RuntimeException - if a new target instance cannot be created using the default constructor
public T parse(T target,
String text,
String regex,
boolean allowSubstringMatch)
This method assumes the following about regex:
({foo} are actual named group sub-expressions
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
PatternSyntaxException - if the regular expression with the named group property names removed is invalid
PatternSyntaxException - if this method cannot successfully parse the regular expression
IllegalArgumentException - if a named group specfies a property that is not a parseable
property of this instance's target class
public T parse(String text,
Pattern pattern,
Map<Integer,String> patternMap,
boolean allowSubstringMatch)
parse(Object, String, Pattern, Map, boolean) but this method creates the target instance using
the target type's default constructor.
RuntimeException - if a new target instance cannot be created using the default constructor
public T parse(T target,
String text,
Pattern pattern,
Map<Integer,String> patternMap,
boolean allowSubstringMatch)
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
IllegalArgumentException - if the map contains a property that is not a parseable
property of this instance's target class
IllegalArgumentException - if a subgroup index key in patternMap is out of boundspublic Map<String,PropertyDescriptor> getPropertyMap()
public void setProperty(T obj,
PropertyDescriptor property,
String substring)
The implementation in SimpleObjectParser simply invokes setSimpleProperty(T, java.beans.PropertyDescriptor, java.lang.String).
Other property types can be handled by overriding this method.
obj - newly created instanceproperty - descriptor for the property being setsubstring - matched substring
IllegalArgumentException - if substring cannot be successfully parsed
IllegalArgumentException - if an exception is thrown attempting to set the property
public void setSimpleProperty(T obj,
PropertyDescriptor property,
String substring)
The implementation in SimpleObjectParser handles primitives using the corresponding
valueOf method; String values are handled by setting the value directly.
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 propertyprotected void postProcess(T obj)
The implementation in SimpleObjectParser does nothing. Subclasses may override if needed.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||