1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.vectomatic.dom.svg.utils;
19
20 import org.vectomatic.dom.svg.impl.SVGParserImpl;
21
22 import com.google.gwt.core.client.GWT;
23 import com.google.gwt.dom.client.Element;
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
30
31
32
33
34 public class HttpRequestXmlLoader implements AsyncXmlLoader {
35
36 @Override
37 public void loadResource(final String resourceUrl, final AsyncXmlLoaderCallback callback) {
38 String currentResourceUrl = resourceUrl + ((resourceUrl.indexOf("?") == -1) ? ("?ts=" + System.currentTimeMillis()) : ("&ts=" + + System.currentTimeMillis()));
39 RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, currentResourceUrl);
40 requestBuilder.setCallback(new RequestCallback() {
41 public void onError(Request request, Throwable exception) {
42 callback.onError(resourceUrl, exception);
43 }
44
45 private void onSuccess(Request request, Response response) {
46 SVGParserImpl impl = GWT.create(SVGParserImpl.class);
47 Element root = impl.parse(response.getText(), true);
48 callback.onSuccess(resourceUrl, root);
49 }
50
51 public void onResponseReceived(Request request, Response response) {
52 if (response.getStatusCode() == Response.SC_OK) {
53 onSuccess(request, response);
54 } else {
55 onError(request, null);
56 }
57 }
58 });
59 try {
60 requestBuilder.send();
61 } catch (RequestException e) {
62 GWT.log("Cannot fetch " + resourceUrl, e);
63 }
64 }
65 }