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 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  }