1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
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
61
62
63
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
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 }