com.gargoylesoftware.htmlunit.html.HTMLParser Java Examples

The following examples show how to use com.gargoylesoftware.htmlunit.html.HTMLParser. 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: Range.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Parses an HTML snippet.
 * @param valueAsString text that contains text and tags to be converted to a document fragment
 * @return a document fragment
 * @see <a href="https://developer.mozilla.org/en-US/docs/DOM/range.createContextualFragment">Mozilla
 * documentation</a>
 */
@JsxFunction
public Object createContextualFragment(final String valueAsString) {
    final SgmlPage page = startContainer_.getDomNodeOrDie().getPage();
    final DomDocumentFragment fragment = new DomDocumentFragment(page);
    try {
        HTMLParser.parseFragment(fragment, startContainer_.getDomNodeOrDie(), valueAsString);
    }
    catch (final Exception e) {
        LogFactory.getLog(Range.class).error("Unexpected exception occurred in createContextualFragment", e);
        throw Context.reportRuntimeError("Unexpected exception occurred in createContextualFragment: "
                + e.getMessage());
    }

    return fragment.getScriptableObject();
}
 
Example #2
Source File: HTMLOptionsCollection.java    From HtmlUnit-Android with Apache License 2.0 6 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 {
        for (int i = currentLength; i < newLength; i++) {
            final HtmlOption option = (HtmlOption) HTMLParser.getFactory(HtmlOption.TAG_NAME).createElement(
                    htmlSelect_.getPage(), HtmlOption.TAG_NAME, null);
            htmlSelect_.appendOption(option);
            if (!getBrowserVersion().hasFeature(JS_SELECT_OPTIONS_DONT_ADD_EMPTY_TEXT_CHILD_WHEN_EXPANDING)) {
                option.appendChild(new DomText(option.getPage(), ""));
            }
        }
    }
}
 
Example #3
Source File: PackageDetectorsITest.java    From warnings-ng-plugin with MIT License 5 votes vote down vote up
private void checkWebPageForExpectedEmptyResult(final AnalysisResult result) {
    try (WebClient webClient = createWebClient(false)) {
        WebResponse webResponse = webClient.getPage(result.getOwner(), DEFAULT_ENTRY_PATH).getWebResponse();
        HtmlPage htmlPage = HTMLParser.parseHtml(webResponse, webClient.getCurrentWindow());
        assertThat(getLinksWithGivenTargetName(htmlPage, DEFAULT_TAB_TO_INVESTIGATE)).isEmpty();
    }
    catch (IOException | SAXException e) {
        throw new AssertionError(e);
    }
}
 
Example #4
Source File: HTMLImageElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * JavaScript constructor.
 */
@JsxConstructor({CHROME, FF, EDGE})
public void jsConstructor() {
    final SgmlPage page = (SgmlPage) getWindow().getWebWindow().getEnclosedPage();
    final DomElement fake =
            HTMLParser.getFactory(HtmlImage.TAG_NAME).createElement(page, HtmlImage.TAG_NAME, new AttributesImpl());
    setDomNode(fake);
}
 
Example #5
Source File: DefaultPageCreator.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Create a Page object for the specified web response.
 *
 * @param webResponse the response from the server
 * @param webWindow the window that this page will be loaded into
 * @exception IOException if an IO problem occurs
 * @return the new page object
 */
@Override
public Page createPage(final WebResponse webResponse, final WebWindow webWindow) throws IOException {
    final String contentType = determineContentType(webResponse);

    final PageType pageType = determinePageType(contentType);
    switch (pageType) {
        case HTML:
            return createHtmlPage(webResponse, webWindow);

        case JAVASCRIPT:
            return createHtmlPage(webResponse, webWindow);

        case XML:
            final SgmlPage sgmlPage = createXmlPage(webResponse, webWindow);
            final DomElement doc = sgmlPage.getDocumentElement();
            if (doc != null && HTMLParser.XHTML_NAMESPACE.equals(doc.getNamespaceURI())) {
                return createXHtmlPage(webResponse, webWindow);
            }
            return sgmlPage;

        case TEXT:
            return createTextPage(webResponse, webWindow);

        default:
            return createUnexpectedPage(webResponse, webWindow);
    }
}
 
Example #6
Source File: UIServletTest.java    From celos with Apache License 2.0 5 votes vote down vote up
@Test
public void testRender() throws Exception {
    ScheduledTime end = new ScheduledTime("2015-09-03T13:17Z");
    ScheduledTime start = new ScheduledTime("2015-09-03T13:11Z");
    NavigableSet<ScheduledTime> tileTimes = new TreeSet<>(ImmutableSet.of(new ScheduledTime("2015-09-03T13:10Z"), new ScheduledTime("2015-09-03T13:15Z")));
    WorkflowID id = new WorkflowID("foo");
    List<WorkflowGroup> groups = ImmutableList.of(new WorkflowGroup("All workflows", ImmutableList.of(id)));
    WorkflowInfo workflowInfo = new WorkflowInfo(new URL("http://example.com"), ImmutableList.of());
    SlotState state1 = new SlotState(new SlotID(id, new ScheduledTime("2015-09-03T13:16Z")), SlotState.Status.FAILURE);
    SlotState state2 = new SlotState(new SlotID(id, new ScheduledTime("2015-09-03T13:12Z")), SlotState.Status.WAITING);
    List<SlotState> slotStates = ImmutableList.of(state1, state2);
    Map<WorkflowID, WorkflowStatus> statuses = ImmutableMap.of(id, new WorkflowStatus(workflowInfo, slotStates, false));
    UIConfiguration conf = new UIConfiguration(start, end, tileTimes, groups, statuses, new URL("http://example.com"));
    
    StringWebResponse response = new StringWebResponse(UIServlet.render(conf), new URL("http://example.com"));
    WebClient webClient = new WebClient();
    webClient.setThrowExceptionOnFailingStatusCode(false);
    HtmlPage page = HTMLParser.parse(response, new TopLevelWindow("top", webClient));
    
    // Some basic sanity checking
    
    List<HtmlTableDataCell> slotCells = (List<HtmlTableDataCell>) page.getByXPath("//td[contains(@class, 'slot')]");
    Assert.assertEquals("fail", slotCells.get(0).getTextContent());
    Assert.assertEquals("wait", slotCells.get(1).getTextContent());
    
    List<HtmlTableDataCell> hourCells = (List<HtmlTableDataCell>) page.getByXPath("//td[contains(@class, 'hour')]");
    Assert.assertEquals("1315", hourCells.get(0).getTextContent());
    Assert.assertEquals("1310", hourCells.get(1).getTextContent());
    
    List<HtmlTableDataCell> workflowCells = (List<HtmlTableDataCell>) page.getByXPath("//td[@class='workflow']");
    Assert.assertEquals("foo", workflowCells.get(0).getTextContent());
    
    System.out.println(response.getContentAsString());
}
 
Example #7
Source File: XmlUtil.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
private static DomNode createFrom(final SgmlPage page, final Node source, final boolean handleXHTMLAsHTML,
        final Map<Integer, List<String>> attributesOrderMap) {
    if (source.getNodeType() == Node.TEXT_NODE) {
        return new DomText(page, source.getNodeValue());
    }
    if (source.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
        return new DomProcessingInstruction(page, source.getNodeName(), source.getNodeValue());
    }
    if (source.getNodeType() == Node.COMMENT_NODE) {
        return new DomComment(page, source.getNodeValue());
    }
    if (source.getNodeType() == Node.DOCUMENT_TYPE_NODE) {
        final DocumentType documentType = (DocumentType) source;
        return new DomDocumentType(page, documentType.getName(), documentType.getPublicId(),
                documentType.getSystemId());
    }
    final String ns = source.getNamespaceURI();
    String localName = source.getLocalName();
    if (handleXHTMLAsHTML && HTMLParser.XHTML_NAMESPACE.equals(ns)) {
        final ElementFactory factory = HTMLParser.getFactory(localName);
        return factory.createElementNS(page, ns, localName,
                namedNodeMapToSaxAttributes(source.getAttributes(), attributesOrderMap, source));
    }
    final NamedNodeMap nodeAttributes = source.getAttributes();
    if (page != null && page.isHtmlPage()) {
        localName = localName.toUpperCase(Locale.ROOT);
    }
    final String qualifiedName;
    if (source.getPrefix() == null) {
        qualifiedName = localName;
    }
    else {
        qualifiedName = source.getPrefix() + ':' + localName;
    }

    final String namespaceURI = source.getNamespaceURI();
    if (HTMLParser.SVG_NAMESPACE.equals(namespaceURI)) {
        return HTMLParser.SVG_FACTORY.createElementNS(page, namespaceURI, qualifiedName,
                namedNodeMapToSaxAttributes(nodeAttributes, attributesOrderMap, source));
    }

    final Map<String, DomAttr> attributes = new LinkedHashMap<>();
    for (int i = 0; i < nodeAttributes.getLength(); i++) {
        final int orderedIndex = getIndex(nodeAttributes, attributesOrderMap, source, i);
        final Attr attribute = (Attr) nodeAttributes.item(orderedIndex);
        final String attributeNamespaceURI = attribute.getNamespaceURI();
        final String attributeQualifiedName;
        if (attribute.getPrefix() != null) {
            attributeQualifiedName = attribute.getPrefix() + ':' + attribute.getLocalName();
        }
        else {
            attributeQualifiedName = attribute.getLocalName();
        }
        final String value = attribute.getNodeValue();
        final boolean specified = attribute.getSpecified();
        final DomAttr xmlAttribute =
                new DomAttr(page, attributeNamespaceURI, attributeQualifiedName, value, specified);
        attributes.put(attribute.getNodeName(), xmlAttribute);
    }
    return new DomElement(namespaceURI, qualifiedName, page, attributes);
}
 
Example #8
Source File: DefaultPageCreator.java    From HtmlUnit-Android with Apache License 2.0 2 votes vote down vote up
/**
 * Creates an HtmlPage for this WebResponse.
 *
 * @param webResponse the page's source
 * @param webWindow the WebWindow to place the HtmlPage in
 * @return the newly created HtmlPage
 * @throws IOException if the page could not be created
 */
protected HtmlPage createHtmlPage(final WebResponse webResponse, final WebWindow webWindow) throws IOException {
    return HTMLParser.parseHtml(webResponse, webWindow);
}
 
Example #9
Source File: DefaultPageCreator.java    From HtmlUnit-Android with Apache License 2.0 2 votes vote down vote up
/**
 * Creates an XHtmlPage for this WebResponse.
 *
 * @param webResponse the page's source
 * @param webWindow the WebWindow to place the HtmlPage in
 * @return the newly created XHtmlPage
 * @throws IOException if the page could not be created
 */
protected XHtmlPage createXHtmlPage(final WebResponse webResponse, final WebWindow webWindow) throws IOException {
    return HTMLParser.parseXHtml(webResponse, webWindow);
}