Example usage for org.jsoup.nodes Document createElement

List of usage examples for org.jsoup.nodes Document createElement

Introduction

In this page you can find the example usage for org.jsoup.nodes Document createElement.

Prototype

public Element createElement(String tagName) 

Source Link

Document

Create a new Element, with this document's base uri.

Usage

From source file:uk.co.blackpepper.support.retrofit.jsoup.spring.AbstractBeanHtmlConverterTest.java

private static Element newElementWithText(Document document, String text) {
    return document.createElement("_element").text(text);
}

From source file:uk.co.blackpepper.support.retrofit.jsoup.spring.AbstractBeanHtmlConverterTest.java

private static Element newElementWithIdAndText(Document document, String id, String text) {
    return document.createElement("_element").attr("id", id).text(text);
}

From source file:uk.co.blackpepper.support.retrofit.jsoup.spring.AbstractBeanHtmlConverterTest.java

private static Element newElementWithClassAndText(Document document, String className, String text) {
    return document.createElement("_element").addClass(className).text(text);
}

From source file:uk.co.blackpepper.support.retrofit.jsoup.spring.AbstractBeanHtmlConverterTest.java

private static Element newElementWithIdAndAttribute(Document document, String id, String attributeName,
        String attributeValue) {//from  w  w  w. j av a 2s.  com
    return document.createElement("_element").attr("id", id).attr(attributeName, attributeValue);
}

From source file:uk.co.blackpepper.support.retrofit.jsoup.spring.AbstractBeanHtmlConverterTest.java

private static Element newElementWithClassAndAttribute(Document document, String className,
        String attributeName, String attributeValue) {
    return document.createElement("_element").addClass(className).attr(attributeName, attributeValue);
}

From source file:ie.nuim.cs.dri.metadata.WebSearch.java

/**
 *
 * @param doi/*from  w  w w  .j av  a 2  s .  c  o m*/
 * @param ti
 * @param au
 * @param pubYear
 * @param xmlFile
 */
public static void createROS(String doi, String ti, String au, String pubYear, String xmlFile) {
    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = null;
        try {
            docBuilder = docFactory.newDocumentBuilder();

        } catch (ParserConfigurationException ex) {
            Logger.getLogger(MetadataExtractor.class.getName()).log(Level.SEVERE, null, ex);
        }
        org.w3c.dom.Document rosDoc = docBuilder.newDocument();
        org.w3c.dom.Element rootElement = rosDoc.createElement("ROS");
        rosDoc.appendChild((rootElement));
        org.w3c.dom.Element DOI = rosDoc.createElement("DOI");
        org.w3c.dom.Element title = rosDoc.createElement("Title");
        org.w3c.dom.Element authors = rosDoc.createElement("Authors");
        org.w3c.dom.Element institution = rosDoc.createElement("Institution");
        org.w3c.dom.Element year = rosDoc.createElement("Year");
        org.w3c.dom.Element publication = rosDoc.createElement("Publication");
        org.w3c.dom.Element conference = rosDoc.createElement("Conference");

        rootElement.appendChild(DOI);
        rootElement.appendChild(title);
        rootElement.appendChild(authors);
        rootElement.appendChild(institution);
        rootElement.appendChild(year);
        rootElement.appendChild(publication);
        rootElement.appendChild(conference);

        DOI.appendChild(rosDoc.createTextNode(doi));
        title.appendChild(rosDoc.createTextNode(ti));
        authors.appendChild(rosDoc.createTextNode(au));
        year.appendChild(rosDoc.createTextNode(pubYear));

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(rosDoc);
        xmlFile = xmlFile.replace(".xml", "MD.xml");
        System.out.println("Writing to:\n" + xmlFile);
        StreamResult result = new StreamResult(new File(xmlFile));
        transformer.transform(source, result);
        //result = new StreamResult(System.out);

    } catch (TransformerException ex) {
        Logger.getLogger(MetadataExtractor.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:ru.xxlabaza.popa.pack.PackingService.java

private void processCss(Document document) {
    document.select("link[rel=stylesheet]:not([href^=http])").forEach(link -> {
        Path path = build.resolve(createPath(link.attr("href")));
        log.info("Processing style '{}'", path);

        String content = commentRemoveService.removeComments(path);
        content = correctURLs(path, content);

        if (!path.getFileName().toString().endsWith(".min.css")) {
            content = compressService.compress(content, CSS);
        }/*from  w  w  w  .  j a  v  a 2 s  . c o m*/

        Element style = document.createElement("style");
        style.html(content);

        link.after(style);
        link.remove();
    });
}

From source file:org.shareok.data.sagedata.SageSourceDataHandlerImpl.java

/** 
 * Convert the article data to dublin core xml metadata and save the the file
 * //from w  w  w .  jav a  2s .  c  om
 * @param journalData : the SageJournalData
 * @param fileName : the root folder contains all the uploading article data
 */
public void exportXmlByJournalData(SageJournalData journalData, String outputPath) {

    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        org.w3c.dom.Document doc = docBuilder.newDocument();
        org.w3c.dom.Element rootElement = doc.createElement("dublin_core");
        doc.appendChild(rootElement);

        // Add the type node:
        org.w3c.dom.Element element = doc.createElement("dcvalue");
        element.appendChild(doc.createTextNode(journalData.getType()));
        rootElement.appendChild(element);

        Attr attr = doc.createAttribute("element");
        attr.setValue("type");
        element.setAttributeNode(attr);

        attr = doc.createAttribute("language");
        attr.setValue("en_US");
        element.setAttributeNode(attr);

        attr = doc.createAttribute("qualifier");
        attr.setValue("none");
        element.setAttributeNode(attr);

        // Add the abstract node:
        String abs = journalData.getAbstractText();
        if (null != abs) {
            org.w3c.dom.Element elementAbs = doc.createElement("dcvalue");
            elementAbs.appendChild(doc.createTextNode(abs));
            rootElement.appendChild(elementAbs);

            attr = doc.createAttribute("element");
            attr.setValue("description");
            elementAbs.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementAbs.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("abstract");
            elementAbs.setAttributeNode(attr);
        }

        // Add the language node:
        String lang = journalData.getLanguage();
        if (null != lang) {
            org.w3c.dom.Element elementLang = doc.createElement("dcvalue");
            elementLang.appendChild(doc.createTextNode(lang));
            rootElement.appendChild(elementLang);

            attr = doc.createAttribute("element");
            attr.setValue("language");
            elementLang.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementLang.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("iso");
            elementLang.setAttributeNode(attr);
        }

        // Add the title node:
        String tit = journalData.getTitle();
        if (null != tit) {
            org.w3c.dom.Element elementTitle = doc.createElement("dcvalue");
            elementTitle.appendChild(doc.createTextNode(tit));
            rootElement.appendChild(elementTitle);

            attr = doc.createAttribute("element");
            attr.setValue("title");
            elementTitle.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementTitle.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("none");
            elementTitle.setAttributeNode(attr);
        }

        // Add the available date node:
        //            Element elementAvailable = doc.createElement("dcvalue");
        //            elementAvailable.appendChild(doc.createTextNode(getDateAvailable().toString()));
        //            rootElement.appendChild(elementAvailable);
        //            
        //            attr = doc.createAttribute("element");
        //            attr.setValue("date");
        //            elementAvailable.setAttributeNode(attr);
        //            
        //            attr = doc.createAttribute("qualifier");
        //            attr.setValue("available");
        //            elementAvailable.setAttributeNode(attr);

        // Add the issued date node:
        Date issueDate = journalData.getDateIssued();
        if (null != issueDate) {
            SimpleDateFormat format_issuedDate = new SimpleDateFormat("yyyy-MM-dd");
            org.w3c.dom.Element elementIssued = doc.createElement("dcvalue");
            elementIssued.appendChild(doc.createTextNode(format_issuedDate.format(issueDate)));
            rootElement.appendChild(elementIssued);

            attr = doc.createAttribute("element");
            attr.setValue("date");
            elementIssued.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("issued");
            elementIssued.setAttributeNode(attr);
        }

        // Add the author nodes:
        String[] authorSet = journalData.getAuthors();
        if (null != authorSet && authorSet.length > 0) {
            for (String author : authorSet) {
                org.w3c.dom.Element elementAuthor = doc.createElement("dcvalue");
                elementAuthor.appendChild(doc.createTextNode(author));
                rootElement.appendChild(elementAuthor);

                attr = doc.createAttribute("element");
                attr.setValue("contributor");
                elementAuthor.setAttributeNode(attr);

                attr = doc.createAttribute("qualifier");
                attr.setValue("author");
                elementAuthor.setAttributeNode(attr);
            }
        }

        // Add the acknowledgements node:
        String ack = journalData.getAcknowledgements();
        if (null != ack) {
            org.w3c.dom.Element elementAck = doc.createElement("dcvalue");
            elementAck.appendChild(doc.createTextNode(ack));
            rootElement.appendChild(elementAck);

            attr = doc.createAttribute("element");
            attr.setValue("description");
            elementAck.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementAck.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("none");
            elementAck.setAttributeNode(attr);
        }

        // Add the author contributions node:
        String contrib = journalData.getAuthorContributions();
        if (null != contrib) {
            org.w3c.dom.Element elementContribution = doc.createElement("dcvalue");
            elementContribution.appendChild(doc.createTextNode(contrib));
            rootElement.appendChild(elementContribution);

            attr = doc.createAttribute("element");
            attr.setValue("description");
            elementContribution.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementContribution.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("none");
            elementContribution.setAttributeNode(attr);
        }

        // Add the publisher node:
        String puber = journalData.getPublisher();
        if (null != puber) {
            org.w3c.dom.Element elementPublisher = doc.createElement("dcvalue");
            elementPublisher.appendChild(doc.createTextNode(puber));
            rootElement.appendChild(elementPublisher);

            attr = doc.createAttribute("element");
            attr.setValue("publisher");
            elementPublisher.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("none");
            elementPublisher.setAttributeNode(attr);
        }

        // Add the citation node:
        String cit = journalData.getCitation();
        if (null != cit) {
            org.w3c.dom.Element elementCitation = doc.createElement("dcvalue");
            elementCitation.appendChild(doc.createTextNode(cit));
            rootElement.appendChild(elementCitation);

            attr = doc.createAttribute("element");
            attr.setValue("identifier");
            elementCitation.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementCitation.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("citation");
            elementCitation.setAttributeNode(attr);
        }

        // Add the rights node:
        String rit = journalData.getRights();
        if (null != rit) {
            org.w3c.dom.Element elementRights = doc.createElement("dcvalue");
            elementRights.appendChild(doc.createTextNode(rit));
            rootElement.appendChild(elementRights);

            attr = doc.createAttribute("element");
            attr.setValue("rights");
            elementRights.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("none");
            elementRights.setAttributeNode(attr);
        }

        // Add the rights URI node:
        String ritUri = journalData.getRightsUri();
        if (null != ritUri) {
            org.w3c.dom.Element elementRightsUri = doc.createElement("dcvalue");
            elementRightsUri.appendChild(doc.createTextNode(ritUri));
            rootElement.appendChild(elementRightsUri);

            attr = doc.createAttribute("element");
            attr.setValue("rights");
            elementRightsUri.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("uri");
            elementRightsUri.setAttributeNode(attr);
        }

        // Add the rights requestable node:
        org.w3c.dom.Element elementRightsRequestable = doc.createElement("dcvalue");
        elementRightsRequestable
                .appendChild(doc.createTextNode(Boolean.toString(journalData.isRightsRequestable())));
        rootElement.appendChild(elementRightsRequestable);

        attr = doc.createAttribute("element");
        attr.setValue("rights");
        elementRightsRequestable.setAttributeNode(attr);

        attr = doc.createAttribute("language");
        attr.setValue("en_US");
        elementRightsRequestable.setAttributeNode(attr);

        attr = doc.createAttribute("qualifier");
        attr.setValue("requestable");
        elementRightsRequestable.setAttributeNode(attr);

        // Add the is part of node:
        String partOf = journalData.getIsPartOfSeries();
        if (null != partOf) {
            org.w3c.dom.Element elementIsPartOf = doc.createElement("dcvalue");
            elementIsPartOf.appendChild(doc.createTextNode(partOf));
            rootElement.appendChild(elementIsPartOf);

            attr = doc.createAttribute("element");
            attr.setValue("relation");
            elementIsPartOf.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("ispartofseries");
            elementIsPartOf.setAttributeNode(attr);
        }

        // Add the relation uri node:
        String reUri = journalData.getRelationUri();
        if (null != reUri) {
            org.w3c.dom.Element elementRelationUri = doc.createElement("dcvalue");
            elementRelationUri.appendChild(doc.createTextNode(reUri));
            rootElement.appendChild(elementRelationUri);

            attr = doc.createAttribute("element");
            attr.setValue("relation");
            elementRelationUri.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("uri");
            elementRelationUri.setAttributeNode(attr);
        }

        // Add the subject nodes:
        String[] subjectSet = journalData.getSubjects();
        if (null != subjectSet && subjectSet.length > 0) {
            for (String subject : subjectSet) {
                org.w3c.dom.Element elementSubject = doc.createElement("dcvalue");
                elementSubject.appendChild(doc.createTextNode(subject));
                rootElement.appendChild(elementSubject);

                attr = doc.createAttribute("element");
                attr.setValue("subject");
                elementSubject.setAttributeNode(attr);

                attr = doc.createAttribute("language");
                attr.setValue("en_US");
                elementSubject.setAttributeNode(attr);

                attr = doc.createAttribute("qualifier");
                attr.setValue("none");
                elementSubject.setAttributeNode(attr);
            }
        }

        // Add the peerReview node:
        String review = journalData.getPeerReview();
        if (null != review) {
            org.w3c.dom.Element elementPeerReview = doc.createElement("dcvalue");
            elementPeerReview.appendChild(doc.createTextNode(review));
            rootElement.appendChild(elementPeerReview);

            attr = doc.createAttribute("element");
            attr.setValue("description");
            elementPeerReview.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementPeerReview.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("peerreview");
            elementPeerReview.setAttributeNode(attr);
        }

        // Add the peer review notes node:
        String peer = journalData.getPeerReviewNotes();
        if (null != peer) {
            org.w3c.dom.Element elementPeerReviewNotes = doc.createElement("dcvalue");
            elementPeerReviewNotes.appendChild(doc.createTextNode(peer));
            rootElement.appendChild(elementPeerReviewNotes);

            attr = doc.createAttribute("element");
            attr.setValue("description");
            elementPeerReviewNotes.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementPeerReviewNotes.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("peerreviewnotes");
            elementPeerReviewNotes.setAttributeNode(attr);
        }

        // Add the doi node:
        String doi = journalData.getDoi();
        if (null != doi) {
            org.w3c.dom.Element elementDoi = doc.createElement("dcvalue");
            elementDoi.appendChild(doc.createTextNode(doi));
            rootElement.appendChild(elementDoi);

            attr = doc.createAttribute("element");
            attr.setValue("identifier");
            elementDoi.setAttributeNode(attr);

            attr = doc.createAttribute("language");
            attr.setValue("en_US");
            elementDoi.setAttributeNode(attr);

            attr = doc.createAttribute("qualifier");
            attr.setValue("doi");
            elementDoi.setAttributeNode(attr);
        }

        File outputFolder = new File(outputPath + File.separator + journalData.getDoi().replaceAll("/", "."));
        if (!outputFolder.exists()) {
            outputFolder.mkdirs();
        }
        String filePath = outputFolder + File.separator + "dublin_core.xml";
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(filePath));

        transformer.transform(source, result);

    } catch (ParserConfigurationException | DOMException | BeansException pce) {
        pce.printStackTrace();
    } catch (TransformerException ex) {
        Logger.getLogger(SageSourceDataHandlerImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.craftercms.social.migration.controllers.MainController.java

protected void getHtml(final FileWriter writer) throws TransformerException, IOException {
    final URL in = getClass().getResource(
            MigrationTool.systemProperties.getString("crafter" + ".migration" + "" + ".loggerTemplate"));
    if (in == null) {
        log.error("Unable to find {} "
                + MigrationTool.systemProperties.getString("crafter" + ".migration" + "" + ".loggerTemplate"));
    }// w w  w. j  a v  a2s  .c o m
    final Document loggingDoc = Jsoup.parse(IOUtils.toString(in));
    final Element logs = loggingDoc.getElementById("logs");
    for (Object o : logTable.getItems()) {
        if (o instanceof UserLogEntry) {
            UserLogEntry userLogEntry = (UserLogEntry) o;
            String dateFormat = new SimpleDateFormat("yyyy MM dd hh:mm:ss zzz").format(userLogEntry.getDate());
            final Element tr = loggingDoc.createElement("tr");
            tr.attr("class", userLogEntry.getLevel().getCssClass());
            final Element tmigrator = loggingDoc.createElement("td");
            final Element tdate = loggingDoc.createElement("td");
            final Element tmessage = loggingDoc.createElement("td");
            tmessage.attr("class", "text-center");
            tmessage.text(userLogEntry.getMessage());
            tdate.text(dateFormat);
            tmigrator.text(userLogEntry.getSource());
            tr.appendChild(tmigrator);
            tr.appendChild(tdate);
            tr.appendChild(tmessage);
            logs.appendChild(tr);
        }
    }
    IOUtils.write(loggingDoc.toString(), writer);
    //        Transformer transformer = TransformerFactory.newInstance().newTransformer();
    //        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    //        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    //        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    //        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    //        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    //        transformer.transform(new DOMSource(loggingDoc), new StreamResult(writer));
    writer.flush();
    writer.close();
}

From source file:org.jboss.tools.windup.ui.internal.issues.IssueDetailsView.java

public static void addPrism(Document doc) {
    try {// ww w .ja  va 2 s . c  om
        Bundle bundle = WindupUIPlugin.getDefault().getBundle();
        Elements codeElements = doc.getElementsByTag("code");
        codeElements.forEach(element -> {
            Set<String> classNames = element.classNames();
            Set<String> newNames = Sets.newHashSet();
            classNames.forEach(className -> {
                // prismjs requires prefix, i'm not sure about another/easier workaround.
                newNames.add("language-" + className);
            });
            element.classNames(newNames);
        });

        DocumentType type = new DocumentType("html", "", "", "");
        doc.insertChildren(0, Lists.newArrayList(type));

        Element head = doc.head();
        Element css = doc.createElement("link");

        URL fileURL = FileLocator.find(bundle, new Path("html/prism.css"), null);
        String srcPath = FileLocator.resolve(fileURL).getPath();

        css.attr("href", srcPath);
        css.attr("rel", "stylesheet");
        head.appendChild(css);

        Element body = doc.body();
        Element script = doc.createElement("script");

        fileURL = FileLocator.find(bundle, new Path("html/prism.js"), null);
        srcPath = FileLocator.resolve(fileURL).getPath();

        script.attr("src", srcPath);
        body.appendChild(script);
    } catch (Exception e) {
        WindupUIPlugin.log(e);
    }
}