Skip to content

Commit b9a54aa

Browse files
Kate IvanovaiText-CI
authored andcommitted
Add log messages handler in svg
DEVSIX-2904
1 parent 45f468b commit b9a54aa

File tree

25 files changed

+132
-58
lines changed

25 files changed

+132
-58
lines changed

styled-xml-parser/src/main/java/com/itextpdf/styledxmlparser/node/impl/jsoup/JsoupXmlParser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ This file is part of the iText (R) project.
5353
import com.itextpdf.styledxmlparser.jsoup.nodes.Element;
5454
import com.itextpdf.styledxmlparser.jsoup.nodes.Node;
5555
import com.itextpdf.styledxmlparser.jsoup.nodes.TextNode;
56+
import com.itextpdf.styledxmlparser.jsoup.nodes.XmlDeclaration;
5657
import com.itextpdf.styledxmlparser.node.IDocumentNode;
5758
import com.itextpdf.styledxmlparser.node.INode;
5859
import com.itextpdf.styledxmlparser.node.impl.jsoup.node.JsoupDataNode;
@@ -123,7 +124,8 @@ private INode wrapJsoupHierarchy(Node jsoupNode) {
123124
resultNode = new JsoupDataNode((DataNode) jsoupNode);
124125
} else if (jsoupNode instanceof DocumentType) {
125126
resultNode = new JsoupDocumentTypeNode((DocumentType) jsoupNode);
126-
} else if (jsoupNode instanceof Comment) {
127+
} else if (jsoupNode instanceof Comment || jsoupNode instanceof XmlDeclaration) {
128+
// Ignore. We should do this to avoid redundant log message
127129
} else {
128130
logger.error(MessageFormatUtil.format(LogMessageConstant.ERROR_PARSING_COULD_NOT_MAP_NODE, jsoupNode.getClass()));
129131
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.itextpdf.styledxmlparser.jsoup;
2+
3+
import com.itextpdf.styledxmlparser.node.IDocumentNode;
4+
import com.itextpdf.styledxmlparser.node.impl.jsoup.JsoupXmlParser;
5+
import com.itextpdf.test.annotations.type.UnitTest;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
import org.junit.experimental.categories.Category;
9+
10+
import java.io.ByteArrayInputStream;
11+
import java.io.IOException;
12+
import java.io.InputStream;
13+
14+
@Category(UnitTest.class)
15+
public class JsoupXmlParserTest {
16+
@Test
17+
public void testXmlDeclarationAndComment() throws IOException {
18+
String xml = "<?xml version=\"1.0\" standalone=\"no\"?>\n" +
19+
"<!-- just declaration and comment -->";
20+
InputStream stream = new ByteArrayInputStream(xml.getBytes());
21+
IDocumentNode node = new JsoupXmlParser().parse(stream, "UTF-8");
22+
// only text (whitespace) child node shall be fetched.
23+
Assert.assertEquals(1, node.childNodes().size());
24+
}
25+
}

svg/src/main/java/com/itextpdf/svg/converter/SvgConverter.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -946,16 +946,17 @@ public static float[] extractWidthAndHeight(ISvgNodeRenderer topSvgRenderer) {
946946
for (int i = 0; i < values.length; i++) {
947947
values[i] = CssUtils.parseAbsoluteLength(valueStrings.get(i));
948948
}
949+
viewBoxPresent = true;
949950
}
950951
float width, height;
951952
String wString, hString;
952953
wString = topSvgRenderer.getAttribute(SvgConstants.Attributes.WIDTH);
953954
if (wString == null) {
954-
//Log Warning
955-
LOGGER.warn(SvgLogMessageConstant.MISSING_WIDTH);
956955
if (viewBoxPresent) {
957956
width = values[2];
958957
} else {
958+
//Log Warning
959+
LOGGER.warn(SvgLogMessageConstant.MISSING_WIDTH);
959960
//Set to browser default
960961
width = CssUtils.parseAbsoluteLength("300px");
961962
}
@@ -964,11 +965,11 @@ public static float[] extractWidthAndHeight(ISvgNodeRenderer topSvgRenderer) {
964965
}
965966
hString = topSvgRenderer.getAttribute(SvgConstants.Attributes.HEIGHT);
966967
if (hString == null) {
967-
//Log Warning
968-
LOGGER.warn(SvgLogMessageConstant.MISSING_HEIGHT);
969968
if (viewBoxPresent) {
970969
height = values[3];
971970
} else {
971+
//Log Warning
972+
LOGGER.warn(SvgLogMessageConstant.MISSING_HEIGHT);
972973
//Set to browser default
973974
height = CssUtils.parseAbsoluteLength("150px");
974975
}

svg/src/test/java/com/itextpdf/svg/converter/SvgConverterIntegrationTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ This file is part of the iText (R) project.
5555
import com.itextpdf.layout.element.Image;
5656
import com.itextpdf.layout.font.FontProvider;
5757
import com.itextpdf.layout.font.FontSet;
58+
import com.itextpdf.styledxmlparser.LogMessageConstant;
5859
import com.itextpdf.svg.dummy.sdk.ExceptionInputStream;
60+
import com.itextpdf.svg.exceptions.SvgLogMessageConstant;
5961
import com.itextpdf.svg.processors.ISvgConverterProperties;
6062
import com.itextpdf.svg.processors.ISvgProcessorResult;
6163
import com.itextpdf.svg.processors.impl.SvgConverterProperties;
@@ -65,6 +67,8 @@ This file is part of the iText (R) project.
6567
import com.itextpdf.svg.renderers.impl.RectangleSvgNodeRenderer;
6668
import com.itextpdf.svg.renderers.impl.SvgTagSvgNodeRenderer;
6769
import com.itextpdf.test.ITextTest;
70+
import com.itextpdf.test.annotations.LogMessage;
71+
import com.itextpdf.test.annotations.LogMessages;
6872
import com.itextpdf.test.annotations.type.IntegrationTest;
6973
import org.junit.Assert;
7074
import org.junit.BeforeClass;
@@ -144,6 +148,9 @@ public void basicIntegrationTest() throws IOException, InterruptedException {
144148
}
145149

146150
@Test
151+
@LogMessages(messages = {
152+
@LogMessage(messageTemplate = SvgLogMessageConstant.UNMAPPEDTAG),
153+
})
147154
public void nonExistingTagIntegrationTest() {
148155
String contents = "<svg width='100pt' height='100pt'> <nonExistingTag/> </svg>";
149156
PdfDocument doc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
@@ -154,6 +161,9 @@ public void nonExistingTagIntegrationTest() {
154161
}
155162

156163
@Test
164+
@LogMessages(messages = {
165+
@LogMessage(messageTemplate = SvgLogMessageConstant.UNMAPPEDTAG, count = 14),
166+
})
157167
public void caseSensitiveTagTest() {
158168
String contents = "<svg width='100pt' height='100pt'>" +
159169
"<altGlyph /><altglyph />" +

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,26 @@ public void stylesOfSvgTagProcessingTest() {
186186

187187
Assert.assertEquals(expected, actual);
188188
}
189+
190+
@Test
191+
//TODO DEVSIX-2058
192+
public void fontResolverIntegrationTest() throws com.itextpdf.io.IOException, InterruptedException, java.io.IOException {
193+
convertAndCompareVisually(sourceFolder, destinationFolder, "fontssvg");
194+
}
195+
196+
@Test
197+
public void validLocalFontTest() throws com.itextpdf.io.IOException, InterruptedException, java.io.IOException {
198+
convertAndCompareVisually(sourceFolder, destinationFolder, "validLocalFontTest");
199+
}
200+
201+
@Test
202+
public void fontWeightTest() throws com.itextpdf.io.IOException, InterruptedException, java.io.IOException {
203+
convertAndCompareVisually(sourceFolder, destinationFolder, "fontWeightTest");
204+
}
205+
206+
@Test
207+
//TODO DEVSIX-2264: that test shall fail after the fix.
208+
public void googleFontsTest() throws com.itextpdf.io.IOException, InterruptedException, java.io.IOException {
209+
convertAndCompareVisually(sourceFolder, destinationFolder, "googleFontsTest");
210+
}
189211
}

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

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,13 @@ This file is part of the iText (R) project.
5252
import com.itextpdf.styledxmlparser.jsoup.nodes.Element;
5353
import com.itextpdf.styledxmlparser.jsoup.nodes.TextNode;
5454
import com.itextpdf.styledxmlparser.jsoup.parser.Tag;
55-
import com.itextpdf.styledxmlparser.node.IDocumentNode;
5655
import com.itextpdf.styledxmlparser.node.INode;
57-
import com.itextpdf.styledxmlparser.node.impl.jsoup.JsoupXmlParser;
5856
import com.itextpdf.styledxmlparser.node.impl.jsoup.node.JsoupElementNode;
5957
import com.itextpdf.styledxmlparser.node.impl.jsoup.node.JsoupTextNode;
6058
import com.itextpdf.svg.SvgConstants;
6159
import com.itextpdf.svg.css.impl.SvgStyleResolver;
6260
import com.itextpdf.svg.processors.impl.SvgConverterProperties;
6361
import com.itextpdf.svg.processors.impl.SvgProcessorContext;
64-
import com.itextpdf.svg.renderers.SvgIntegrationTest;
65-
import com.itextpdf.test.ITextTest;
6662
import com.itextpdf.test.annotations.LogMessage;
6763
import com.itextpdf.test.annotations.LogMessages;
6864
import com.itextpdf.test.annotations.type.UnitTest;
@@ -72,19 +68,12 @@ This file is part of the iText (R) project.
7268
import java.util.Map;
7369

7470
import org.junit.Assert;
75-
import org.junit.BeforeClass;
7671
import org.junit.Test;
7772
import org.junit.experimental.categories.Category;
7873

7974
@Category(UnitTest.class)
80-
public class SvgStyleResolverTest extends SvgIntegrationTest {
81-
private static final String sourceFolder = "./src/test/resources/com/itextpdf/svg/css/SvgStyleResolver/";
82-
private static final String destinationFolder = "./target/test/com/itextpdf/svg/css/SvgStyleResolver/";
83-
84-
@BeforeClass
85-
public static void beforeClass() {
86-
ITextTest.createDestinationFolder(destinationFolder);
87-
}
75+
public class SvgStyleResolverTest {
76+
private static final String baseUri = "./src/test/resources/com/itextpdf/svg/css/SvgStyleResolver/";
8877

8978
//Single element test
9079
//Inherits values from parent?
@@ -128,13 +117,13 @@ public void svgCssResolverXlinkTest() {
128117
JsoupElementNode node = new JsoupElementNode(jsoupImage);
129118

130119
SvgConverterProperties scp = new SvgConverterProperties();
131-
scp.setBaseUri(sourceFolder);
120+
scp.setBaseUri(baseUri);
132121

133122
SvgProcessorContext processorContext = new SvgProcessorContext(scp);
134123
SvgStyleResolver sr = new SvgStyleResolver(node, processorContext);
135124
Map<String, String> attr = sr.resolveStyles(node, new SvgCssContext());
136125

137-
String fileName = sourceFolder + "itis.jpg";
126+
String fileName = baseUri + "itis.jpg";
138127
String expectedURL = UrlUtil.toNormalizedURI(fileName).toString();
139128

140129
Assert.assertEquals(expectedURL, attr.get("xlink:href"));
@@ -148,7 +137,7 @@ public void svgCssResolveHashXlinkTest() {
148137
JsoupElementNode node = new JsoupElementNode(jsoupImage);
149138

150139
SvgConverterProperties scp = new SvgConverterProperties();
151-
scp.setBaseUri(sourceFolder);
140+
scp.setBaseUri(baseUri);
152141

153142
SvgProcessorContext processorContext = new SvgProcessorContext(scp);
154143
SvgStyleResolver sr = new SvgStyleResolver(node, processorContext);
@@ -227,29 +216,4 @@ public void fontsResolverTagTest() {
227216
Assert.assertEquals(1, fontFaceRuleList.size());
228217
Assert.assertEquals(2, fontFaceRuleList.get(0).getProperties().size());
229218
}
230-
231-
@Test
232-
//TODO DEVSIX-2058
233-
public void fontResolverIntegrationTest() throws com.itextpdf.io.IOException, InterruptedException, java.io.IOException {
234-
convertAndCompareVisually(sourceFolder, destinationFolder, "fontssvg");
235-
}
236-
237-
@Test
238-
public void validLocalFontTest() throws com.itextpdf.io.IOException, InterruptedException, java.io.IOException {
239-
convertAndCompareVisually(sourceFolder, destinationFolder, "validLocalFontTest");
240-
}
241-
242-
@Test
243-
public void fontWeightTest() throws com.itextpdf.io.IOException, InterruptedException, java.io.IOException {
244-
convertAndCompareVisually(sourceFolder, destinationFolder, "fontWeightTest");
245-
}
246-
247-
/**
248-
* The following test should fail when RND-1042 is resolved
249-
*/
250-
@Test
251-
public void googleFontsTest() throws com.itextpdf.io.IOException, InterruptedException, java.io.IOException {
252-
convertAndCompareVisually(sourceFolder, destinationFolder, "googleFontsTest");
253-
}
254-
255219
}

svg/src/test/java/com/itextpdf/svg/processors/impl/font/FontFaceTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ public void fontSelectorTest01() throws IOException, InterruptedException {
108108
}
109109

110110
@Test
111-
@LogMessages(messages = {@LogMessage(messageTemplate = SvgLogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI)})
112111
public void fontFaceGrammarTest() throws IOException, InterruptedException {
113112
convertAndCompareVisually(sourceFolder, destinationFolder, "fontFaceGrammarTest");
114113
}

svg/src/test/java/com/itextpdf/svg/processors/impl/font/FontSizeTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.svg.processors.impl.font;
4444

45+
import com.itextpdf.styledxmlparser.LogMessageConstant;
46+
import com.itextpdf.svg.exceptions.SvgLogMessageConstant;
4547
import com.itextpdf.svg.renderers.SvgIntegrationTest;
4648
import com.itextpdf.test.ITextTest;
49+
import com.itextpdf.test.annotations.LogMessage;
50+
import com.itextpdf.test.annotations.LogMessages;
4751
import com.itextpdf.test.annotations.type.IntegrationTest;
4852

4953
import java.io.IOException;
@@ -69,11 +73,19 @@ public void fontSize01Test() throws IOException, InterruptedException {
6973
}
7074

7175
@Test
76+
@LogMessages(messages = {
77+
@LogMessage(messageTemplate = LogMessageConstant.UNKNOWN_ABSOLUTE_METRIC_LENGTH_PARSED),
78+
})
7279
public void fontSize02Test() throws IOException, InterruptedException {
7380
String name = "fontSizeTest02";
7481
convertAndCompareVisually(SOURCE_FOLDER, DESTINATION_FOLDER,name);
7582
}
7683

84+
@Test
85+
public void fontSize03Test() throws IOException, InterruptedException {
86+
String name = "fontSizeTest03";
87+
convertAndCompareVisually(SOURCE_FOLDER, DESTINATION_FOLDER,name);
88+
}
7789
@Test
7890
public void fontAbsoluteKeywords() throws IOException, InterruptedException {
7991
String name = "fontAbsoluteKeywords";

svg/src/test/java/com/itextpdf/svg/renderers/SvgIntegrationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,13 @@ This file is part of the iText (R) project.
5656
import java.io.InputStream;
5757
import java.io.OutputStream;
5858

59+
import com.itextpdf.test.ExtendedITextTest;
5960
import com.itextpdf.test.annotations.type.IntegrationTest;
6061
import org.junit.Assert;
6162
import org.junit.experimental.categories.Category;
6263

6364
@Category(IntegrationTest.class)
64-
public class SvgIntegrationTest {
65+
public class SvgIntegrationTest extends ExtendedITextTest {
6566

6667
public void convert(InputStream svg, OutputStream pdfOutputStream) throws IOException {
6768
PdfDocument doc = new PdfDocument(new PdfWriter(pdfOutputStream, new WriterProperties().setCompressionLevel(0)));

svg/src/test/java/com/itextpdf/svg/renderers/impl/LineSvgNodeRendererTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ This file is part of the iText (R) project.
5151
import com.itextpdf.styledxmlparser.exceptions.StyledXMLParserException;
5252
import com.itextpdf.svg.renderers.ISvgNodeRenderer;
5353
import com.itextpdf.svg.renderers.SvgDrawContext;
54+
import com.itextpdf.svg.renderers.SvgIntegrationTest;
5455
import com.itextpdf.test.ITextTest;
5556
import com.itextpdf.test.annotations.LogMessage;
5657
import com.itextpdf.test.annotations.LogMessages;
@@ -69,7 +70,7 @@ This file is part of the iText (R) project.
6970
import org.junit.rules.ExpectedException;
7071

7172
@Category(IntegrationTest.class)
72-
public class LineSvgNodeRendererTest {
73+
public class LineSvgNodeRendererTest extends SvgIntegrationTest{
7374

7475
@Rule
7576
public ExpectedException junitExpectedException = ExpectedException.none();
@@ -153,7 +154,7 @@ public void invalidAttributeTest01() {
153154

154155

155156
@Test
156-
@LogMessages(messages = @LogMessage(messageTemplate = com.itextpdf.styledxmlparser.LogMessageConstant.UNKNOWN_ABSOLUTE_METRIC_LENGTH_PARSED))
157+
@LogMessages(messages = @LogMessage(messageTemplate = com.itextpdf.styledxmlparser.LogMessageConstant.UNKNOWN_ABSOLUTE_METRIC_LENGTH_PARSED, count = 2))
157158
public void invalidAttributeTest02() throws IOException {
158159
Map<String, String> lineProperties = new HashMap<>();
159160
lineProperties.put("x1", "100");
@@ -225,4 +226,5 @@ public void getNotPresentAttributeTest() {
225226

226227
Assert.assertEquals(expected, actual, 0f);
227228
}
229+
228230
}

0 commit comments

Comments
 (0)