Skip to content

Commit ce007d7

Browse files
committed
PDFBOX-5660: refactor, as suggested by Valery Bokov; closes #250
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1928290 13f79535-47bb-0310-9956-ffa450edef68
1 parent 3efb831 commit ce007d7

File tree

6 files changed

+44
-60
lines changed

6 files changed

+44
-60
lines changed

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFAnnotation.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,8 @@ protected FDFAnnotation(Element element) throws IOException
195195
{
196196
throw new IOException("Error: missing attribute 'rect'");
197197
}
198-
String[] rectValues = rect.split(",");
199-
if (rectValues.length != 4)
200-
{
201-
throw new IOException("Error: wrong amount of numbers in attribute 'rect'");
202-
}
203-
float[] values = new float[4];
204-
for (int i = 0; i < 4; i++)
205-
{
206-
values[i] = Float.parseFloat(rectValues[i]);
207-
}
198+
float[] values = parseRectangleAttributes(
199+
rect, "Error: wrong amount of numbers in attribute 'rect'");
208200
setRectangle(new PDRectangle(COSArray.of(values)));
209201

210202
setTitle(element.getAttribute("title"));
@@ -313,6 +305,37 @@ protected FDFAnnotation(Element element) throws IOException
313305
}
314306
}
315307

308+
float[] parseRectangleAttributes(String rect, String errorMessage) throws IOException
309+
{
310+
String[] rectValues = rect.split(",");
311+
if (rectValues.length != 4)
312+
{
313+
throw new IOException(errorMessage);
314+
}
315+
float[] values = new float[4];
316+
values[0] = Float.parseFloat(rectValues[0]);
317+
values[1] = Float.parseFloat(rectValues[1]);
318+
values[2] = Float.parseFloat(rectValues[2]);
319+
values[3] = Float.parseFloat(rectValues[3]);
320+
return values;
321+
}
322+
323+
PDRectangle createRectangleFromAttributes(String rect, String errorMessage) throws IOException
324+
{
325+
String[] rectValues = rect.split(",");
326+
if (rectValues.length != 4)
327+
{
328+
throw new IOException(errorMessage);
329+
}
330+
PDRectangle rectangle = new PDRectangle();
331+
rectangle.setLowerLeftX(Float.parseFloat(rectValues[0]));
332+
rectangle.setLowerLeftY(Float.parseFloat(rectValues[1]));
333+
rectangle.setUpperRightX(Float.parseFloat(rectValues[2]));
334+
rectangle.setUpperRightY(Float.parseFloat(rectValues[3]));
335+
336+
return rectangle;
337+
}
338+
316339
/**
317340
* Create the correct FDFAnnotation.
318341
*

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFAnnotationCaret.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,9 @@ private void initFringe(Element element) throws IOException
8181
String fringe = element.getAttribute("fringe");
8282
if (fringe != null && !fringe.isEmpty())
8383
{
84-
String[] fringeValues = fringe.split(",");
85-
if (fringeValues.length != 4)
86-
{
87-
throw new IOException("Error: wrong amount of numbers in attribute 'fringe'");
88-
}
89-
PDRectangle rect = new PDRectangle();
90-
rect.setLowerLeftX(Float.parseFloat(fringeValues[0]));
91-
rect.setLowerLeftY(Float.parseFloat(fringeValues[1]));
92-
rect.setUpperRightX(Float.parseFloat(fringeValues[2]));
93-
rect.setUpperRightY(Float.parseFloat(fringeValues[3]));
84+
PDRectangle rect = createRectangleFromAttributes(
85+
fringe, "Error: wrong amount of numbers in attribute 'fringe'");
86+
9487
setFringe(rect);
9588
}
9689
}

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFAnnotationCircle.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,8 @@ private void initFringe(Element element) throws IOException
8383
String fringe = element.getAttribute("fringe");
8484
if (fringe != null && !fringe.isEmpty())
8585
{
86-
String[] fringeValues = fringe.split(",");
87-
if (fringeValues.length != 4)
88-
{
89-
throw new IOException("Error: wrong amount of numbers in attribute 'fringe'");
90-
}
91-
PDRectangle rect = new PDRectangle();
92-
rect.setLowerLeftX(Float.parseFloat(fringeValues[0]));
93-
rect.setLowerLeftY(Float.parseFloat(fringeValues[1]));
94-
rect.setUpperRightX(Float.parseFloat(fringeValues[2]));
95-
rect.setUpperRightY(Float.parseFloat(fringeValues[3]));
86+
PDRectangle rect = createRectangleFromAttributes(
87+
fringe, "Error: wrong amount of numbers in attribute 'fringe'");
9688
setFringe(rect);
9789
}
9890
}

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFAnnotationFreeText.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,8 @@ private void initFringe(Element element) throws IOException
104104
String fringe = element.getAttribute("fringe");
105105
if (fringe != null && !fringe.isEmpty())
106106
{
107-
String[] fringeValues = fringe.split(",");
108-
if (fringeValues.length != 4)
109-
{
110-
throw new IOException("Error: wrong amount of numbers in attribute 'fringe'");
111-
}
112-
PDRectangle rect = new PDRectangle();
113-
rect.setLowerLeftX(Float.parseFloat(fringeValues[0]));
114-
rect.setLowerLeftY(Float.parseFloat(fringeValues[1]));
115-
rect.setUpperRightX(Float.parseFloat(fringeValues[2]));
116-
rect.setUpperRightY(Float.parseFloat(fringeValues[3]));
107+
PDRectangle rect = createRectangleFromAttributes(
108+
fringe, "Error: wrong amount of numbers in attribute 'fringe'");
117109
setFringe(rect);
118110
}
119111
}

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFAnnotationLine.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,8 @@ public FDFAnnotationLine(Element element) throws IOException
8181
throw new IOException("Error: missing attribute 'end'");
8282
}
8383
String line = startCoords + "," + endCoords;
84-
String[] lineValues = line.split(",");
85-
if (lineValues.length != 4)
86-
{
87-
throw new IOException("Error: wrong amount of line coordinates");
88-
}
89-
float[] values = new float[4];
90-
for (int i = 0; i < 4; i++)
91-
{
92-
values[i] = Float.parseFloat(lineValues[i]);
93-
}
84+
float[] values = parseRectangleAttributes(
85+
line, "Error: wrong amount of line coordinates");
9486
setLine(values);
9587

9688
String leaderLine = element.getAttribute("leaderLength");

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFAnnotationSquare.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,8 @@ private void initFringe(Element element) throws IOException
8484
String fringe = element.getAttribute("fringe");
8585
if (fringe != null && !fringe.isEmpty())
8686
{
87-
String[] fringeValues = fringe.split(",");
88-
if (fringeValues.length != 4)
89-
{
90-
throw new IOException("Error: wrong amount of numbers in attribute 'fringe'");
91-
}
92-
PDRectangle rect = new PDRectangle();
93-
rect.setLowerLeftX(Float.parseFloat(fringeValues[0]));
94-
rect.setLowerLeftY(Float.parseFloat(fringeValues[1]));
95-
rect.setUpperRightX(Float.parseFloat(fringeValues[2]));
96-
rect.setUpperRightY(Float.parseFloat(fringeValues[3]));
87+
PDRectangle rect = createRectangleFromAttributes(
88+
fringe, "Error: wrong amount of numbers in attribute 'fringe'");
9789
setFringe(rect);
9890
}
9991
}

0 commit comments

Comments
 (0)