org.dellroad.stuff.string
Class SimpleObjectParser<T>

java.lang.Object
  extended by org.dellroad.stuff.string.SimpleObjectParser<T>

public class SimpleObjectParser<T>
extends Object

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

SimpleObjectParser

public SimpleObjectParser(Class<T> targetClass)
Constructor.

Parameters:
targetClass - type of target object we will be parsing
Method Detail

getTargetClass

public Class<T> getTargetClass()
Get the target class.


parse

public 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.

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,
               String regex,
               boolean allowSubstringMatch)
Parse the given text using the provided named group regular expression.

This method assumes the following about regex:

Parameters:
target - target instance
text - string to parse
regex - named group regular expression containing object property names
allowSubstringMatch - 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 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
Since:
1.0.95

parse

public 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.

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 instance
text - string to parse
pattern - pattern with substring matching groups that match object properties
patternMap - mapping from pattern substring group index to object property name
allowSubstringMatch - 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 class
IllegalArgumentException - if a subgroup index key in patternMap is out of bounds
Since:
1.0.95

getPropertyMap

public Map<String,PropertyDescriptor> getPropertyMap()
Get the mapping from property name to setter method.


setProperty

public void setProperty(T obj,
                        PropertyDescriptor property,
                        String substring)
Set a property value.

The implementation in SimpleObjectParser simply invokes setSimpleProperty(T, java.beans.PropertyDescriptor, java.lang.String). Other property types can be handled by overriding this method.

Parameters:
obj - newly created instance
property - descriptor for the property being set
substring - matched substring
Throws:
IllegalArgumentException - if substring cannot be successfully parsed
IllegalArgumentException - if an exception is thrown attempting to set the property

setSimpleProperty

public void setSimpleProperty(T obj,
                              PropertyDescriptor property,
                              String substring)
Set a primitive or string property value.

The implementation in SimpleObjectParser handles primitives using the corresponding valueOf method; String values are handled by setting the value directly.

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

protected void postProcess(T obj)
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.