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.dom.svg.utils;
19  
20  import org.vectomatic.dom.svg.impl.SVGParserImpl;
21  import org.vectomatic.dom.svg.utils.DOMHelper;
22  
23  import com.google.gwt.core.client.GWT;
24  import com.google.gwt.dom.client.Element;
25  import com.google.gwt.http.client.Request;
26  import com.google.gwt.http.client.RequestBuilder;
27  import com.google.gwt.http.client.RequestCallback;
28  import com.google.gwt.http.client.RequestException;
29  import com.google.gwt.http.client.Response;
30  
31  
32  /**
33   * Implementation of the AsyncXmlLoader based on RequestBuilder.
34   */
35  public class HttpRequestXmlLoader implements AsyncXmlLoader {
36  
37  	@Override
38  	public void loadResource(final String resourceUrl, final AsyncXmlLoaderCallback callback) {
39  		String currentResourceUrl = resourceUrl + ((resourceUrl.indexOf("?") == -1) ? ("?ts=" + System.currentTimeMillis()) : ("&ts=" + + System.currentTimeMillis()));
40  		RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, currentResourceUrl);
41  		requestBuilder.setCallback(new RequestCallback() {
42  			public void onError(Request request, Throwable exception) {
43  				callback.onError(resourceUrl, exception);
44  			}
45  
46  			private void onSuccess(Request request, Response response) {
47  				SVGParserImpl impl = GWT.create(SVGParserImpl.class);
48  				Element root = impl.parse(response.getText());
49  				callback.onSuccess(resourceUrl, root);
50  			}
51  			
52  			public void onResponseReceived(Request request, Response response) {
53  				if (response.getStatusCode() == Response.SC_OK) {
54  					onSuccess(request, response);
55  				} else {
56  					onError(request, null);
57  				}
58  			}
59  		});
60  		try {
61  			requestBuilder.send();
62  		} catch (RequestException e) {
63  			GWT.log("Cannot fetch " + resourceUrl, e);
64  		}
65  	}
66  }