use of org.jdom2.JDOMException in project JMRI by JMRI.
the class ProfilePreferencesPanel method btnExportProfileActionPerformed.
//GEN-LAST:event_btnRemoveSearchPathActionPerformed
private void btnExportProfileActionPerformed(ActionEvent evt) {
//GEN-FIRST:event_btnExportProfileActionPerformed
Profile p = ProfileManager.getDefault().getProfiles(profilesTbl.getSelectedRow());
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new FileNameExtensionFilter("ZIP Archives", "zip"));
chooser.setFileView(new ProfileFileView());
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setSelectedFile(new File(p.getName() + ".zip"));
if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
try {
if (chooser.getSelectedFile().exists()) {
int result = JOptionPane.showConfirmDialog(this, Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.overwriteMessage", chooser.getSelectedFile().getName(), chooser.getSelectedFile().getParentFile().getName()), Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.overwriteTitle"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (result == JOptionPane.YES_OPTION) {
if (!chooser.getSelectedFile().delete()) {
JOptionPane.showMessageDialog(this, Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.failureToDeleteMessage", chooser.getSelectedFile().getName(), chooser.getSelectedFile().getParentFile().getName()), Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.failureToDeleteTitle"), JOptionPane.ERROR_MESSAGE);
}
} else {
this.btnExportProfileActionPerformed(evt);
return;
}
}
boolean exportExternalUserFiles = false;
boolean exportExternalRoster = false;
if (!(new File(FileUtil.getUserFilesPath())).getCanonicalPath().startsWith(p.getPath().getCanonicalPath())) {
int result = JOptionPane.showConfirmDialog(this, Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.externalUserFilesMessage"), Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.externalUserFilesTitle"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (result == JOptionPane.YES_OPTION) {
exportExternalUserFiles = true;
}
}
if (!(new File(Roster.getDefault().getRosterLocation())).getCanonicalPath().startsWith(p.getPath().getCanonicalPath()) && !Roster.getDefault().getRosterLocation().startsWith(FileUtil.getUserFilesPath())) {
int result = JOptionPane.showConfirmDialog(this, Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.externalRosterMessage"), Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.externalRosterTitle"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (result == JOptionPane.YES_OPTION) {
exportExternalRoster = true;
}
}
//if (ProfileManager.getDefault().getActiveProfile() == p) {
// // TODO: save roster, panels, operations if needed and safe to do so
//}
ProfileManager.getDefault().export(p, chooser.getSelectedFile(), exportExternalUserFiles, exportExternalRoster);
log.info("Profile \"{}\" exported to \"{}\"", p.getName(), chooser.getSelectedFile().getName());
JOptionPane.showMessageDialog(this, Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.successMessage", p.getName(), chooser.getSelectedFile().getName()), Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.successTitle"), JOptionPane.INFORMATION_MESSAGE);
} catch (IOException | JDOMException ex) {
log.warn("Unable to export profile \"{}\" to {}", p.getName(), chooser.getSelectedFile().getPath(), ex);
JOptionPane.showMessageDialog(this, Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.errorMessage", p.getName(), chooser.getSelectedFile().getPath(), ex.getLocalizedMessage()), Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.errorTitle"), JOptionPane.ERROR_MESSAGE);
}
}
}
use of org.jdom2.JDOMException in project JMRI by JMRI.
the class ProfileManager method export.
/**
* Export the {@link jmri.profile.Profile} to a zip file.
*
* @param profile The profile to export
* @param target The file to export the profile into
* @param exportExternalUserFiles If the User Files are not within the
* profile directory, should they be
* included?
* @param exportExternalRoster It the roster is not within the profile
* directory, should it be included?
* @throws java.io.IOException if unable to write a file during the
* export
* @throws org.jdom2.JDOMException if unable to create a new profile
* configuration file in the exported
* Profile
*/
public void export(Profile profile, File target, boolean exportExternalUserFiles, boolean exportExternalRoster) throws IOException, JDOMException {
if (!target.exists() && !target.createNewFile()) {
throw new IOException("Unable to create file " + target);
}
// NOI18N
String tempDirPath = System.getProperty("java.io.tmpdir") + File.separator + "JMRI" + System.currentTimeMillis();
FileUtil.createDirectory(tempDirPath);
File tempDir = new File(tempDirPath);
File tempProfilePath = new File(tempDir, profile.getPath().getName());
FileUtil.copy(profile.getPath(), tempProfilePath);
// NOI18N
File config = new File(tempProfilePath, "ProfileConfig.xml");
Document doc = (new SAXBuilder()).build(config);
if (exportExternalUserFiles) {
FileUtil.copy(new File(FileUtil.getUserFilesPath()), tempProfilePath);
// NOI18N
Element fileLocations = doc.getRootElement().getChild("fileLocations");
for (Element fl : fileLocations.getChildren()) {
if (fl.getAttribute("defaultUserLocation") != null) {
// NOI18N
// NOI18N
fl.setAttribute("defaultUserLocation", "profile:");
}
}
}
if (exportExternalRoster) {
// NOI18N
FileUtil.copy(new File(Roster.getDefault().getRosterIndexPath()), new File(tempProfilePath, "roster.xml"));
// NOI18N
FileUtil.copy(new File(Roster.getDefault().getRosterLocation(), "roster"), new File(tempProfilePath, "roster"));
// NOI18N
Element roster = doc.getRootElement().getChild("roster");
// NOI18N
roster.removeAttribute("directory");
}
if (exportExternalUserFiles || exportExternalRoster) {
try (FileWriter fw = new FileWriter(config)) {
XMLOutputter fmt = new XMLOutputter();
fmt.setFormat(Format.getPrettyFormat().setLineSeparator(System.getProperty("line.separator")).setTextMode(Format.TextMode.PRESERVE));
fmt.output(doc, fw);
}
}
try (FileOutputStream out = new FileOutputStream(target);
ZipOutputStream zip = new ZipOutputStream(out)) {
this.exportDirectory(zip, tempProfilePath, tempProfilePath.getPath());
}
FileUtil.delete(tempDir);
}
use of org.jdom2.JDOMException in project gocd by gocd.
the class XmlUtils method buildXmlDocument.
private static Document buildXmlDocument(InputStream inputStream, SAXBuilder builder) throws JDOMException, IOException {
XsdErrorTranslator errorHandler = new XsdErrorTranslator();
builder.setErrorHandler(errorHandler);
Document cruiseRoot = builder.build(inputStream);
if (errorHandler.hasValidationError()) {
throw new XsdValidationException(errorHandler.translate());
}
return cruiseRoot;
}
use of org.jdom2.JDOMException in project opentheso by miledrousset.
the class importxml method ouvreFichier.
/*
for (Table user : DataTable) {
for (LineOfData lineOfData : user.getLineOfDatas()) {
writeLine(lineOfData.getColomne(), lineOfData.getValue());
}
*/
/*
public void choisirfichier (HikariDataSource ds){
JFileChooser fileChooser = new JFileChooser();
int seleccion = fileChooser.showOpenDialog(null);
fichero = fileChooser.getSelectedFile();
//Acciones que se quieran realizar
ouvreFichier();
}*/
/**
* cette funtion permet de ouvrir un fichier pour comencée a faire une
* injection de données.
* C'est seulement pour la creation de un nouvelle BDD.
* la funtion generique est plus ba
* @param con
* @param archive
* @throws ClassNotFoundException
* @throws SQLException
*/
public void ouvreFichier(Connection con, 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();
// le nom de la colone
lineOfData.setColomne(colonne.getName());
// le value que le correspond
lineOfData.setValue(colonne.getText());
lineOfDatas.add(lineOfData);
}
insertLine(con, lineOfDatas, nombreTabla);
lineOfDatas.clear();
}
// / mettre à jour la table dans la BDD
}
} catch (IOException | JDOMException io) {
System.out.println("error");
}
// 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 GpsQuery method getlisteAlign.
private ArrayList<NodeAlignment> getlisteAlign(String xmlrecord) {
ArrayList<NodeAlignment> listeAlign1 = new ArrayList<>();
// Se crea un SAXBuilder para poder parsear el archivo
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File(xmlrecord);
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'
List list = rootNode.getChildren("geoname");
// Se recorre la lista de hijos de 'tables'
for (int i = 0; i < list.size(); i++) {
// Se obtiene el elemento 'tabla'
Element tabla = (Element) list.get(i);
// Se obtiene la lista de hijos del tag 'tabla'
List lista_campos = tabla.getChildren();
// Se recorre la lista de campos
for (int j = 0; j < lista_campos.size(); j++) {
// Se obtiene el elemento 'campo'
Element campo = (Element) lista_campos.get(j);
// Se obtienen los valores que estan entre los tags '<campo></campo>'
// Se obtiene el valor que esta entre los tags '<nombre></nombre>'
String nombre = campo.getChildTextTrim("name");
// Se obtiene el valor que esta entre los tags '<tipo></tipo>'
String tname = campo.getChildTextTrim("toponymName");
// Se obtiene el valor que esta entre los tags '<valor></valor>'
String lat = campo.getChildTextTrim("lat");
String lng = campo.getChildTextTrim("lng");
System.out.println("\t" + nombre + "\t\t" + tname + "\t\t" + lat + "\t\t" + lng);
}
}
} catch (IOException io) {
System.out.println(io.getMessage());
} catch (JDOMException jdomex) {
System.out.println(jdomex.getMessage());
}
return listeAlign1;
}
Aggregations