Search in sources :

Example 26 with ProcessingInstruction

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

the class ThrottleFrame method saveThrottle.

private void saveThrottle(String sfile) {
    // Save throttle: title / window position
    // as strongly linked to extended throttles and roster presence, do not save function buttons and background window as they're stored in the roster entry
    XmlFile xf = new XmlFile() {
    };
    // odd syntax is due to XmlFile being abstract
    xf.makeBackupFile(sfile);
    File file = new File(sfile);
    try {
        //The file does not exist, create it before writing
        File parentDir = file.getParentFile();
        if (!parentDir.exists()) {
            if (// make directory and check result
            !parentDir.mkdir()) {
                log.error("could not make parent directory");
            }
        }
        if (// create file, check success
        !file.createNewFile()) {
            log.error("createNewFile failed");
        }
    } catch (Exception exp) {
        log.error("Exception while writing the throttle file, may not be complete: " + exp);
    }
    try {
        Element root = new Element("throttle-config");
        Document doc = XmlFile.newDocument(root, XmlFile.getDefaultDtdLocation() + "throttle-config.dtd");
        // add XSLT processing instruction
        // <?xml-stylesheet type="text/xsl" href="XSLT/throttle.xsl"?>
        /*   java.util.Map<String,String> m = new java.util.HashMap<String,String>();
             m.put("type", "text/xsl");
             m.put("href", jmri.jmrit.XmlFile.xsltLocation+"throttle.xsl");
             ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
             doc.addContent(0,p);*/
        Element throttleElement = getXml();
        //   throttleElement.getChild("AddressPanel").removeChild("locoaddress");
        if (// don't save function buttons labels, they're in roster entry 
        (this.getRosterEntry() != null) && (getDefaultThrottleFolder() + addressPanel.getRosterEntry().getId().trim() + ".xml").compareTo(sfile) == 0) {
            throttleElement.getChild("FunctionPanel").removeChildren("FunctionButton");
        }
        root.setContent(throttleElement);
        xf.writeXML(file, doc);
        setLastUsedSaveFile(sfile);
    } catch (Exception ex) {
        log.warn("Exception while storing throttle xml: " + ex);
    }
}
Also used : XmlFile(jmri.jmrit.XmlFile) Element(org.jdom2.Element) Document(org.jdom2.Document) File(java.io.File) XmlFile(jmri.jmrit.XmlFile)

Example 27 with ProcessingInstruction

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

the class StoreXmlThrottlesLayoutAction method saveThrottlesLayout.

public void saveThrottlesLayout(java.io.File f) {
    try {
        Element root = new Element("throttle-layout-config");
        Document doc = XmlFile.newDocument(root, XmlFile.getDefaultDtdLocation() + "throttle-layout-config.dtd");
        // add XSLT processing instruction
        // <?xml-stylesheet type="text/xsl" href="XSLT/throttle-layout-config.xsl"?>
        /*TODO   java.util.Map<String,String> m = new java.util.HashMap<String,String>();
             m.put("type", "text/xsl");
             m.put("href", jmri.jmrit.XmlFile.xsltLocation + "throttle-layout-config.xsl");
             ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
             doc.addContent(0, p); */
        java.util.ArrayList<Element> children = new java.util.ArrayList<Element>(5);
        // throttle list window
        children.add(ThrottleFrameManager.instance().getThrottlesListPanel().getXml());
        // throttle windows
        for (Iterator<ThrottleWindow> i = ThrottleFrameManager.instance().getThrottleWindows(); i.hasNext(); ) {
            ThrottleWindow tw = i.next();
            Element throttleElement = tw.getXml();
            children.add(throttleElement);
        }
        root.setContent(children);
        FileOutputStream o = new java.io.FileOutputStream(f);
        try {
            XMLOutputter fmt = new XMLOutputter();
            fmt.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.PRESERVE));
            fmt.output(doc, o);
        } catch (IOException ex) {
            log.warn("Exception in storing throttle xml: " + ex);
        } finally {
            o.close();
        }
    } catch (FileNotFoundException ex) {
        log.warn("Exception in storing throttle xml: " + ex);
    } catch (IOException ex) {
        log.warn("Exception in storing throttle xml: " + ex);
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) Element(org.jdom2.Element) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Document(org.jdom2.Document)

Example 28 with ProcessingInstruction

use of org.jdom2.ProcessingInstruction in project mycore by MyCoRe-Org.

the class MCROAIDataProvider method addXSLStyle.

/**
 * Add link to XSL stylesheet for displaying OAI response in web browser.
 */
private Document addXSLStyle(Document doc) {
    String styleSheet = MCROAIAdapter.PREFIX + getServletName() + ".ResponseStylesheet";
    String xsl = MCRConfiguration.instance().getString(styleSheet, "oai/oai2.xsl");
    if (!xsl.isEmpty()) {
        Map<String, String> pairs = new HashMap<>();
        pairs.put("type", "text/xsl");
        pairs.put("href", MCRFrontendUtil.getBaseURL() + xsl);
        doc.addContent(0, new ProcessingInstruction("xml-stylesheet", pairs));
    }
    return doc;
}
Also used : HashMap(java.util.HashMap) ProcessingInstruction(org.jdom2.ProcessingInstruction)

Example 29 with ProcessingInstruction

use of org.jdom2.ProcessingInstruction in project jspwiki by apache.

the class JSPWikiMarkupParser method paragraphify.

/**
 *  Checks out that the first paragraph is correctly installed.
 *
 *  @param rootElement
 */
private void paragraphify(final Element rootElement) {
    // 
    // Add the paragraph tag to the first paragraph
    // 
    final List<Content> kids = rootElement.getContent();
    if (rootElement.getChild("p") != null) {
        final ArrayList<Content> ls = new ArrayList<>();
        int idxOfFirstContent = 0;
        int count = 0;
        for (final Iterator<Content> i = kids.iterator(); i.hasNext(); count++) {
            final Content c = i.next();
            if (c instanceof Element) {
                final String name = ((Element) c).getName();
                if (isBlockLevel(name))
                    break;
            }
            if (!(c instanceof ProcessingInstruction)) {
                ls.add(c);
                if (idxOfFirstContent == 0)
                    idxOfFirstContent = count;
            }
        }
        // 
        if (ls.size() > 0) {
            final Element newel = new Element("p");
            for (final Iterator<Content> i = ls.iterator(); i.hasNext(); ) {
                final Content c = i.next();
                c.detach();
                newel.addContent(c);
            }
            // 
            if (!newel.getTextTrim().isEmpty() || !newel.getChildren().isEmpty())
                rootElement.addContent(idxOfFirstContent, newel);
        }
    }
}
Also used : Content(org.jdom2.Content) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) ProcessingInstruction(org.jdom2.ProcessingInstruction)

Aggregations

Element (org.jdom2.Element)24 Document (org.jdom2.Document)20 ProcessingInstruction (org.jdom2.ProcessingInstruction)19 File (java.io.File)18 XmlFile (jmri.jmrit.XmlFile)9 IOException (java.io.IOException)5 HashMap (java.util.HashMap)4 FileNotFoundException (java.io.FileNotFoundException)3 XMLOutputter (org.jdom2.output.XMLOutputter)3 FileOutputStream (java.io.FileOutputStream)2 ArrayList (java.util.ArrayList)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 LinkedHashMap (java.util.LinkedHashMap)1 Block (jmri.Block)1 CatalogTree (jmri.CatalogTree)1 CatalogTreeManager (jmri.CatalogTreeManager)1 Consist (jmri.Consist)1 CatalogTreeNode (jmri.jmrit.catalog.CatalogTreeNode)1