1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.vectomatic.dev.svg.impl.gen;
19
20 import org.vectomatic.dom.svg.ui.SVGResource;
21 import org.vectomatic.dom.svg.utils.OMSVGParser;
22 import org.vectomatic.dom.svg.utils.SVGConstants;
23 import org.w3c.dom.Element;
24 import org.w3c.dom.Node;
25 import org.w3c.dom.NodeList;
26
27 import com.google.gwt.core.ext.UnableToCompleteException;
28 import com.google.gwt.core.ext.typeinfo.JClassType;
29 import com.google.gwt.uibinder.elementparsers.ElementParser;
30 import com.google.gwt.uibinder.elementparsers.SvgInterpreter;
31 import com.google.gwt.uibinder.rebind.UiBinderWriter;
32 import com.google.gwt.uibinder.rebind.XMLElement;
33
34 public class SVGImageParser implements ElementParser {
35 private static final String ATTR_RESOURCE = "resource";
36 private static final String ATTR_VALIDATED = "validated";
37 public SVGImageParser() {
38 }
39
40 @Override
41 public void parse(XMLElement elem, String fieldName, JClassType type,
42 UiBinderWriter writer) throws UnableToCompleteException {
43 if (elem.hasAttribute(ATTR_RESOURCE) && elem.hasChildNodes()) {
44 writer.die("In %s, attribute \"%s\" and inline svg are mutually exclusive", elem, ATTR_RESOURCE);
45 }
46 if (!(elem.hasAttribute(ATTR_RESOURCE) || elem.hasChildNodes())) {
47 writer.die("In %s, attribute \"%s\" or inline svg must be present", elem, ATTR_RESOURCE);
48 }
49 if (elem.hasAttribute(ATTR_RESOURCE)) {
50 JClassType svgResourceType = writer.getOracle().findType(SVGResource.class.getCanonicalName());
51 String resource = elem.consumeAttribute(ATTR_RESOURCE, svgResourceType);
52 writer.addStatement("%s.setResource(%s);", fieldName, resource);
53 } else {
54 boolean validated = true;
55 if (elem.hasAttribute(ATTR_VALIDATED)) {
56 String value = elem.consumeBooleanAttribute(ATTR_VALIDATED);
57 validated = Boolean.valueOf(value);
58 }
59 Element container = elem.getElement();
60 NodeList childNodes = container.getChildNodes();
61 Element root = null;
62 for (int i = 0, length = childNodes.getLength(); i < length; i++) {
63 Node node = childNodes.item(i);
64 if (node.getNodeType() == Node.ELEMENT_NODE) {
65 if (root == null
66 && SVGConstants.SVG_NAMESPACE_URI.equals(node.getNamespaceURI())
67 && SVGConstants.SVG_SVG_TAG.equals(node.getLocalName())) {
68 root = (Element)node;
69 } else {
70 writer.die("In %s, attribute \"%s\" or inline svg must be present", elem, ATTR_RESOURCE);
71 }
72 }
73 }
74 if (root == null) {
75 writer.die("In %s, attribute \"%s\" or inline svg must be present", elem, ATTR_RESOURCE);
76 }
77 writer.beginAttachedSection(fieldName + ".getElement()");
78 SvgInterpreter interpreter = SvgInterpreter.newInterpreterForUiObject(writer, fieldName, root);
79 String rawSvg = elem.consumeInnerHtml(interpreter);
80 if (validated) {
81 SVGValidator.validate(rawSvg, elem.getLocation().getSystemId(), null, writer);
82 }
83 String omSvgParser = OMSVGParser.class.getName();
84 writer.addStatement("%s.setSvgElement(%s.parse(\"%s\"));", fieldName, omSvgParser, rawSvg);
85 writer.endAttachedSection();
86 }
87 }
88
89 }