diff options
author | Vincent Hennebert <vhennebert@apache.org> | 2009-08-12 10:46:39 +0000 |
---|---|---|
committer | Vincent Hennebert <vhennebert@apache.org> | 2009-08-12 10:46:39 +0000 |
commit | 40af8ffb2a374456d335f96770bba02fc17ae0d8 (patch) | |
tree | 2dcc425ec24a3de156a47933f261ed7d37db6416 /src/java/org/apache/fop/render/extensions | |
parent | cd6bbe20847b715034dbaf8b42af4f3e1d80a73f (diff) | |
download | xmlgraphics-fop-40af8ffb2a374456d335f96770bba02fc17ae0d8.tar.gz xmlgraphics-fop-40af8ffb2a374456d335f96770bba02fc17ae0d8.zip |
Renamed PageScaleAttributes class into PageScale.
Simplified and improved its behaviour:
- return null for an empty string
- allow for an arbitrary sequence of white spaces between the scales
- check that number of arguments is no more than 2
- other small improvements
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@803440 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop/render/extensions')
-rw-r--r-- | src/java/org/apache/fop/render/extensions/prepress/PageScale.java (renamed from src/java/org/apache/fop/render/extensions/prepress/PageScaleAttributes.java) | 46 |
1 files changed, 29 insertions, 17 deletions
diff --git a/src/java/org/apache/fop/render/extensions/prepress/PageScaleAttributes.java b/src/java/org/apache/fop/render/extensions/prepress/PageScale.java index d57ea708b..361423753 100644 --- a/src/java/org/apache/fop/render/extensions/prepress/PageScaleAttributes.java +++ b/src/java/org/apache/fop/render/extensions/prepress/PageScale.java @@ -21,6 +21,7 @@ package org.apache.fop.render.extensions.prepress; import java.awt.geom.Point2D; import java.text.MessageFormat; +import java.util.regex.Pattern; import org.apache.xmlgraphics.util.QName; @@ -29,7 +30,7 @@ import org.apache.fop.fo.extensions.ExtensionElementMapping; /** * This class provides utility methods to parse the 'fox:scale' extension attribute. */ -public final class PageScaleAttributes { +public final class PageScale { /** * The extension 'scale' attribute for the simple-page-master element. @@ -37,11 +38,12 @@ public final class PageScaleAttributes { public static final QName EXT_PAGE_SCALE = new QName(ExtensionElementMapping.URI, null, "scale"); + private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+"); /** * Utility classes should not have a public or default constructor */ - private PageScaleAttributes() { + private PageScale() { } /** @@ -50,31 +52,41 @@ public final class PageScaleAttributes { * @param scale scale attribute, input format: scaleX [scaleY] * @return the pair of (sx, sy) values */ - public static Point2D.Double getScaleAttributes(String scale) { + public static Point2D getScale(String scale) { + // TODO throw appropriate exceptions that can be caught by the event + // notification mechanism final String err = "Extension 'scale' attribute has incorrect value(s): {0}"; - if (scale == null) { + if (scale == null || scale.equals("")) { return null; } - Point2D.Double result = null; - + String[] scales = WHITESPACE_PATTERN.split(scale); + double scaleX; try { - String[] scales = scale.split(" "); - if (scales.length > 0) { - result = new Point2D.Double(Double.parseDouble(scales[0]), - Double.parseDouble(scales[0])); - } - if (scales.length > 1) { - result.y = Double.parseDouble(scales[1]); - } - if (result.x <= 0 || result.y <= 0) { + scaleX = Double.parseDouble(scales[0]); + } catch (NumberFormatException nfe) { + throw new IllegalArgumentException(MessageFormat.format(err, new Object[]{scale})); + } + double scaleY; + switch (scales.length) { + case 1: + scaleY = scaleX; + break; + case 2: + try { + scaleY = Double.parseDouble(scales[1]); + } catch (NumberFormatException nfe) { throw new IllegalArgumentException(MessageFormat.format(err, new Object[]{scale})); } - } catch (NumberFormatException nfe) { + break; + default: + throw new IllegalArgumentException("Too many arguments"); + } + if (scaleX <= 0 || scaleY <= 0) { throw new IllegalArgumentException(MessageFormat.format(err, new Object[]{scale})); } - return result; + return new Point2D.Double(scaleX, scaleY); } } |