Skip to content

Commit dc3f248

Browse files
BATIK-1388: Show css error message when no base uri
1 parent 8b21d8b commit dc3f248

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

batik-css/src/main/java/org/apache/batik/css/engine/CSSEngine.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,9 +1220,8 @@ public void parseStyleSheet(StyleSheet ss,
12201220
// e.printStackTrace();
12211221
String m = e.getMessage();
12221222
if (m == null) m = "";
1223-
String s = Messages.formatMessage
1224-
("stylesheet.syntax.error",
1225-
new Object[] { uri.toString(), rules, m });
1223+
String uriStr = uri == null ? "<unknown>" : uri.toString();
1224+
String s = Messages.formatMessage("stylesheet.syntax.error", new Object[] { uriStr, rules, m });
12261225
DOMException de = new DOMException(DOMException.SYNTAX_ERR, s);
12271226
if (userAgent == null) throw de;
12281227
userAgent.displayError(de);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
3+
Licensed to the Apache Software Foundation (ASF) under one or more
4+
contributor license agreements. See the NOTICE file distributed with
5+
this work for additional information regarding copyright ownership.
6+
The ASF licenses this file to You under the Apache License, Version 2.0
7+
(the "License"); you may not use this file except in compliance with
8+
the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
*/
19+
package org.apache.batik.css.engine;
20+
21+
import org.apache.batik.transcoder.ErrorHandler;
22+
import org.apache.batik.transcoder.TranscoderException;
23+
import org.apache.batik.transcoder.TranscoderInput;
24+
import org.apache.batik.transcoder.TranscoderOutput;
25+
import org.apache.batik.transcoder.image.PNGTranscoder;
26+
import org.junit.Assert;
27+
import org.junit.Test;
28+
29+
import java.io.ByteArrayInputStream;
30+
import java.io.ByteArrayOutputStream;
31+
32+
public class CSSEngineTestCase {
33+
@Test
34+
public void testParseErrorWithNoDataUri() throws Exception {
35+
String svgContent = "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 1337 528\" width=\"1337\" height=\"528\">\n" +
36+
" <text dx=\"0\" dy=\"0\" font-family=\"Allianz_Neo\" font-size=\"25pt\" font-weight=\"bold\" " +
37+
"transform=\"translate(622.771987 374.659548)\" stroke-width=\"0\">\n" +
38+
" <tspan y=\"0\" font-weight=\"bold\" stroke-width=\"0\">H5005912</tspan>\n" +
39+
" </text>\n" +
40+
" <style>@font-face {font-family: 'Allianz_Neo';font-style: Allianz_Neo;font-weight: bold;src: " +
41+
"url(data:font/ttf;charset=utf-8;base64,xx) format('truetype');}</style>\n" +
42+
"</svg>";
43+
PNGTranscoder transcoder = new PNGTranscoder();
44+
TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(svgContent.getBytes()));
45+
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
46+
TranscoderOutput output = new TranscoderOutput(ostream);
47+
final String[] ex = {""};
48+
transcoder.setErrorHandler(new ErrorHandler() {
49+
public void error(TranscoderException e) {
50+
ex[0] = e.getMessage();
51+
}
52+
public void fatalError(TranscoderException ex) {
53+
}
54+
public void warning(TranscoderException ex) {
55+
}
56+
});
57+
try {
58+
transcoder.transcode(input, output);
59+
} catch (RuntimeException e) {
60+
//ignore
61+
}
62+
Assert.assertEquals(ex[0], "null\n" +
63+
"Enclosed Exception:\n" +
64+
"<unknown>:\n" +
65+
"The following stylesheet represents an invalid \n" +
66+
"CSS document.\n" +
67+
"@font-face {font-family: 'Allianz_Neo';font-style: Allianz_Neo;font-weight: " +
68+
"bold;src: url(data:font/ttf;charset=utf-8;base64,xx) format('truetype');}\n" +
69+
"Original message:\n" +
70+
"The \"Allianz_Neo\" identifier is not a valid value for the \"font-style\" property. ");
71+
}
72+
}

0 commit comments

Comments
 (0)