Ignore:
Timestamp:
2014-09-23T16:12:17+02:00 (10 years ago)
Author:
donvip
Message:

[josm_commandline] download and install default commands at startup if none is installed

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  
    3030import java.awt.event.MouseEvent;
    3131import java.io.File;
     32import java.io.FileOutputStream;
    3233import java.io.IOException;
    3334import java.io.InputStream;
     
    3536import java.io.OutputStreamWriter;
    3637import java.io.PrintWriter;
     38import java.net.URL;
     39import java.nio.charset.StandardCharsets;
    3740import java.util.ArrayList;
    3841import java.util.Collection;
    3942import java.util.List;
     43import java.util.zip.ZipEntry;
     44import java.util.zip.ZipInputStream;
    4045
    4146import javax.swing.JMenu;
     47import javax.swing.JOptionPane;
    4248import javax.swing.JTextField;
    4349import javax.swing.JToolBar;
     
    247253    private void loadCommands() {
    248254        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        }
    249272        for (Command command : commands) {
    250273            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            }
    251308        }
    252309    }
  • applications/editors/josm/plugins/CommandLine/src/CommandLine/Loader.java

    r30671 r30677  
    3737        try {
    3838            // Creating parser
    39             SAXParserFactory spf = SAXParserFactory.newInstance();
    40             SAXParser sp = spf.newSAXParser();
     39            SAXParser sp = SAXParserFactory.newInstance().newSAXParser();
    4140
    4241            // 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                    }
    5049                }
    51         }
    52         catch (Exception e) {
    53             System.err.println(e);
     50            }
     51        } catch (Exception e) {
     52            Main.error(e);
    5453        }
    5554        return loadingCommands;
     
    6160            Main.info(a);
    6261            parser.parse(a, this);
    63         }
    64         catch (Exception e) {
     62        } catch (Exception e) {
    6563            Main.error(e);
    6664        }
     
    129127
    130128    @Override
    131     public void characters(char ch[], int start, int length)
    132     {
     129    public void characters(char ch[], int start, int length) {
    133130        String text = (new String(ch, start, length)).trim();
    134131        if (currentParameter != null) {
     
    172169    @Override
    173170    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());
    175172    }
    176173
    177174    @Override
    178175    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());
    180177    }
    181178
    182179    @Override
    183180    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());
    185182        throw ex;
    186183    }
Note: See TracChangeset for help on using the changeset viewer.