Example usage for org.dom4j Element add

List of usage examples for org.dom4j Element add

Introduction

In this page you can find the example usage for org.dom4j Element add.

Prototype

void add(Namespace namespace);

Source Link

Document

Adds the given Namespace to this element.

Usage

From source file:lost.tok.html.DiscussionPage.java

License:Open Source License

@Override
protected String getBody() {
    Element body = DocumentHelper.createElement("div"); //$NON-NLS-1$
    body.addAttribute("id", "discussion"); //$NON-NLS-1$ //$NON-NLS-2$
    body.addAttribute("class", "main_content"); //$NON-NLS-1$ //$NON-NLS-2$

    body.addElement("h1").addText(disc.getDiscName()); //$NON-NLS-1$

    body.addText(Messages.getString("DiscussionPage.CreatedBy")); //$NON-NLS-1$
    body.addElement("em").addText(disc.getCreatorName()); //$NON-NLS-1$
    body.addElement("p").addText(disc.getDescription()); //$NON-NLS-1$

    if (disc.getLink() != null)
        body.add(getLinkInfoElement());

    Element opinionList = body.addElement("ol"); //$NON-NLS-1$
    for (Opinion o : disc.getOpinions()) {
        Element listItem = opinionList.addElement("li"); //$NON-NLS-1$
        listItem.add(getOpinionElement(o));
    }/*from w ww .  ja  v a  2 s  . c  om*/

    // TODO(Shay, low): Consider adding related root and source files

    return GeneralFunctions.elementToString(body);
}

From source file:lost.tok.html.DiscussionPage.java

License:Open Source License

/**
 * Returns a div element containing all the information related to the discussion's link
 *//*w w w .j a va 2 s . co m*/
private Element getLinkInfoElement() {
    Element div = DocumentHelper.createElement("div"); //$NON-NLS-1$

    Link l = disc.getLink();

    div.addAttribute("class", "linkInfo"); //$NON-NLS-1$ //$NON-NLS-2$
    div.addElement("h2").addText(l.getSubject()); //$NON-NLS-1$
    Element typeElement = div.addElement("p"); //$NON-NLS-1$

    typeElement.addText(Messages.getString("DiscussionPage.linkType")); //$NON-NLS-1$
    typeElement.addElement("em").addText(l.getDisplayLinkType()); //$NON-NLS-1$

    Element sublinksList = div.addElement("ul"); //$NON-NLS-1$
    for (SubLink sl : l.getSubLinkList()) {
        LinkedList<Excerption> excerptions = new LinkedList<Excerption>();
        for (Excerption e : sl.getExcerption())
            excerptions.add(e);

        String eText = Excerption.concat(excerptions);
        Source src = sl.getLinkedSource();

        Element listItem = sublinksList.addElement("li"); //$NON-NLS-1$

        Element quoteElm = listItem.addElement("blockquote"); //$NON-NLS-1$
        quoteElm.addAttribute("class", "sublink"); //$NON-NLS-1$ //$NON-NLS-2$
        quoteElm.addAttribute("cite", getPathToSrc(src)); //$NON-NLS-1$
        quoteElm.addElement("p").addText(eText); //$NON-NLS-1$

        Element srcLinkParagraph = listItem.addElement("p"); //$NON-NLS-1$
        srcLinkParagraph.addText(Messages.getString("DiscussionPage.Source")).add(getLinkSrcElm(src)); //$NON-NLS-1$
    }

    return div;
}

From source file:lost.tok.html.DiscussionPage.java

License:Open Source License

/**
 * Creates a div element describing the given opinion
 * @param o the opinion to generate html div for
 * @return a div element that contains all the information relating the opinion
 */// ww w  .j a  v  a2 s  .  co  m
private Element getOpinionElement(Opinion o) {
    Element div = DocumentHelper.createElement("div"); //$NON-NLS-1$

    // 1. Create the title
    Element opTitle = div.addElement("h2"); //$NON-NLS-1$
    opTitle.addText(o.getName());
    opTitle.addAttribute("id", "discItem" + o.getId()); //$NON-NLS-1$ //$NON-NLS-2$

    Quote[] quotes = disc.getSortedQuotes(o.getName());

    // add quotes only if there are any
    if (quotes.length > 0) {
        Element quotesList = div.addElement("ul"); //$NON-NLS-1$

        // 2. Add the quotes
        for (Quote q : quotes) {
            // TODO(Shay, medium-low): Sort the quotes according to author importance

            Element listItem = quotesList.addElement("li"); //$NON-NLS-1$
            listItem.add(getQuoteElement(q));
        }
    }
    // TODO(Shay, low): Consider adding relation data

    return div;
}

From source file:lost.tok.html.DiscussionPage.java

License:Open Source License

/**
 * Creates a div element describing the given quote
 * @param q the quote to generate html div for
 * @return a div element that contains all the information relating the quote
 *///from  w w  w. j  a  v  a2 s. c o  m
private Element getQuoteElement(Quote q) {
    Element div = DocumentHelper.createElement("div"); //$NON-NLS-1$

    // 1. Get the quote's text and find a good title for it
    String qText = q.getText();
    int actualLen = qText.indexOf(' ', QUOTE_TITLE_LENGTH);
    actualLen = (actualLen != -1) ? actualLen : qText.length();
    String qTitle = qText.substring(0, actualLen);

    // 2. Get the src we should link to
    Source src = q.getSource();

    // 3. Add the title
    Element quoteTitle = div.addElement("h3"); //$NON-NLS-1$
    quoteTitle.addText(qTitle);
    quoteTitle.addAttribute("id", "discItem" + q.getID()); //$NON-NLS-1$ //$NON-NLS-2$

    // 4. Add the quote itself
    Element quoteElm = div.addElement("blockquote"); //$NON-NLS-1$
    quoteElm.addAttribute("class", "quote"); //$NON-NLS-1$ //$NON-NLS-2$
    quoteElm.addAttribute("cite", getPathToSrc(src)); //$NON-NLS-1$
    quoteElm.addElement("p").addText(qText); //$NON-NLS-1$

    // 5. Add a comment, if there is one
    if (!q.getComment().equals("")) //$NON-NLS-1$
    {
        Element pComment = div.addElement("p"); //$NON-NLS-1$
        pComment.addText(Messages.getString("DiscussionPage.Comment")); //$NON-NLS-1$
        pComment.addElement("em").addText(q.getComment()); //$NON-NLS-1$

        quoteElm.addAttribute("title", q.getComment()); //$NON-NLS-1$
    }

    // 6. Add a link to the related source
    Element srcLinkParagraph = div.addElement("p"); //$NON-NLS-1$
    srcLinkParagraph.addText(Messages.getString("DiscussionPage.From")).add(getLinkSrcElm(src)); //$NON-NLS-1$

    // TODO(Shay, low): 7. Consider adding relation data

    return div;
}

From source file:lost.tok.html.IndexPage.java

License:Open Source License

@Override
protected String getBody() {
    Element body = DocumentHelper.createElement("div"); //$NON-NLS-1$
    body.addAttribute("id", "index"); //$NON-NLS-1$ //$NON-NLS-2$
    body.addAttribute("class", "main_content"); //$NON-NLS-1$ //$NON-NLS-2$

    body.addElement("h1").addText(tok.getProject().getName()); //$NON-NLS-1$

    Element author = body.addElement("p").addText(Messages.getString("IndexPage.CreatedBy")); //$NON-NLS-1$ //$NON-NLS-2$
    author.addElement("em").addText(tok.getProjectCreator()); //$NON-NLS-1$

    body.add(getSourcesElement(true));
    body.add(getSourcesElement(false));//from w  w w.ja v  a  2s . c o m
    body.add(getDiscussionElement());

    return GeneralFunctions.elementToString(body);
}

From source file:lost.tok.Link.java

License:Open Source License

/**
 * Links an existing discussion to a segment in the root of the ToK project
 */// ww w  .  j  av a 2  s. c  o m
public void linkDiscussionRoot(Source linkedSource, LinkedList<Excerption> exp) {

    SubLink sl = new SubLink(linkedSource, exp);
    subLinkList.add(sl);

    String discFileName = linkedDiscussion.getDiscFileName();

    // Open the Links file
    Document doc = GeneralFunctions.readFromXML(linkFile);

    Node link = doc.selectSingleNode("//link/discussionFile[text()=\"" //$NON-NLS-1$
            + discFileName + "\"]"); //$NON-NLS-1$
    Element newLink = null;
    if (link != null) {
        newLink = link.getParent();
    } else {

        Element links = doc.getRootElement();
        newLink = links.addElement("link"); //$NON-NLS-1$
        newLink.addElement("discussionFile").addText(discFileName); //$NON-NLS-1$
        newLink.addElement("type").addText(linkTypeXML); //$NON-NLS-1$
        newLink.addElement("linkSubject").addText(subject); //$NON-NLS-1$
    }

    Element subLink = newLink.addElement("sublink"); //$NON-NLS-1$
    subLink.addElement("sourceFile").addText(linkedSource.toString()); //$NON-NLS-1$
    for (Excerption element : exp) {

        subLink.add(element.toXML());

    }

    GeneralFunctions.writeToXml(linkFile, doc);
}

From source file:main.FrevoMain.java

License:Open Source License

/**
 * Starts a saving procedure calling the method's own saveResults function.
 * Run-specific data will be added to the end of the file.
 * //from   www  .  j a va 2s.  c  o m
 * @param fileName
 *            Name of the saved file without the extension. (E.g.
 *            solution_generation_13)
 * @param method
 *            The corresponding method instance to be called for saving.
 */
public static File saveResult(String fileName, final Element representationRootElement, long startSeed,
        long currentActiveSeed) {
    // create new document for output
    Document doc = DocumentHelper.createDocument();
    doc.addDocType("frevo", null, System.getProperty("user.dir") + "//Components//ISave.dtd");
    Element dfrevo = doc.addElement("frevo");

    String fileLocation = "Undefined";

    // export sessionconfig
    Element sessionconfig = dfrevo.addElement("sessionconfig");

    // custom name
    Element configentry = sessionconfig.addElement("configentry");
    configentry.addAttribute("key", "CustomName");
    configentry.addAttribute("type", "STRING");
    configentry.addAttribute("value", customName);

    // number of runs
    Element runentry = sessionconfig.addElement("configentry");
    runentry.addAttribute("key", "NumberofRuns");
    runentry.addAttribute("type", "INT");
    runentry.addAttribute("value", Integer.toString(getNumberOfSimulationRuns()));

    // starting seed
    Element seedentry = sessionconfig.addElement("configentry");
    seedentry.addAttribute("key", "StartingSeed");
    seedentry.addAttribute("type", "LONG");
    // seedentry.addAttribute("value", Long.toString(getSeed()));
    seedentry.addAttribute("value", Long.toString(startSeed));

    // active seed
    Element aseedentry = sessionconfig.addElement("configentry");
    aseedentry.addAttribute("key", "CurrentSeed");
    aseedentry.addAttribute("type", "LONG");
    aseedentry.addAttribute("value", Long.toString(currentActiveSeed));

    try {
        // export problem
        Element problemsettings = dfrevo.addElement("problem");
        ComponentXMLData problem = FrevoMain.SELECTED_PROBLEM;
        problemsettings.addAttribute("class", problem.getClassName());
        Vector<String> keys = new Vector<String>(problem.getProperties().keySet());
        for (String k : keys) {
            Element entry = problemsettings.addElement("problementry");
            entry.addAttribute("key", k);
            entry.addAttribute("type", problem.getTypeOfProperty(k).toString());
            entry.addAttribute("value", problem.getValueOfProperty(k));
        }

        // export method
        Element methodsettings = dfrevo.addElement("method");
        ComponentXMLData method = FrevoMain.SELECTED_METHOD;
        methodsettings.addAttribute("class", method.getClassName());
        keys = new Vector<String>(method.getProperties().keySet());
        for (String k : keys) {
            Element entry = methodsettings.addElement("methodentry");
            entry.addAttribute("key", k);
            entry.addAttribute("type", method.getTypeOfProperty(k).toString());
            entry.addAttribute("value", method.getValueOfProperty(k));
        }

        // export ranking
        Element rankingsettings = dfrevo.addElement("ranking");
        ComponentXMLData ranking = FrevoMain.SELECTED_RANKING;
        rankingsettings.addAttribute("class", ranking.getClassName());
        keys = new Vector<String>(ranking.getProperties().keySet());
        for (String k : keys) {
            Element entry = rankingsettings.addElement("rankingentry");
            entry.addAttribute("key", k);
            entry.addAttribute("type", ranking.getTypeOfProperty(k).toString());
            entry.addAttribute("value", ranking.getValueOfProperty(k));
        }

        // export representation
        Element repsettings = dfrevo.addElement("representation");
        ComponentXMLData representation = FrevoMain.SELECTED_REPRESENTATION;
        repsettings.addAttribute("class", representation.getClassName());
        keys = new Vector<String>(representation.getProperties().keySet());
        for (String k : keys) {
            Element entry = repsettings.addElement("representationentry");
            entry.addAttribute("key", k);
            entry.addAttribute("type", representation.getTypeOfProperty(k).toString());
            entry.addAttribute("value", representation.getValueOfProperty(k));
        }

        // call method's own save solution
        dfrevo.add(representationRootElement);

        // save contents to file

        String location = FREVO_INSTALL_DIRECTORY + File.separator + "Results" + File.separator + customName;
        File rootSaveDir = new File(location);

        // remove spaces from filename
        fileName.replaceAll(" ", "_");

        // create save directory based on given custom name
        rootSaveDir.mkdirs();

        // create sub-directories for different seeds
        if (FrevoMain.getNumberOfSimulationRuns() > 1) {
            // create seed directory if there are more than one run
            File seedDir = new File(location + File.separator + "seed_" + startSeed);
            seedDir.mkdir();
            fileLocation = seedDir + File.separator + fileName + ".zre";
        } else {
            // save it the root location
            fileLocation = rootSaveDir + File.separator + fileName + ".zre";
        }

        File saveFile = new File(fileLocation);

        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setLineSeparator(System.getProperty("line.separator"));

        saveFile.createNewFile();
        FileWriter out = new FileWriter(saveFile);
        BufferedWriter bw = new BufferedWriter(out);
        XMLWriter wr = new XMLWriter(bw, format);
        wr.write(doc);
        wr.close();
        System.out.println("XML Writing Completed: " + fileLocation);

        if (isFrevoWithGraphics()) {
            mainWindow.addRecentResult(saveFile);
        }
        return saveFile;
    } catch (OutOfMemoryError mem) {
        System.err.println("Could not export! (Out of memory)");
    } catch (IOException e) {
        System.err.println("IOException while writing to XML! Check path at: " + fileLocation);
        e.printStackTrace();
    }
    return null;
}

From source file:mesquite.chromaseq.lib.SequenceProfile.java

License:Open Source License

public String getXML() {
    Element mesquiteElement = DocumentHelper.createElement("mesquite");
    Document doc = DocumentHelper.createDocument(mesquiteElement);
    Element sequenceProfileElement = DocumentHelper.createElement("sequenceProfile");
    mesquiteElement.add(sequenceProfileElement);
    XMLUtil.addFilledElement(sequenceProfileElement, "version", "1");
    Element boundedByTokensElement = DocumentHelper.createElement("boundedByTokens");
    sequenceProfileElement.add(boundedByTokensElement);
    XMLUtil.addFilledElement(boundedByTokensElement, "name", name);
    XMLUtil.addFilledElement(boundedByTokensElement, "description", DocumentHelper.createCDATA(description));
    XMLUtil.addFilledElement(boundedByTokensElement, "location", DocumentHelper.createCDATA(location));
    XMLUtil.addFilledElement(boundedByTokensElement, "moltype", DocumentHelper.createCDATA(moltype));
    XMLUtil.addFilledElement(boundedByTokensElement, "productName", DocumentHelper.createCDATA(productName));
    XMLUtil.addFilledElement(boundedByTokensElement, "seqIDSuffix", DocumentHelper.createCDATA(seqIDSuffix));
    XMLUtil.addFilledElement(boundedByTokensElement, "gcode", DocumentHelper.createCDATA("" + gcode));
    XMLUtil.addFilledElement(boundedByTokensElement, "note", DocumentHelper.createCDATA("" + note));
    //      XMLUtil.addFilledElement(boundedByTokensElement, "CDS",DocumentHelper.createCDATA(MesquiteBoolean.toTrueFalseString(CDS)));
    return XMLUtil.getDocumentAsXMLString(doc);
}

From source file:mesquite.chromaseq.SampleAndPrimerFileNameParser.ChromFileNameParsing.java

License:Open Source License

public String getXML() {
    Element mesquiteElement = DocumentHelper.createElement("mesquite");
    Document doc = DocumentHelper.createDocument(mesquiteElement);
    Element nameRules = DocumentHelper.createElement("chromFileNameParsingRules");
    mesquiteElement.add(nameRules);
    XMLUtil.addFilledElement(nameRules, "version", "1");
    Element boundedByTokensElement = DocumentHelper.createElement("boundedByTokens");
    nameRules.add(boundedByTokensElement);
    XMLUtil.addFilledElement(boundedByTokensElement, "name", name);
    XMLUtil.addFilledElement(boundedByTokensElement, "sampleCodeFirst",
            MesquiteBoolean.toTrueFalseString(sampleCodeFirst));
    XMLUtil.addFilledElement(boundedByTokensElement, "dnaCodeStartToken",
            DocumentHelper.createCDATA(dnaCodeStartToken));
    XMLUtil.addFilledElement(boundedByTokensElement, "dnaCodeEndToken",
            DocumentHelper.createCDATA(dnaCodeEndToken));
    XMLUtil.addFilledElement(boundedByTokensElement, "dnaCodeSuffixToken",
            DocumentHelper.createCDATA(dnaCodeSuffixToken));
    XMLUtil.addFilledElement(boundedByTokensElement, "dnaCodeRemovalToken",
            DocumentHelper.createCDATA(dnaCodeRemovalToken));
    XMLUtil.addFilledElement(boundedByTokensElement, "primerStartToken",
            DocumentHelper.createCDATA(primerStartToken));
    XMLUtil.addFilledElement(boundedByTokensElement, "primerEndToken",
            DocumentHelper.createCDATA(primerEndToken));
    return XMLUtil.getDocumentAsXMLString(doc);
}

From source file:mesquite.lib.MesquiteXMLPreferencesModule.java

License:Open Source License

/**
 * Ideally this should not be static but since we don't always have control over the superclass,
 * make it available for classes that might otherwise not be able to use it due to inheritance
 * constraints/*w  w  w  .  ja  v  a  2s  .  c om*/
 * @param provider
 * @param versionInt
 * @return
 */
public static String preparePreferencesForXML(PropertyNamesProvider provider, int versionInt) {
    String[] propertyNames = provider.getPreferencePropertyNames();
    Element rootElement = DocumentHelper.createElement("mesquite");
    Document preferencesDoc = DocumentHelper.createDocument(rootElement);
    preferencesDoc.setRootElement(rootElement);
    Element classElement = DocumentHelper.createElement(getShortClassName(provider.getClass()));
    rootElement.add(classElement);
    Element versionElement = DocumentHelper.createElement(VERSION);
    versionElement.setText("" + versionInt);
    classElement.add(versionElement);
    for (int i = 0; i < propertyNames.length; i++) {
        String currentPropertyName = propertyNames[i];
        try {
            Object prefsContent = PropertyUtils.read(provider, currentPropertyName);
            if (prefsContent != null) {
                Element nextPrefsElement = DocumentHelper.createElement(PREFERENCE);
                nextPrefsElement.addAttribute(KEY, currentPropertyName);
                nextPrefsElement.add(DocumentHelper.createCDATA(prefsContent.toString()));
                classElement.add(nextPrefsElement);
            }
        } catch (Exception e) {
            MesquiteMessage.warnProgrammer("Could not read property value " + currentPropertyName
                    + " for writing xml preferences on module: " + provider);
        }
        //nextPrefsElement.addContent(new CDATA())
    }
    return XMLUtil.getDocumentAsXMLString(preferencesDoc);
}