Revision: 4353
          http://sourceforge.net/p/jump-pilot/code/4353
Author:   michaudm
Date:     2015-03-22 18:46:04 +0000 (Sun, 22 Mar 2015)
Log Message:
-----------
Improve AttributeType casting in ViewSchema plugin

Modified Paths:
--------------
    
core/trunk/src/com/vividsolutions/jump/workbench/ui/plugin/ViewSchemaPlugIn.java

Modified: 
core/trunk/src/com/vividsolutions/jump/workbench/ui/plugin/ViewSchemaPlugIn.java
===================================================================
--- 
core/trunk/src/com/vividsolutions/jump/workbench/ui/plugin/ViewSchemaPlugIn.java
    2015-03-22 18:03:40 UTC (rev 4352)
+++ 
core/trunk/src/com/vividsolutions/jump/workbench/ui/plugin/ViewSchemaPlugIn.java
    2015-03-22 18:46:04 UTC (rev 4353)
@@ -65,6 +65,7 @@
 import java.text.DateFormat;
 
 import java.util.*;
+import java.util.regex.Pattern;
 
 import javax.swing.ImageIcon;
 import javax.swing.JInternalFrame;
@@ -334,6 +335,9 @@
         return StringUtil.limitLength(s, 30);
     }
 
+    Pattern TRUE_PATTERN = Pattern.compile("(?i)^(T(rue)?|Y(es)?|V(rai)?|1)$");
+    Pattern FALSE_PATTERN = 
Pattern.compile("(?i)^(F(alse)?|N(o)?|F(aux)?|0)$");
+
     private Object convert(Object oldValue, AttributeType oldType,
         AttributeType newType, String name,
         boolean forcingInvalidConversionsToNull) throws ConversionException {
@@ -352,7 +356,7 @@
 
                 if (newType == AttributeType.INTEGER) {
                     try {
-                        return new Integer(oldString);
+                        return new Integer(oldString.replaceAll("^0*",""));
                     } catch (NumberFormatException e) {
                         throw new ConversionException(
                             
I18N.get("ui.plugin.ViewSchemaPlugIn.cannot-convert-to-integer")+" \"" +
@@ -361,6 +365,17 @@
                     }
                 }
 
+                if (newType == AttributeType.LONG) {
+                    try {
+                        return new Long(oldString.replaceAll("^0*",""));
+                    } catch (NumberFormatException e) {
+                        throw new ConversionException(
+                                
I18N.get("ui.plugin.ViewSchemaPlugIn.cannot-convert-to-long")+" \"" +
+                                        limitLength(oldValue.toString()) + "\" 
(" + name +
+                                        ")");
+                    }
+                }
+
                 if (newType == AttributeType.DOUBLE) {
                     try {
                         return new Double(oldString);
@@ -372,6 +387,15 @@
                     }
                 }
 
+                if (newType == AttributeType.BOOLEAN) {
+                    if (FALSE_PATTERN.matcher(oldString).matches()) return 
Boolean.FALSE;
+                    else if (TRUE_PATTERN.matcher(oldString).matches()) return 
Boolean.TRUE;
+                    throw new ConversionException(
+                            
I18N.get("ui.plugin.ViewSchemaPlugIn.cannot-convert-to-boolean")+" \"" +
+                                        limitLength(oldValue.toString()) + "\" 
(" + name +
+                                        ")");
+                }
+
                 if (newType == AttributeType.GEOMETRY) {
                     try {
                         return wktReader.read(oldString);
@@ -406,10 +430,18 @@
                     return oldValue;
                 }
 
+                if (newType == AttributeType.LONG) {
+                    return new Long(oldInt);
+                }
+
                 if (newType == AttributeType.DOUBLE) {
                     return new Double(oldInt);
                 }
 
+                if (newType == AttributeType.BOOLEAN) {
+                    return oldInt == 0 ? Boolean.FALSE : Boolean.TRUE;
+                }
+
                 if (newType == AttributeType.GEOMETRY) {
                     throw new ConversionException(
                         
I18N.get("ui.plugin.ViewSchemaPlugIn.cannot-convert-to-geometry")+" \"" +
@@ -439,10 +471,18 @@
                     return new Integer((int) oldDouble);
                 }
 
+                if (newType == AttributeType.LONG) {
+                    return new Long((long) oldDouble);
+                }
+
                 if (newType == AttributeType.DOUBLE) {
                     return oldValue;
                 }
 
+                if (newType == AttributeType.BOOLEAN) {
+                    return oldDouble == 0.0 ? Boolean.FALSE : Boolean.TRUE;
+                }
+
                 if (newType == AttributeType.GEOMETRY) {
                     throw new ConversionException(
                         
I18N.get("ui.plugin.ViewSchemaPlugIn.cannot-convert-to-geometry")+" \"" +
@@ -468,12 +508,24 @@
                         limitLength(oldValue.toString()) + "\" (" + name + 
")");
                 }
 
+                if (newType == AttributeType.LONG) {
+                    throw new ConversionException(
+                            
I18N.get("ui.plugin.ViewSchemaPlugIn.cannot-convert-to-long")+" \"" +
+                                    limitLength(oldValue.toString()) + "\" (" 
+ name + ")");
+                }
+
                 if (newType == AttributeType.DOUBLE) {
                     throw new ConversionException(
                         
I18N.get("ui.plugin.ViewSchemaPlugIn.cannot-convert-to-double")+" \"" +
                         limitLength(oldValue.toString()) + "\" (" + name + 
")");
                 }
 
+                if (newType == AttributeType.BOOLEAN) {
+                    throw new ConversionException(
+                            
I18N.get("ui.plugin.ViewSchemaPlugIn.cannot-convert-to-boolean")+" \"" +
+                                    limitLength(oldValue.toString()) + "\" (" 
+ name + ")");
+                }
+
                 if (newType == AttributeType.GEOMETRY) {
                     return oldGeometry;
                 }
@@ -495,10 +547,20 @@
                     return new Integer((int) oldDate.getTime());
                 }
 
+                if (newType == AttributeType.LONG) {
+                    return oldDate.getTime();
+                }
+
                 if (newType == AttributeType.DOUBLE) {
                     return new Double(oldDate.getTime());
                 }
 
+                if (newType == AttributeType.BOOLEAN) {
+                    throw new ConversionException(
+                            
I18N.get("ui.plugin.ViewSchemaPlugIn.cannot-convert-to-boolean")+" \"" +
+                                    limitLength(oldValue.toString()) + "\" (" 
+ name + ")");
+                }
+
                 if (newType == AttributeType.GEOMETRY) {
                     throw new ConversionException(
                         
I18N.get("ui.plugin.ViewSchemaPlugIn.cannot-convert-to-geometry")+" \"" +
@@ -525,6 +587,15 @@
                         limitLength(oldValue.toString()) + "\" (" + name + 
")");
                 }
 
+                if (newType == AttributeType.LONG) {
+                    if (oldValue instanceof Number) {
+                        return new Long(((Number)oldValue).longValue());
+                    }
+                    throw new ConversionException(
+                            
I18N.get("ui.plugin.ViewSchemaPlugIn.cannot-convert-to-integer")+" \"" +
+                                    limitLength(oldValue.toString()) + "\" (" 
+ name + ")");
+                }
+
                 if (newType == AttributeType.DOUBLE) {
                     if (oldValue instanceof Number) {
                         return new Double(((Number)oldValue).doubleValue());
@@ -534,6 +605,20 @@
                         limitLength(oldValue.toString()) + "\" (" + name + 
")");
                 }
 
+                if (newType == AttributeType.BOOLEAN) {
+                    if (oldValue instanceof Boolean) {
+                        return oldValue;
+                    } else if (oldValue instanceof Number) {
+                        return ((Number)oldValue).intValue() == 0 ? 
Boolean.FALSE : Boolean.TRUE;
+                    } else if (oldValue instanceof String) {
+                        if 
(FALSE_PATTERN.matcher(oldValue.toString()).matches()) return Boolean.FALSE;
+                        if 
(TRUE_PATTERN.matcher(oldValue.toString()).matches()) return Boolean.TRUE;
+                    }
+                    throw new ConversionException(
+                            
I18N.get("ui.plugin.ViewSchemaPlugIn.cannot-convert-to-boolean")+" \"" +
+                                    limitLength(oldValue.toString()) + "\" (" 
+ name + ")");
+                }
+
                 if (newType == AttributeType.GEOMETRY) {
                     if (oldValue instanceof Geometry) return oldValue;
                     throw new ConversionException(


------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the 
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Jump-pilot-devel mailing list
Jump-pilot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel

Reply via email to