aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndreas L. Delmelle <adelmelle@apache.org>2008-07-06 09:17:14 +0000
committerAndreas L. Delmelle <adelmelle@apache.org>2008-07-06 09:17:14 +0000
commitf4a4b961297c7cb7af08d47e9c804b64fcf65875 (patch)
tree71c1ce92673a2ebd47f5a66859f0a6b93bdb135f
parentd1be091687e009562c1e9a56c83cdee47c96459a (diff)
downloadxmlgraphics-fop-f4a4b961297c7cb7af08d47e9c804b64fcf65875.tar.gz
xmlgraphics-fop-f4a4b961297c7cb7af08d47e9c804b64fcf65875.zip
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(). git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@674276 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--src/java/org/apache/fop/area/AreaTreeParser.java68
-rw-r--r--src/java/org/apache/fop/util/ConversionUtils.java110
2 files changed, 130 insertions, 48 deletions
diff --git a/src/java/org/apache/fop/area/AreaTreeParser.java b/src/java/org/apache/fop/area/AreaTreeParser.java
index 68ad6eb6d..6d9fd4f32 100644
--- a/src/java/org/apache/fop/area/AreaTreeParser.java
+++ b/src/java/org/apache/fop/area/AreaTreeParser.java
@@ -78,6 +78,7 @@ import org.apache.fop.traits.BorderProps;
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 @@ public class AreaTreeParser {
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 @@ public class AreaTreeParser {
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 @@ public class AreaTreeParser {
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 @@ public class AreaTreeParser {
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 @@ public class AreaTreeParser {
}
}
- 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 @@ public class AreaTreeParser {
}
}
- 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 @@ public class AreaTreeParser {
}
}
- 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) {
diff --git a/src/java/org/apache/fop/util/ConversionUtils.java b/src/java/org/apache/fop/util/ConversionUtils.java
new file mode 100644
index 000000000..e2d93fbd3
--- /dev/null
+++ b/src/java/org/apache/fop/util/ConversionUtils.java
@@ -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;
+
+ }
+
+}