Return-Path: Delivered-To: apmail-xmlgraphics-fop-commits-archive@www.apache.org Received: (qmail 38939 invoked from network); 6 Jul 2008 09:18:07 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 6 Jul 2008 09:18:07 -0000 Received: (qmail 46674 invoked by uid 500); 6 Jul 2008 09:18:07 -0000 Delivered-To: apmail-xmlgraphics-fop-commits-archive@xmlgraphics.apache.org Received: (qmail 46652 invoked by uid 500); 6 Jul 2008 09:18:07 -0000 Mailing-List: contact fop-commits-help@xmlgraphics.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: fop-dev@xmlgraphics.apache.org Delivered-To: mailing list fop-commits@xmlgraphics.apache.org Received: (qmail 46643 invoked by uid 99); 6 Jul 2008 09:18:07 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 06 Jul 2008 02:18:07 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 06 Jul 2008 09:17:24 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 040F023889F3; Sun, 6 Jul 2008 02:17:16 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: fop-commits@xmlgraphics.apache.org From: adelmelle@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080706091716.040F023889F3@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 String into + * an array of int, splitting the base along the + * given separator pattern. + * Note: this method assumes the input is a string containing + * only decimal integers, signed or unsigned, that are parsable + * by java.lang.Integer.parseInt(String). If this + * is not the case, the resulting NumberFormatException + * will have to be handled by the caller. + * + * @param baseString the base string + * @param separatorPattern the pattern separating the integer values + * (if this is null, the baseString is parsed as one + * integer value) + * @return an array of int whose size is equal to the number + * values in the input string; null 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 String into + * an array of double, splitting the base along the + * given separator pattern. + * Note: this method assumes the input is a string containing + * only decimal doubles, signed or unsigned, that are parsable + * by java.lang.Double.parseDouble(String). If this + * is not the case, the resulting NumberFormatException + * will have to be handled by the caller. + * + * @param baseString the base string + * @param separatorPattern the pattern separating the integer values + * (if this is null, the baseString is parsed as one + * double value) + * @return an array of double whose size is equal to the number + * values in the input string; null 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