Ignore:
Timestamp:
2016-07-03T22:13:58+02:00 (9 years ago)
Author:
donvip
Message:

checkstyle

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/opendata/util/opendata/ModuleListGenerator.java

    r30910 r32545  
    1515import java.util.zip.ZipOutputStream;
    1616
    17 public class ModuleListGenerator {
     17public final class ModuleListGenerator {
    1818
    19         /**
    20          * @param args
    21          */
    22         public static void main(String[] args) {
    23                 final String url = "http://svn.openstreetmap.org/applications/editors/josm/plugins/opendata/dist/";
    24                 String baseDir = "";
    25                 if (args.length > 0) {
    26                         baseDir = args[0];
    27                 }
    28                 try (
    29                         BufferedWriter list = new BufferedWriter(new FileWriter(baseDir+"modules.txt"));
    30                         ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(baseDir+"modules-icons.zip"));
    31                 ) {
    32                         for (File file : new File(baseDir+"dist").listFiles(new FilenameFilter() {
    33                                 @Override
    34                                 public boolean accept(File dir, String name) {
    35                                         return name.endsWith(".jar");
    36                                 }
    37                         })) {
    38                                 try {
    39                                         String filename = file.getName();
    40                                         System.out.println("Processing "+filename);
    41                                         list.write(filename+";"+url+filename); list.newLine();
    42                                         Manifest mf = new JarFile(file).getManifest();
    43                                         for (Object att : mf.getMainAttributes().keySet()) {
    44                                                 Object value = mf.getMainAttributes().get(att);
    45                                                 if (value != null) {
    46                                                         list.write("\t"+att+": "+value.toString()); list.newLine();
    47                                                         if (att.toString().equals("Module-Icon")) {
    48                                                                 // Directory with jar name, including extension
    49                                                                 String name = filename+"/";
    50                                                                 zip.putNextEntry(new ZipEntry(name));
    51                                                                 // Directory tree to image
    52                                                                 String[] items = value.toString().split("/");
    53                                                                 for (int i=0; i<items.length-1; i++) {
    54                                                                         zip.putNextEntry(new ZipEntry(name += items[i]+"/"));
    55                                                                 }
    56                                                                 // Image file
    57                                                                 zip.putNextEntry(new ZipEntry(name += items[items.length-1]));
    58                                                                 try {
    59                                                                         FileInputStream in;
    60                                                                         try {
    61                                                                                 in = new FileInputStream(baseDir+"modules/"+filename.replace(".jar", "/")+value.toString());
    62                                                                         } catch (FileNotFoundException e) {
    63                                                                                 // If not in module dir, may be in main images directory
    64                                                                                 if (value.toString().startsWith("images/")) {
    65                                                                                         in = new FileInputStream(baseDir+value.toString());
    66                                                                                 } else {
    67                                                                                         throw e;
    68                                                                                 }
    69                                                                         }
    70                                                                         try {
    71                                                                                 byte[] buffer = new byte[4096];
    72                                                                                 int n = -1;
    73                                                                                 while ((n = in.read(buffer)) > 0) {
    74                                                                                         zip.write(buffer, 0, n);
    75                                                                                 }
    76                                                                         } finally {
    77                                                                                 in.close();
    78                                                                         }
    79                                                                 } catch (IOException e) {
    80                                                                         System.err.println("Cannot load Image-Icon: "+value.toString());
    81                                                                 } finally {
    82                                                                         zip.closeEntry();
    83                                                                 }
    84                                                         }
    85                                                 }
    86                                         }
    87                                        
    88                                 } catch (IOException e) {
    89                                         e.printStackTrace();
    90                                 }
    91                         }
    92                 } catch (IOException e) {
    93                         e.printStackTrace();
    94                 }
    95         }
     19    private ModuleListGenerator() {
     20        // Hide default constructor for utilities classes
     21    }
     22
     23    /**
     24     * @param args Main program arguments
     25     */
     26    public static void main(String[] args) {
     27        final String url = "http://svn.openstreetmap.org/applications/editors/josm/plugins/opendata/dist/";
     28        String baseDir = "";
     29        if (args.length > 0) {
     30            baseDir = args[0];
     31        }
     32        try (
     33                BufferedWriter list = new BufferedWriter(new FileWriter(baseDir+"modules.txt"));
     34                ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(baseDir+"modules-icons.zip"));
     35                ) {
     36            for (File file : new File(baseDir+"dist").listFiles(new FilenameFilter() {
     37                @Override
     38                public boolean accept(File dir, String name) {
     39                    return name.endsWith(".jar");
     40                }
     41            })) {
     42                try {
     43                    String filename = file.getName();
     44                    System.out.println("Processing "+filename);
     45                    list.write(filename+";"+url+filename); list.newLine();
     46                    Manifest mf = new JarFile(file).getManifest();
     47                    for (Object att : mf.getMainAttributes().keySet()) {
     48                        Object value = mf.getMainAttributes().get(att);
     49                        if (value != null) {
     50                            list.write("\t"+att+": "+value.toString()); list.newLine();
     51                            if (att.toString().equals("Module-Icon")) {
     52                                // Directory with jar name, including extension
     53                                String name = filename+"/";
     54                                zip.putNextEntry(new ZipEntry(name));
     55                                // Directory tree to image
     56                                String[] items = value.toString().split("/");
     57                                for (int i = 0; i < items.length-1; i++) {
     58                                    zip.putNextEntry(new ZipEntry(name += items[i]+"/"));
     59                                }
     60                                // Image file
     61                                zip.putNextEntry(new ZipEntry(name += items[items.length-1]));
     62                                try {
     63                                    FileInputStream in;
     64                                    try {
     65                                        in = new FileInputStream(baseDir+"modules/"+filename.replace(".jar", "/")+value.toString());
     66                                    } catch (FileNotFoundException e) {
     67                                        // If not in module dir, may be in main images directory
     68                                        if (value.toString().startsWith("images/")) {
     69                                            in = new FileInputStream(baseDir+value.toString());
     70                                        } else {
     71                                            throw e;
     72                                        }
     73                                    }
     74                                    try {
     75                                        byte[] buffer = new byte[4096];
     76                                        int n = -1;
     77                                        while ((n = in.read(buffer)) > 0) {
     78                                            zip.write(buffer, 0, n);
     79                                        }
     80                                    } finally {
     81                                        in.close();
     82                                    }
     83                                } catch (IOException e) {
     84                                    System.err.println("Cannot load Image-Icon: "+value.toString());
     85                                } finally {
     86                                    zip.closeEntry();
     87                                }
     88                            }
     89                        }
     90                    }
     91
     92                } catch (IOException e) {
     93                    e.printStackTrace();
     94                }
     95            }
     96        } catch (IOException e) {
     97            e.printStackTrace();
     98        }
     99    }
    96100}
Note: See TracChangeset for help on using the changeset viewer.