Java Code Examples for com.gargoylesoftware.htmlunit.Page#isHtmlPage()

The following examples show how to use com.gargoylesoftware.htmlunit.Page#isHtmlPage() . 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: Window.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
private URL makeUrlForOpenWindow(final String urlString) {
    if (urlString.isEmpty()) {
        return WebClient.URL_ABOUT_BLANK;
    }

    try {
        final Page page = getWebWindow().getEnclosedPage();
        if (page != null && page.isHtmlPage()) {
            return ((HtmlPage) page).getFullyQualifiedUrl(urlString);
        }
        return new URL(urlString);
    }
    catch (final MalformedURLException e) {
        if (LOG.isWarnEnabled()) {
            LOG.error("Unable to create URL for openWindow: relativeUrl=[" + urlString + "]", e);
        }
        return null;
    }
}
 
Example 2
Source File: HtmlRadioButtonInput.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Override of default clickAction that makes this radio button the selected
 * one when it is clicked.
 * {@inheritDoc}
 *
 * @throws IOException if an IO error occurred
 */
@Override
protected boolean doClickStateUpdate(final boolean shiftKey, final boolean ctrlKey) throws IOException {
    final HtmlForm form = getEnclosingForm();
    final boolean changed = !isChecked();

    final Page page = getPage();
    if (form != null) {
        form.setCheckedRadioButton(this);
    }
    else if (page != null && page.isHtmlPage()) {
        setCheckedForPage((HtmlPage) page);
    }
    super.doClickStateUpdate(shiftKey, ctrlKey);
    return changed;
}
 
Example 3
Source File: HtmlRadioButtonInput.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the {@code checked} attribute.
 *
 * @param isChecked true if this element is to be selected
 * @return the page that occupies this window after setting checked status
 * It may be the same window or it may be a freshly loaded one.
 */
@Override
public Page setChecked(final boolean isChecked) {
    Page page = getPage();

    final boolean changed = isChecked() != isChecked;
    checkedState_ = isChecked;
    if (isChecked) {
        final HtmlForm form = getEnclosingForm();
        if (form != null) {
            form.setCheckedRadioButton(this);
        }
        else if (page != null && page.isHtmlPage()) {
            setCheckedForPage((HtmlPage) page);
        }
    }

    if (changed) {
        final ScriptResult scriptResult = fireEvent(Event.TYPE_CHANGE);
        if (scriptResult != null) {
            page = scriptResult.getNewPage();
        }
    }
    return page;
}
 
Example 4
Source File: HtmlRadioButtonInput.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Override of default clickAction that makes this radio button the selected
 * one when it is clicked.
 * {@inheritDoc}
 *
 * @throws IOException if an IO error occurred
 */
@Override
protected boolean doClickStateUpdate(final boolean shiftKey, final boolean ctrlKey) throws IOException {
    final HtmlForm form = getEnclosingForm();
    final boolean changed = !isChecked();

    final Page page = getPage();
    if (form != null) {
        form.setCheckedRadioButton(this);
    }
    else if (page != null && page.isHtmlPage()) {
        setCheckedForPage((HtmlPage) page);
    }
    super.doClickStateUpdate(shiftKey, ctrlKey);
    return changed;
}
 
Example 5
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
private URL makeUrlForOpenWindow(final String urlString) {
    if (urlString.isEmpty()) {
        return WebClient.URL_ABOUT_BLANK;
    }

    try {
        final Page page = getWebWindow().getEnclosedPage();
        if (page != null && page.isHtmlPage()) {
            return ((HtmlPage) page).getFullyQualifiedUrl(urlString);
        }
        return new URL(urlString);
    }
    catch (final MalformedURLException e) {
        LOG.error("Unable to create URL for openWindow: relativeUrl=[" + urlString + "]", e);
        return null;
    }
}
 
Example 6
Source File: HtmlPage.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Deregister frames that are no longer in use.
 */
public void deregisterFramesIfNeeded() {
    for (final WebWindow window : getFrames()) {
        getWebClient().deregisterWebWindow(window);
        final Page page = window.getEnclosedPage();
        if (page != null && page.isHtmlPage()) {
            // seems quite silly, but for instance if the src attribute of an iframe is not
            // set, the error only occurs when leaving the page
            ((HtmlPage) page).deregisterFramesIfNeeded();
        }
    }
}
 
Example 7
Source File: HTMLParser.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a body element to the current page, if necessary. Strictly speaking, this should
 * probably be done by NekoHTML. See the bug linked below. If and when that bug is fixed,
 * we may be able to get rid of this code.
 *
 * http://sourceforge.net/p/nekohtml/bugs/15/
 * @param page
 * @param originalCall
 * @param checkInsideFrameOnly true if the original page had body that was removed by JavaScript
 */
private static void addBodyToPageIfNecessary(
        final HtmlPage page, final boolean originalCall, final boolean checkInsideFrameOnly) {
    // IE waits for the whole page to load before initializing bodies for frames.
    final boolean waitToLoad = page.hasFeature(PAGE_WAIT_LOAD_BEFORE_BODY);
    if (page.getEnclosingWindow() instanceof FrameWindow && originalCall && waitToLoad) {
        return;
    }

    // Find out if the document already has a body element (or frameset).
    final Element doc = page.getDocumentElement();
    boolean hasBody = false;
    for (Node child = doc.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child instanceof HtmlBody || child instanceof HtmlFrameSet) {
            hasBody = true;
            break;
        }
    }

    // If the document does not have a body, add it.
    if (!hasBody && !checkInsideFrameOnly) {
        final HtmlBody body = new HtmlBody("body", page, null, false);
        doc.appendChild(body);
    }

    // If this is IE, we need to initialize the bodies of any frames, as well.
    // This will already have been done when emulating FF (see above).
    if (waitToLoad) {
        for (final FrameWindow frame : page.getFrames()) {
            final Page containedPage = frame.getEnclosedPage();
            if (containedPage != null && containedPage.isHtmlPage()) {
                addBodyToPageIfNecessary((HtmlPage) containedPage, false, false);
            }
        }
    }
}
 
Example 8
Source File: XmlSerializer.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private static String getFileExtension(final Page enclosedPage) {
    if (enclosedPage != null) {
        if (enclosedPage.isHtmlPage()) {
            return "html";
        }

        final URL url = enclosedPage.getUrl();
        if (url.getPath().contains(".")) {
            return StringUtils.substringAfterLast(url.getPath(), ".");
        }
    }

    return ".unknown";
}
 
Example 9
Source File: XmlSerializer.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private Map<String, DomAttr> getAttributesFor(final BaseFrameElement frame) throws IOException {
    final Map<String, DomAttr> map = createAttributesCopyWithClonedAttribute(frame, "src");
    final DomAttr srcAttr = map.get("src");
    if (srcAttr == null) {
        return map;
    }

    final Page enclosedPage = frame.getEnclosedPage();
    final String suffix = getFileExtension(enclosedPage);
    final File file = createFile(srcAttr.getValue(), "." + suffix);

    if (enclosedPage != null) {
        if (enclosedPage.isHtmlPage()) {
            file.delete(); // TODO: refactor as it is stupid to create empty file at one place
            // and then to complain that it already exists
            ((HtmlPage) enclosedPage).save(file);
        }
        else {
            try (InputStream is = enclosedPage.getWebResponse().getContentAsStream()) {
                try (FileOutputStream fos = new FileOutputStream(file)) {
                    IOUtils.copyLarge(is, fos);
                }
            }
        }
    }

    srcAttr.setValue(file.getParentFile().getName() + FILE_SEPARATOR + file.getName());
    return map;
}
 
Example 10
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the object.
 * @param enclosedPage the page containing the JavaScript
 */
public void initialize(final Page enclosedPage) {
    if (enclosedPage != null && enclosedPage.isHtmlPage()) {
        final HtmlPage htmlPage = (HtmlPage) enclosedPage;

        // Windows don't have corresponding DomNodes so set the domNode
        // variable to be the page. If this isn't set then SimpleScriptable.get()
        // won't work properly
        setDomNode(htmlPage);
        clearEventListenersContainer();

        WebAssert.notNull("document_", document_);
        document_.setDomNode(htmlPage);
    }
}
 
Example 11
Source File: BaseFrameElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Called after the node for the {@code frame} or {@code iframe} has been added to the containing page.
 * The node needs to be added first to allow JavaScript in the frame to see the frame in the parent.
 * @throws FailingHttpStatusCodeException if the server returns a failing status code AND the property
 *      {@link com.gargoylesoftware.htmlunit.WebClientOptions#setThrowExceptionOnFailingStatusCode(boolean)} is
 *      set to true
 */

public void loadInnerPage() throws FailingHttpStatusCodeException {
    String source = getSrcAttribute();
    if (source.isEmpty() || StringUtils.startsWithIgnoreCase(source, WebClient.ABOUT_SCHEME)) {
        source = WebClient.ABOUT_BLANK;
    }

    loadInnerPageIfPossible(source);

    final Page enclosedPage = getEnclosedPage();
    if (enclosedPage != null && enclosedPage.isHtmlPage()) {
        final HtmlPage htmlPage = (HtmlPage) enclosedPage;

        final AbstractJavaScriptEngine<?> jsEngine = getPage().getWebClient().getJavaScriptEngine();
        if (jsEngine != null && jsEngine.isScriptRunning()) {
            final PostponedAction action = new PostponedAction(getPage()) {
                @Override
                public void execute() throws Exception {
                    htmlPage.setReadyState(READY_STATE_COMPLETE);
                }
            };
            jsEngine.addPostponedAction(action);
        }
        else {
            htmlPage.setReadyState(READY_STATE_COMPLETE);
        }
    }
}
 
Example 12
Source File: HtmlPage.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Deregister frames that are no longer in use.
 */
public void deregisterFramesIfNeeded() {
    for (final WebWindow window : getFrames()) {
        getWebClient().deregisterWebWindow(window);
        final Page page = window.getEnclosedPage();
        if (page != null && page.isHtmlPage()) {
            // seems quite silly, but for instance if the src attribute of an iframe is not
            // set, the error only occurs when leaving the page
            ((HtmlPage) page).deregisterFramesIfNeeded();
        }
    }
}
 
Example 13
Source File: TvMaoCrawler.java    From MyTv with Apache License 2.0 5 votes vote down vote up
@Override
public HtmlPage makeObject(TvMaoObjectKey key) throws Exception {
	Page page = WebCrawler.crawl(key.url);
	if (page.isHtmlPage()) {
		return (HtmlPage) page;
	}
	throw new MyTvException("invalid web page which url: " + key.url);
}
 
Example 14
Source File: BaseFrameElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Called after the node for the {@code frame} or {@code iframe} has been added to the containing page.
 * The node needs to be added first to allow JavaScript in the frame to see the frame in the parent.
 * @throws FailingHttpStatusCodeException if the server returns a failing status code AND the property
 *      {@link com.gargoylesoftware.htmlunit.WebClientOptions#setThrowExceptionOnFailingStatusCode(boolean)} is
 *      set to true
 */

public void loadInnerPage() throws FailingHttpStatusCodeException {
    String source = getSrcAttribute();
    if (source.isEmpty() || StringUtils.startsWithIgnoreCase(source, WebClient.ABOUT_SCHEME)) {
        source = WebClient.ABOUT_BLANK;
    }

    loadInnerPageIfPossible(source);

    final Page enclosedPage = getEnclosedPage();
    if (enclosedPage != null && enclosedPage.isHtmlPage()) {
        final HtmlPage htmlPage = (HtmlPage) enclosedPage;
        final AbstractJavaScriptEngine<?> jsEngine = getPage().getWebClient().getJavaScriptEngine();
        if (jsEngine.isScriptRunning()) {
            final PostponedAction action = new PostponedAction(getPage()) {
                @Override
                public void execute() throws Exception {
                    htmlPage.setReadyState(READY_STATE_COMPLETE);
                }
            };
            jsEngine.addPostponedAction(action);
        }
        else {
            htmlPage.setReadyState(READY_STATE_COMPLETE);
        }
    }
}
 
Example 15
Source File: HtmlUnitNekoHtmlParser.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a body element to the current page, if necessary. Strictly speaking, this should
 * probably be done by NekoHTML. See the bug linked below. If and when that bug is fixed,
 * we may be able to get rid of this code.
 *
 * http://sourceforge.net/p/nekohtml/bugs/15/
 * @param page
 * @param originalCall
 * @param checkInsideFrameOnly true if the original page had body that was removed by JavaScript
 */
private void addBodyToPageIfNecessary(
        final HtmlPage page, final boolean originalCall, final boolean checkInsideFrameOnly) {
    // IE waits for the whole page to load before initializing bodies for frames.
    final boolean waitToLoad = page.hasFeature(PAGE_WAIT_LOAD_BEFORE_BODY);
    if (page.getEnclosingWindow() instanceof FrameWindow && originalCall && waitToLoad) {
        return;
    }

    // Find out if the document already has a body element (or frameset).
    final Element doc = page.getDocumentElement();
    boolean hasBody = false;
    for (Node child = doc.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child instanceof HtmlBody || child instanceof HtmlFrameSet) {
            hasBody = true;
            break;
        }
    }

    // If the document does not have a body, add it.
    if (!hasBody && !checkInsideFrameOnly) {
        final DomElement body = getFactory("body").createElement(page, "body", null);
        doc.appendChild(body);
    }

    // If this is IE, we need to initialize the bodies of any frames, as well.
    // This will already have been done when emulating FF (see above).
    if (waitToLoad) {
        for (final FrameWindow frame : page.getFrames()) {
            final Page containedPage = frame.getEnclosedPage();
            if (containedPage != null && containedPage.isHtmlPage()) {
                addBodyToPageIfNecessary((HtmlPage) containedPage, false, false);
            }
        }
    }
}
 
Example 16
Source File: XmlSerializer.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private static String getFileExtension(final Page enclosedPage) {
    if (enclosedPage != null) {
        if (enclosedPage.isHtmlPage()) {
            return "html";
        }

        final URL url = enclosedPage.getUrl();
        if (url.getPath().contains(".")) {
            return StringUtils.substringAfterLast(url.getPath(), ".");
        }
    }

    return ".unknown";
}
 
Example 17
Source File: XmlSerializer.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private Map<String, DomAttr> getAttributesFor(final BaseFrameElement frame) throws IOException {
    final Map<String, DomAttr> map = createAttributesCopyWithClonedAttribute(frame, DomElement.SRC_ATTRIBUTE);
    final DomAttr srcAttr = map.get(DomElement.SRC_ATTRIBUTE);
    if (srcAttr == null) {
        return map;
    }

    final Page enclosedPage = frame.getEnclosedPage();
    final String suffix = getFileExtension(enclosedPage);
    final File file = createFile(srcAttr.getValue(), "." + suffix);

    if (enclosedPage != null) {
        if (enclosedPage.isHtmlPage()) {
            file.delete(); // TODO: refactor as it is stupid to create empty file at one place
            // and then to complain that it already exists
            ((HtmlPage) enclosedPage).save(file);
        }
        else {
            try (InputStream is = enclosedPage.getWebResponse().getContentAsStream()) {
                try (OutputStream fos = Files.newOutputStream(file.toPath())) {
                    IOUtils.copyLarge(is, fos);
                }
            }
        }
    }

    srcAttr.setValue(file.getParentFile().getName() + FILE_SEPARATOR + file.getName());
    return map;
}
 
Example 18
Source File: Window.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the object.
 * @param enclosedPage the page containing the JavaScript
 */
public void initialize(final Page enclosedPage) {
    if (enclosedPage != null && enclosedPage.isHtmlPage()) {
        final HtmlPage htmlPage = (HtmlPage) enclosedPage;

        // Windows don't have corresponding DomNodes so set the domNode
        // variable to be the page. If this isn't set then SimpleScriptable.get()
        // won't work properly
        setDomNode(htmlPage);
        clearEventListenersContainer();

        WebAssert.notNull("document_", document_);
        document_.setDomNode(htmlPage);
    }
}
 
Example 19
Source File: JavaScriptExecutionJob.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public void run() {
    final WebWindow w = window_.get();
    if (w == null) {
        // The window has been garbage collected! No need to execute, obviously.
        return;
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("Executing " + this + ".");
    }

    try {
        // Verify that the window is still open
        if (w.isClosed()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Enclosing window is now closed. Execution cancelled.");
            }
            return;
        }
        if (!w.getWebClient().containsWebWindow(w)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Enclosing window is now closed. Execution cancelled.");
            }
            return;
        }

        // Verify that the current page is still available and a html page
        final Page enclosedPage = w.getEnclosedPage();
        if (enclosedPage == null || !enclosedPage.isHtmlPage()) {
            if (enclosedPage == null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("The page that originated this job doesn't exist anymore. Execution cancelled.");
                }
                return;
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("The page that originated this job is no html page ("
                            + enclosedPage.getClass().getName() + "). Execution cancelled.");
            }
            return;
        }

        runJavaScript((HtmlPage) enclosedPage);
    }
    finally {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finished executing " + this + ".");
        }
    }
}
 
Example 20
Source File: JavaScriptExecutionJob.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public void run() {
    final WebWindow w = window_.get();
    if (w == null) {
        // The window has been garbage collected! No need to execute, obviously.
        return;
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("Executing " + this + ".");
    }

    try {
        // Verify that the window is still open
        if (w.isClosed()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Enclosing window is now closed. Execution cancelled.");
            }
            return;
        }
        if (!w.getWebClient().containsWebWindow(w)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Enclosing window is now closed. Execution cancelled.");
            }
            return;
        }

        // Verify that the current page is still available and a html page
        final Page enclosedPage = w.getEnclosedPage();
        if (enclosedPage == null || !enclosedPage.isHtmlPage()) {
            if (enclosedPage == null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("The page that originated this job doesn't exist anymore. Execution cancelled.");
                }
                return;
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("The page that originated this job is no html page ("
                            + enclosedPage.getClass().getName() + "). Execution cancelled.");
            }
            return;
        }

        runJavaScript((HtmlPage) enclosedPage);
    }
    finally {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finished executing " + this + ".");
        }
    }
}