Skip to content

Commit 7c3111c

Browse files
XGC-145: Avoid parsing uri to read page number
1 parent 70d800f commit 7c3111c

File tree

2 files changed

+25
-38
lines changed

2 files changed

+25
-38
lines changed

src/main/java/org/apache/xmlgraphics/image/loader/util/ImageUtil.java

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
import java.lang.reflect.InvocationTargetException;
2626
import java.lang.reflect.Method;
2727
import java.lang.reflect.Proxy;
28-
import java.net.URI;
29-
import java.net.URISyntaxException;
3028
import java.util.Map;
3129
import java.util.zip.GZIPInputStream;
3230

@@ -267,33 +265,24 @@ public static Integer getPageIndexFromURI(String uri) {
267265
if (uri.indexOf('#') < 0) {
268266
return null;
269267
}
270-
try {
271-
URI u = new URI(uri);
272-
String fragment = u.getFragment();
273-
if (fragment != null) {
274-
int pos = fragment.indexOf(PAGE_INDICATOR);
275-
if (pos >= 0) {
276-
pos += PAGE_INDICATOR.length();
277-
StringBuffer sb = new StringBuffer();
278-
while (pos < fragment.length()) {
279-
char c = fragment.charAt(pos);
280-
if (c >= '0' && c <= '9') {
281-
sb.append(c);
282-
} else {
283-
break;
284-
}
285-
pos++;
286-
}
287-
if (sb.length() > 0) {
288-
int pageIndex = Integer.parseInt(sb.toString()) - 1;
289-
pageIndex = Math.max(0, pageIndex);
290-
return pageIndex;
291-
}
268+
int pos = uri.indexOf(PAGE_INDICATOR);
269+
if (pos >= 0) {
270+
pos += PAGE_INDICATOR.length();
271+
StringBuilder sb = new StringBuilder();
272+
while (pos < uri.length()) {
273+
char c = uri.charAt(pos);
274+
if (c >= '0' && c <= '9') {
275+
sb.append(c);
276+
} else {
277+
break;
292278
}
279+
pos++;
280+
}
281+
if (sb.length() > 0) {
282+
int pageIndex = Integer.parseInt(sb.toString()) - 1;
283+
pageIndex = Math.max(0, pageIndex);
284+
return pageIndex;
293285
}
294-
} catch (URISyntaxException e) {
295-
throw new IllegalArgumentException("URI is invalid: "
296-
+ e.getLocalizedMessage());
297286
}
298287
return null;
299288
}

src/test/java/org/apache/xmlgraphics/image/loader/ImageUtilTestCase.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import static org.junit.Assert.assertEquals;
2525
import static org.junit.Assert.assertNull;
26-
import static org.junit.Assert.fail;
2726

2827
import org.apache.xmlgraphics.image.loader.util.ImageUtil;
2928

@@ -34,10 +33,9 @@ public class ImageUtilTestCase {
3433

3534
/**
3635
* Tests {@link ImageUtil.needPageIndexFromURI(String)}.
37-
* @throws Exception if an error occurs
3836
*/
3937
@Test
40-
public void testNeedPageIndex() throws Exception {
38+
public void testNeedPageIndex() {
4139
int pageIndex;
4240

4341
pageIndex = ImageUtil.needPageIndexFromURI("http://localhost/images/scan1.tif");
@@ -56,23 +54,23 @@ public void testNeedPageIndex() throws Exception {
5654
assertEquals(1, pageIndex);
5755

5856
//Not a valid URI
59-
try {
60-
pageIndex = ImageUtil.needPageIndexFromURI("C:\\images\\scan1.tif#page=44");
61-
fail("Invalid URI. Method must fail.");
62-
} catch (IllegalArgumentException e) {
63-
//expected
64-
}
57+
pageIndex = ImageUtil.needPageIndexFromURI("C:\\images\\scan1.tif#page=44");
58+
assertEquals(43, pageIndex);
59+
6560
//Valid URI
6661
pageIndex = ImageUtil.needPageIndexFromURI("file:///C:/images/scan1.tif#page=44");
6762
assertEquals(43, pageIndex);
63+
64+
pageIndex = ImageUtil.needPageIndexFromURI(
65+
"Balesetbiztosítás_ kötvénycsomag - e-mail_3000000637_Biztosítási kötvény melléklettel.pdf#page=1");
66+
assertEquals(0, pageIndex);
6867
}
6968

7069
/**
7170
* Tests {@link ImageUtil.getPageIndexFromURI(String)}.
72-
* @throws Exception if an error occurs
7371
*/
7472
@Test
75-
public void testGetPageIndex() throws Exception {
73+
public void testGetPageIndex() {
7674
Integer pageIndex;
7775

7876
pageIndex = ImageUtil.getPageIndexFromURI("http://localhost/images/scan1.tif");

0 commit comments

Comments
 (0)