Java Code Examples for net.sourceforge.htmlunit.corejs.javascript.Context#reportRuntimeError()

The following examples show how to use net.sourceforge.htmlunit.corejs.javascript.Context#reportRuntimeError() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HTMLImageElement.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the value of the {@code src} attribute.
 * @return the value of the {@code src} attribute
 */
@JsxGetter
public String getSrc() {
    final HtmlImage img = (HtmlImage) getDomNodeOrDie();
    final String src = img.getSrcAttribute();
    if ("".equals(src)) {
        return src;
    }
    try {
        final HtmlPage page = (HtmlPage) img.getPage();
        return page.getFullyQualifiedUrl(src).toExternalForm();
    }
    catch (final MalformedURLException e) {
        final String msg = "Unable to create fully qualified URL for src attribute of image " + e.getMessage();
        throw Context.reportRuntimeError(msg);
    }
}
 
Example 2
Source File: XMLHTTPRequest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the response entity body as a string.
 * @return the response entity body as a string
 */
@JsxGetter
public String getResponseText() {
    if (state_ == STATE_UNSENT) {
        throw Context.reportRuntimeError(
                "The data necessary to complete this operation is not yet available (request not opened).");
    }
    if (state_ != STATE_DONE) {
        throw Context.reportRuntimeError("Unspecified error (request not sent).");
    }
    if (webResponse_ != null) {
        final String content = webResponse_.getContentAsString();
        if (content == null) {
            return "";
        }
        return content;
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("XMLHTTPRequest.responseText was retrieved before the response was available.");
    }
    return "";
}
 
Example 3
Source File: HTMLOptionsCollection.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Removes the option at the specified index.
 * @param index the option index
 */
@JsxFunction
public void remove(final int index) {
    int idx = index;
    final BrowserVersion browser = getBrowserVersion();
    if (idx < 0) {
        if (browser.hasFeature(JS_SELECT_OPTIONS_REMOVE_IGNORE_IF_INDEX_NEGATIVE)) {
            return;
        }
        if (index < 0 && getBrowserVersion().hasFeature(JS_SELECT_OPTIONS_REMOVE_THROWS_IF_NEGATIV)) {
            throw Context.reportRuntimeError("Invalid index for option collection: " + index);
        }
    }

    idx = Math.max(idx, 0);
    if (idx >= getLength()) {
        if (browser.hasFeature(JS_SELECT_OPTIONS_REMOVE_IGNORE_IF_INDEX_TOO_LARGE)) {
            return;
        }
        idx = 0;
    }
    htmlSelect_.removeOption(idx);
}
 
Example 4
Source File: Document.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
private static org.w3c.dom.traversal.NodeFilter createFilterWrapper(final Scriptable filter,
        final boolean filterFunctionOnly) {
    org.w3c.dom.traversal.NodeFilter filterWrapper = null;
    if (filter != null) {
        filterWrapper = new org.w3c.dom.traversal.NodeFilter() {
            @Override
            public short acceptNode(final org.w3c.dom.Node n) {
                final Object[] args = new Object[] {((DomNode) n).getScriptableObject()};
                final Object response;
                if (filter instanceof Callable) {
                    response = ((Callable) filter).call(Context.getCurrentContext(), filter, filter, args);
                }
                else {
                    if (filterFunctionOnly) {
                        throw Context.reportRuntimeError("only a function is allowed as filter");
                    }
                    response = ScriptableObject.callMethod(filter, "acceptNode", args);
                }
                return (short) Context.toNumber(response);
            }
        };
    }
    return filterWrapper;
}
 
Example 5
Source File: HTMLInputElement.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the value of {@code selectionEnd} attribute.
 * @param end selection end
 */
@JsxSetter
public void setSelectionEnd(final int end) {
    final DomNode dom = getDomNodeOrDie();
    if (dom instanceof SelectableTextInput) {
        if ("number".equalsIgnoreCase(getType())
                && getBrowserVersion().hasFeature(JS_INPUT_NUMBER_SELECTION_START_END_NULL)) {
            throw Context.reportRuntimeError("Failed to set the 'selectionEnd' property"
                    + "from 'HTMLInputElement': "
                    + "The input element's type ('number') does not support selection.");
        }

        ((SelectableTextInput) dom).setSelectionEnd(end);
        return;
    }

    throw Context.reportRuntimeError("Failed to set the 'selectionEnd' property from 'HTMLInputElement': "
            + "The input element's type (" + getType() + ") does not support selection.");
}
 
Example 6
Source File: Document.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the value of the {@code forms} property.
 * @return the value of the {@code forms} property
 */
@JsxGetter({CHROME, IE})
public Object getForms() {
    return new HTMLCollection(getDomNodeOrDie(), false) {
        @Override
        protected boolean isMatching(final DomNode node) {
            return node instanceof HtmlForm && node.getPrefix() == null;
        }

        @Override
        public Object call(final Context cx, final Scriptable scope,
                final Scriptable thisObj, final Object[] args) {
            if (getBrowserVersion().hasFeature(JS_DOCUMENT_FORMS_FUNCTION_SUPPORTED)) {
                return super.call(cx, scope, thisObj, args);
            }
            throw Context.reportRuntimeError("TypeError: document.forms is not a function");
        }
    };
}
 
Example 7
Source File: HTMLImageElement.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the value of the {@code src} attribute.
 * @return the value of the {@code src} attribute
 */
@JsxGetter
public String getSrc() {
    final HtmlImage img = (HtmlImage) getDomNodeOrDie();
    final String src = img.getSrcAttribute();
    if ("".equals(src)) {
        return src;
    }
    try {
        final HtmlPage page = (HtmlPage) img.getPage();
        return page.getFullyQualifiedUrl(src).toExternalForm();
    }
    catch (final MalformedURLException e) {
        final String msg = "Unable to create fully qualified URL for src attribute of image " + e.getMessage();
        throw Context.reportRuntimeError(msg);
    }
}
 
Example 8
Source File: XMLDOMDocument.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Attempting to set the value of documents generates an error.
 * @param value the new value to set
 */
@Override
public void setNodeValue(final String value) {
    if (value == null || "null".equals(value)) {
        throw Context.reportRuntimeError("Type mismatch.");
    }
    throw Context.reportRuntimeError("This operation cannot be performed with a node of type DOCUMENT.");
}
 
Example 9
Source File: HTMLListElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the value of the {@code type} property.
 * @param type the value of the {@code type} property
 */
protected void setType(final String type) {
    final boolean acceptArbitraryValues = getBrowserVersion().hasFeature(JS_TYPE_ACCEPTS_ARBITRARY_VALUES);
    if (acceptArbitraryValues
            || "1".equals(type)
            || "a".equals(type)
            || "A".equals(type)
            || "i".equals(type)
            || "I".equals(type)) {
        getDomNodeOrDie().setAttribute("type", type);
        return;
    }

    throw Context.reportRuntimeError("Cannot set the type property to invalid value: '" + type + "'");
}
 
Example 10
Source File: XMLDOMElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Replaces all children of this element with the supplied value.
 * @param value the new value for the contents of this node
 */
@Override
public void setText(final Object value) {
    if (value == null || "null".equals(value)) {
        throw Context.reportRuntimeError("Type mismatch.");
    }

    super.setText(value);
}
 
Example 11
Source File: WeakMap.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Adds the specified pair.
 * @param key the key
 * @param value the value
 * @return the Map object.
 */
@JsxFunction
public WeakMap set(Object key, final Object value) {
    if (key instanceof Delegator) {
        key = ((Delegator) key).getDelegee();
    }
    if (!(key instanceof ScriptableObject)) {
        throw Context.reportRuntimeError("TypeError: key is not an object");
    }
    map_.put(key, value);
    return this;
}
 
Example 12
Source File: HtmlUnitScriptable.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void put(final String name, final Scriptable start, final Object value) {
    try {
        super.put(name, start, value);
    }
    catch (final IllegalArgumentException e) {
        // is it the right place or should Rhino throw a RuntimeError instead of an IllegalArgumentException?
        throw Context.reportRuntimeError("'set "
            + name + "' called on an object that does not implement interface " + getClassName());
    }
}
 
Example 13
Source File: HTMLOptionsCollection.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Changes the number of options: removes options if the new length
 * is less than the current one else add new empty options to reach the
 * new length.
 * @param newLength the new length property value
 */
@JsxSetter
public void setLength(final int newLength) {
    if (newLength < 0) {
        if (getBrowserVersion().hasFeature(JS_SELECT_OPTIONS_IGNORE_NEGATIVE_LENGTH)) {
            return;
        }
        throw Context.reportRuntimeError("Length is negative");
    }

    final int currentLength = htmlSelect_.getOptionSize();
    if (currentLength > newLength) {
        htmlSelect_.setOptionSize(newLength);
    }
    else {
        final SgmlPage page = htmlSelect_.getPage();
        final ElementFactory factory = page.getWebClient().getPageCreator()
                                        .getHtmlParser().getFactory(HtmlOption.TAG_NAME);
        for (int i = currentLength; i < newLength; i++) {
            final HtmlOption option = (HtmlOption) factory.createElement(page, HtmlOption.TAG_NAME, null);
            htmlSelect_.appendOption(option);
            if (getBrowserVersion().hasFeature(JS_SELECT_OPTIONS_ADD_EMPTY_TEXT_CHILD_WHEN_EXPANDING)) {
                option.appendChild(new DomText(option.getPage(), ""));
            }
        }
    }
}
 
Example 14
Source File: XMLHTTPRequest.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the HTTP response line status.
 * @return the HTTP response line status
 */
@JsxGetter
public String getStatusText() {
    if (state_ != STATE_DONE) {
        throw Context.reportRuntimeError("Unspecified error (request not sent).");
    }
    if (webResponse_ != null) {
        return webResponse_.getStatusMessage();
    }

    LOG.error("XMLHTTPRequest.statusText was retrieved without a response available (readyState: "
        + state_ + ").");
    return null;
}
 
Example 15
Source File: HTMLTableElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the {@code tHead}.
 * @param o the {@code tHead}
 */
@JsxSetter
public void setTHead(final Object o) {
    if (!(o instanceof HTMLTableSectionElement
        && "THEAD".equals(((HTMLTableSectionElement) o).getTagName()))) {
        throw Context.reportRuntimeError("Not a tHead");
    }

    // remove old caption (if any)
    deleteTHead();

    final HTMLTableSectionElement thead = (HTMLTableSectionElement) o;
    getDomNodeOrDie().appendChild(thead.getDomNodeOrDie());
}
 
Example 16
Source File: HTMLTableElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the caption.
 * @param o the caption
 */
@JsxSetter
public void setCaption(final Object o) {
    if (!(o instanceof HTMLTableCaptionElement)) {
        throw Context.reportRuntimeError("Not a caption");
    }

    // remove old caption (if any)
    deleteCaption();

    final HTMLTableCaptionElement caption = (HTMLTableCaptionElement) o;
    getDomNodeOrDie().appendChild(caption.getDomNodeOrDie());
}
 
Example 17
Source File: XMLDOMDocumentType.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Attempting to set the value of document types generates an error.
 * @param newValue the new value to set
 */
@Override
public void setNodeValue(final String newValue) {
    if (newValue == null || "null".equals(newValue)) {
        throw Context.reportRuntimeError("Type mismatch.");
    }
    throw Context.reportRuntimeError("This operation cannot be performed with a node of type DTD.");
}
 
Example 18
Source File: HTMLHtmlElement.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Overwritten to throw an exception because this is readonly.
 * @param value the new value for the contents of this node
 */
@Override
protected void setInnerTextImpl(final String value) {
    throw Context.reportRuntimeError("innerText is read-only for tag 'html'");
}
 
Example 19
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Object call(final Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
    throw Context.reportRuntimeError("Window is not a function.");
}
 
Example 20
Source File: Node.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Replaces a child DOM node with another DOM node.
 * @param newChildObject the node to add as a child of this node
 * @param oldChildObject the node to remove as a child of this node
 * @return the removed child node
 */
@JsxFunction
public Object replaceChild(final Object newChildObject, final Object oldChildObject) {
    Object removedChild = null;

    if (newChildObject instanceof DocumentFragment) {
        final DocumentFragment fragment = (DocumentFragment) newChildObject;
        Node firstNode = null;
        final Node refChildObject = ((Node) oldChildObject).getNextSibling();
        for (final DomNode node : fragment.getDomNodeOrDie().getChildren()) {
            if (firstNode == null) {
                replaceChild(node.getScriptableObject(), oldChildObject);
                firstNode = (Node) node.getScriptableObject();
            }
            else {
                insertBeforeImpl(new Object[] {node.getScriptableObject(), refChildObject});
            }
        }
        if (firstNode == null) {
            removeChild(oldChildObject);
        }
        removedChild = oldChildObject;
    }
    else if (newChildObject instanceof Node && oldChildObject instanceof Node) {
        final Node newChild = (Node) newChildObject;

        // is the node allowed here?
        if (!isNodeInsertable(newChild)) {
            throw Context.reportRuntimeError("Node cannot be inserted at the specified point in the hierarchy");
        }

        // Get XML nodes for the DOM nodes passed in
        final DomNode newChildNode = newChild.getDomNodeOrDie();
        final DomNode oldChildNode = ((Node) oldChildObject).getDomNodeOrDie();

        // Replace the old child with the new child.
        oldChildNode.replace(newChildNode);
        removedChild = oldChildObject;
    }

    return removedChild;
}