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