diff options
author | Andreas L. Delmelle <adelmelle@apache.org> | 2008-07-06 09:17:14 +0000 |
---|---|---|
committer | Andreas L. Delmelle <adelmelle@apache.org> | 2008-07-06 09:17:14 +0000 |
commit | f4a4b961297c7cb7af08d47e9c804b64fcf65875 (patch) | |
tree | 71c1ce92673a2ebd47f5a66859f0a6b93bdb135f /src/java/org/apache/fop/util | |
parent | d1be091687e009562c1e9a56c83cdee47c96459a (diff) | |
download | xmlgraphics-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
Diffstat (limited to 'src/java/org/apache/fop/util')
-rw-r--r-- | src/java/org/apache/fop/util/ConversionUtils.java | 110 |
1 files changed, 110 insertions, 0 deletions
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; + + } + +} |