View Javadoc

1   /**********************************************
2    * Copyright (C) 2010 Lukas Laag
3    * This file is part of lib-gwt-svg.
4    * 
5    * libgwtsvg is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * libgwtsvg is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   * 
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with libgwtsvg.  If not, see http://www.gnu.org/licenses/
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.UiBinderWriter;
27  import com.google.gwt.uibinder.rebind.XMLElement;
28  
29  public class SvgFieldInterpreter implements XMLElement.Interpreter<String> {
30  
31  	private final UiBinderWriter writer;
32  	private final String ancestorExpression;
33  	private final Element root;
34  
35  	public SvgFieldInterpreter(UiBinderWriter writer, String ancestorExpression, Element root) {
36  		this.writer = writer;
37  		this.ancestorExpression = ancestorExpression;
38  		this.root = root;
39  	}
40  
41  	public String interpretElement(XMLElement elem) throws UnableToCompleteException {
42  		String fieldName = writer.declareFieldIfNeeded(elem);
43  		if (fieldName != null) {
44  			JClassType type = writer.findFieldType(elem);
45  		    writer.setFieldInitializer(fieldName, "null");
46  		    writer.addInitStatement(
47  		        "%s = (" + type.getQualifiedSourceName() + ")" + SVGWidget.class.getName() + ".getUiBinderField(%s, \"%s\");",
48  		        fieldName, ancestorExpression, getXpath(elem.getElement()));
49  		    return null;
50  		}
51  
52  		/*
53  		 * Return null because we don't want to replace the dom element with any
54  		 * particular string (though we may have consumed its id or gwt:field)
55  		 */
56  		return null;
57  	}
58  	
59  	private String getXpath(Element element) {
60  		StringBuilder buffer = new StringBuilder();
61  		while (!root.isSameNode(element)) {
62  			int index = 1;
63  			Node sibling = element.getPreviousSibling();
64  			while (sibling != null) {
65  				if (sibling.getNodeType() == Node.ELEMENT_NODE) {
66  					index++;
67  				}
68  				sibling = sibling.getPreviousSibling();
69  			}
70  			buffer.insert(0, "/*[" + index + "]");
71  			Node node = element.getParentNode();
72  			while (node.getNodeType() != Node.ELEMENT_NODE) {
73  				node = node.getParentNode();
74  			}
75  			element = (Element)node;
76  		}
77  		buffer.insert(0, ".");
78  		return buffer.toString();
79  	}
80  }