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);
}
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;
}
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;
}
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")));
}
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());
}
}
Aggregations