I wrote an helper class to convert between different types
through the use of strings. Maybe you can use it to "adapt"
a Filter's literal strings to a FeatureType.

Bye
Paolo Rizzi

******************************************************************
public class DataMagic {
                //TODO review!!! This more or less the same code found in
MetaTypeAttr,
                //so the two should refactored together in a good utility
method or something
        public static Object getConvertedValue(Class clazz,Object value)
throws Exception
        {
                if( value == null )                                     //a
null remains a null...
                        return null;
                
                        //if clazz is a primitive type, use its
corresponding wrapper class
                        //TODO review!!! Primitive types are a fixed set,
but maybe we can do better...
                if( clazz.isPrimitive() )
                {
                        if( boolean.class.equals(clazz) )
                                clazz = Boolean.class;
                        else if( char.class.equals(clazz) )
                                clazz = Character.class;
                        else if( byte.class.equals(clazz) )
                                clazz = Byte.class;
                        else if( short.class.equals(clazz) )
                                clazz = Short.class;
                        else if( int.class.equals(clazz) )
                                clazz = Integer.class;
                        else if( long.class.equals(clazz) )
                                clazz = Long.class;
                        else if( float.class.equals(clazz) )
                                clazz = Float.class;
                        else if( double.class.equals(clazz) )
                                clazz = Double.class;
                }
                
                //if already of assignable types, nothing to do
                if( clazz.isAssignableFrom(value.getClass()) )
                        return value;
                
                        //TODO hack!!! To support Boolean/Char conversion
for Oracle
                        //note  that is not a proper Character, but a String
of 1 character
                if( Boolean.class.equals(clazz) && value instanceof String )
                        if( ((String)value).length() == 1 )
                                return "0".equals(value) ? Boolean.FALSE :
Boolean.TRUE;
                        
                if( String.class.equals(clazz) && value instanceof Boolean )
                        return ((Boolean)value).booleanValue() ? "1" : "0";
                
                value = String.valueOf(value);                  //convert to
String
                
                try
                {               //try the constructor(String) first
                        Constructor ctor = clazz.getConstructor(new Class[]
{String.class});
                        return ctor.newInstance(new Object[] { value });
                } catch(Exception ex) {}
//TODO review!!! Should log here???
        
                        //then try the valueOf(String) static method
                Method method = clazz.getMethod("valueOf",new Class[]
{String.class});
                return method.invoke(clazz,new Object[] { value });
        }       

        public static Class getPrimitiveClass(Class clazz)
        {
                if( clazz == null )                                     //a
null remains a null...
                        return null;
                
                        //if clazz is a wrapper for a primitive type,
returns the primitive class
                        //TODO review!!! Primitive types are a fixed set,
but maybe we can do better...
                if( Boolean.class.equals(clazz) ) return boolean.class;
                if( Character.class.equals(clazz) ) return char.class;
                if( Byte.class.equals(clazz) ) return byte.class;
                if( Short.class.equals(clazz) ) return short.class;
                if( Integer.class.equals(clazz) ) return int.class;
                if( Long.class.equals(clazz) ) return long.class;
                if( Float.class.equals(clazz) ) return float.class;
                if( Double.class.equals(clazz) ) return double.class;
                
                return clazz;
        }       

        public static boolean isImportNeeded(Class clazz)
        {
                if( clazz == null )                                     
                        return false;
                
                if( clazz.isPrimitive() ) 
                        return false;                                   //no
need to import a primitive type
                
                if( getPrimitiveClass(clazz).isPrimitive() ) 
                        return false;                                   //no
need to import a wrapper on a primitive type

                if(
Package.getPackage("java.lang").equals(clazz.getPackage()) )
                        return false;
                
                return true;                            //TODO review!!! Be
smarter!!!
        }       
}
*************************************************************************


> -----Messaggio originale-----
> Da: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> Inviato: mercoledì 9 novembre 2005 5.37
> A: [EMAIL PROTECTED]
> Cc: [email protected]
> Oggetto: Re: Re:[Geotools-devel] Erroneous handling of Literals in
> ExpressionParser
> 
> 
> Jürgen,
> 
> Yes, the filter-and-literal stuff was poorly designed.
> 
> I've put a bunch of patches in to minimize the damage and considered
> hiring a dutch boy to stick his finger in the cracks.
> 
> Basically, what needs to happen is the actual filter class should just
> store the literals as strings, and the filter evaluation code 
> should do
> any necessary conversion.  The main problem is that the Filter parser
> cannot, apriori, know what type the <Literal> elements are, but it
> guesses anyways.  Whats needed is the FeatureType that the Filter is
> being evaluated against (this can get more complicated if the
> FeatureType has <choice> elements in it).
> 
> This isnt a giant change, but the problem is that there is a lot of
> geotools (and, I'm sure, other code) that relies on the 
> apriori parsing
> of the Filter.  There was a recent oracle datastore bug that was being
> caused because the Oracle Datastore was assuming that the Filter type
> assumptions was correct.  So, there could be a lot of code that would
> get subtly screwed over if we actually change this.
> 
> I'm hip to see this done (as are a lot of others), but I dont think
> anyone's done it because there's a large number of subtle 
> problems that
> could be caused!
> 
> One possible bandaid would be a function that takes a Filter 
> object and
> a FeatureType then 'fixes' all the literals in it.  We'll have to make
> sure that this works with the new Feature Model.
> 
> NOTE: a single Filter object can work with multiple FeatureTypes, each
> wanting different literal types!
> 
> dave
> 
> 
> 
> 
> ----------------------------------------------------------
> This mail sent through IMP: https://webmail.limegroup.com/
> 
> 
> -------------------------------------------------------
> SF.Net email is sponsored by:
> Tame your development challenges with Apache's Geronimo App 
> Server. Download
> it for free - -and be entered to win a 42" plasma tv or your very own
> Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
> _______________________________________________
> Geotools-devel mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/geotools-devel
> 




AVVERTENZE AI SENSI DEL D. LGS. 196/2003  
Le informazioni contenute in questo messaggio di posta elettronica e/o nel/i
file/s allegato/i, sono da considerarsi strettamente riservate. Il loro
utilizzo è consentito esclusivamente al destinatario del messaggio, per le
finalità indicate nel messaggio stesso. Qualora riceveste questo messaggio
senza esserne il destinatario, Vi preghiamo cortesemente di darcene notizia
via e-mail e di procedere alla distruzione del messaggio stesso,
cancellandolo dal Vostro sistema; costituisce comportamento contrario ai
principi dettati dal D. Lgs. 196/2003 il trattenere il messaggio stesso,
divulgarlo anche in parte, distribuirlo ad altri soggetti, copiarlo, od
utilizzarlo per finalità diverse.


-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Geotools-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-devel

Reply via email to