/**
   * Loads an XML document using the supplied string.
   *
   * @param strXML A string containing the XML string to load into this XML document object This
   *     string can contain an entire XML document or a well-formed fragment.
   * @return true if the load succeeded; false if the load failed
   */
  public boolean jsxFunction_loadXML(final String strXML) {
    try {
      final WebWindow webWindow = getWindow().getWebWindow();

      // determine the charset of the page
      String charset = TextUtil.DEFAULT_CHARSET;
      final SgmlPage sgmlPage = (SgmlPage) webWindow.getEnclosedPage();
      if (sgmlPage != null) {
        final String contentCharset = sgmlPage.getWebResponse().getContentCharset();
        if (contentCharset != null) {
          charset = contentCharset;
        }
      }

      // build a dummy WebResponse
      List<NameValuePair> headers = Collections.emptyList();
      final WebResponseData data =
          new WebResponseData(
              TextUtil.stringToByteArray(strXML, charset), HttpStatus.SC_OK, null, headers);
      final URL hackUrl =
          new URL("http://-htmlunit-internal/XMLDocument.loadXML"); // hack! better solution?
      final WebResponse webResponse = new WebResponse(data, hackUrl, (HttpMethod) null, 0);
      webResponse.getWebRequest().setCharset(charset);

      final XmlPage page = new XmlPage(webResponse, webWindow);
      setDomNode(page);
      return true;
    } catch (final IOException e) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Error parsing XML\n" + strXML, e);
      }
      return false;
    }
  }
Beispiel #2
0
  /**
   * Loads an XML document using the supplied string.
   *
   * @param strXML A string containing the XML string to load into this XML document object This
   *     string can contain an entire XML document or a well-formed fragment.
   * @return true if the load succeeded; false if the load failed
   */
  @JsxFunction({@WebBrowser(IE), @WebBrowser(CHROME)})
  public boolean loadXML(final String strXML) {
    try {
      final WebWindow webWindow = getWindow().getWebWindow();

      final WebResponse webResponse =
          new StringWebResponse(strXML, webWindow.getEnclosedPage().getUrl());

      final XmlPage page = new XmlPage(webResponse, webWindow);
      setDomNode(page);
      return true;
    } catch (final IOException e) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Error parsing XML\n" + strXML, e);
      }
      return false;
    }
  }
  @SuppressWarnings("unchecked")
  @Override
  public ExceptionOrReturnValue invoke(
      BrowserChannelClient channel, Value thisObj, String methodName, Value[] args) {
    if (logger.isLoggable(TreeLogger.DEBUG)) {
      logger.log(
          TreeLogger.DEBUG,
          "INVOKE: thisObj: " + thisObj + ", methodName: " + methodName + ", args: " + args);
    }
    /*
     * 1. lookup functions by name. 2. Find context and scope. 3. Convert
     * thisObject to ScriptableObject 4. Convert args 5. Get return value
     */
    Context jsContext = Context.getCurrentContext();
    ScriptableObject jsThis = null;
    if (thisObj.getType() == ValueType.NULL) {
      jsThis = window;
    } else {
      Object obj = makeJsvalFromValue(jsContext, thisObj);
      if (obj instanceof ScriptableObject) {
        jsThis = (ScriptableObject) obj;
      } else if (obj instanceof SimpleScriptableProxy<?>) {
        jsThis = ((SimpleScriptableProxy<SimpleScriptable>) obj).getDelegee();
      } else {
        logger.log(
            TreeLogger.ERROR,
            "Unable to convert "
                + obj
                + " to either "
                + " ScriptableObject or SimpleScriptableProxy");
        return new ExceptionOrReturnValue(true, new Value(null));
      }
    }
    Object functionObject = ScriptableObject.getProperty(window, methodName);
    if (functionObject == ScriptableObject.NOT_FOUND) {
      logger.log(
          TreeLogger.ERROR,
          "function "
              + methodName
              + " NOT FOUND, thisObj: "
              + jsThis
              + ", methodName: "
              + methodName);
      // TODO: see if this maps to QUIT
      return new ExceptionOrReturnValue(true, new Value(null));
    }
    Function jsFunction = (Function) functionObject;
    if (logger.isLoggable(TreeLogger.SPAM)) {
      logger.log(TreeLogger.SPAM, "INVOKE: jsFunction: " + jsFunction);
    }

    Object jsArgs[] = new Object[args.length];
    for (int i = 0; i < args.length; i++) {
      jsArgs[i] = makeJsvalFromValue(jsContext, args[i]);
    }
    Object result = null;
    try {
      if (args.length == 1 && methodName.indexOf(REPLACE_METHOD_SIGNATURE) != -1) {
        // getUrl() is not visible
        String currentUrl = window.jsxGet_location().toString();
        currentUrl = getUrlBeforeHash(currentUrl);
        String newUrl = getUrlBeforeHash((String) args[0].getValue());
        if (!newUrl.equals(currentUrl)) {
          WebWindow webWindow = window.getWebWindow();
          do {
            webWindow.getJobManager().removeAllJobs();
            webWindow = webWindow.getParentWindow();
          } while (webWindow != webWindow.getTopWindow());
        }
      }
      result = jsEngine.callFunction(htmlPage, jsFunction, jsContext, window, jsThis, jsArgs);
    } catch (JavaScriptException ex) {
      if (logger.isLoggable(TreeLogger.INFO)) {
        logger.log(
            TreeLogger.INFO,
            "INVOKE: JavaScriptException "
                + ex
                + ", message: "
                + ex.getMessage()
                + " when invoking "
                + methodName);
      }
      return new ExceptionOrReturnValue(true, makeValueFromJsval(jsContext, ex.getValue()));
    } catch (Exception ex) {
      if (logger.isLoggable(TreeLogger.INFO)) {
        logger.log(
            TreeLogger.INFO,
            "INVOKE: exception "
                + ex
                + ", message: "
                + ex.getMessage()
                + " when invoking "
                + methodName);
      }
      return new ExceptionOrReturnValue(true, makeValueFromJsval(jsContext, Undefined.instance));
    }
    if (logger.isLoggable(TreeLogger.INFO)) {
      logger.log(TreeLogger.INFO, "INVOKE: result: " + result + " of jsFunction: " + jsFunction);
    }
    return new ExceptionOrReturnValue(false, makeValueFromJsval(jsContext, result));
  }
Beispiel #4
0
 /** {@inheritDoc} */
 @Override
 public Document getDelegee() {
   final Window w = (Window) webWindow_.getScriptObject();
   return w.getDocument();
 }