Search in sources :

Example 36 with JDOMException

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

the class EngineManagerXml method readFile.

/**
     * Read the contents of a roster XML file into this object. Note that this
     * does not clear any existing entries.
     */
@Override
public void readFile(String name) throws org.jdom2.JDOMException, java.io.IOException {
    // suppress rootFromName(name) warning message by checking to see if file exists
    if (findFile(name) == null) {
        log.debug("{} file could not be found", name);
        return;
    }
    // find root
    Element root = rootFromName(name);
    if (root == null) {
        log.debug("{} file could not be read", name);
        return;
    }
    EngineModels.instance().load(root);
    EngineTypes.instance().load(root);
    EngineLengths.instance().load(root);
    EngineManager.instance().load(root);
    log.debug("Engines have been loaded!");
    RollingStockLogger.instance().enableEngineLogging(Setup.isEngineLoggerEnabled());
    // clear dirty bit
    setDirty(false);
    // clear location dirty flag, locations get modified during the loading of cars and locos
    LocationManagerXml.instance().setDirty(false);
}
Also used : Element(org.jdom2.Element)

Example 37 with JDOMException

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

the class RosterEntry method fromFile.

/**
     * Create a RosterEntry from a file.
     *
     * @param file The file containing the RosterEntry
     * @return a new RosterEntry
     * @throws JDOMException if unable to parse file
     * @throws IOException if unable to read file
     */
public static RosterEntry fromFile(@Nonnull File file) throws JDOMException, IOException {
    Element loco = (new LocoFile()).rootFromFile(file).getChild("locomotive");
    if (loco == null) {
        throw new JDOMException("missing expected element");
    }
    RosterEntry re = new RosterEntry(loco);
    re.setFileName(file.getName());
    return re;
}
Also used : Element(org.jdom2.Element) BasicRosterEntry(jmri.BasicRosterEntry) JDOMException(org.jdom2.JDOMException)

Example 38 with JDOMException

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

the class DefaultSignalAppearanceMap method loadMap.

static DefaultSignalAppearanceMap loadMap(String signalSystemName, String aspectMapName) {
    DefaultSignalAppearanceMap map = new DefaultSignalAppearanceMap("map:" + signalSystemName + ":" + aspectMapName);
    maps.put("map:" + signalSystemName + ":" + aspectMapName, map);
    String path = "signals/" + signalSystemName + "/appearance-" + aspectMapName + ".xml";
    URL file = FileUtil.findURL(path, "resources", "xml");
    if (file == null) {
        log.error("appearance file (xml/{}) doesn't exist", path);
        throw new IllegalArgumentException("appearance file (xml/" + path + ") doesn't exist");
    }
    jmri.jmrit.XmlFile xf = new jmri.jmrit.XmlFile() {
    };
    Element root;
    try {
        root = xf.rootFromURL(file);
        // get appearances
        List<Element> l = root.getChild("appearances").getChildren("appearance");
        // find all appearances, include them by aspect name, 
        log.debug("   reading {} aspectname elements", l.size());
        for (int i = 0; i < l.size(); i++) {
            String name = l.get(i).getChild("aspectname").getText();
            if (log.isDebugEnabled()) {
                log.debug("aspect name " + name);
            }
            // add 'show' sub-elements as ints
            List<Element> c = l.get(i).getChildren("show");
            int[] appearances = new int[c.size()];
            for (int j = 0; j < c.size(); j++) {
                // note: includes setting name; redundant, but needed
                int ival;
                String sval = c.get(j).getText().toUpperCase();
                if (sval.equals("LUNAR")) {
                    ival = SignalHead.LUNAR;
                } else if (sval.equals("GREEN")) {
                    ival = SignalHead.GREEN;
                } else if (sval.equals("YELLOW")) {
                    ival = SignalHead.YELLOW;
                } else if (sval.equals("RED")) {
                    ival = SignalHead.RED;
                } else if (sval.equals("FLASHLUNAR")) {
                    ival = SignalHead.FLASHLUNAR;
                } else if (sval.equals("FLASHGREEN")) {
                    ival = SignalHead.FLASHGREEN;
                } else if (sval.equals("FLASHYELLOW")) {
                    ival = SignalHead.FLASHYELLOW;
                } else if (sval.equals("FLASHRED")) {
                    ival = SignalHead.FLASHRED;
                } else if (sval.equals("DARK")) {
                    ival = SignalHead.DARK;
                } else {
                    log.error("found invalid content: {}", sval);
                    throw new JDOMException("invalid content: " + sval);
                }
                appearances[j] = ival;
            }
            map.addAspect(name, appearances);
            List<Element> img = l.get(i).getChildren("imagelink");
            loadImageMaps(img, name, map);
            // now add the rest of the attributes
            java.util.Hashtable<String, String> hm = new java.util.Hashtable<String, String>();
            List<Element> a = l.get(i).getChildren();
            for (int j = 0; j < a.size(); j++) {
                String key = a.get(j).getName();
                String value = a.get(j).getText();
                hm.put(key, value);
            }
            map.aspectAttributeMap.put(name, hm);
        }
        loadSpecificMap(signalSystemName, aspectMapName, map, root);
        loadAspectRelationMap(signalSystemName, aspectMapName, map, root);
        log.debug("loading complete");
    } catch (java.io.IOException | org.jdom2.JDOMException e) {
        log.error("error reading file " + file.getPath(), e);
        return null;
    }
    return map;
}
Also used : Element(org.jdom2.Element) JDOMException(org.jdom2.JDOMException) URL(java.net.URL)

Example 39 with JDOMException

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

the class importxml method ouvreFichier2.

/**
 * cette funtion permet de ouvrir un fichier pour comencée a faire une
 * injection de données.
 * Cette funtion c'est la generique
 * @param ds
 * @param archive
 * @throws ClassNotFoundException
 * @throws SQLException
 */
public void ouvreFichier2(HikariDataSource ds, File archive) throws ClassNotFoundException, SQLException {
    LanguageBean langueBean = new LanguageBean();
    SAXBuilder builder = new SAXBuilder();
    ArrayList<Table> toutTables = new ArrayList<>();
    ArrayList<LineOfData> lineOfDatas = new ArrayList<>();
    try {
        // on crée le document a partir du fichier que on a selectioné
        Document document = (Document) builder.build(archive);
        // Se obtiene la raiz 'tables'
        Element rootNode = document.getRootElement();
        // ici on a toutes les tables (les enfants de la racine)
        List list = rootNode.getChildren("table");
        // ici on fait le tour pour les enfants de 'tables'
        for (int i = 0; i < list.size(); i++) {
            // ici on a la première table
            Element tabla = (Element) list.get(i);
            // ici on a le nom de la table
            String nombreTabla = tabla.getAttributeValue("nom");
            // ici c'est la liste des lignes de la table
            List lista_campos = tabla.getChildren();
            // ici on découpe la liste des lignes
            for (int j = 0; j < lista_campos.size(); j++) {
                // ici on a une ligne de la table
                Element campo = (Element) lista_campos.get(j);
                for (Element colonne : campo.getChildren()) {
                    LineOfData lineOfData = new LineOfData();
                    lineOfData.setColomne(colonne.getName());
                    lineOfData.setValue(colonne.getText());
                    lineOfDatas.add(lineOfData);
                }
                insertLine2(ds, lineOfDatas, nombreTabla);
                lineOfDatas.clear();
            }
        // / mettre à jour la table dans la BDD
        }
    } catch (IOException | JDOMException io) {
        System.out.println(io.toString());
    }
// FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(langueBean.getMsg("info") + " :", langueBean.getMsg("impBDD.info1")));
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) Table(mom.trd.opentheso.core.exports.privatesdatas.tables.Table) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) LineOfData(mom.trd.opentheso.core.exports.privatesdatas.LineOfData) ArrayList(java.util.ArrayList) List(java.util.List) LanguageBean(mom.trd.opentheso.SelectedBeans.LanguageBean)

Example 40 with JDOMException

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

the class testimportxml method ouvreFichier.

// TODO add test methods here.
// The methods must be annotated with annotation @Test. For example:
// 
@Test
public void ouvreFichier() {
    HikariDataSource conn = openConnexionPool();
    SAXBuilder builder = new SAXBuilder();
    ArrayList<Table> toutTables = new ArrayList<>();
    ArrayList<LineOfData> lineOfDatas = new ArrayList<>();
    File xmlFile = new File("C:/Users/antonio.perez/Desktop/testbon.xml");
    try {
        // Se crea el documento a traves del archivo
        Document document = (Document) builder.build(xmlFile);
        // Se obtiene la raiz 'tables'
        Element rootNode = document.getRootElement();
        // Se obtiene la lista de hijos de la raiz 'tables'
        // ici on a toutes les tables (les enfants de la racine)
        List list = rootNode.getChildren("table");
        // Se recorre la lista de hijos de 'tables'
        for (int i = 0; i < list.size(); i++) {
            // Se obtiene el elemento 'tabla'
            // ici on a la première table
            Element tabla = (Element) list.get(i);
            // Se obtiene el atributo 'nombre' que esta en el tag 'tabla'
            // ici on a le nom de la table
            String nombreTabla = tabla.getAttributeValue("nom");
            System.out.println("Nom de la table : " + nombreTabla);
            // Se obtiene la lista de hijos del tag 'tabla'
            // ici c'est la liste des lignes de la table
            List lista_campos = tabla.getChildren();
            // ici on découpe la liste des lignes
            for (int j = 0; j < lista_campos.size(); j++) {
                // Se obtiene el elemento 'campo'
                // ici on a une ligne de la table
                Element campo = (Element) lista_campos.get(j);
                // System.out.println("nouvelle ligne table "+ nombreTabla);
                for (Element colonne : campo.getChildren()) {
                    LineOfData lineOfData = new LineOfData();
                    // System.out.println("Nom de la colonne = " + colonne.getName());
                    // System.out.println("valeur de la colonne = " + colonne.getText());
                    lineOfData.setColomne(colonne.getName());
                    lineOfData.setValue(colonne.getText());
                    lineOfDatas.add(lineOfData);
                }
                insertLine(conn, lineOfDatas, nombreTabla);
                lineOfDatas.clear();
            }
        // / mettre à jour la table dans la BDD
        }
    } catch (IOException io) {
        System.out.println(io.getMessage());
    } catch (JDOMException jdomex) {
        System.out.println(jdomex.getMessage());
    }
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) HikariDataSource(com.zaxxer.hikari.HikariDataSource) Table(mom.trd.opentheso.core.exports.privatesdatas.tables.Table) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) LineOfData(mom.trd.opentheso.core.exports.privatesdatas.LineOfData) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) Test(org.junit.Test)

Aggregations

Element (org.jdom2.Element)154 Document (org.jdom2.Document)113 JDOMException (org.jdom2.JDOMException)89 IOException (java.io.IOException)75 SAXBuilder (org.jdom2.input.SAXBuilder)67 Test (org.junit.Test)36 File (java.io.File)32 ArrayList (java.util.ArrayList)22 InputStream (java.io.InputStream)17 Attribute (org.jdom2.Attribute)16 StringReader (java.io.StringReader)15 MCRNodeBuilder (org.mycore.common.xml.MCRNodeBuilder)14 HashMap (java.util.HashMap)13 XMLOutputter (org.jdom2.output.XMLOutputter)13 SAXException (org.xml.sax.SAXException)13 URL (java.net.URL)12 XmlFile (jmri.jmrit.XmlFile)12 List (java.util.List)11 MCRObject (org.mycore.datamodel.metadata.MCRObject)11 MCRException (org.mycore.common.MCRException)10