1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.vectomatic.dom.svg.impl;
17
18 import org.vectomatic.dom.svg.OMSVGSVGElement;
19 import org.vectomatic.dom.svg.ui.ExternalSVGResource;
20 import org.vectomatic.dom.svg.ui.SVGResource;
21 import org.vectomatic.dom.svg.utils.OMSVGParser;
22
23 import com.google.gwt.core.client.JavaScriptObject;
24 import com.google.gwt.http.client.Request;
25 import com.google.gwt.http.client.RequestBuilder;
26 import com.google.gwt.http.client.RequestCallback;
27 import com.google.gwt.http.client.RequestException;
28 import com.google.gwt.http.client.Response;
29 import com.google.gwt.resources.client.ResourceCallback;
30 import com.google.gwt.resources.client.ResourceException;
31 import com.google.gwt.safehtml.shared.SafeUri;
32 import com.google.gwt.safehtml.shared.UriUtils;
33
34
35
36
37
38
39 public class ExternalSVGResourcePrototype implements ExternalSVGResource {
40
41
42
43
44 private class ESRCallback implements RequestCallback {
45 final ResourceCallback<SVGResource> callback;
46
47 public ESRCallback(ResourceCallback<SVGResource> callback) {
48 this.callback = callback;
49 }
50
51 public void onError(Request request, Throwable exception) {
52 callback.onError(new ResourceException(
53 ExternalSVGResourcePrototype.this,
54 "Unable to retrieve external resource", exception));
55 }
56
57 public void onResponseReceived(Request request, final Response response) {
58
59 String responseText = response.getText();
60
61
62 JavaScriptObject jso = evalObject(responseText);
63 if (jso == null) {
64 callback.onError(new ResourceException(
65 ExternalSVGResourcePrototype.this,
66 "eval() returned null"));
67 return;
68 }
69
70
71 for (int i = 0; i < cache.length; i++) {
72 final String resourceText = extractString(jso, i);
73 cache[i] = new SVGResource() {
74
75 @Override
76 public String getName() {
77 return name;
78 }
79
80 @Override
81 public OMSVGSVGElement getSvg() {
82 return OMSVGParser.parse(resourceText);
83 }
84
85 @Override
86 public SafeUri getSafeUri() {
87 return UriUtils.fromSafeConstant("data:image/svg+xml;utf8," + resourceText);
88 }
89
90 @Override
91 public String getUrl() {
92 return "data:image/svg+xml;utf8," + resourceText;
93 }
94
95 };
96 }
97
98
99 callback.onSuccess(cache[index]);
100 }
101 }
102
103
104
105
106
107
108
109
110
111
112 private static native JavaScriptObject evalObject(String data)
113
114
115
116
117
118
119
120
121 ;
122
123
124
125
126
127
128
129
130
131 private static native String extractString(JavaScriptObject jso, int index)
132
133 ;
134
135
136
137
138
139
140 private final SVGResource[] cache;
141 private final int index;
142 private final String name;
143 private final String url;
144
145
146
147
148
149
150
151
152 public ExternalSVGResourcePrototype(String name, String url,
153 SVGResource[] cache, int index) {
154 this.name = name;
155 this.url = url;
156 this.cache = cache;
157 this.index = index;
158 }
159
160
161
162
163
164 public String getName() {
165 return name;
166 }
167
168
169
170
171
172 public void getSvg(ResourceCallback<SVGResource> callback)
173 throws ResourceException {
174
175
176 if (cache[index] != null) {
177 callback.onSuccess(cache[index]);
178 return;
179 }
180
181
182 RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, url);
183 try {
184 rb.sendRequest("", new ESRCallback(callback));
185 } catch (RequestException e) {
186 throw new ResourceException(this,
187 "Unable to initiate request for external resource", e);
188 }
189 }
190 }