1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.google.gwt.uibinder.elementparsers;
19
20 import org.vectomatic.dom.svg.ui.SVGWidget;
21 import org.w3c.dom.Element;
22 import org.w3c.dom.Node;
23
24 import com.google.gwt.core.ext.UnableToCompleteException;
25 import com.google.gwt.core.ext.typeinfo.JClassType;
26 import com.google.gwt.uibinder.rebind.FieldManager;
27 import com.google.gwt.uibinder.rebind.FieldWriter;
28 import com.google.gwt.uibinder.rebind.UiBinderWriter;
29 import com.google.gwt.uibinder.rebind.XMLElement;
30
31 public class SvgFieldInterpreter implements XMLElement.Interpreter<String> {
32
33 private final UiBinderWriter writer;
34 private final String ancestorExpression;
35 private final Element root;
36
37 public SvgFieldInterpreter(UiBinderWriter writer, String ancestorExpression, Element root) {
38 this.writer = writer;
39 this.ancestorExpression = ancestorExpression;
40 this.root = root;
41 }
42
43 public String interpretElement(XMLElement elem) throws UnableToCompleteException {
44 String fieldName = writer.declareFieldIfNeeded(elem);
45 if (fieldName != null) {
46 JClassType type = writer.findFieldType(elem);
47 StringBuilder builder = new StringBuilder();
48 builder.append("(");
49 builder.append(type.getQualifiedSourceName());
50 builder.append(")");
51 builder.append(SVGWidget.class.getName());
52 builder.append(".getUiBinderField(");
53 FieldManager fieldManager = writer.getFieldManager();
54 builder.append(fieldManager.convertFieldToGetter(ancestorExpression));
55 builder.append(".getSvgElement().getElement(), \"");
56 builder.append(getXpath(elem.getElement()));
57 builder.append("\")");
58 FieldWriter ancestorWriter = fieldManager.require(ancestorExpression);
59 ancestorWriter.setBuildPrecedence(1 + ancestorWriter.getBuildPrecedence());
60 writer.setFieldInitializer(fieldName, builder.toString());
61
62
63
64 return null;
65 }
66
67
68
69
70
71 return null;
72 }
73
74 private String getXpath(Element element) {
75 StringBuilder buffer = new StringBuilder();
76 while (!root.isSameNode(element)) {
77 int index = 1;
78 Node sibling = element.getPreviousSibling();
79 while (sibling != null) {
80 if (sibling.getNodeType() == Node.ELEMENT_NODE) {
81 index++;
82 }
83 sibling = sibling.getPreviousSibling();
84 }
85 buffer.insert(0, "/*[" + index + "]");
86 Node node = element.getParentNode();
87 while (node.getNodeType() != Node.ELEMENT_NODE) {
88 node = node.getParentNode();
89 }
90 element = (Element)node;
91 }
92 buffer.insert(0, ".");
93 return buffer.toString();
94 }
95 }