Example usage for org.w3c.dom Document getLastChild

List of usage examples for org.w3c.dom Document getLastChild

Introduction

In this page you can find the example usage for org.w3c.dom Document getLastChild.

Prototype

public Node getLastChild();

Source Link

Document

The last child of this node.

Usage

From source file:Main.java

public static Document AppendContentToEnd(Document doc, String tag, String content) {
    if (tag == null || tag == "" || content == null || content == "")
        return doc;
    Element e = doc.createElement(tag);
    e.setTextContent(content);//from w ww. j a  va 2  s  .co  m
    Node lastone = doc.getLastChild();
    lastone.appendChild(e);
    return doc;
}

From source file:Main.java

public static String toXmlString(Document document) {
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    Transformer transformer;/*from  w ww  .ja  va2 s.co  m*/
    try {
        transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(document.getLastChild()), result);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return sw.toString();
}

From source file:org.gvnix.web.screen.roo.addon.SeleniumServicesImpl.java

/**
 * Get template html content and update with current name.
 * /*from w w w.java2 s  .co  m*/
 * @param name Name to write into html content
 * @param document Document to write into
 * @return Html element with required name
 */
protected Element getHtmlElement(String name, Document document) {

    Element root = (Element) document.getLastChild();
    if (root == null || !"html".equals(root.getNodeName())) {

        throw new IllegalArgumentException("Could not parse selenium test case template file!");
    }
    XmlUtils.findRequiredElement("/html/head/title", root).setTextContent(name);
    XmlUtils.findRequiredElement("/html/body/table/thead/tr/td", root).setTextContent(name);

    return root;
}

From source file:org.gvnix.web.screen.roo.addon.SeleniumServicesImpl.java

/**
 * Install central Selenium configuration if not already and reference a new
 * test.//  w  w w . j a  v  a2  s . c  o  m
 * 
 * @param testPath New test path
 * @param name Test name
 * @param serverURL Application base URL
 */
protected void manageTestSuite(String testPath, String name, String serverURL) {

    String relativePath = "selenium/test-suite.xhtml";
    String seleniumPath = projectOperations.getPathResolver()
            .getIdentifier(LogicalPath.getInstance(Path.SRC_MAIN_WEBAPP, ""), relativePath);

    Document suite;
    try {

        if (fileManager.exists(seleniumPath)) {

            suite = XmlUtils.readXml(fileManager.getInputStream(seleniumPath));

        } else {

            InputStream templateInputStream = FileUtils.getInputStream(SeleniumOperationsImpl.class,
                    "selenium-test-suite-template.xhtml");
            Validate.notNull(templateInputStream, "Could not acquire selenium test suite template");
            suite = XmlUtils.readXml(templateInputStream);
        }
    } catch (Exception e) {

        throw new IllegalStateException(e);
    }

    ProjectMetadata projectMetadata = projectOperations
            .getProjectMetadata(projectOperations.getFocusedModuleName());
    Validate.notNull(projectMetadata, "Unable to obtain project metadata");

    Element root = (Element) suite.getLastChild();

    XmlUtils.findRequiredElement("/html/head/title", root).setTextContent("Test suite for "
            + projectOperations.getProjectName(projectOperations.getFocusedModuleName()) + "project");

    Element tr = suite.createElement("tr");
    Element td = suite.createElement("td");
    tr.appendChild(td);
    Element a = suite.createElement("a");
    a.setAttribute("href",
            serverURL + projectOperations.getProjectName(projectOperations.getFocusedModuleName())
                    + "/resources/" + testPath);
    a.setTextContent(name);
    td.appendChild(a);

    XmlUtils.findRequiredElement("/html/body/table", root).appendChild(tr);

    fileManager.createOrUpdateTextFileIfRequired(seleniumPath, XmlUtils.nodeToString(suite), false);

    menuOperations.addMenuItem(new JavaSymbolName("SeleniumTests"), new JavaSymbolName("Test"), "Test",
            "selenium_menu_test_suite", "/resources/" + relativePath, "si_",
            LogicalPath.getInstance(Path.SRC_MAIN_WEBAPP, ""));
}

From source file:org.gvnix.web.screen.roo.addon.SeleniumServicesImpl.java

/**
 * Install into Maven if not already the Selenium plugin.
 *//*from   w ww .ja  va 2s.c o m*/
protected void installMavenPlugin() {

    PathResolver pathResolver = projectOperations.getPathResolver();
    String pom = pathResolver.getIdentifier(LogicalPath.getInstance(Path.ROOT, ""), "/pom.xml");
    Document document = XmlUtils.readXml(fileManager.getInputStream(pom));
    Element root = (Element) document.getLastChild();

    // Stop if the plugin is already installed
    if (XmlUtils.findFirstElement("/project/build/plugins/plugin[artifactId = 'selenium-maven-plugin']",
            root) != null) {
        return;
    }

    Element configuration = XmlUtils.getConfiguration(SeleniumOperationsImpl.class);
    Element plugin = XmlUtils.findFirstElement("/configuration/selenium/plugin", configuration);

    // Now install the plugin itself
    if (plugin != null) {
        projectOperations.addBuildPlugin(projectOperations.getFocusedModuleName(), new Plugin(plugin));
    }
}

From source file:ru.runa.wf.web.FormPresentationUtils.java

public static String adjustForm(PageContext pageContext, Long definitionId, String formHtml,
        VariableProvider variableProvider, List<String> requiredVarNames) {
    try {//from  www  .  j a v a  2 s  .c o  m
        Map<String, String> userErrors = FormSubmissionUtils.getUserInputErrors(pageContext.getRequest());
        Document document = HTMLUtils.readHtml(formHtml.getBytes(Charsets.UTF_8));
        adjustUrls(pageContext, document, definitionId, "form.ftl");
        NodeList htmlTagElements = document.getElementsByTagName("input");
        for (int i = 0; i < htmlTagElements.getLength(); i++) {
            Element node = (Element) htmlTagElements.item(i);
            String typeName = node.getAttribute(TYPE_ATTR);
            String inputName = node.getAttribute(NAME_ATTR);
            if (WebResources.isHighlightRequiredFields() && requiredVarNames.contains(inputName)) {
                Element requiredNode = node;
                if ("file".equalsIgnoreCase(typeName) && WebResources.isAjaxFileInputEnabled()) {
                    requiredNode = (Element) node.getParentNode();
                }
                addRequiredClassAttribute(requiredNode);
            }
            handleErrors(userErrors, inputName, pageContext, document, node);
        }
        NodeList textareaElements = document.getElementsByTagName("textarea");
        for (int i = 0; i < textareaElements.getLength(); i++) {
            Element node = (Element) textareaElements.item(i);
            String inputName = node.getAttribute(NAME_ATTR);
            if (WebResources.isHighlightRequiredFields() && requiredVarNames.contains(inputName)) {
                addRequiredClassAttribute(node);
            }
            handleErrors(userErrors, inputName, pageContext, document, node);
        }
        NodeList selectElements = document.getElementsByTagName("select");
        for (int i = 0; i < selectElements.getLength(); i++) {
            Element node = (Element) selectElements.item(i);
            String inputName = node.getAttribute(NAME_ATTR);
            if (WebResources.isHighlightRequiredFields() && requiredVarNames.contains(inputName)) {
                wrapSelectToErrorContainer(document, node, inputName, true);
            }
            handleErrors(userErrors, inputName, pageContext, document, node);
        }
        if (!userErrors.isEmpty()) {
            Set<String> messages = Sets.newHashSet();
            for (String variableName : userErrors.keySet()) {
                messages.add(variableName + ": " + getErrorText(pageContext, userErrors, variableName));
            }
            log.debug("Not for all errors inputs found, appending errors to the end of the form: " + messages);
            document.getLastChild().appendChild(document.createElement("hr"));
            Element font = document.createElement("font");
            for (String message : messages) {
                font.appendChild(document.createElement("br"));
                font.setAttribute(CSS_CLASS_ATTR, "error");
                Element b = document.createElement("b");
                b.appendChild(document.createTextNode(message));
                font.appendChild(b);
            }
            document.getLastChild().appendChild(font);
        }
        return HTMLUtils.writeHtml(document);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}