Example usage for org.dom4j.io OutputFormat setLineSeparator

List of usage examples for org.dom4j.io OutputFormat setLineSeparator

Introduction

In this page you can find the example usage for org.dom4j.io OutputFormat setLineSeparator.

Prototype

public void setLineSeparator(String separator) 

Source Link

Document

This will set the new-line separator.

Usage

From source file:org.ecocean.servlet.WriteOutScanTask.java

License:Open Source License

public boolean i3sWriteThis(Shepherd myShepherd, MatchObject[] matches, String num, String newEncDate,
        String newEncShark, String newEncSize, boolean rightSide, double cutoff, String context) {
    try {//  www  .  j a va  2s.  c o m

        System.out.println("scanWorkItemResultsHandler: Prepping to write I3S XML file for encounter " + num);

        //now setup the XML write for the encounter
        //int resultsSize=results.size();

        Arrays.sort(matches, new NewI3SMatchComparator());
        StringBuffer resultsXML = new StringBuffer();
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("matchSet");
        root.addAttribute("scanDate", (new java.util.Date()).toString());
        //System.out.println("Total num matches for I3S printing: "+matches.length);

        int numMatches = matches.length;

        //hard limit this to 100 matches...no human really goes beyond this...
        if (numMatches > 100)
            numMatches = 100;

        for (int i = 0; i < numMatches; i++) {
            try {
                //System.out.println();
                MatchObject mo = matches[i];
                //System.out.println("I3S match value: "+mo.getI3SMatchValue());
                if ((mo.getI3SMatchValue() > 0.001) && (mo.getI3SMatchValue() <= 2.0)) {
                    Element match = root.addElement("match");
                    String finalscore = (new Double(mo.getI3SMatchValue())).toString();
                    if (finalscore.length() > 7) {
                        finalscore = finalscore.substring(0, 6);
                    }
                    match.addAttribute("finalscore", finalscore);
                    match.addAttribute("evaluation", mo.getEvaluation());

                    Element enc = match.addElement("encounter");
                    enc.addAttribute("number", mo.getEncounterNumber());
                    enc.addAttribute("date", mo.getDate());

                    if (mo.getSex() != null) {
                        enc.addAttribute("sex", mo.getSex());
                    } else {
                        enc.addAttribute("sex", "unknown");
                    }

                    enc.addAttribute("assignedToShark", mo.getIndividualName());
                    enc.addAttribute("size", (new Double(mo.getSize())).toString());

                    //get the Map
                    Vector map = mo.getMap2();
                    int mapSize = map.size();
                    Encounter e1 = myShepherd.getEncounter(mo.getEncounterNumber());
                    for (int f = 0; f < mapSize; f++) {
                        Pair tempPair = (com.reijns.I3S.Pair) map.get(f);
                        int M1 = tempPair.getM1();
                        ArrayList<SuperSpot> spts = new ArrayList<SuperSpot>();
                        if (rightSide) {
                            spts = e1.getRightSpots();
                        } else {
                            spts = e1.getSpots();
                        }
                        //System.out.println("scanWorkItemResultsHandler: I3S spots: "+spts.size()+" vs mapSize: "+mapSize);
                        Element spot = enc.addElement("spot");
                        spot.addAttribute("x",
                                (new Double(spts.get(M1).getTheSpot().getCentroidX())).toString());
                        spot.addAttribute("y",
                                (new Double(spts.get(M1).getTheSpot().getCentroidY())).toString());
                    }

                    Element enc2 = match.addElement("encounter");
                    enc2.addAttribute("number", num);
                    enc2.addAttribute("date", newEncDate);
                    enc2.addAttribute("sex", mo.getNewSex());
                    enc2.addAttribute("assignedToShark", newEncShark);
                    enc2.addAttribute("size", newEncSize);

                    //reset the Iterator
                    Encounter e2 = myShepherd.getEncounter(num);
                    for (int g = 0; g < mapSize; g++) {
                        Pair tempPair = (com.reijns.I3S.Pair) map.get(g);
                        int M2 = tempPair.getM2();
                        ArrayList<SuperSpot> spts = new ArrayList<SuperSpot>();
                        if (rightSide) {
                            spts = e2.getRightSpots();
                        } else {
                            spts = e2.getSpots();
                        }
                        Element spot = enc2.addElement("spot");
                        //System.out.println("scanWorkItemResultsHandler: I3S next spots: "+spts.size()+" vs mapSize: "+mapSize);
                        spot.addAttribute("x",
                                (new Double(spts.get(M2).getTheSpot().getCentroidX())).toString());
                        spot.addAttribute("y",
                                (new Double(spts.get(M2).getTheSpot().getCentroidY())).toString());
                    }

                }
            } catch (NullPointerException npe) {
                npe.printStackTrace();
            }
        }

        //prep for writing out the XML

        //in case this is a right-side scan, change file name to save to
        String fileAddition = "";
        if (rightSide) {
            fileAddition = "Right";
        }

        //setup data dir
        String rootWebappPath = getServletContext().getRealPath("/");
        File webappsDir = new File(rootWebappPath).getParentFile();
        File shepherdDataDir = new File(webappsDir, CommonConfiguration.getDataDirectoryName(context));
        //if(!shepherdDataDir.exists()){shepherdDataDir.mkdirs();}
        File encountersDir = new File(shepherdDataDir.getAbsolutePath() + "/encounters");
        //if(!encountersDir.exists()){encountersDir.mkdirs();}

        //File file=new File((new File(".")).getCanonicalPath()+File.separator+"webapps"+File.separator+"ROOT"+File.separator+"encounters"+File.separator+num+File.separator+"lastFull"+fileAddition+"I3SScan.xml");
        File file = new File(Encounter.dir(shepherdDataDir, num) + "/lastFull" + fileAddition + "I3SScan.xml");

        FileWriter mywriter = new FileWriter(file);
        org.dom4j.io.OutputFormat format = org.dom4j.io.OutputFormat.createPrettyPrint();
        format.setLineSeparator(System.getProperty("line.separator"));
        org.dom4j.io.XMLWriter writer = new org.dom4j.io.XMLWriter(mywriter, format);
        writer.write(document);
        writer.close();
        System.out.println("writeOutScanTask: Successful I3S write.");
        return true;
    } catch (Exception e) {
        System.out.println("writeOutScanTask: Failed to write out I3S results!");
        e.printStackTrace();
        return false;
    }

}

From source file:utils.ComponentCreator.java

License:Open Source License

public ComponentCreator() {
    super("FREVO Component Creator");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    this.setSize(300, 300);
    this.setLocationRelativeTo(null);
    this.setResizable(false);

    typebox.setMaximumSize(new Dimension(250, 20));
    typebox.setSelectedIndex(-1);// select nothing
    typebox.addActionListener(new ActionListener() {

        @Override/*from   w  w w.j a v a 2s  . c om*/
        public void actionPerformed(ActionEvent e) {
            istyped = true;
            check();
        }
    });

    namefield.setMinimumSize(new Dimension(250, 23));
    namefield.setMaximumSize(new Dimension(250, 23));
    DocumentListener myListener = new CheckListener(2);

    namefield.getDocument().addDocumentListener(myListener);

    packagefield.setMinimumSize(new Dimension(250, 23));
    packagefield.setMaximumSize(new Dimension(250, 23));
    DocumentListener packageListener = new CheckListener(4);

    packagefield.getDocument().addDocumentListener(packageListener);

    descriptionArea.setMinimumSize(new Dimension(250, 100));
    descriptionArea.setMaximumSize(new Dimension(250, 100));
    descriptionArea.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    descriptionArea.setLineWrap(true);
    // descriptionArea.setText("Enter component description");
    DocumentListener descListener = new CheckListener(3);
    descriptionArea.getDocument().addDocumentListener(descListener);

    GroupLayout layout = new GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setAutoCreateGaps(true);
    layout.setAutoCreateContainerGaps(true);

    // frame layout
    layout.setVerticalGroup(layout.createSequentialGroup().addComponent(typelabel).addComponent(typebox)
            .addComponent(namelabel).addComponent(namefield).addComponent(packagelabel)
            .addComponent(packagefield).addComponent(descriptionlabel).addComponent(descriptionArea)
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER).addComponent(createbutton)));

    layout.setHorizontalGroup(layout.createParallelGroup()
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER).addComponent(typelabel)
                    .addComponent(typebox).addComponent(namelabel).addComponent(namefield)
                    .addComponent(packagelabel).addComponent(packagefield).addComponent(descriptionlabel)
                    .addComponent(descriptionArea))
            .addGroup(layout.createSequentialGroup().addComponent(createbutton)));

    createbutton.setEnabled(false);
    pack();

    createbutton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // create files
            String compname = namefield.getText();
            String dirname, dir = "";
            String packagename = packagefield.getText();
            String packagenamedir = packagename.replace('.', '/');

            int type = typebox.getSelectedIndex();
            File xmlfile = null;
            File classfile = null;

            try {
                // create component directory
                switch (type) {
                case 0: {
                    dir = "Components/Problems";
                    dirname = "./" + dir + "/" + compname;
                    // create component dir
                    new File(dirname).mkdir();

                    // create component package dir
                    new File("./" + dir + "/" + compname + "/" + packagenamedir).mkdirs();

                    // copy xml file to component dir
                    xmlfile = new File("./" + dir + "/" + compname + ".xml");
                    copyFile(new File("./src/utils/problemxml.template"), xmlfile);

                    // copy class file to package dir
                    classfile = new File(
                            "./" + dir + "/" + compname + "/" + packagenamedir + "/" + compname + ".java");
                    copyFile(new File("./src/utils/singleproblemclass.template"), classfile);
                    break;
                }
                case 1: {
                    dir = "Components/Problems";
                    dirname = "./" + dir + "/" + compname;
                    // create component dir
                    new File(dirname).mkdir();
                    // create component package dir
                    new File("./" + dir + "/" + compname + "/" + packagenamedir).mkdirs();

                    // copy xml file to component dir
                    xmlfile = new File("./" + dir + "/" + compname + ".xml");
                    copyFile(new File("./src/utils/problemxml.template"), xmlfile);

                    // copy class file to package dir
                    classfile = new File(
                            "./" + dir + "/" + compname + "/" + packagenamedir + "/" + compname + ".java");
                    copyFile(new File("./src/utils/multiproblemclass.template"), classfile);
                    break;
                }
                case 2: {
                    dir = "Components/Methods";
                    dirname = "./" + dir + "/" + compname;
                    // create component dir
                    new File(dirname).mkdir();
                    // create component package dir
                    new File("./" + dir + "/" + compname + "/" + packagenamedir).mkdirs();

                    // copy xml file to component dir
                    xmlfile = new File("./" + dir + "/" + compname + ".xml");
                    copyFile(new File("./src/utils/methodxml.template"), xmlfile);

                    // copy class file to package dir
                    classfile = new File(
                            "./" + dir + "/" + compname + "/" + packagenamedir + "/" + compname + ".java");
                    copyFile(new File("./src/utils/methodclass.template"), classfile);
                    break;
                }
                case 3: {
                    dir = "Components/Representations";
                    dirname = "./" + dir + "/" + compname;
                    // create component dir
                    new File(dirname).mkdir();
                    // create component package dir
                    new File("./" + dir + "/" + compname + "/" + packagenamedir).mkdirs();

                    // copy xml file to component dir
                    xmlfile = new File("./" + dir + "/" + compname + ".xml");
                    copyFile(new File("./src/utils/representationxml.template"), xmlfile);

                    // copy class file to package dir
                    classfile = new File(
                            "./" + dir + "/" + compname + "/" + packagenamedir + "/" + compname + ".java");
                    copyFile(new File("./src/utils/representationclass.template"), classfile);
                    break;
                }
                case 4: {
                    dir = "Components/Rankings";
                    dirname = "./" + dir + "/" + compname;
                    // create component dir
                    new File(dirname).mkdir();
                    // create component package dir
                    new File("./" + dir + "/" + compname + "/" + packagenamedir).mkdirs();

                    // copy xml file to component dir
                    xmlfile = new File("./" + dir + "/" + compname + ".xml");
                    copyFile(new File("./src/utils/rankingxml.template"), xmlfile);

                    // copy class file to package dir
                    classfile = new File(
                            "./" + dir + "/" + compname + "/" + packagenamedir + "/" + compname + ".java");
                    copyFile(new File("./src/utils/rankingclass.template"), classfile);
                    break;
                }
                }
                // replace TEMP in xml
                BufferedReader reader = new BufferedReader(new FileReader(xmlfile));
                String line = "", oldtext = "";
                while ((line = reader.readLine()) != null) {
                    oldtext += line + "\r\n";
                }
                reader.close();

                String newtext = oldtext.replaceAll("TEMPCLASSDIR", compname + "/" + packagename);
                newtext = newtext.replaceAll("TEMP", compname);
                newtext = newtext.replaceAll("TDESC", descriptionArea.getText());
                newtext = newtext.replaceAll("PACKAGE", packagename);

                FileWriter writer = new FileWriter(xmlfile);
                writer.write(newtext);
                writer.close();

                // replace TEMP in class
                reader = new BufferedReader(new FileReader(classfile));
                line = "";
                oldtext = "";
                while ((line = reader.readLine()) != null) {
                    oldtext += line + "\r\n";
                }
                reader.close();

                newtext = oldtext.replaceAll("TEMP", compname);
                newtext = newtext.replaceAll("PACKAGENAME", packagename);

                writer = new FileWriter(classfile);
                writer.write(newtext);
                writer.close();

                // Update classpath xml
                File classpath = new File(FrevoMain.getInstallDirectory() + File.separator + ".classpath");

                if (classpath.exists()) {
                    SAXReader xmlReader = new SAXReader(false);
                    Document doc = xmlReader.read(classpath);

                    doc.getRootElement().addElement("classpathentry").addAttribute("kind", "src")
                            .addAttribute("output", dir + "/" + compname)
                            .addAttribute("path", dir + "/" + compname);

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

                    FileWriter out = new FileWriter(classpath);
                    BufferedWriter bw = new BufferedWriter(out);
                    XMLWriter wr = new XMLWriter(bw, format);
                    wr.write(doc);
                    wr.close();
                }

            } catch (Exception ex) {
                ex.printStackTrace();
            }

            // TODO compile newly created classfile

            JOptionPane.showMessageDialog(ComponentCreator.this, "Component " + compname
                    + " has been created! Please refresh your project in Eclipse for the changes to take effect!",
                    "Done!", JOptionPane.DEFAULT_OPTION);
            // add to classpath
            ComponentCreator.this.dispose();
        }
    });

}