org.compass.core.converter.basic
Class AbstractBasicConverter<T>

java.lang.Object
  extended by org.compass.core.converter.basic.AbstractBasicConverter<T>
All Implemented Interfaces:
Converter<T>, ContextResourcePropertyConverter<T>, ResourcePropertyConverter<T>
Direct Known Subclasses:
AbstractDynamicConverter, AbstractFormatConverter, AtomicBooleanConverter, BooleanConverter, ByteConverter, CharConverter, EnumConverter, FileConverter, LocaleConverter, StringBufferConverter, StringBuilderConverter, StringConverter, URIConverter, URLConverter

public abstract class AbstractBasicConverter<T>
extends Object
implements ContextResourcePropertyConverter<T>

An easy to use abstact class for Basic converters. Handles converters that usually deals with String as a result of the conversion.

Allows to override the actual marshalling and un-marshalling of object to strings. In order to override marshalling, override doToString(Object,org.compass.core.mapping.ResourcePropertyMapping,org.compass.core.marshall.MarshallingContext) and in order to override un-marshalling overrode doFromString(String,org.compass.core.mapping.ResourcePropertyMapping,org.compass.core.marshall.MarshallingContext).

Author:
kimchy

Constructor Summary
AbstractBasicConverter()
           
 
Method Summary
 boolean canNormalize()
          Return false.
protected  Property createProperty(String value, ResourcePropertyMapping resourcePropertyMapping, MarshallingContext context)
          Creates a new property to be added to the resource during the marshalling process.
protected abstract  T doFromString(String str, ResourcePropertyMapping resourcePropertyMapping, MarshallingContext context)
          Override the from String in order to un-marshall the String back into its object representation.
protected  void doSetBoost(Property property, T root, ResourcePropertyMapping resourcePropertyMapping, MarshallingContext context)
          A simple extension point that allows to set the boost value for the created Property.
protected  String doToString(T o, ResourcePropertyMapping resourcePropertyMapping, MarshallingContext context)
          Allows to override to toString operation.
 T fromString(String str, ResourcePropertyMapping resourcePropertyMapping)
          Calls fromString(String, org.compass.core.mapping.ResourcePropertyMapping, org.compass.core.marshall.MarshallingContext) with a null value for the context.
 T fromString(String str, ResourcePropertyMapping resourcePropertyMapping, MarshallingContext context)
          Performs null checks (by calling isNullValue(String, org.compass.core.mapping.ResourcePropertyMapping, org.compass.core.marshall.MarshallingContext)) and then calls doFromString(String, org.compass.core.mapping.ResourcePropertyMapping, org.compass.core.marshall.MarshallingContext) if the value is not null.
protected  String getNullValue(ResourcePropertyMapping resourcePropertyMapping, MarshallingContext context)
          If the converter handle nulls, the value that will be stored in the search engine for null values (during the marshall process).
protected  boolean handleNulls(ResourcePropertyMapping resourcePropertyMapping, MarshallingContext context)
          Should the converter handle nulls?
protected  boolean isNullValue(String value, ResourcePropertyMapping resourcePropertyMapping, MarshallingContext context)
          Does this value represents a null value.
 boolean marshall(Resource resource, T root, Mapping mapping, MarshallingContext context)
          Marshall the given Object to the given Resource.
 Property.Index suggestIndex()
          By default for all converters simply return null.
 Boolean suggestOmitNorms()
          By default for all converters simply return null.
 Boolean suggestOmitTf()
          By default for all converters simply return null.
 Property.Store suggestStore()
          The converter can suggest the store type for this type in case no explicit one is configured.
 Property.TermVector suggestTermVector()
          The converter can suggest if term vectors should be saved for this type in case no explicit one is configured.
 String toString(T o, ResourcePropertyMapping resourcePropertyMapping)
          Implementation calls toString(Object,org.compass.core.mapping.ResourcePropertyMapping,org.compass.core.marshall.MarshallingContext) with null value for the context parameter.
 String toString(T o, ResourcePropertyMapping resourcePropertyMapping, MarshallingContext context)
          Implementation handle nulls and if the object is not null, delegates to doToString(Object,org.compass.core.mapping.ResourcePropertyMapping,org.compass.core.marshall.MarshallingContext).
 T unmarshall(Resource resource, Mapping mapping, MarshallingContext context)
          Unmarshall the given Resource to the appropiate Object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractBasicConverter

public AbstractBasicConverter()
Method Detail

marshall

public boolean marshall(Resource resource,
                        T root,
                        Mapping mapping,
                        MarshallingContext context)
                 throws ConversionException
Description copied from interface: Converter
Marshall the given Object to the given Resource. Will use the mapping definition as to how to marshall the object.

Returns true if data was saved in the index, and it can be read as well (i.e. stored).

Specified by:
marshall in interface Converter<T>
Parameters:
resource - The resource to marhsall the object to
root - The Object to marshall to the resource
mapping - The mapping definition of how to marshall the Object to the resoruce
context - The context for the current marhslling process
Returns:
true if data was saved in the the index that can be read.
Throws:
ConversionException

unmarshall

public T unmarshall(Resource resource,
                    Mapping mapping,
                    MarshallingContext context)
             throws ConversionException
Description copied from interface: Converter
Unmarshall the given Resource to the appropiate Object.

Specified by:
unmarshall in interface Converter<T>
Parameters:
resource - The resource to unmarshall into an Object
mapping - The mapping definition of how to unmarshall the Resource into an Object
context - The context for the current marshalling process
Returns:
The object unmarshalled
Throws:
ConversionException

createProperty

protected Property createProperty(String value,
                                  ResourcePropertyMapping resourcePropertyMapping,
                                  MarshallingContext context)
Creates a new property to be added to the resource during the marshalling process. Allows sub classes to override ti in order to modify the created property.

Parameters:
value - The value of the property
resourcePropertyMapping - The resource mapping definition of the property
context - The context (allows to get the search engine from it)
Returns:
The property to be added to the Resource

handleNulls

protected boolean handleNulls(ResourcePropertyMapping resourcePropertyMapping,
                              MarshallingContext context)

Should the converter handle nulls? Handling nulls means should the converter process nulls or not. Usually the converter will not persist null values, but sometimes it might be needed (MarshallingContext.handleNulls()).

If a specific null value is configured with the ResourcePropertyMapping then the converter will always handle nulls and write it.

Parameters:
context - The marshalling context
Returns:
true if the converter should handle null values

getNullValue

protected String getNullValue(ResourcePropertyMapping resourcePropertyMapping,
                              MarshallingContext context)
If the converter handle nulls, the value that will be stored in the search engine for null values (during the marshall process). Uses ResourcePropertyMapping.getNullValue().

Parameters:
resourcePropertyMapping - The resource proeprty mapping to get the null value from
context - The marshalling context
Returns:
Null value that will be inserted for nulls.

isNullValue

protected boolean isNullValue(String value,
                              ResourcePropertyMapping resourcePropertyMapping,
                              MarshallingContext context)
Does this value represents a null value. If the ResourcePropertyMapping is configured with a null value, then returns true if the null value equals the value read from the index. If the resource property mapping is not configured with a null value, checks if this it has the default value representing a null value.


doSetBoost

protected void doSetBoost(Property property,
                          T root,
                          ResourcePropertyMapping resourcePropertyMapping,
                          MarshallingContext context)
                   throws ConversionException

A simple extension point that allows to set the boost value for the created Property.

The default implemenation uses the statically defined boost value in the mapping definition (ResourcePropertyMapping.getBoost()) to set the boost level using Property.setBoost(float)

Parameters:
property - The property to set the boost on
root - The object that is marshalled into a property
resourcePropertyMapping - The Resource Property Mapping definition
Throws:
ConversionException

toString

public String toString(T o,
                       ResourcePropertyMapping resourcePropertyMapping)
Implementation calls toString(Object,org.compass.core.mapping.ResourcePropertyMapping,org.compass.core.marshall.MarshallingContext) with null value for the context parameter.

Note, please don't override this method, please override doToString(Object,org.compass.core.mapping.ResourcePropertyMapping,org.compass.core.marshall.MarshallingContext) to change the how the object gets marshalled into a String.

Specified by:
toString in interface ResourcePropertyConverter<T>
Parameters:
o - The Object to convert from
resourcePropertyMapping - The resource proeprty mapping
Returns:
The String converted from the Object

toString

public String toString(T o,
                       ResourcePropertyMapping resourcePropertyMapping,
                       MarshallingContext context)
Implementation handle nulls and if the object is not null, delegates to doToString(Object,org.compass.core.mapping.ResourcePropertyMapping,org.compass.core.marshall.MarshallingContext).

Note, please don't override this method, please override doToString(Object,org.compass.core.mapping.ResourcePropertyMapping,org.compass.core.marshall.MarshallingContext) to change the how the object gets marshalled into a String.

Specified by:
toString in interface ContextResourcePropertyConverter<T>
Parameters:
o - The Object to convert from
resourcePropertyMapping - The resource proeprty mapping
Returns:
The String converted from the Object

doToString

protected String doToString(T o,
                            ResourcePropertyMapping resourcePropertyMapping,
                            MarshallingContext context)
Allows to override to toString operation. Default implementation calls the object toString.

Note, the marshalling context might be null.


fromString

public T fromString(String str,
                    ResourcePropertyMapping resourcePropertyMapping)
             throws ConversionException
Calls fromString(String, org.compass.core.mapping.ResourcePropertyMapping, org.compass.core.marshall.MarshallingContext) with a null value for the context.

Specified by:
fromString in interface ResourcePropertyConverter<T>
Parameters:
str - The string to convert from
resourcePropertyMapping - The resource property mapping
Returns:
Theh object converterd from the String
Throws:
ConversionException

fromString

public T fromString(String str,
                    ResourcePropertyMapping resourcePropertyMapping,
                    MarshallingContext context)
             throws ConversionException
Performs null checks (by calling isNullValue(String, org.compass.core.mapping.ResourcePropertyMapping, org.compass.core.marshall.MarshallingContext)) and then calls doFromString(String, org.compass.core.mapping.ResourcePropertyMapping, org.compass.core.marshall.MarshallingContext) if the value is not null.

Specified by:
fromString in interface ContextResourcePropertyConverter<T>
Parameters:
str - The string to convert from
resourcePropertyMapping - The resource property mapping
Returns:
Theh object converterd from the String
Throws:
ConversionException

doFromString

protected abstract T doFromString(String str,
                                  ResourcePropertyMapping resourcePropertyMapping,
                                  MarshallingContext context)
                           throws ConversionException
Override the from String in order to un-marshall the String back into its object representation.

Throws:
ConversionException

canNormalize

public boolean canNormalize()
Return false. Specific parsers that can convert on query string should override this method and return true.

Specified by:
canNormalize in interface ResourcePropertyConverter<T>

suggestIndex

public Property.Index suggestIndex()
By default for all converters simply return null.

Specified by:
suggestIndex in interface ResourcePropertyConverter<T>

suggestTermVector

public Property.TermVector suggestTermVector()
Description copied from interface: ResourcePropertyConverter
The converter can suggest if term vectors should be saved for this type in case no explicit one is configured. Can return null and will let global Compass defaults to be used.

Specified by:
suggestTermVector in interface ResourcePropertyConverter<T>

suggestStore

public Property.Store suggestStore()
Description copied from interface: ResourcePropertyConverter
The converter can suggest the store type for this type in case no explicit one is configured. Can return null and will let global Compass defaults to be used.

Specified by:
suggestStore in interface ResourcePropertyConverter<T>

suggestOmitNorms

public Boolean suggestOmitNorms()
By default for all converters simply return null.

Specified by:
suggestOmitNorms in interface ResourcePropertyConverter<T>

suggestOmitTf

public Boolean suggestOmitTf()
By default for all converters simply return null.

Specified by:
suggestOmitTf in interface ResourcePropertyConverter<T>


Copyright (c) 2004-2009 The Compass Project.