Example usage for org.dom4j Document addElement

List of usage examples for org.dom4j Document addElement

Introduction

In this page you can find the example usage for org.dom4j Document addElement.

Prototype

Element addElement(String name);

Source Link

Document

Adds a new Element node with the given name to this branch and returns a reference to the new node.

Usage

From source file:gtu._work.etc.HotnoteMakerUI.java

License:Open Source License

private void initGUI() {
    try {//from  www .  j  a v  a  2s .  c om
        ToolTipManager.sharedInstance().setInitialDelay(0);
        BorderLayout thisLayout = new BorderLayout();
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(thisLayout);
        {
            jTabbedPane1 = new JTabbedPane();
            getContentPane().add(jTabbedPane1, BorderLayout.CENTER);
            {
                jPanel1 = new JPanel();
                BorderLayout jPanel1Layout = new BorderLayout();
                jPanel1.setLayout(jPanel1Layout);
                jTabbedPane1.addTab("hott notes - checklist", null, jPanel1, null);
                {
                    jScrollPane1 = new JScrollPane();
                    jPanel1.add(jScrollPane1, BorderLayout.CENTER);
                    jScrollPane1.setPreferredSize(new java.awt.Dimension(612, 348));
                    {
                        checkListArea = new JTextArea();
                        jScrollPane1.setViewportView(checkListArea);
                        checkListArea.addMouseListener(new MouseAdapter() {

                            String randomColor() {
                                StringBuilder sb = new StringBuilder().append("#");
                                for (int ii = 0; ii < 6; ii++) {
                                    sb.append(RandomUtil.randomChar('a', 'b', 'c', 'd', 'f', '0', '1', '2', '3',
                                            '4', '5', '6', '7', '8', '9'));
                                }
                                return sb.toString();
                            }

                            void saveXml(Document document, File file) {
                                OutputFormat format = OutputFormat.createPrettyPrint();
                                format.setEncoding("utf-16");
                                XMLWriter writer = null;
                                try {
                                    writer = new XMLWriter(new FileWriter(file), format);
                                    writer.write(document);
                                } catch (IOException e) {
                                    JCommonUtil.handleException(e);
                                } finally {
                                    if (writer != null) {
                                        try {
                                            writer.close();
                                        } catch (IOException e) {
                                            JCommonUtil.handleException(e);
                                        }
                                    }
                                }
                            }

                            public void mouseClicked(MouseEvent evt) {
                                if (!JMouseEventUtil.buttonLeftClick(2, evt)) {
                                    return;
                                }

                                if (StringUtils.isEmpty(checkListArea.getText())) {
                                    JCommonUtil
                                            ._jOptionPane_showMessageDialog_error("checklist area is empty!");
                                    return;
                                }

                                File file = JCommonUtil._jFileChooser_selectFileOnly_saveFile();
                                if (file == null) {
                                    JCommonUtil._jOptionPane_showMessageDialog_error("file is not correct!");
                                    return;
                                }

                                //XXX
                                StringTokenizer tok = new StringTokenizer(checkListArea.getText(), "\t\n\r\f");
                                List<String> list = new ArrayList<String>();
                                String tmp = null;
                                for (; tok.hasMoreElements();) {
                                    tmp = ((String) tok.nextElement()).trim();
                                    System.out.println(tmp);
                                    list.add(tmp);
                                }
                                //XXX

                                Document document = DocumentHelper.createDocument();
                                Element rootHot = document.addElement("hottnote");
                                rootHot.addAttribute("creationtime",
                                        new Timestamp(System.currentTimeMillis()).toString());
                                rootHot.addAttribute("lastmodified",
                                        new Timestamp(System.currentTimeMillis()).toString());
                                rootHot.addAttribute("type", "checklist");
                                //appearence
                                Element appearenceE = rootHot.addElement("appearence");
                                appearenceE.addAttribute("alpha", "204");
                                Element fontE = appearenceE.addElement("font");
                                fontE.addAttribute("face", "Default");
                                fontE.addAttribute("size", "0");
                                Element styleE = appearenceE.addElement("style");
                                styleE.addElement("bg2color").addAttribute("color", randomColor());
                                styleE.addElement("bgcolor").addAttribute("color", randomColor());
                                styleE.addElement("textcolor").addAttribute("color", randomColor());
                                styleE.addElement("titlecolor").addAttribute("color", randomColor());
                                //behavior
                                rootHot.addElement("behavior");
                                //content
                                Element contentE = rootHot.addElement("content");
                                Element checklistE = contentE.addElement("checklist");
                                for (String val : list) {
                                    checklistE.addElement("item").addCDATA(val);
                                }
                                //desktop
                                Element desktopE = rootHot.addElement("desktop");
                                desktopE.addElement("position").addAttribute("x", RandomUtil.numberStr(3))
                                        .addAttribute("y", RandomUtil.numberStr(3));
                                desktopE.addElement("size").addAttribute("height", "200").addAttribute("width",
                                        "200");
                                //title
                                Element titleE = rootHot.addElement("title");
                                titleE.addCDATA(StringUtils.defaultIfEmpty(checkListTitle.getText(),
                                        DateFormatUtils.format(System.currentTimeMillis(), "dd/MM/yyyy")));

                                if (!file.getName().toLowerCase().endsWith(".hottnote")) {
                                    file = new File(file.getParentFile(), file.getName() + ".hottnote");
                                }

                                saveXml(document, file);
                                JCommonUtil._jOptionPane_showMessageDialog_info("completed!\n" + file);
                            }
                        });
                    }
                }
                {
                    checkListTitle = new JTextField();
                    checkListTitle.setToolTipText("title");
                    jPanel1.add(checkListTitle, BorderLayout.NORTH);
                }
            }
        }
        pack();
        this.setSize(633, 415);
    } catch (Exception e) {
        //add your error handling code here
        e.printStackTrace();
    }
}

From source file:hebbNet.HebbNet.java

License:Open Source License

@Override
public void exportToFile(File saveFile) {
    String extension = FrevoMain.getExtension(saveFile);
    if (extension.equals("net")) {
        System.out.println("Exporting Pajek network file to " + saveFile.getName());
        try {//  w  w w . j a va  2s.c  o m
            // Create file
            FileWriter fstream = new FileWriter(saveFile);
            BufferedWriter out = new BufferedWriter(fstream);
            out.write("*Vertices " + this.nodes);
            out.newLine();
            // input neurons
            for (int i = 1; i < input_nodes + 1; i++) {
                out.write(i + " \"I" + i + "\"");
                out.newLine();
            }
            for (int h = input_nodes + 1; h < nodes - output_nodes + 1; h++) {
                out.write(h + " \"H" + (h - input_nodes) + "\"");
                out.newLine();
            }
            int a = 1;
            for (int o = nodes - output_nodes + 1; o < nodes + 1; o++) {
                out.write(o + " \"O" + a + "\"");
                out.newLine();
                a++;
            }

            // Edges
            out.write("*Edges");
            out.newLine();
            for (int n = 0; n < input_nodes - 1; n++) {
                for (int p = n + 1; p < input_nodes; p++) {
                    if (n != p) {
                        out.write((n + 1) + " " + (p + 1) + " " + 0);
                        out.newLine();
                    }
                }
            }

            for (int n = input_nodes; n < nodes; n++) {
                for (int from = 0; from < nodes; from++) {
                    out.write((n + 1) + " " + (from + 1) + " " + weight[from][n]);
                    // out.write((n+1)+" "+(from+1)+" "+plasticity[from][n]);
                    out.newLine();
                }
            }

            // Close the output stream
            out.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    } else if (extension.equals("xml")) {
        System.out.println("Saving to XML");

        Document doc = DocumentHelper.createDocument();
        doc.addDocType("HebbNet", null,
                System.getProperty("user.dir") + "//Components//Representations//HebbNet//src//HebbNet.dtd");
        Element cnetwork = doc.addElement("HebbNet");
        this.exportToXmlElement(cnetwork);

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

        try {
            saveFile.createNewFile();
            FileWriter out = new FileWriter(saveFile);
            BufferedWriter bw = new BufferedWriter(out);
            XMLWriter wr = new XMLWriter(bw, format);
            wr.write(doc);
            wr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:hello.SampleSimpleApplication.java

License:Apache License

Document createAutoDocument() {
    Document autoDocument = DocumentHelper.createDocument();
    Element htmElAutoDocument = autoDocument.addElement("html");
    Element headElAddElement = htmElAutoDocument.addElement("head");
    addUtf8(headElAddElement);/*from  w  w w.  ja v a2 s .c  o  m*/
    htmElAutoDocument.addElement("body");
    return autoDocument;
}

From source file:hvv_admin4.state.HVV_StateKeeperXML.java

public void SaveState() {
    if (theApp.GetCurrentStep() < 20)
        return;//  ww w .  ja  v a 2  s . c  o m

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");

    try {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("SaveStateXML");

        // 
        root.addElement("CurrentStep").addText("" + theApp.GetCurrentStep());

        //?   
        root.addElement("SerialNumber").addText(theApp.GetSerial());

        //  
        root.addElement("ProcessedDeviceType").addText("" + theApp.GetProcessedDeviceType());

        // ? 
        if (theApp.GetDtmTOEnd() == null)
            root.addElement("dtmTOEnd").addText("NULL");
        else
            root.addElement("dtmTOEnd").addText(formatter.format(theApp.GetDtmTOEnd()));

        Set set = theApp.SecretSteps().entrySet();
        Iterator it = set.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();

            String strKey = (String) entry.getKey();
            Object objValue = entry.getValue();

            Element innerRoot = root.addElement("Step" + strKey);
            switch (strKey) {
            case "001":
            case "061":
            case "062":
            case "064":
            case "101":
            case "103":
            case "105":
            case "141":
            case "143":
            case "181":
                ((TechProcessStepCommon) objValue).SaveItemXML(innerRoot,
                        theApp.GetStepNameWithNum(Integer.parseInt(strKey)));
                break;

            case "021":
            case "022":
            case "041":
            case "042":
            case "043":
            case "044":
            case "102":
            case "104":
            case "106":
            case "142":
            case "144":
                ((TechProcessHvProcessInfo) objValue).SaveItemXML(innerRoot,
                        theApp.GetStepNameWithNum(Integer.parseInt(strKey)));
                break;

            case "063":
            case "121":
            case "161":
                ((TechProcessGetterInfo) objValue).SaveItemXML(innerRoot,
                        theApp.GetStepNameWithNum(Integer.parseInt(strKey)));
                break;

            case "081":
            case "182":
                ((TechProcessIgenIextProcessInfo) objValue).SaveItemXML(innerRoot,
                        theApp.GetStepNameWithNum(Integer.parseInt(strKey)));
                break;

            case "082":
            case "183":
                ((TechProcessUacProcessInfo) objValue).SaveItemXML(innerRoot,
                        theApp.GetStepNameWithNum(Integer.parseInt(strKey)));
                break;

            case "083":
            case "184":
                ((TechProcessCommentInfo) objValue).SaveItemXML(innerRoot,
                        theApp.GetStepNameWithNum(Integer.parseInt(strKey)));
                break;

            /*
            case "122":
            case "162":
            (( TechProcessHFInfo ) objValue).SaveItemXML( innerRoot); break;
            */
            }
        }
        OutputFormat format = OutputFormat.createPrettyPrint();

        //File fl = new File( m_strStateKeepFileName);
        //fl.createNewFile();
        XMLWriter writer = new XMLWriter(new FileWriter(m_strStateKeepFileName), format);

        writer.write(document);
        writer.close();
    } catch (IOException ex) {
        logger.error("IOException caught while saving state!", ex);
    }
}

From source file:hvv_constructor.FrmMainWindow.java

private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
    Document saveFile = DocumentHelper.createDocument();
    Element program = saveFile.addElement("Program");

    Set set = theApp.m_program.entrySet();
    Iterator it = set.iterator();
    while (it.hasNext()) {
        Map.Entry entry = (Map.Entry) it.next();
        int nLineNumber = (int) entry.getKey();
        JProg.JProgAStatement abstractStatement = (JProg.JProgAStatement) entry.getValue();
        Element statement = program.addElement("LineNumber").addText("" + nLineNumber);
        abstractStatement.AddXMLStatement(statement);
    }//from   w w  w. j av  a 2  s.  c om

    OutputFormat format = OutputFormat.createPrettyPrint();

    final JFileChooser fc = new JFileChooser();
    fc.setFileFilter(new MyXMLFilter());
    fc.setCurrentDirectory(new File(theApp.GetAMSRoot() + "/ReadyPrograms"));

    int returnVal = fc.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        String strFilePathName = fc.getSelectedFile().getAbsolutePath();
        if (!strFilePathName.endsWith(".xml"))
            strFilePathName += ".xml";
        File file = new File(strFilePathName);
        XMLWriter writer;
        try {
            writer = new XMLWriter(new FileWriter(file.getAbsolutePath()), format);
            writer.write(saveFile);
            writer.close();
        } catch (IOException ex) {
            logger.error("IOException: ", ex);
        }

    } else {
        logger.error(
                "   ?   ?? .");
    }

}

From source file:i.am.jiongxuan.deapk.GenerateProjectOperator.java

License:Apache License

public void generateClassPathFile() {
    Document document = DocumentHelper.createDocument();
    Element classPathElement = document.addElement("classpath");
    classPathElement.addElement("classpathentry").addAttribute("kind", "src").addAttribute("path", "src");
    classPathElement.addElement("classpathentry").addAttribute("kind", "src").addAttribute("path", "gen");
    classPathElement.addElement("classpathentry").addAttribute("kind", "con").addAttribute("path",
            "com.android.ide.eclipse.adt.ANDROID_FRAMEWORK");
    classPathElement.addElement("classpathentry").addAttribute("kind", "con").addAttribute("path",
            "com.android.ide.eclipse.adt.LIBRARIES");
    classPathElement.addElement("classpathentry").addAttribute("kind", "con")
            .addAttribute("path", "com.android.ide.eclipse.adt.DEPENDENCIES").addAttribute("exported", "true");
    classPathElement.addElement("classpathentry").addAttribute("kind", "output").addAttribute("path",
            "bin/classes");

    try {/*from w ww  .  j  a va  2  s. c  o m*/
        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(new File(mProjectFile, ".classpath")));
        xmlWriter.write(document);
        xmlWriter.close();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:i.am.jiongxuan.deapk.GenerateProjectOperator.java

License:Apache License

public void generateProjectFile() {
    Document document = DocumentHelper.createDocument();
    Element projectDescriptionElement = document.addElement("projectDescription");
    projectDescriptionElement.addElement("name").addText(getProjectNameInManifestXml());

    Element buildSpecElement = projectDescriptionElement.addElement("buildSpec");
    buildSpecElement.addElement("buildCommand").addElement("name")
            .addText("com.android.ide.eclipse.adt.ResourceManagerBuilder");
    buildSpecElement.addElement("buildCommand").addElement("name")
            .addText("com.android.ide.eclipse.adt.PreCompilerBuilder");
    buildSpecElement.addElement("buildCommand").addElement("name").addText("org.eclipse.jdt.core.javabuilder");
    buildSpecElement.addElement("buildCommand").addElement("name")
            .addText("com.android.ide.eclipse.adt.ApkBuilder");

    Element naturesElement = projectDescriptionElement.addElement("natures");
    naturesElement.addElement("nature").addText("com.android.ide.eclipse.adt.AndroidNature");
    naturesElement.addElement("nature").addText("org.eclipse.jdt.core.javanature");

    try {//from  ww  w  .ja  v a2  s .  c o  m
        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(new File(mProjectFile, ".project")));
        xmlWriter.write(document);
        xmlWriter.close();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:io.mashin.oep.model.Workflow.java

License:Open Source License

@Override
public void write(org.dom4j.Element parent) {
    Document document = parent.getDocument();
    parent.detach();/*  ww  w  .  ja v  a2s.  c om*/

    Element rootElement = document.addElement("workflow-app");
    Element graphicalInfoElement = DocumentHelper.createElement("workflow");

    XMLWriteUtils.writeWorkflowSchemaVersion(getSchemaVersion(), rootElement);
    XMLWriteUtils.writeSLAVersion(this, rootElement);
    XMLWriteUtils.writeTextPropertyAsAttribute(name, rootElement, "name");
    XMLWriteUtils.writePropertiesCollection(parameters, rootElement, "parameters", "property");
    XMLWriteUtils.writeGlobalProperty(global, rootElement);
    XMLWriteUtils.writeCredentialsCollection(credentials, rootElement);

    startNode.write(rootElement);
    for (Node node : nodes) {
        if (!(node.equals(startNode) || node.equals(endNode))) {
            node.write(rootElement);
        }
        graphicalInfoElement.addElement("node").addAttribute("name", node.getName())
                .addAttribute("x", node.getPosition().x + "").addAttribute("y", node.getPosition().y + "");
    }
    endNode.write(rootElement);

    XMLWriteUtils.writeSLAProperty(this, sla, rootElement);

    Comment graphicalInfoNode = null;
    try {
        StringWriter stringWriter = new StringWriter();
        XMLWriter writer = new XMLWriter(stringWriter, OutputFormat.createPrettyPrint());
        writer.write(graphicalInfoElement);
        writer.flush();
        graphicalInfoNode = DocumentHelper.createComment(stringWriter.toString());
    } catch (Exception e) {
        graphicalInfoNode = DocumentHelper.createComment(graphicalInfoElement.asXML());
    }
    document.add(graphicalInfoNode);
}

From source file:io.mashin.oep.ui.editor.WorkflowEditor.java

License:Open Source License

private String writeModel() {
    try {//  w  ww  . j  a va2s  .co  m
        Document document = DocumentHelper.createDocument();
        workflow.write(document.addElement("dummy"));
        OutputFormat format = OutputFormat.createPrettyPrint();
        StringWriter stringWriter = new StringWriter();
        XMLWriter writer = new XMLWriter(stringWriter, format);
        writer.write(document);
        stringWriter.flush();
        String hpdl = stringWriter.toString();
        hpdl = XMLUtils.schemaVersionToXmlns(hpdl);
        //hpdl = XMLUtils.slaVersionToXmlnsSLA(hpdl);
        return hpdl;
    } catch (IOException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:io.selendroid.util.JsonXmlUtil.java

License:Apache License

private static Document buildXmlDoc(JSONObject tree) {
    Document document = DocumentHelper.createDocument();
    document.setXMLEncoding("UTF-8");
    Element root = document.addElement("views");
    buildXmlNode(tree, root);//from w w  w  .ja v a 2s. c  om
    return document;
}