1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.vectomatic.dom.svg.impl;
19
20 import java.util.Stack;
21
22 import org.vectomatic.dom.svg.OMSVGAnimatedString;
23 import org.vectomatic.dom.svg.OMSVGStyle;
24 import org.vectomatic.dom.svg.utils.DOMHelper;
25 import org.vectomatic.dom.svg.utils.ParserException;
26 import org.vectomatic.dom.svg.utils.SVGConstants;
27
28 import com.google.gwt.dom.client.Element;
29 import com.google.gwt.dom.client.Node;
30 import com.google.gwt.dom.client.NodeList;
31 import com.google.gwt.dom.client.Text;
32 import com.google.gwt.regexp.shared.MatchResult;
33 import com.google.gwt.regexp.shared.RegExp;
34
35
36
37
38
39
40 public class SVGParserImplOpera extends SVGParserImpl {
41 private static final String URL = "^url\\((['\"])?[^#\\)]*#([^'\"\\)]*)\\1\\)$";
42 private static final RegExp URLExp = RegExp.compile(URL, "g");
43 private static String getRef(String expr) {
44 URLExp.setLastIndex(0);
45 MatchResult result = URLExp.exec(expr);
46 if (result != null && result.getGroupCount() == 3) {
47 return result.getGroup(2);
48 }
49 return null;
50 }
51
52
53
54
55
56
57
58 public final SVGSVGElement parse(String rawSvg) throws ParserException {
59 SVGDocument doc = parseFromString(rawSvg, "text/xml").cast();
60 Element elt = doc.getDocumentElement();
61 if ("parsererror".equals(DOMHelper.getLocalName(elt))) {
62 String message = "Parsing error";
63 if (elt.getFirstChild() != null) {
64 message = elt.getFirstChild().<Text>cast().getData();
65 }
66 throw new ParserException(ParserException.Type.NotWellFormed, message);
67 }
68 SVGSVGElement svg = DOMHelper.importNode(DOMHelper.getCurrentDocument(), elt, true).cast();
69 operaFix(svg);
70 return svg;
71 }
72
73
74
75
76
77
78
79
80
81 private static void operaFix(Element root) {
82 Stack<Element> stack = new Stack<Element>();
83 stack.push(root);
84 while(!stack.empty()) {
85 Element element = stack.pop();
86 if (SVGConstants.SVG_NAMESPACE_URI.equals(DOMHelper.getNamespaceURI(element))) {
87 OMSVGAnimatedString cn = element.<SVGElement>cast().getClassName_();
88 if (cn != null) {
89 String value = cn.getBaseVal();
90 if (value != null && value.length() > 0) {
91 cn.setBaseVal(value);
92 }
93 }
94 if (element.hasAttribute(SVGConstants.SVG_STYLE_ATTRIBUTE)) {
95 element.setAttribute(SVGConstants.SVG_STYLE_ATTRIBUTE, element.getAttribute(SVGConstants.SVG_STYLE_ATTRIBUTE));
96 OMSVGStyle style = element.getStyle().<OMSVGStyle>cast();
97 fixProperty(style, SVGConstants.CSS_FILL_PROPERTY);
98 fixProperty(style, SVGConstants.CSS_STROKE_PROPERTY);
99 }
100 }
101 NodeList<Node> childNodes = element.getChildNodes();
102 for (int i = 0, length = childNodes.getLength(); i < length; i++) {
103 Node node = childNodes.getItem(i);
104 if (node.getNodeType() == Node.ELEMENT_NODE) {
105 stack.push(node.<Element>cast());
106 }
107 }
108 }
109 }
110 private static void fixProperty(OMSVGStyle style, String propertyName) {
111 String propertyValue = style.getSVGProperty(propertyName);
112 if (propertyValue != null && propertyValue.length() > 0) {
113 String ref = getRef(propertyValue);
114 if (ref != null) {
115 String url = "url('#" + ref + "')";
116 style.setSVGProperty(propertyName, url);
117 }
118 }
119 }
120 }