[Libreoffice-commits] core.git: xmerge/source

Robert Antoni Buj i Gelonch robert.buj at gmail.com
Thu Oct 2 01:53:04 PDT 2014


 xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/sxc/ColumnStyle.java |   13 ----
 xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/sxc/RowStyle.java    |   14 ----
 xmerge/source/xmerge/java/org/openoffice/xmerge/util/TwipsConverter.java           |   29 ++++++++--
 3 files changed, 27 insertions(+), 29 deletions(-)

New commits:
commit ca7899d64d8fc8b6902af33d50a2b50ca3c841ac
Author: Robert Antoni Buj i Gelonch <robert.buj at gmail.com>
Date:   Wed Oct 1 19:41:50 2014 +0200

    xmerge: reuse the value of value.indexOf and remove duplicated code
    
    Change-Id: I44d72f7d9f8c58436657ea1517d8eb76332abb4b
    Reviewed-on: https://gerrit.libreoffice.org/11747
    Reviewed-by: Noel Grandin <noelgrandin at gmail.com>
    Tested-by: Noel Grandin <noelgrandin at gmail.com>

diff --git a/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/sxc/ColumnStyle.java b/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/sxc/ColumnStyle.java
index bd33a30..68c689f 100644
--- a/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/sxc/ColumnStyle.java
+++ b/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/sxc/ColumnStyle.java
@@ -125,18 +125,7 @@ public class ColumnStyle extends Style implements Cloneable {
      * @return  The twips equivalent.
      */
     private int parseColWidth(String value) {
-
-        int width = 255;    // Default value
-
-        if(value.indexOf("cm")!=-1) {
-            float widthCM = Float.parseFloat(value.substring(0,value.indexOf('c')));
-            width = TwipsConverter.cm2twips(widthCM);
-        } else if(value.indexOf("inch")!=-1) {
-            float widthInch = Float.parseFloat(value.substring(0,value.indexOf('i')));
-            width = TwipsConverter.inches2twips(widthInch);
-        }
-
-        return (width);
+        return TwipsConverter.convert2twips(value, 255);
     }
 
     /**
diff --git a/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/sxc/RowStyle.java b/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/sxc/RowStyle.java
index e8cd0e0..3f8075c 100644
--- a/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/sxc/RowStyle.java
+++ b/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/sxc/RowStyle.java
@@ -124,19 +124,7 @@ public class RowStyle extends Style implements Cloneable {
      * @return  The twips equivalent.
      */
     private int parseRowHeight(String value) {
-
-        int height = 255;   // Default value
-
-        if(value.indexOf("cm")!=-1) {
-            float heightCM = Float.parseFloat(value.substring(0,value.indexOf('c')));
-            height = TwipsConverter.cm2twips(heightCM);
-        } else if(value.indexOf("inch")!=-1) {
-            float heightInch = Float.parseFloat(value.substring(0,value.indexOf('i')));
-            height = TwipsConverter.inches2twips(heightInch);
-        }
-
-        return (height);
-
+        return TwipsConverter.convert2twips(value, 255);
     }
 
     /**
diff --git a/xmerge/source/xmerge/java/org/openoffice/xmerge/util/TwipsConverter.java b/xmerge/source/xmerge/java/org/openoffice/xmerge/util/TwipsConverter.java
index 5ae1139..8ab83be 100644
--- a/xmerge/source/xmerge/java/org/openoffice/xmerge/util/TwipsConverter.java
+++ b/xmerge/source/xmerge/java/org/openoffice/xmerge/util/TwipsConverter.java
@@ -39,8 +39,7 @@ public class TwipsConverter {
     /**
      * Convert from cm's to twips.
      *
-     * @param   value  {@code byte} array containing the LE representation of
-     *                 the value.
+     * @param   value  {@code float} containing the representation of the value.
      *
      * @return  {@code int} containing the converted value.
      */
@@ -52,12 +51,34 @@ public class TwipsConverter {
     /**
      * Convert from cm's to twips.
      *
-     * @param   value  {@code byte} array containing the LE representation of
-     *                 the value.
+     * @param   value  {@code float} containing the representation of the value.
      *
      * @return  {@code int} containing the converted value.
      */
     public static int inches2twips(float value) {
         return (int) (value*1440);
     }
+
+    /**
+     * Convert {@code String} to twips.
+     *
+     * @param  value        {@code String} in the form {@literal "1.234cm"} or
+     *                      {@literal "1.234inch"}.
+     * @param  defaultValue the default value.
+     * @return the converted value if {@code value} is a well-formatted {@code
+     *         String}, {@code defaultValue} otherwise.
+     */
+    public static int convert2twips(String value, int defaultValue) {
+        int posi;
+
+        if ((posi = value.indexOf("cm")) != -1) {
+            float cm = Float.parseFloat(value.substring(0,posi));
+            return cm2twips(cm);
+        } else if ((posi = value.indexOf("inch")) != -1) {
+            float inches = Float.parseFloat(value.substring(0,posi));
+            return inches2twips(inches);
+        }
+
+        return defaultValue;
+    }
 }
\ No newline at end of file


More information about the Libreoffice-commits mailing list