/**
   * Sends a JSONP request by embedding a SCRIPT tag into the document.
   *
   * @param id the id used for the script tag and to identify the callback
   * @param request the request to be made
   * @param parameters the parameters for the request
   * @param function a function that transforms the response into the type that the callback needs
   * @param callback the callback that should be called with the transformed response
   */
  private <T> void sendJsonpRequest(
      String id,
      String request,
      Map<String, Object> parameters,
      final Function<String, ? extends T> function,
      final AsyncCallback<T> callback) {
    Preconditions.checkNotNull(id);

    // Prepare an intermediate callback that converts the String result to T.
    if (callback != null) {
      Preconditions.checkNotNull(function);
      CALLBACKS.put(
          id,
          new AsyncCallback<String>() {
            @Override
            public void onSuccess(String jsonResult) {
              T result;
              try {
                result = function.apply(jsonResult);
              } catch (RuntimeException e) {
                callback.onFailure(e);
                return;
              }
              callback.onSuccess(result);
            }

            @Override
            public void onFailure(Throwable caught) {
              callback.onFailure(caught);
            }
          });
    }

    // Insert a script tag into the document.
    Document document = Document.get();
    ScriptElement script = document.createScriptElement();
    String uri = makeURI(request, parameters, id);
    script.setSrc(uri);
    script.setId(id);
    Element bodyElement = document.getElementsByTagName("body").getItem(0);
    Element previous = document.getElementById(id);
    if (previous != null) {
      bodyElement.replaceChild(script, previous);
    } else {
      bodyElement.appendChild(script);
    }
  }
Ejemplo n.º 2
0
 /**
  * Remove all 'TaggedElement' tags from the rootElement. Note this could potentially invalidate
  * other TaggedEntities that share an rootElement.
  */
 public void unwrap() {
   Main.out.println("TaggedEntity.unwrap() : " + getText());
   Element parentElement = rootElement.getParentElement();
   Node textNode = JSFunctions.createTextNode(getText());
   parentElement.replaceChild(textNode, rootElement);
 }