prop2prefs

Unnamed repository; edit this file 'description' to name the repository.
git clone http://git.code.weiherhei.de/prop2prefs.git
Log | Files | Refs

Prop2Prefs.java (3258B)


      1 package de.jandankert.prefs;
      2 
      3 import java.io.File;
      4 import java.io.FileFilter;
      5 import java.io.FileInputStream;
      6 import java.io.IOException;
      7 import java.util.ArrayList;
      8 import java.util.List;
      9 import java.util.Properties;
     10 
     11 import de.jandankert.prefs.unix.PrefsXMLCreator;
     12 import de.jandankert.prefs.win32.RegistryImport;
     13 
     14 /**
     15  * Generator for Java-Preferences. Creates from *.properties-files a windows-REG-file and
     16  * Unix-prefs.xml-files.
     17  * 
     18  * @author Jan Dankert
     19  */
     20 public class Prop2Prefs
     21 {
     22 
     23     /**
     24      * Define all Property-Sinks.
     25      */
     26     private static PrefSink[] prefSinks = new PrefSink[]
     27     {new PrefsXMLCreator(), new RegistryImport()};
     28 
     29     /**
     30      * Main program.
     31      * 
     32      * @param args No args needed for this application
     33      */
     34     public static void main(String[] args)
     35     {
     36         try
     37         {
     38             // Prepare all sinks
     39             for (PrefSink sink : prefSinks)
     40                 sink.open();
     41 
     42             // Recursively traverse through all subdirectories
     43             investigateDirectory(new ArrayList<String>());
     44 
     45             // Close all sinks
     46             for (PrefSink sink : prefSinks)
     47                 sink.close();
     48         }
     49         catch (IOException e)
     50         {
     51             // Shit happenz
     52             e.printStackTrace(System.err);
     53 
     54             System.exit(-4); // Inform the caller about the mess.
     55         }
     56 
     57         // all OK :)
     58     }
     59 
     60     /**
     61      * Investigate a directory. This methode will be recursivly called for all subdirectories.
     62      * 
     63      * @param path Path
     64      * @throws IOException
     65      */
     66     private static void investigateDirectory(List<String> path) throws IOException
     67     {
     68 
     69         System.out.println("Investigate directory: " + path.toString());
     70         File file = new File("./" + Utils.join(path, "/"));
     71         File[] files = file.listFiles(new PropertyFileFilter());
     72         for (File propFile : files)
     73         {
     74             parsePropertyFile(propFile, path);
     75         }
     76 
     77         File[] dirs = file.listFiles(new DirectoryFilter());
     78         for (File subDir : dirs)
     79         {
     80             ArrayList<String> newsubDir = new ArrayList<String>(path);
     81             newsubDir.add(subDir.getName());
     82             investigateDirectory(newsubDir);
     83         }
     84     }
     85 
     86     /**
     87      * @param propFile
     88      * @param path
     89      * @throws IOException
     90      */
     91     private static void parsePropertyFile(File propFile, List<String> path) throws IOException
     92     {
     93         System.out.println("Investigate property-file: " + propFile.getName());
     94 
     95         Properties props = new Properties();
     96         props.load(new FileInputStream(propFile));
     97 
     98         // Call all sinks with the loaded properties.
     99         for (PrefSink sink : prefSinks)
    100             sink.sinkProperties(props, path);
    101     }
    102 
    103     private static class DirectoryFilter implements FileFilter
    104     {
    105 
    106         public boolean accept(File pathname)
    107         {
    108             return pathname.isDirectory();
    109         }
    110     }
    111 
    112     private static class PropertyFileFilter implements FileFilter
    113     {
    114 
    115         public boolean accept(File pathname)
    116         {
    117             return pathname.getName().endsWith(".properties");
    118         }
    119     }
    120 
    121 }