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 java.net.URL;
21  
22  import org.vectomatic.dom.svg.OMSVGSVGElement;
23  import org.vectomatic.dom.svg.ui.SVGResource;
24  import org.vectomatic.dom.svg.ui.SVGResource.Validated;
25  import org.vectomatic.dom.svg.utils.OMSVGParser;
26  
27  import com.google.gwt.core.ext.Generator;
28  import com.google.gwt.core.ext.TreeLogger;
29  import com.google.gwt.core.ext.UnableToCompleteException;
30  import com.google.gwt.core.ext.typeinfo.JMethod;
31  import com.google.gwt.dev.util.Util;
32  import com.google.gwt.resources.ext.AbstractResourceGenerator;
33  import com.google.gwt.resources.ext.ResourceContext;
34  import com.google.gwt.resources.ext.ResourceGeneratorUtil;
35  import com.google.gwt.user.rebind.SourceWriter;
36  import com.google.gwt.user.rebind.StringSourceWriter;
37  
38  /**
39   * Provides implementations of SVGResource.
40   */
41  public class SVGResourceGenerator extends AbstractResourceGenerator {
42  
43  	@Override
44  	public String createAssignment(
45  		TreeLogger logger, 
46  		ResourceContext context,
47  		JMethod method) throws UnableToCompleteException {
48  
49  		// Extract the SVG name from the @Source annotation
50  	    URL[] resources = ResourceGeneratorUtil.findResources(logger, context, method);
51  		if (resources.length != 1) {
52  			logger.log(TreeLogger.ERROR, "Exactly one resource must be specified", null);
53  			throw new UnableToCompleteException();
54  		}
55  		URL resource = resources[0];
56  
57  		// The SVGResource is implemented as an anonymous inner class
58  		// xxx = new SVGResource() {
59  		//   public OMSVGSVGElement getSvg() {
60  		//     return OMSVGParser.parse("...");
61  		//   }
62  		// };
63  		SourceWriter sw = new StringSourceWriter();
64  		sw.println("new " + SVGResource.class.getName() + "() {");
65  		sw.indent();
66  
67  		// Convenience when examining the generated code.
68  		sw.println("// " + resource.toExternalForm());
69  
70  		sw.println("public " + OMSVGSVGElement.class.getName() + " getSvg() {");
71  		sw.indent();
72  		String toWrite = Util.readURLAsString(resource);
73  		if (getValidated(method)) {
74  			SVGValidator.validate(toWrite, resource.toExternalForm(), logger, null);
75  		}
76  		sw.println("return " + OMSVGParser.class.getName() + ".parse(\"" + Generator.escape(toWrite) + "\");");
77  		sw.outdent();
78  		sw.println("}");
79  
80  	    sw.println("public String getName() {");
81  	    sw.indent();
82  	    sw.println("return \"" + method.getName() + "\";");
83  	    sw.outdent();
84  	    sw.println("}");
85  
86  		sw.outdent();
87  		sw.println("}");
88  
89  		return sw.toString();
90  	}
91  
92  	private boolean getValidated(JMethod method) {
93  		Validated validated = method.getAnnotation(Validated.class);
94  		if (validated == null) {
95  			return true;
96  		} else {
97  			return validated.validated();
98  		}
99  	}
100 	
101 }