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
32
33
34
35
36
37 public class ExternalSVGResourcePrototype implements ExternalSVGResource {
38
39
40
41
42 private class ESRCallback implements RequestCallback {
43 final ResourceCallback<SVGResource> callback;
44
45 public ESRCallback(ResourceCallback<SVGResource> callback) {
46 this.callback = callback;
47 }
48
49 public void onError(Request request, Throwable exception) {
50 callback.onError(new ResourceException(
51 ExternalSVGResourcePrototype.this,
52 "Unable to retrieve external resource", exception));
53 }
54
55 public void onResponseReceived(Request request, final Response response) {
56
57 String responseText = response.getText();
58
59
60 JavaScriptObject jso = evalObject(responseText);
61 if (jso == null) {
62 callback.onError(new ResourceException(
63 ExternalSVGResourcePrototype.this,
64 "eval() returned null"));
65 return;
66 }
67
68
69 for (int i = 0; i < cache.length; i++) {
70 final String resourceText = extractString(jso, i);
71 cache[i] = new SVGResource() {
72
73 public String getName() {
74 return name;
75 }
76
77 public OMSVGSVGElement getSvg() {
78 return OMSVGParser.parse(resourceText);
79 }
80
81 };
82 }
83
84
85 callback.onSuccess(cache[index]);
86 }
87 }
88
89
90
91
92
93
94
95
96
97
98 private static native JavaScriptObject evalObject(String data)
99
100
101
102
103
104
105
106
107 ;
108
109
110
111
112
113
114
115
116
117 private static native String extractString(JavaScriptObject jso, int index)
118
119 ;
120
121
122
123
124
125
126 private final SVGResource[] cache;
127 private final int index;
128 private final String name;
129 private final String url;
130
131
132
133
134
135
136
137
138 public ExternalSVGResourcePrototype(String name, String url,
139 SVGResource[] cache, int index) {
140 this.name = name;
141 this.url = url;
142 this.cache = cache;
143 this.index = index;
144 }
145
146
147
148
149
150 public String getName() {
151 return name;
152 }
153
154
155
156
157
158 public void getSvg(ResourceCallback<SVGResource> callback)
159 throws ResourceException {
160
161
162 if (cache[index] != null) {
163 callback.onSuccess(cache[index]);
164 return;
165 }
166
167
168 RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, url);
169 try {
170 rb.sendRequest("", new ESRCallback(callback));
171 } catch (RequestException e) {
172 throw new ResourceException(this,
173 "Unable to initiate request for external resource", e);
174 }
175 }
176 }