Skip to content

Commit 194d087

Browse files
author
Eugene Bochilo
committed
Fix dummy log messages when we are processing xlink href with data
DEVSIX-4840
1 parent aa468ca commit 194d087

File tree

10 files changed

+94
-7
lines changed

10 files changed

+94
-7
lines changed

styled-xml-parser/src/main/java/com/itextpdf/styledxmlparser/LogMessageConstant.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ public final class LogMessageConstant {
9393
public static final String NAN = "The passed value (@{0}) is not a number";
9494
/** The Constant RESOURCE_WITH_GIVEN_URL_WAS_FILTERED_OUT. */
9595
public static final String RESOURCE_WITH_GIVEN_URL_WAS_FILTERED_OUT = "Resource with given URL ({0}) was filtered out.";
96+
/** The Constant UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_DATA_URI. */
97+
public static final String UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_DATA_URI =
98+
"Unable to retrieve image with data URI {0}";
9699
/** The Constant UNABLE_TO_RETRIEVE_RESOURCE_WITH_GIVEN_RESOURCE_SIZE_BYTE_LIMIT. */
97100
public static final String UNABLE_TO_RETRIEVE_RESOURCE_WITH_GIVEN_RESOURCE_SIZE_BYTE_LIMIT = "Unable to retrieve resource with given URL ({0}) and resource size byte limit ({1}).";
98101
/** The Constant UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_BASE_URI. */

styled-xml-parser/src/main/java/com/itextpdf/styledxmlparser/resolver/resource/ResourceResolver.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,13 @@ public PdfXObject retrieveImageExtended(String src) {
192192
return imageXObject;
193193
}
194194
}
195-
logger.error(MessageFormatUtil.format(LogMessageConstant.UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_BASE_URI,
196-
uriResolver.getBaseUri(), src));
195+
if (isDataSrc(src)) {
196+
logger.error(MessageFormatUtil.format(LogMessageConstant.UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_DATA_URI,
197+
src));
198+
} else {
199+
logger.error(MessageFormatUtil.format(LogMessageConstant.UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_BASE_URI,
200+
uriResolver.getBaseUri(), src));
201+
}
197202
return null;
198203
}
199204

@@ -283,7 +288,7 @@ public InputStream retrieveResourceAsInputStream(String src) {
283288
* @return true if source is under data URI scheme
284289
*/
285290
public boolean isDataSrc(String src) {
286-
return src.startsWith(DATA_SCHEMA_PREFIX) && src.contains(",");
291+
return src != null && src.toLowerCase().startsWith(DATA_SCHEMA_PREFIX) && src.contains(",");
287292
}
288293

289294
/**

styled-xml-parser/src/test/java/com/itextpdf/styledxmlparser/resolver/resource/ResourceResolverTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,15 @@ public void retrieveImageExtendedBase64Test() {
198198
}
199199

200200
@Test
201-
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_BASE_URI))
201+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_DATA_URI))
202202
public void retrieveImageExtendedIncorrectBase64Test() {
203203
ResourceResolver resourceResolver = new ResourceResolver(baseUri);
204204
PdfXObject image = resourceResolver.retrieveImageExtended(bLogoCorruptedData);
205205
Assert.assertNull(image);
206206
}
207207

208208
@Test
209-
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_BASE_URI, logLevel = LogLevelConstants.ERROR))
209+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_DATA_URI, logLevel = LogLevelConstants.ERROR))
210210
public void retrieveImageExtendedCorruptedDataBase64Test() {
211211
ResourceResolver resourceResolver = new ResourceResolver(baseUri);
212212
PdfXObject image = resourceResolver.retrieveImageExtended(bLogoCorruptedData);

svg/src/main/java/com/itextpdf/svg/css/impl/SvgStyleResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ private Map<String, String> resolveStyles(INode element, SvgCssContext context)
338338
*/
339339
private void processXLink(final IAttribute attr, final Map<String, String> attributesMap) {
340340
String xlinkValue = attr.getValue();
341-
if (!isStartedWithHash(xlinkValue)) {
341+
if (!isStartedWithHash(xlinkValue) && !new ResourceResolver("").isDataSrc(xlinkValue)) {
342342
try {
343343
xlinkValue = this.resourceResolver.resolveAgainstBaseUri(attr.getValue()).toExternalForm();
344344
} catch (MalformedURLException mue) {

svg/src/test/java/com/itextpdf/svg/css/XLinkTest.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ This file is part of the iText (R) project.
4848
import com.itextpdf.styledxmlparser.jsoup.nodes.Element;
4949
import com.itextpdf.styledxmlparser.jsoup.parser.Tag;
5050
import com.itextpdf.styledxmlparser.node.impl.jsoup.node.JsoupElementNode;
51+
import com.itextpdf.svg.SvgConstants;
5152
import com.itextpdf.svg.css.impl.SvgStyleResolver;
5253
import com.itextpdf.svg.processors.impl.SvgConverterProperties;
5354
import com.itextpdf.svg.processors.impl.SvgProcessorContext;
@@ -70,12 +71,40 @@ public void svgCssResolveMalformedXlinkTest() {
7071
Element jsoupImage = new Element(Tag.valueOf("image"), "");
7172
Attributes imageAttributes = jsoupImage.attributes();
7273

73-
String value = "htt://are";
74+
String value = "http://are::";
7475
imageAttributes.put(new Attribute("xlink:href", value));
7576
JsoupElementNode node = new JsoupElementNode(jsoupImage);
7677

7778
SvgStyleResolver sr = new SvgStyleResolver(new SvgProcessorContext(new SvgConverterProperties()));
7879
Map<String, String> attr = sr.resolveStyles(node, new SvgCssContext());
7980
Assert.assertEquals(value, attr.get("xlink:href"));
8081
}
82+
83+
@Test
84+
public void svgCssResolveDataXlinkTest() {
85+
Element jsoupImage = new Element(Tag.valueOf(SvgConstants.Tags.IMAGE), "");
86+
Attributes imageAttributes = jsoupImage.attributes();
87+
JsoupElementNode node = new JsoupElementNode(jsoupImage);
88+
89+
String value1 = "data:image/png;base64,iVBORw0KGgoAAAANSU";
90+
imageAttributes.put(new Attribute("xlink:href", value1));
91+
92+
SvgStyleResolver sr = new SvgStyleResolver(new SvgProcessorContext(new SvgConverterProperties()));
93+
Map<String, String> attr = sr.resolveStyles(node, new SvgCssContext());
94+
Assert.assertEquals(value1, attr.get("xlink:href"));
95+
96+
String value2 = "data:...,.";
97+
imageAttributes.put(new Attribute("xlink:href", value2));
98+
99+
sr = new SvgStyleResolver(new SvgProcessorContext(new SvgConverterProperties()));
100+
attr = sr.resolveStyles(node, new SvgCssContext());
101+
Assert.assertEquals(value2, attr.get("xlink:href"));
102+
103+
String value3 = "dAtA:...,.";
104+
imageAttributes.put(new Attribute("xlink:href", value3));
105+
106+
sr = new SvgStyleResolver(new SvgProcessorContext(new SvgConverterProperties()));
107+
attr = sr.resolveStyles(node, new SvgCssContext());
108+
Assert.assertEquals(value3, attr.get("xlink:href"));
109+
}
81110
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.itextpdf.svg.renderers.impl;
2+
3+
import com.itextpdf.styledxmlparser.LogMessageConstant;
4+
import com.itextpdf.svg.renderers.SvgIntegrationTest;
5+
import com.itextpdf.test.ITextTest;
6+
import com.itextpdf.test.annotations.LogMessage;
7+
import com.itextpdf.test.annotations.LogMessages;
8+
import com.itextpdf.test.annotations.type.IntegrationTest;
9+
import org.junit.BeforeClass;
10+
import org.junit.Rule;
11+
import org.junit.Test;
12+
import org.junit.experimental.categories.Category;
13+
import org.junit.rules.ExpectedException;
14+
15+
import java.io.IOException;
16+
17+
@Category(IntegrationTest.class)
18+
public class XLinkDataTest extends SvgIntegrationTest {
19+
@Rule
20+
public ExpectedException junitExpectedException = ExpectedException.none();
21+
22+
private static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/svg/renderers/impl/XLinkDataTest/";
23+
private static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/svg/renderers/impl/XLinkDataTest/";
24+
25+
@BeforeClass
26+
public static void beforeClass() {
27+
ITextTest.createDestinationFolder(DESTINATION_FOLDER);
28+
}
29+
30+
@Test
31+
public void correctImageWithDataTest() throws IOException, InterruptedException {
32+
convertAndCompare(SOURCE_FOLDER, DESTINATION_FOLDER,"correctImageWithData");
33+
}
34+
35+
@Test
36+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_DATA_URI))
37+
public void incorrectImageWithDataTest() throws IOException, InterruptedException {
38+
convertAndCompare(SOURCE_FOLDER, DESTINATION_FOLDER, "incorrectImageWithData");
39+
}
40+
}
Lines changed: 5 additions & 0 deletions
Loading
Lines changed: 5 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)