Example usage for org.jdom2 Document getRootElement

List of usage examples for org.jdom2 Document getRootElement

Introduction

In this page you can find the example usage for org.jdom2 Document getRootElement.

Prototype

public Element getRootElement() 

Source Link

Document

This will return the root Element for this Document

Usage

From source file:de.tor.tribes.control.GenericManager.java

License:Apache License

public void saveElements(String pFile) {
    Document doc = JDomUtils.createDocument();
    logger.debug("Starting saving");
    doc.getRootElement().addContent(getExportData(Arrays.asList(getGroups())));
    logger.debug("Writing file {}", pFile);
    JDomUtils.saveDocument(doc, pFile);//from   w  w w  . j  a v a 2  s .  co m
    logger.debug("Finished");
}

From source file:de.tor.tribes.ui.windows.DSWorkbenchMainFrame.java

License:Apache License

private void fireExportEvent(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fireExportEvent
    if (evt.getSource() == jExportButton) {
        //do export
        logger.debug("Building export data");

        List<String> plansToExport = new LinkedList<>();
        for (int i = 0; i < jAttackExportTable.getRowCount(); i++) {
            String plan = (String) jAttackExportTable.getValueAt(i, 0);
            Boolean export = (Boolean) jAttackExportTable.getValueAt(i, 1);
            if (export) {
                logger.debug("Selecting attack plan '" + plan + "' to export list");
                plansToExport.add(plan);
            }//from   www .  j a v a 2s  .co m

        }

        List<String> setsToExport = new LinkedList<>();
        for (int i = 0; i < jMarkerSetExportTable.getRowCount(); i++) {
            String set = (String) jMarkerSetExportTable.getValueAt(i, 0);
            Boolean export = (Boolean) jMarkerSetExportTable.getValueAt(i, 1);
            if (export) {
                logger.debug("Selecting marker set '" + set + "' to export list");
                setsToExport.add(set);
            }

        }
        List<String> reportsToExport = new LinkedList<>();
        for (int i = 0; i < jReportSetExportTable.getRowCount(); i++) {
            String set = (String) jReportSetExportTable.getValueAt(i, 0);
            Boolean export = (Boolean) jReportSetExportTable.getValueAt(i, 1);
            if (export) {
                logger.debug("Selecting report set '" + set + "' to export list");
                reportsToExport.add(set);
            }
        }

        List<String> troopSetsToExport = new LinkedList<>();
        for (int i = 0; i < jTroopSetExportTable.getRowCount(); i++) {
            String set = (String) jTroopSetExportTable.getValueAt(i, 0);
            Boolean export = (Boolean) jTroopSetExportTable.getValueAt(i, 1);
            if (export) {
                logger.debug("Selecting troop set '" + set + "' to export list");
                troopSetsToExport.add(set);
            }
        }
        List<String> noteSetsToExport = new LinkedList<>();
        for (int i = 0; i < jNoteSetExportTable.getRowCount(); i++) {
            String set = (String) jNoteSetExportTable.getValueAt(i, 0);
            Boolean export = (Boolean) jNoteSetExportTable.getValueAt(i, 1);
            if (export) {
                logger.debug("Selecting note set '" + set + "' to export list");
                noteSetsToExport.add(set);
            }
        }
        boolean needExport = false;
        needExport = !plansToExport.isEmpty();
        needExport |= !setsToExport.isEmpty();
        needExport |= !reportsToExport.isEmpty();
        needExport |= jExportTags.isSelected();
        needExport |= !troopSetsToExport.isEmpty();
        needExport |= jExportForms.isSelected();
        needExport |= !noteSetsToExport.isEmpty();
        needExport |= jExportVillageInformation.isSelected();
        needExport |= jExportFarminfos.isSelected();
        needExport |= jExportSOS.isSelected();
        needExport |= jExportSplits.isSelected();
        needExport |= jExportStdAttacks.isSelected();

        if (!needExport) {
            JOptionPaneHelper.showWarningBox(jExportDialog, "Keine Daten fr den Export gewhlt", "Export");
            return;

        }
        String dir = GlobalOptions.getProperty("screen.dir");
        if (dir == null) {
            dir = ".";
        }

        JFileChooser chooser;
        try {
            chooser = new JFileChooser(dir);
        } catch (Exception e) {
            JOptionPaneHelper.showErrorBox(this,
                    "Konnte Dateiauswahldialog nicht ffnen.\nMglicherweise verwendest du Windows Vista. Ist dies der Fall, beende DS Workbench, klicke mit der rechten Maustaste auf DSWorkbench.exe,\n"
                            + "whle 'Eigenschaften' und deaktiviere dort unter 'Kompatibilitt' den Windows XP Kompatibilittsmodus.",
                    "Fehler");
            return;
        }
        chooser.setDialogTitle("Datei auswhlen");
        chooser.setSelectedFile(new File("export.xml"));
        chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {

            @Override
            public boolean accept(File f) {
                return (f != null) && (f.isDirectory() || f.getName().endsWith(".xml"));

            }

            @Override
            public String getDescription() {
                return "*.xml";
            }
        });
        int ret = chooser.showSaveDialog(jExportDialog);
        if (ret == JFileChooser.APPROVE_OPTION) {
            try {
                File f = chooser.getSelectedFile();
                String file = f.getCanonicalPath();
                if (!file.endsWith(".xml")) {
                    file += ".xml";
                }

                File target = new File(file);
                if (target.exists()) {
                    if (JOptionPaneHelper.showQuestionConfirmBox(jExportDialog,
                            "Bestehende Datei berschreiben?", "Export", "Nein",
                            "Ja") == JOptionPane.NO_OPTION) {
                        return;
                    }
                }

                Document doc = JDomUtils.createDocument();
                Element backup = doc.getRootElement();
                if (!plansToExport.isEmpty()) {
                    backup.addContent(AttackManager.getSingleton().getExportData(plansToExport));
                }
                if (!setsToExport.isEmpty()) {
                    backup.addContent(MarkerManager.getSingleton().getExportData(setsToExport));
                }
                if (!reportsToExport.isEmpty()) {
                    backup.addContent(ReportManager.getSingleton().getExportData(reportsToExport));
                }
                if (jExportTags.isSelected()) {
                    backup.addContent(TagManager.getSingleton().getExportData(null));
                }

                if (!troopSetsToExport.isEmpty()) {
                    backup.addContent(TroopsManager.getSingleton().getExportData(troopSetsToExport));
                }

                if (jExportForms.isSelected()) {
                    backup.addContent(FormManager.getSingleton().getExportData(null));
                }

                if (!noteSetsToExport.isEmpty()) {
                    backup.addContent(NoteManager.getSingleton().getExportData(noteSetsToExport));
                }

                if (jExportVillageInformation.isSelected()) {
                    backup.addContent(KnownVillageManager.getSingleton().getExportData(null));
                }

                if (jExportFarminfos.isSelected()) {
                    backup.addContent(FarmManager.getSingleton().getExportData(null));
                }

                if (jExportSOS.isSelected()) {
                    backup.addContent(SOSManager.getSingleton().getExportData(null));
                }

                if (jExportSplits.isSelected()) {
                    backup.addContent(SplitSetHelper.getExportData());
                }

                if (jExportStdAttacks.isSelected()) {
                    backup.addContent(StandardAttackManager.getSingleton().getExportData(null));
                }

                logger.debug("Writing data to disk");
                JDomUtils.saveDocument(doc, file);
                logger.debug("Export finished successfully");
                JOptionPaneHelper.showInformationBox(jExportDialog, "Export erfolgreich beendet.", "Export");
            } catch (Exception e) {
                logger.error("Failed to export data", e);
                JOptionPaneHelper.showErrorBox(this, "Export fehlgeschlagen.", "Export");
            }

        } else {
            //cancel pressed
            return;
        }

    }
    jExportDialog.setAlwaysOnTop(false);
    jExportDialog.setVisible(false);
}

From source file:de.tor.tribes.ui.windows.DSWorkbenchMainFrame.java

License:Apache License

private String performImport(File pSource, String pExtension) {
    int importedNum[] = new int[20];
    Element data;//from  ww  w  .j  av a  2 s .co m
    try {
        Document doc = JDomUtils.getDocument(pSource);
        data = doc.getRootElement();
    } catch (Exception e) {
        logger.error("Es ist ein Fehler aufgetreten", e);
        return ("Fehler " + e.getMessage());
    }
    importedNum[0] = AttackManager.getSingleton().importData(data, pExtension);
    importedNum[1] = StandardAttackManager.getSingleton().importData(data, pExtension);
    importedNum[2] = FarmManager.getSingleton().importData(data, pExtension);
    importedNum[3] = FormManager.getSingleton().importData(data, pExtension);
    importedNum[4] = MarkerManager.getSingleton().importData(data, pExtension);
    importedNum[5] = NoteManager.getSingleton().importData(data, pExtension);
    importedNum[6] = ReportManager.getSingleton().importData(data, pExtension);
    importedNum[7] = SOSManager.getSingleton().importData(data, pExtension);
    importedNum[8] = TagManager.getSingleton().importData(data, pExtension);
    importedNum[9] = TroopsManager.getSingleton().importData(data, null);
    importedNum[10] = KnownVillageManager.getSingleton().importData(data, pExtension);
    importedNum[11] = SplitSetHelper.importData(data, pExtension);

    String names[] = new String[] { "Angriffe", "Standard-Angriffe", "Farmen", "Formen", "Markierungen",
            "Notizen", "Berichte", "SOS-Infos", "Gruppen", "Truppen", "Dorfinfos", "Splits" };
    boolean allOk = true;
    int sum = 0;
    for (int i = 0; i < names.length; i++) {
        if (importedNum[i] < 0) {
            allOk = false;
            sum += (-1) * importedNum[i] - 1;
        } else {
            sum += importedNum[i];
        }
    }

    StringBuilder message = new StringBuilder();
    message.append("Import ").append(allOk ? "erfolgreich " : "").append("beendet.\n");

    for (int i = 0; i < names.length; i++) {
        if (importedNum[i] < 0) {
            message.append("Trotz fehler ");
        }
        message.append(importedNum[i]).append(" ").append(names[i]).append(" erfolgreich eingelesen\n");
    }
    return message.toString();
}

From source file:de.tor.tribes.ui.windows.DSWorkbenchMainFrame.java

License:Apache License

@Override
public void run() {
    try {//from  w  w  w .j  ava  2  s . c  o m
        logger.debug("Starting backup");
        Document doc = JDomUtils.createDocument();
        Element backup = doc.getRootElement();
        logger.debug(" - Backing up attacks");
        backup.addContent(AttackManager.getSingleton()
                .getExportData(Arrays.asList(AttackManager.getSingleton().getGroups())));
        logger.debug(" - Backing up std-attacks");
        backup.addContent(StandardAttackManager.getSingleton().getExportData(null));
        logger.debug(" - Backing up farms");
        backup.addContent(FarmManager.getSingleton().getExportData(null));
        logger.debug(" - Backing up forms");
        backup.addContent(FormManager.getSingleton().getExportData(null));
        logger.debug(" - Backing up markers");
        backup.addContent(MarkerManager.getSingleton()
                .getExportData(Arrays.asList(MarkerManager.getSingleton().getGroups())));
        logger.debug(" - Backing up notes");
        backup.addContent(NoteManager.getSingleton()
                .getExportData(Arrays.asList(NoteManager.getSingleton().getGroups())));
        logger.debug(" - Backing up reports");
        backup.addContent(ReportManager.getSingleton()
                .getExportData(Arrays.asList(ReportManager.getSingleton().getGroups())));
        logger.debug(" - Backing up sos-infos");
        backup.addContent(SOSManager.getSingleton().getExportData(null));
        logger.debug(" - Backing up tags");
        backup.addContent(TagManager.getSingleton().getExportData(null));
        logger.debug(" - Backing up troops");
        backup.addContent(TroopsManager.getSingleton()
                .getExportData(Arrays.asList(TroopsManager.getSingleton().getGroups())));
        logger.debug(" - Backing up known-villages");
        backup.addContent(KnownVillageManager.getSingleton().getExportData(null));
        logger.debug(" - Backing up split-sets");
        backup.addContent(SplitSetHelper.getExportData());

        logger.debug("Writing backup data to disk");
        JDomUtils.saveDocument(doc, GlobalOptions.getSelectedProfile().getProfileDirectory() + "/backup.xml");
        logger.debug("Backup finished successfully");
    } catch (Exception e) {
        logger.error("Failed to create backup", e);
    }

}

From source file:de.tor.tribes.util.BuildingSettings.java

public static boolean loadSettings(String pServerID) {
    Arrays.fill(MAX_LEVEL, -1);/*from   w w w . ja v a2  s .  c om*/
    Arrays.fill(MIN_LEVEL, -1);

    Arrays.fill(BUILD_WOOD, -1);
    Arrays.fill(BUILD_STONE, -1);
    Arrays.fill(BUILD_IRON, -1);
    Arrays.fill(BUILD_POP, -1);
    Arrays.fill(BUILD_TIME, -1);
    Arrays.fill(BUILD_WOOD_FACTOR, Double.NaN);
    Arrays.fill(BUILD_STONE_FACTOR, Double.NaN);
    Arrays.fill(BUILD_IRON_FACTOR, Double.NaN);
    Arrays.fill(BUILD_POP_FACTOR, Double.NaN);
    Arrays.fill(BUILD_TIME_FACTOR, Double.NaN);

    try {
        logger.debug("Loading server buildings");
        String buildingsPath = Constants.SERVER_DIR + "/" + pServerID + "/buildings.xml";

        logger.debug("Parse buildings from '" + buildingsPath + "'");
        Document d = JDomUtils.getDocument(new File(buildingsPath));

        for (Element b : d.getRootElement().getChildren()) {
            String name = b.getName();
            int index = ArrayUtils.indexOf(BUILDING_NAMES, name);

            if (index < 0) {
                logger.warn("Found unknown Building {}", name);
                continue;
            }

            try {
                MAX_LEVEL[index] = Integer.parseInt(b.getChildTextTrim("max_level"));
                MIN_LEVEL[index] = Integer.parseInt(b.getChildTextTrim("min_level"));

                BUILD_WOOD[index] = Integer.parseInt(b.getChildTextTrim("wood"));
                BUILD_STONE[index] = Integer.parseInt(b.getChildTextTrim("stone"));
                BUILD_IRON[index] = Integer.parseInt(b.getChildTextTrim("iron"));
                BUILD_POP[index] = Integer.parseInt(b.getChildTextTrim("pop"));
                BUILD_TIME[index] = Integer.parseInt(b.getChildTextTrim("build_time"));
                BUILD_WOOD_FACTOR[index] = Double.parseDouble(b.getChildTextTrim("wood_factor"));
                BUILD_STONE_FACTOR[index] = Double.parseDouble(b.getChildTextTrim("stone_factor"));
                BUILD_IRON_FACTOR[index] = Double.parseDouble(b.getChildTextTrim("iron_factor"));
                BUILD_POP_FACTOR[index] = Double.parseDouble(b.getChildTextTrim("pop_factor"));
                BUILD_TIME_FACTOR[index] = Double.parseDouble(b.getChildTextTrim("build_time_factor"));
            } catch (Exception e) {
                logger.error("Got an excetion during reading of buildings", e);
            }
        }
    } catch (Exception e) {
        logger.error("Failed to load buildings", e);
        return false;
    }
    logger.info(Arrays.toString(MAX_LEVEL));
    logger.info(Arrays.toString(MIN_LEVEL));
    logger.info(Arrays.toString(BUILD_WOOD));
    logger.info(Arrays.toString(BUILD_STONE));
    logger.info(Arrays.toString(BUILD_IRON));
    logger.info(Arrays.toString(BUILD_POP));
    logger.info(Arrays.toString(BUILD_TIME));
    logger.info(Arrays.toString(BUILD_WOOD_FACTOR));
    logger.info(Arrays.toString(BUILD_STONE_FACTOR));
    logger.info(Arrays.toString(BUILD_IRON_FACTOR));
    logger.info(Arrays.toString(BUILD_POP_FACTOR));
    logger.info(Arrays.toString(BUILD_TIME_FACTOR));
    logger.debug("Successfully read buildings for server '" + pServerID + "'");
    return true;
}

From source file:de.tor.tribes.util.xml.JDomUtils.java

License:Apache License

public static List<Element> getNodes(Document document, String path) {
    return getList(document.getRootElement(), path);
}

From source file:de.tor.tribes.util.xml.JDomUtils.java

License:Apache License

/** Get the value of the available first node.
 * @return the value of the first available node
 *///from ww  w .  java 2s. com
public static String getNodeValue(Document document, String path) {
    return getNodeValue(document.getRootElement(), path);
}

From source file:de.tor.tribes.util.xml.JDomUtils.java

License:Apache License

public static String toShortString(Element pElm) {
    Document doc = createDocument();
    doc.getRootElement().addContent(pElm);
    return toShortString(doc);
}

From source file:de.tsystems.mms.apm.performancesignature.viewer.rest.DashboardXMLReader.java

License:Apache License

void parseXML(final String xml) throws IOException, JDOMException {
    Document jdomDoc = useSAXParser(xml);
    Element root = jdomDoc.getRootElement();
    List<Element> xmlDashboardReports = root.getChildren("dashboardReport");
    for (Element xmlDashboardReport : xmlDashboardReports) {
        DashboardReport dashboardReport = new DashboardReport(xmlDashboardReport.getChildText("name"));
        List<Element> chartDashletElements = xmlDashboardReport.getChildren("chartDashlet");
        for (Element chartDashletElement : chartDashletElements) {
            ChartDashlet chartDashlet = new ChartDashlet(chartDashletElement);

            List<Element> measureElements = chartDashletElement.getChildren("measure");
            for (Element measureElement : measureElements) {
                Measure measure = new Measure(measureElement);

                List<Element> measurementElements = measureElement.getChildren("measurement");
                for (Element mearsurementElement : measurementElements) {
                    Measurement measurement = new Measurement(mearsurementElement);
                    measure.addMeasurement(measurement);
                }/*w  ww  .j  a  va  2  s.c  o m*/
                chartDashlet.addMeasure(measure);
            }
            dashboardReport.addChartDashlet(chartDashlet);
        }

        List<Element> incidentElements = xmlDashboardReport.getChildren("incident");
        for (Element incidentElement : incidentElements) {
            IncidentChart incidentChart = new IncidentChart(incidentElement);

            List<Element> violationElements = incidentElement.getChildren("violation");
            for (Element violationElement : violationElements) {
                IncidentViolation incidentViolation = new IncidentViolation(violationElement);
                incidentChart.add(incidentViolation);
            }
            dashboardReport.addIncident(incidentChart);
        }

        dashboardReports.add(dashboardReport);
    }
}

From source file:de.unigoettingen.sub.search.opac.ConfigOpacCatalogue.java

License:Open Source License

public Node executeBeautifier(Node myHitlist) {
    /* Ausgabe des Opac-Ergebnissen in Datei */

    //        if (!ConfigurationHelper.getInstance().getDebugFolder().equals("") && Files.isWritable(Paths.get(ConfigurationHelper.getInstance().getDebugFolder()))) {
    //            debugMyNode(myHitlist, ConfigurationHelper.getInstance().getDebugFolder() + "/opacBeautifyBefore.xml");
    //        }/*w ww .  j  a v a 2 s . co m*/

    /*
     * --------------------- aus dem Dom-Node ein JDom-Object machen -------------------
     */
    Document doc = new DOMBuilder().build(myHitlist.getOwnerDocument());

    /*
     * --------------------- Im JDom-Object alle Felder durchlaufen und die notwendigen Ersetzungen vornehmen -------------------
     */
    /* alle Records durchlaufen */
    List<Element> elements = doc.getRootElement().getChildren();
    for (Element el : elements) {
        // Element el = (Element) it.next();
        /* in jedem Record den Beautifier anwenden */
        executeBeautifierForElement(el);
    }

    /*
     * --------------------- aus dem JDom-Object wieder ein Dom-Node machen -------------------
     */
    DOMOutputter doutputter = new DOMOutputter();
    try {
        myHitlist = doutputter.output(doc);
        myHitlist = myHitlist.getFirstChild();
    } catch (JDOMException e) {
        logger.error("JDOMException in executeBeautifier(Node)", e);
    }

    /* Ausgabe des berarbeiteten Opac-Ergebnisses */
    if (!ConfigurationHelper.getInstance().getDebugFolder().equals("") && StorageProvider.getInstance()
            .isWritable(Paths.get(ConfigurationHelper.getInstance().getDebugFolder()))) {
        debugMyNode(myHitlist, ConfigurationHelper.getInstance().getDebugFolder() + "/opacBeautifyAfter.xml");
    }
    return myHitlist;
}