xmlgraphics-fop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adelme...@apache.org
Subject svn commit: r674276 - in /xmlgraphics/fop/trunk/src/java/org/apache/fop: area/AreaTreeParser.java util/ConversionUtils.java
Date Sun, 06 Jul 2008 09:17:15 GMT
Author: adelmelle
Date: Sun Jul  6 02:17:14 2008
New Revision: 674276

URL: http://svn.apache.org/viewvc?rev=674276&view=rev
Log:
Extracted conversion methods for String to int[] or double[] to a utility class.
Made AreaTreeParser.getAttributeAsXXX() methods static to stress their utility character,
and removed the private parseRect() in favor of getAttributeAsRectangle2D().

Added:
    xmlgraphics/fop/trunk/src/java/org/apache/fop/util/ConversionUtils.java   (with props)
Modified:
    xmlgraphics/fop/trunk/src/java/org/apache/fop/area/AreaTreeParser.java

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/area/AreaTreeParser.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/area/AreaTreeParser.java?rev=674276&r1=674275&r2=674276&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/area/AreaTreeParser.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/area/AreaTreeParser.java Sun Jul  6 02:17:14
2008
@@ -78,6 +78,7 @@
 import org.apache.fop.util.ColorUtil;
 import org.apache.fop.util.ContentHandlerFactory;
 import org.apache.fop.util.ContentHandlerFactoryRegistry;
+import org.apache.fop.util.ConversionUtils;
 import org.apache.fop.util.DefaultErrorListener;
 
 /**
@@ -187,15 +188,6 @@
             makers.put("destination", new DestinationMaker());
         }
 
-        private static Rectangle2D parseRect(String rect) {
-            StringTokenizer tokenizer = new StringTokenizer(rect, " ");
-            return new Rectangle2D.Double(
-                    Double.parseDouble(tokenizer.nextToken()),
-                    Double.parseDouble(tokenizer.nextToken()),
-                    Double.parseDouble(tokenizer.nextToken()),
-                    Double.parseDouble(tokenizer.nextToken()));
-        }
-
         private Area findAreaType(Class clazz) {
             if (areaStack.size() > 0) {
                 int pos = areaStack.size() - 1;
@@ -394,7 +386,7 @@
                 if (currentPageViewport != null) {
                     throw new IllegalStateException("currentPageViewport must be null");
                 }
-                Rectangle2D viewArea = parseRect(attributes.getValue("bounds"));
+                Rectangle2D viewArea = getAttributeAsRectangle2D(attributes, "bounds");
                 int pageNumber = getAttributeAsInteger(attributes, "nr", -1);
                 String key = attributes.getValue("key");
                 String pageNumberString = attributes.getValue("formatted-nr");
@@ -430,7 +422,7 @@
                 if (rv != null) {
                     throw new IllegalStateException("Current RegionViewport must be null");
                 }
-                Rectangle2D viewArea = parseRect(attributes.getValue("rect"));
+                Rectangle2D viewArea = getAttributeAsRectangle2D(attributes, "rect");
                 rv = new RegionViewport(viewArea);
                 transferForeignObjects(attributes, rv);
                 rv.setClip(getAttributeAsBoolean(attributes, "clipped", false));
@@ -750,25 +742,11 @@
 
         private class WordMaker extends AbstractMaker {
 
-            private int[] toIntArray(String s) {
-                if (s == null || s.length() == 0) {
-                    return null;
-                }
-                StringTokenizer tokenizer = new StringTokenizer(s, " ");
-                List values = new java.util.ArrayList();
-                while (tokenizer.hasMoreTokens()) {
-                    values.add(new Integer(tokenizer.nextToken()));
-                }
-                int[] res = new int[values.size()];
-                for (int i = 0, c = res.length; i < c; i++) {
-                    res[i] = ((Integer)values.get(i)).intValue();
-                }
-                return res;
-            }
-            
             public void endElement() {
                 int offset = getAttributeAsInteger(lastAttributes, "offset", 0);
-                int[] letterAdjust = toIntArray(lastAttributes.getValue("letter-adjust"));
+                int[] letterAdjust 
+                        = ConversionUtils.toIntArray(
+                            lastAttributes.getValue("letter-adjust"), "\\s");
                 content.flip();
                 WordArea word = new WordArea(content.toString().trim(), offset, letterAdjust);
                 AbstractTextArea text = getCurrentText();
@@ -1094,7 +1072,7 @@
             }
         }
 
-        private boolean getAttributeAsBoolean(Attributes attributes, String name,
+        private static boolean getAttributeAsBoolean(Attributes attributes, String name,
                 boolean defaultValue) {
             String s = attributes.getValue(name);
             if (s == null) {
@@ -1104,7 +1082,7 @@
             }
         }
 
-        private int getAttributeAsInteger(Attributes attributes, String name, 
+        private static int getAttributeAsInteger(Attributes attributes, String name, 
                 int defaultValue) {
             String s = attributes.getValue(name);
             if (s == null) {
@@ -1114,36 +1092,30 @@
             }
         }
 
-        private CTM getAttributeAsCTM(Attributes attributes, String name) {
+        private static CTM getAttributeAsCTM(Attributes attributes, String name) {
             String s = attributes.getValue(name).trim();
             if (s.startsWith("[") && s.endsWith("]")) {
                 s = s.substring(1, s.length() - 1);
-                StringTokenizer tokenizer = new StringTokenizer(s, " ");
-                double[] values = new double[] {
-                        Double.parseDouble(tokenizer.nextToken()),
-                        Double.parseDouble(tokenizer.nextToken()),
-                        Double.parseDouble(tokenizer.nextToken()),
-                        Double.parseDouble(tokenizer.nextToken()),
-                        Double.parseDouble(tokenizer.nextToken()),
-                        Double.parseDouble(tokenizer.nextToken())};
+                double[] values = ConversionUtils.toDoubleArray(s, "\\s");
+                if (values.length != 6) {
+                    throw new IllegalArgumentException("CTM must consist of 6 double values!");
+                }
                 return new CTM(values[0], values[1], values[2], values[3], values[4], values[5]);
             } else {
-                throw new IllegalArgumentException("CTM must be surrounded by square brackets");
+                throw new IllegalArgumentException("CTM must be surrounded by square brackets!");
             }
         }
 
-        private Rectangle2D getAttributeAsRectangle2D(Attributes attributes, String name)
{
+        private static Rectangle2D getAttributeAsRectangle2D(Attributes attributes, String
name) {
             String s = attributes.getValue(name).trim();
-            StringTokenizer tokenizer = new StringTokenizer(s, " ");
-            double[] values = new double[] {
-                    Double.parseDouble(tokenizer.nextToken()),
-                    Double.parseDouble(tokenizer.nextToken()),
-                    Double.parseDouble(tokenizer.nextToken()),
-                    Double.parseDouble(tokenizer.nextToken())};
+            double[] values = ConversionUtils.toDoubleArray(s, "\\s");
+            if (values.length != 4) {
+                throw new IllegalArgumentException("Rectangle must consist of 4 double values!");
+            }
             return new Rectangle2D.Double(values[0], values[1], values[2], values[3]);
         }
 
-        private void transferForeignObjects(Attributes atts, AreaTreeObject ato) {
+        private static void transferForeignObjects(Attributes atts, AreaTreeObject ato) {
             for (int i = 0, c = atts.getLength(); i < c; i++) {
                 String ns = atts.getURI(i);
                 if (ns.length() > 0) {

Added: xmlgraphics/fop/trunk/src/java/org/apache/fop/util/ConversionUtils.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/util/ConversionUtils.java?rev=674276&view=auto
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/util/ConversionUtils.java (added)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/util/ConversionUtils.java Sun Jul  6 02:17:14
2008
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/* $Id$ */
+package org.apache.fop.util;
+
+/**
+ * This class contains utility methods for conversions, like
+ * a java.lang.String to an array of int or double.
+ */
+public final class ConversionUtils {
+
+    /**
+     * Converts the given base <code>String</code> into
+     * an array of <code>int</code>, splitting the base along the
+     * given separator pattern.
+     * <em>Note: this method assumes the input is a string containing 
+     * only decimal integers, signed or unsigned, that are parsable
+     * by <code>java.lang.Integer.parseInt(String)</code>. If this
+     * is not the case, the resulting <code>NumberFormatException</code>
+     * will have to be handled by the caller.</em>
+     * 
+     * @param baseString    the base string
+     * @param separatorPattern  the pattern separating the integer values
+     *        (if this is <code>null</code>, the baseString is parsed as one
+     *         integer value) 
+     * @return  an array of <code>int</code> whose size is equal to the number
+     *          values in the input string; <code>null</code> if this number
+     *          is equal to zero.
+     */
+    public static int[] toIntArray(String baseString, String separatorPattern) {
+        
+        if (baseString == null || "".equals(baseString)) {
+            return null;
+        }
+        
+        if (separatorPattern == null || "".equals(separatorPattern)) {
+            return new int[] { Integer.parseInt(baseString) };
+        }
+        
+        String[] values = baseString.split(separatorPattern);
+        int numValues = values.length;
+        if (numValues == 0) {
+            return null;
+        }
+        
+        int[] returnArray = new int[numValues];
+        for (int i = 0; i < numValues; ++i) {
+            returnArray[i] = Integer.parseInt(values[i]);
+        }
+        return returnArray;
+        
+    }
+
+    /**
+     * Converts the given base <code>String</code> into
+     * an array of <code>double</code>, splitting the base along the
+     * given separator pattern.
+     * <em>Note: this method assumes the input is a string containing 
+     * only decimal doubles, signed or unsigned, that are parsable
+     * by <code>java.lang.Double.parseDouble(String)</code>. If this
+     * is not the case, the resulting <code>NumberFormatException</code>
+     * will have to be handled by the caller.</em>
+     * 
+     * @param baseString    the base string
+     * @param separatorPattern  the pattern separating the integer values
+     *        (if this is <code>null</code>, the baseString is parsed as one
+     *         double value) 
+     * @return  an array of <code>double</code> whose size is equal to the number
+     *          values in the input string; <code>null</code> if this number
+     *          is equal to zero.
+     */
+    public static double[] toDoubleArray(String baseString, String separatorPattern) {
+        
+        if (baseString == null || "".equals(baseString)) {
+            return null;
+        }
+        
+        if (separatorPattern == null || "".equals(separatorPattern)) {
+            return new double[] { Double.parseDouble(baseString) };
+        }
+        
+        String[] values = baseString.split(separatorPattern);
+        int numValues = values.length;
+        if (numValues == 0) {
+            return null;
+        }
+        
+        double[] returnArray = new double[numValues];
+        for (int i = 0; i < numValues; ++i) {
+            returnArray[i] = Double.parseDouble(values[i]);
+        }
+        return returnArray;
+        
+    }
+    
+}

Propchange: xmlgraphics/fop/trunk/src/java/org/apache/fop/util/ConversionUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xmlgraphics/fop/trunk/src/java/org/apache/fop/util/ConversionUtils.java
------------------------------------------------------------------------------
    svn:keywords = Id



---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-help@xmlgraphics.apache.org


Mime
View raw message