Search in sources :

Example 1 with Text

use of org.jdom2.Text in project pcgen by PCGen.

the class Localized method update.

/**
	 * 
	 * @param e
	 * @param attribute if {@code null}, use the trimmed text.
	 */
private void update(Element e, String attribute) {
    List<?> children = e.getChildren(ELEMENT_LOC);
    for (Object object : children) {
        if (object instanceof Element) {
            Element child = (Element) object;
            String lang = child.getAttributeValue(ATTRIBUTE_LANGUAGE);
            String name;
            if (attribute == null)
                name = child.getTextTrim();
            else
                name = child.getAttributeValue(attribute);
            if (lang != null && !lang.isEmpty())
                addName(lang, name);
        }
    }
}
Also used : Element(org.jdom2.Element)

Example 2 with Text

use of org.jdom2.Text in project coprhd-controller by CoprHD.

the class XmlDiff method compareXml.

/**
 * Compares two documents and outputs different part info.
 * <p>
 * 1. ignore sequence 2. ignore element text value 3. ignore xml comment element
 * </p>
 *
 * @param oldDocument
 *            The old xml document
 * @param newDocument
 *            The new xml document
 * @return
 *         The instance of diff
 */
public static Pair<String, String> compareXml(Document oldDocument, Document newDocument) {
    Element oldRootElement = oldDocument.getRootElement();
    Element newRootElement = newDocument.getRootElement();
    if (compareElement(oldRootElement, newRootElement)) {
        return null;
    }
    XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
    return new Pair<String, String>(xmlOutputter.outputString(oldRootElement), xmlOutputter.outputString(newRootElement));
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) Element(org.jdom2.Element)

Example 3 with Text

use of org.jdom2.Text in project JMRI by JMRI.

the class SwitchboardServlet method getXmlPanel.

@Override
protected String getXmlPanel(String name) {
    log.debug("Getting {} for {}", getPanelType(), name);
    try {
        SwitchboardEditor editor = (SwitchboardEditor) getEditor(name);
        Element panel = new Element("panel");
        JFrame frame = editor.getTargetFrame();
        panel.setAttribute("name", name);
        panel.setAttribute("height", Integer.toString(frame.getContentPane().getHeight()));
        panel.setAttribute("width", Integer.toString(frame.getContentPane().getWidth()));
        panel.setAttribute("panelheight", Integer.toString(editor.getTargetPanel().getHeight()));
        panel.setAttribute("panelwidth", Integer.toString(editor.getTargetPanel().getWidth()));
        // add more properties
        panel.setAttribute("showtooltips", (editor.showTooltip()) ? "yes" : "no");
        panel.setAttribute("controlling", (editor.allControlling()) ? "yes" : "no");
        panel.setAttribute("hideunconnected", (editor.hideUnconnected()) ? "yes" : "no");
        panel.setAttribute("rangemin", Integer.toString(editor.getPanelMenuRangeMin()));
        panel.setAttribute("rangemax", Integer.toString(editor.getPanelMenuRangeMax()));
        panel.setAttribute("type", editor.getSwitchType());
        panel.setAttribute("connection", editor.getSwitchManu());
        panel.setAttribute("shape", editor.getSwitchShape());
        panel.setAttribute("columns", Integer.toString(editor.getColumns()));
        panel.setAttribute("defaulttextcolor", editor.getDefaultTextColor());
        log.debug("webserver Switchboard attribs ready");
        Element bgColor = new Element("backgroundColor");
        if (editor.getBackgroundColor() == null) {
            // set to light grey
            bgColor.setAttribute("red", Integer.toString(192));
            bgColor.setAttribute("green", Integer.toString(192));
            bgColor.setAttribute("blue", Integer.toString(192));
        } else {
            bgColor.setAttribute("red", Integer.toString(editor.getBackgroundColor().getRed()));
            bgColor.setAttribute("green", Integer.toString(editor.getBackgroundColor().getGreen()));
            bgColor.setAttribute("blue", Integer.toString(editor.getBackgroundColor().getBlue()));
        }
        panel.addContent(bgColor);
        Element text = new Element("text");
        text.setAttribute("color", editor.getDefaultTextColor());
        text.setAttribute("content", "For now, Switchboards only present buttons in JMRI WebServer.");
        panel.addContent(text);
        // include switches, Bug: how to delete the old ones?
        // call method in SwitchboardEditor
        List<BeanSwitch> _switches = editor.getSwitches();
        log.debug("SwbServlet N switches: {}", _switches.size());
        for (BeanSwitch sub : _switches) {
            if (sub != null) {
                try {
                    Element e = ConfigXmlManager.elementFromObject(sub);
                    if (e != null) {
                        log.debug("element name: {}", e.getName());
                        // }
                        try {
                            e.setAttribute("label", sub.getNameString());
                            e.setAttribute(JSON.ID, sub.getNamedBean().getSystemName());
                            if (sub.getNamedBean() == null) {
                                e.setAttribute("connected", "false");
                                log.debug("switch {} NOT connected", sub.getNameString());
                            } else {
                                // activate click action via class
                                e.setAttribute("connected", "true");
                            }
                        } catch (NullPointerException ex) {
                            if (sub.getNamedBean() == null) {
                                log.debug("{} {} does not have an associated NamedBean", e.getName(), e.getAttribute(JSON.NAME));
                            } else {
                                log.debug("{} {} does not have a SystemName", e.getName(), e.getAttribute(JSON.NAME));
                            }
                        }
                        // read shared attribs
                        e.setAttribute("textcolor", editor.getDefaultTextColor());
                        e.setAttribute("type", editor.getSwitchType());
                        e.setAttribute("connection", editor.getSwitchManu());
                        e.setAttribute("shape", editor.getSwitchShape());
                        e.setAttribute("columns", Integer.toString(editor.getColumns()));
                        // process and add
                        parsePortableURIs(e);
                        panel.addContent(e);
                    }
                } catch (Exception ex) {
                    log.error("Error reading xml panel element: " + ex, ex);
                }
            }
        }
        Document doc = new Document(panel);
        XMLOutputter out = new XMLOutputter();
        out.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.TRIM));
        return out.outputString(doc);
    } catch (NullPointerException ex) {
        log.warn("Requested Switchboard [" + name + "] does not exist.");
        return "ERROR Requested Switchboard [" + name + "] does not exist.";
    }
}
Also used : SwitchboardEditor(jmri.jmrit.display.switchboardEditor.SwitchboardEditor) XMLOutputter(org.jdom2.output.XMLOutputter) JFrame(javax.swing.JFrame) Element(org.jdom2.Element) BeanSwitch(jmri.jmrit.display.switchboardEditor.SwitchboardEditor.BeanSwitch) Document(org.jdom2.Document)

Example 4 with Text

use of org.jdom2.Text in project JMRI by JMRI.

the class DefaultCatalogTreeManagerXml method writeCatalogTrees.

/*
     *  Writes out tree values to a file in the user's preferences directory
     */
public void writeCatalogTrees() throws java.io.IOException {
    if (log.isDebugEnabled()) {
        log.debug("entered writeCatalogTreeValues");
    }
    CatalogTreeManager manager = InstanceManager.getDefault(jmri.CatalogTreeManager.class);
    List<String> trees = manager.getSystemNameList();
    boolean found = false;
    Iterator<String> iter = manager.getSystemNameList().iterator();
    while (iter.hasNext()) {
        String sname = iter.next();
        CatalogTree tree = manager.getBySystemName(sname);
        if (log.isDebugEnabled()) {
            log.debug("Tree: sysName= " + sname + ", userName= " + tree.getUserName());
            CatalogTreeNode root = tree.getRoot();
            log.debug("enumerateTree called for root= " + root.toString() + ", has " + root.getChildCount() + " children");
            // root.depthFirstEnumeration isn't fully typed in JDOM2
            @SuppressWarnings("unchecked") Enumeration<CatalogTreeNode> e = root.depthFirstEnumeration();
            while (e.hasMoreElements()) {
                CatalogTreeNode n = e.nextElement();
                log.debug("nodeName= " + n.getUserObject() + " has " + n.getLeaves().size() + " leaves and " + n.getChildCount() + " subnodes.");
            }
        }
        if (sname != null && sname.charAt(1) == CatalogTree.XML) {
            found = true;
            break;
        }
    }
    if (found) {
        // there are trees defined, create root element
        Element root = new Element("catalogTrees");
        Document doc = newDocument(root, dtdLocation + "catalogTree.dtd");
        // add XSLT processing instruction
        // <?xml-stylesheet type="text/xsl" href="XSLT/tree-values.xsl"?>
        java.util.Map<String, String> m = new java.util.HashMap<>();
        m.put("type", "text/xsl");
        m.put("href", xsltLocation + "panelfile.xsl");
        org.jdom2.ProcessingInstruction p = new org.jdom2.ProcessingInstruction("xml-stylesheet", m);
        doc.addContent(0, p);
        store(root, trees);
        try {
            if (!checkFile(DEFAULT_FILE_NAME)) {
                // file does not exist, create it
                File file = new File(DEFAULT_FILE_NAME);
                if (!file.createNewFile()) {
                    log.error("createNewFile failed");
                }
            }
            // write content to file
            writeXML(findFile(DEFAULT_FILE_NAME), doc);
            // memory consistent with file
            jmri.jmrit.catalog.ImageIndexEditor.indexChanged(false);
        } catch (java.io.IOException ioe) {
            log.error("IO Exception " + ioe);
            throw (ioe);
        }
    }
}
Also used : Element(org.jdom2.Element) Document(org.jdom2.Document) CatalogTreeManager(jmri.CatalogTreeManager) CatalogTreeNode(jmri.jmrit.catalog.CatalogTreeNode) CatalogTree(jmri.CatalogTree) File(java.io.File) XmlFile(jmri.jmrit.XmlFile)

Example 5 with Text

use of org.jdom2.Text in project JMRI by JMRI.

the class DecoderIndexFile method writeFile.

public void writeFile(String name, DecoderIndexFile oldIndex, String[] files) throws java.io.IOException {
    if (log.isDebugEnabled()) {
        log.debug("writeFile " + name);
    }
    // This is taken in large part from "Java and XML" page 368
    File file = new File(FileUtil.getUserFilesPath() + name);
    // create root element and document
    Element root = new Element("decoderIndex-config");
    root.setAttribute("noNamespaceSchemaLocation", "http://jmri.org/xml/schema/decoder.xsd", org.jdom2.Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"));
    Document doc = newDocument(root);
    // add XSLT processing instruction
    // <?xml-stylesheet type="text/xsl" href="XSLT/DecoderID.xsl"?>
    java.util.Map<String, String> m = new java.util.HashMap<>();
    m.put("type", "text/xsl");
    m.put("href", xsltLocation + "DecoderID.xsl");
    ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
    doc.addContent(0, p);
    // add top-level elements
    Element index;
    root.addContent(index = new Element("decoderIndex"));
    index.setAttribute("version", Integer.toString(fileVersion));
    log.debug("version written to file as " + fileVersion);
    // add mfg list from existing DecoderIndexFile item
    Element mfgList = new Element("mfgList");
    // copy dates from original mfgList element
    if (oldIndex.nmraListDate != null) {
        mfgList.setAttribute("nmraListDate", oldIndex.nmraListDate);
    }
    if (oldIndex.updated != null) {
        mfgList.setAttribute("updated", oldIndex.updated);
    }
    if (oldIndex.lastAdd != null) {
        mfgList.setAttribute("lastadd", oldIndex.lastAdd);
    }
    // We treat "NMRA" special...
    Element mfg = new Element("manufacturer");
    mfg.setAttribute("mfg", "NMRA");
    mfg.setAttribute("mfgID", "999");
    mfgList.addContent(mfg);
    // start working on the rest of the entries
    List<String> keys = new ArrayList<>(oldIndex._mfgIdFromNameHash.keySet());
    Collections.sort(keys);
    for (Object item : keys) {
        String mfgName = (String) item;
        if (!mfgName.equals("NMRA")) {
            mfg = new Element("manufacturer");
            mfg.setAttribute("mfg", mfgName);
            mfg.setAttribute("mfgID", oldIndex._mfgIdFromNameHash.get(mfgName));
            mfgList.addContent(mfg);
        }
    }
    // add family list by scanning files
    Element familyList = new Element("familyList");
    for (String fileName : files) {
        DecoderFile d = new DecoderFile();
        try {
            Element droot = d.rootFromName(DecoderFile.fileLocation + fileName);
            Element family = droot.getChild("decoder").getChild("family").clone();
            family.setAttribute("file", fileName);
            familyList.addContent(family);
        } catch (org.jdom2.JDOMException exj) {
            log.error("could not parse " + fileName + ": " + exj.getMessage());
        } catch (java.io.FileNotFoundException exj) {
            log.error("could not read " + fileName + ": " + exj.getMessage());
        } catch (IOException exj) {
            log.error("other exception while dealing with " + fileName + ": " + exj.getMessage());
        }
    }
    index.addContent(mfgList);
    index.addContent(familyList);
    writeXML(file, doc);
    // force a read of the new file next time
    resetInstance();
}
Also used : HashMap(java.util.HashMap) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) File(java.io.File) XmlFile(jmri.jmrit.XmlFile) ProcessingInstruction(org.jdom2.ProcessingInstruction)

Aggregations

Element (org.jdom2.Element)83 Document (org.jdom2.Document)36 File (java.io.File)21 ProcessingInstruction (org.jdom2.ProcessingInstruction)17 IOException (java.io.IOException)14 Text (org.jdom2.Text)12 Attribute (org.jdom2.Attribute)11 XMLOutputter (org.jdom2.output.XMLOutputter)10 Test (org.junit.Test)10 HashMap (java.util.HashMap)9 XmlFile (jmri.jmrit.XmlFile)9 ArrayList (java.util.ArrayList)6 JDOMException (org.jdom2.JDOMException)6 StringWriter (java.io.StringWriter)5 NamedIcon (jmri.jmrit.catalog.NamedIcon)5 FileOutputStream (java.io.FileOutputStream)4 LinkedHashMap (java.util.LinkedHashMap)4 FileNotFoundException (java.io.FileNotFoundException)3 URL (java.net.URL)3 Locale (java.util.Locale)3