Changeset 30677 in osm
- Timestamp:
- 2014-09-23T16:12:17+02:00 (10 years ago)
- Location:
- applications/editors/josm/plugins/CommandLine/src/CommandLine
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/CommandLine/src/CommandLine/CommandLine.java
r30671 r30677 30 30 import java.awt.event.MouseEvent; 31 31 import java.io.File; 32 import java.io.FileOutputStream; 32 33 import java.io.IOException; 33 34 import java.io.InputStream; … … 35 36 import java.io.OutputStreamWriter; 36 37 import java.io.PrintWriter; 38 import java.net.URL; 39 import java.nio.charset.StandardCharsets; 37 40 import java.util.ArrayList; 38 41 import java.util.Collection; 39 42 import java.util.List; 43 import java.util.zip.ZipEntry; 44 import java.util.zip.ZipInputStream; 40 45 41 46 import javax.swing.JMenu; 47 import javax.swing.JOptionPane; 42 48 import javax.swing.JTextField; 43 49 import javax.swing.JToolBar; … … 247 253 private void loadCommands() { 248 254 commands = (new Loader(getPluginDir())).load(); 255 if (commands.isEmpty()) { 256 if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(Main.parent, 257 tr("No command has been found. Would you like to download and install default commands now?"), 258 tr("No command found"), JOptionPane.YES_NO_CANCEL_OPTION)) { 259 try { 260 downloadAndInstallDefaultCommands(); 261 commands = (new Loader(getPluginDir())).load(); 262 JOptionPane.showMessageDialog(Main.parent, tr("Default commands have been successfully installed"), 263 tr("Success"), JOptionPane.INFORMATION_MESSAGE); 264 } catch (IOException e) { 265 Main.warn(e); 266 JOptionPane.showMessageDialog(Main.parent, 267 tr("Failed to download and install default commands.\n\nError: {0}", e.getMessage()), 268 tr("Warning"), JOptionPane.WARNING_MESSAGE); 269 } 270 } 271 } 249 272 for (Command command : commands) { 250 273 commandMenu.add(new CommandAction(command, this)); 274 } 275 } 276 277 private void downloadAndInstallDefaultCommands() throws IOException { 278 String url = Main.pref.get("commandline.default.commands.url", 279 "https://github.com/Foxhind/JOSM-CommandLine-commands/archive/master.zip"); 280 try (ZipInputStream zis = new ZipInputStream(Utils.openURL(new URL(url)), StandardCharsets.UTF_8)) { 281 File dir = new File(getPluginDir()); 282 if (!dir.exists()) { 283 dir.mkdirs(); 284 } 285 ZipEntry entry = null; 286 while ( (entry = zis.getNextEntry()) != null ) { 287 if (!entry.isDirectory()) { 288 String name = entry.getName(); 289 if (name.contains("/")) { 290 name = name.substring(name.lastIndexOf("/")); 291 } 292 File file = new File(dir + File.separator + name); 293 Main.info("Installing command file: "+file); 294 if (!file.createNewFile()) { 295 throw new IOException("Could not create file: " + file.getAbsolutePath()); 296 } 297 // Write file 298 try (FileOutputStream fos = new FileOutputStream(file)) { 299 Utils.copyStream(zis, fos); 300 } 301 // Set last modification date 302 long time = entry.getTime(); 303 if (time > -1) { 304 file.setLastModified(time); 305 } 306 } 307 } 251 308 } 252 309 } -
applications/editors/josm/plugins/CommandLine/src/CommandLine/Loader.java
r30671 r30677 37 37 try { 38 38 // Creating parser 39 SAXParserFactory spf = SAXParserFactory.newInstance(); 40 SAXParser sp = spf.newSAXParser(); 39 SAXParser sp = SAXParserFactory.newInstance().newSAXParser(); 41 40 42 41 // Files loading 43 File path = new File(dirToScan + "/");44 String[] list;45 list = path.list();46 for(int i = 0; i < list.length; i++)47 if (list[i].endsWith(".xml")) {48 currentFile = dirToScan + "/" + list[i];49 loadFile(sp, currentFile);42 String[] list = new File(dirToScan + "/").list(); 43 if (list != null) { 44 for (int i = 0; i < list.length; i++) { 45 if (list[i].endsWith(".xml")) { 46 currentFile = dirToScan + "/" + list[i]; 47 loadFile(sp, currentFile); 48 } 50 49 } 51 }52 catch (Exception e) {53 System.err.println(e);50 } 51 } catch (Exception e) { 52 Main.error(e); 54 53 } 55 54 return loadingCommands; … … 61 60 Main.info(a); 62 61 parser.parse(a, this); 63 } 64 catch (Exception e) { 62 } catch (Exception e) { 65 63 Main.error(e); 66 64 } … … 129 127 130 128 @Override 131 public void characters(char ch[], int start, int length) 132 { 129 public void characters(char ch[], int start, int length) { 133 130 String text = (new String(ch, start, length)).trim(); 134 131 if (currentParameter != null) { … … 172 169 @Override 173 170 public void warning(SAXParseException ex) { 174 System.err.println("Warning in command xml file " + currentFile + ": " + ex.getMessage());171 Main.warn("Warning in command xml file " + currentFile + ": " + ex.getMessage()); 175 172 } 176 173 177 174 @Override 178 175 public void error(SAXParseException ex) { 179 System.err.println("Error in command xml file " + currentFile + ": " + ex.getMessage());176 Main.error("Error in command xml file " + currentFile + ": " + ex.getMessage()); 180 177 } 181 178 182 179 @Override 183 180 public void fatalError(SAXParseException ex) throws SAXException { 184 System.err.println("Error in command xml file " + currentFile + ": " + ex.getMessage());181 Main.error("Error in command xml file " + currentFile + ": " + ex.getMessage()); 185 182 throw ex; 186 183 }
Note:
See TracChangeset
for help on using the changeset viewer.