Search in sources :

Example 21 with Text

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

the class OperationsSetupXml method writeFile.

@Override
public void writeFile(String name) throws java.io.FileNotFoundException, java.io.IOException {
    log.debug("writeFile {}", name);
    // This is taken in large part from "Java and XML" page 368
    File file = findFile(name);
    if (file == null) {
        file = new File(name);
    }
    // create root element
    // NOI18N
    Element root = new Element("operations-config");
    // NOI18N
    Document doc = newDocument(root, dtdLocation + "operations-config.dtd");
    // add XSLT processing instruction
    java.util.Map<String, String> m = new java.util.HashMap<String, String>();
    // NOI18N
    m.put("type", "text/xsl");
    // NOI18N
    m.put("href", xsltLocation + "operations-config.xsl");
    // NOI18N
    ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
    doc.addContent(0, p);
    // add top-level elements         
    root.addContent(Setup.store());
    // add manifest header text strings
    root.addContent(TrainManifestHeaderText.store());
    // add manifest text strings
    root.addContent(TrainManifestText.store());
    // add switch list text strings
    root.addContent(TrainSwitchListText.store());
    // add control elements
    root.addContent(Control.store());
    writeXML(file, doc);
    // done, so can't be dirty
    setDirty(false);
}
Also used : Element(org.jdom2.Element) Document(org.jdom2.Document) File(java.io.File) ProcessingInstruction(org.jdom2.ProcessingInstruction)

Example 22 with Text

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

the class TrainManagerXml method writeFile.

@Override
public void writeFile(String name) throws java.io.FileNotFoundException, java.io.IOException {
    log.debug("writeFile {}", name);
    // This is taken in large part from "Java and XML" page 368
    File file = findFile(name);
    if (file == null) {
        file = new File(name);
    }
    // create root element
    // NOI18N
    Element root = new Element("operations-config");
    // NOI18N
    Document doc = newDocument(root, dtdLocation + "operations-trains.dtd");
    // add XSLT processing instruction
    java.util.Map<String, String> m = new java.util.HashMap<String, String>();
    // NOI18N
    m.put("type", "text/xsl");
    // NOI18N
    m.put("href", xsltLocation + "operations-trains.xsl");
    // NOI18N
    ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
    doc.addContent(0, p);
    TrainManager.instance().store(root);
    TrainScheduleManager.instance().store(root);
    AutomationManager.instance().store(root);
    writeXML(file, doc);
    // done - train file now stored, so can't be dirty
    setDirty(false);
}
Also used : Element(org.jdom2.Element) Document(org.jdom2.Document) File(java.io.File) ProcessingInstruction(org.jdom2.ProcessingInstruction)

Example 23 with Text

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

the class VSDecoderPreferences method save.

public void save() {
    if (prefFile == null) {
        return;
    }
    XmlFile xf = new XmlFile() {
    };
    // odd syntax is due to XmlFile being abstract
    xf.makeBackupFile(prefFile);
    File file = new File(prefFile);
    try {
        //The file does not exist, create it before writing
        File parentDir = file.getParentFile();
        if (!parentDir.exists()) {
            if (// make directory, check result
            !parentDir.mkdir()) {
                log.error("failed to make parent directory");
            }
        }
        if (// create file, check result
        !file.createNewFile()) {
            log.error("createNewFile failed");
        }
    } catch (Exception exp) {
        log.error("Exception while writing the new VSDecoder preferences file, may not be complete: " + exp);
    }
    try {
        Element root = new Element("vsdecoder-preferences");
        //Document doc = XmlFile.newDocument(root, XmlFile.dtdLocation+"vsdecoder-preferences.dtd");
        Document doc = XmlFile.newDocument(root);
        // add XSLT processing instruction
        // <?xml-stylesheet type="text/xsl" href="XSLT/throttle.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+"throttles-preferences.xsl");
             ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m);
             doc.addContent(0,p);*/
        root.setContent(store());
        xf.writeXML(file, doc);
    } catch (Exception ex) {
        // TODO fix null value for Attribute
        log.warn("Exception in storing vsdecoder preferences 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 24 with Text

use of org.jdom2.Text in project opentheso by miledrousset.

the class GpsQuery method queryGps2.

public ArrayList<NodeAlignment> queryGps2(String idC, String idTheso, String lexicalValue, String lang, String requete) {
    ArrayList<NodeLang> nodeLangs;
    try {
        lexicalValue = URLEncoder.encode(lexicalValue, "UTF-8");
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(GpsQuery.class.getName()).log(Level.SEVERE, null, ex);
    }
    lexicalValue = lexicalValue.replaceAll(" ", "%20");
    requete = requete.replace("##lang##", lang);
    requete = requete.replace("##value##", lexicalValue);
    try {
        // URL url = new URL("https://" + lang + ".wikipedia.org/w/api.php?action=query&list=search&srwhat=text&format=xml&srsearch=" + lexicalValue + "&srnamespace=0");
        // requete = URLEncoder.encode(requete, "UTF-8");
        URL url = new URL(requete);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/xml");
        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }
        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
        String output;
        String xmlRecord = "";
        while ((output = br.readLine()) != null) {
            xmlRecord += output;
        }
        byte[] bytes = xmlRecord.getBytes();
        xmlRecord = new String(bytes, Charset.forName("UTF-8"));
        conn.disconnect();
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xmlRecord));
            org.w3c.dom.Document doc = db.parse(is);
            NodeList nodes = doc.getElementsByTagName("geoname");
            listeAlign = new ArrayList<>();
            // iterate the employees
            for (int i = 0; i < nodes.getLength(); i++) {
                nodeLangs = new ArrayList<>();
                org.w3c.dom.Element element = (org.w3c.dom.Element) nodes.item(i);
                NodeAlignment nodeAlignment = new NodeAlignment();
                NodeList nodeList = element.getElementsByTagName("name");
                for (int j = 0; j < nodeList.getLength(); j++) {
                    nodeAlignment.setName(nodeList.item(j).getTextContent());
                }
                nodeList = element.getElementsByTagName("lat");
                if (nodeList != null && nodeList.getLength() > 0) {
                    for (int j = 0; j < nodeList.getLength(); j++) {
                        nodeAlignment.setLat(Double.parseDouble(nodeList.item(j).getTextContent()));
                    }
                }
                nodeList = element.getElementsByTagName("lng");
                if (nodeList != null && nodeList.getLength() > 0) {
                    for (int j = 0; j < nodeList.getLength(); j++) {
                        nodeAlignment.setLng(Double.parseDouble(nodeList.item(j).getTextContent()));
                    }
                }
                nodeList = element.getElementsByTagName("geonameId");
                if (nodeList != null && nodeList.getLength() > 0) {
                    for (int j = 0; j < nodeList.getLength(); j++) {
                        nodeAlignment.setIdUrl("http://www.geonames.org/" + (nodeList.item(j).getTextContent()));
                    }
                }
                nodeList = element.getElementsByTagName("countryName");
                if (nodeList != null && nodeList.getLength() > 0) {
                    for (int j = 0; j < nodeList.getLength(); j++) {
                        nodeAlignment.setCountryName(nodeList.item(j).getTextContent());
                    }
                }
                nodeList = element.getElementsByTagName("toponymName");
                if (nodeList != null && nodeList.getLength() > 0) {
                    for (int j = 0; j < nodeList.getLength(); j++) {
                        nodeAlignment.setToponymName(nodeList.item(j).getTextContent());
                    }
                }
                nodeList = element.getElementsByTagName("adminName1");
                if (nodeList != null && nodeList.getLength() > 0) {
                    for (int j = 0; j < nodeList.getLength(); j++) {
                        nodeAlignment.setAdminName1(nodeList.item(j).getTextContent());
                    }
                }
                nodeList = element.getElementsByTagName("adminName2");
                if (nodeList != null && nodeList.getLength() > 0) {
                    for (int j = 0; j < nodeList.getLength(); j++) {
                        nodeAlignment.setAdminName2(nodeList.item(j).getTextContent());
                    }
                }
                nodeList = element.getElementsByTagName("alternateName");
                if (nodeList != null && nodeList.getLength() > 0) {
                    for (int j = 0; j < nodeList.getLength(); j++) {
                        NodeLang nodeLang = new NodeLang();
                        // nodeAlignment.setToponymName(p.item(j).getTextContent());
                        ArrayList<String> langueFound = new ArrayList<>();
                        org.w3c.dom.Element el = (org.w3c.dom.Element) nodeList.item(j);
                        if (el.hasAttribute("lang")) {
                            nodeLang.setCode(el.getAttribute("lang"));
                            nodeLang.setValue(nodeList.item(j).getTextContent());
                        }
                        nodeLangs.add(nodeLang);
                    }
                    nodeAlignment.setAlltraductions(nodeLangs);
                }
                listeAlign.add(nodeAlignment);
            }
        } catch (ParserConfigurationException | SAXException | IOException e) {
        }
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }
    return listeAlign;
}
Also used : InputSource(org.xml.sax.InputSource) MalformedURLException(java.net.MalformedURLException) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) URL(java.net.URL) SAXException(org.xml.sax.SAXException) HttpURLConnection(java.net.HttpURLConnection) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) InputStreamReader(java.io.InputStreamReader) NodeList(org.w3c.dom.NodeList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) NodeAlignment(mom.trd.opentheso.bdd.helper.nodes.NodeAlignment) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeLang(mom.trd.opentheso.bdd.helper.nodes.NodeLang) BufferedReader(java.io.BufferedReader)

Example 25 with Text

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

the class JSPWikiMarkupParser method createAnchor.

/**
 *  Creates a JDOM anchor element.  Can be overridden to change the URL creation,
 *  if you really know what you are doing.
 *
 *  @param type One of the types above
 *  @param link URL to which to link to
 *  @param text Link text
 *  @param section If a particular section identifier is required.
 *  @return An A element.
 *  @since 2.4.78
 */
protected Element createAnchor(int type, String link, String text, String section) {
    text = escapeHTMLEntities(text);
    section = escapeHTMLEntities(section);
    Element el = new Element("a");
    el.setAttribute("class", CLASS_TYPES[type]);
    el.setAttribute("href", link + section);
    el.addContent(text);
    return el;
}
Also used : Element(org.jdom2.Element)

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