commit 673f85e8d380d345eb188a2c35613202f9580019
Author: Jan Dankert <devnull@localhost>
Date: Fri, 6 Jan 2012 00:04:37 +0100
let's start...
Diffstat:
12 files changed, 355 insertions(+), 0 deletions(-)
diff --git a/.classpath b/.classpath
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/.project b/.project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>Prop2Prefs</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
diff --git a/Unix/prop/prefs.xml b/Unix/prop/prefs.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<map>
+ <entry key="test2" value="Test 2" />
+ <entry key="test1" value="Test 1" />
+</map>+
\ No newline at end of file
diff --git a/Unix/prop/subpackage/prefs.xml b/Unix/prop/subpackage/prefs.xml
@@ -0,0 +1 @@
+<?xml version="1.0" encoding="UTF-8"?><map><entry key="test2" value="Test 2"/><entry key="test1" value="Test 1"/></map>+
\ No newline at end of file
diff --git a/Windows/prefs.reg b/Windows/prefs.reg
@@ -0,0 +1,11 @@
+REGEDIT4
+
+[HKEY_CURRENT_USER\Software\JavaSoft\prefs\prop]
+"test2"="Test 2"
+"test1"="Test 1"
+
+[HKEY_CURRENT_USER\Software\JavaSoft\prefs\prop\subpackage]
+"tTest1"="Test 1"
+"t_Test1"="Test 1"
+"t_test2"="Test 1"
+
diff --git a/prop/example.properties b/prop/example.properties
@@ -0,0 +1,2 @@
+test1=Test 1
+test2=Test 2+
\ No newline at end of file
diff --git a/prop/subpackage/example.properties b/prop/subpackage/example.properties
@@ -0,0 +1,2 @@
+test1=Test 1
+test2=Test 2+
\ No newline at end of file
diff --git a/src/de/jandankert/prefs/PrefSink.java b/src/de/jandankert/prefs/PrefSink.java
@@ -0,0 +1,32 @@
+package de.jandankert.prefs;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Properties;
+
+/**
+ * Definition of a Properties-"Sink". The implementation will be OS-specific.
+ *
+ * @author Jan Dankert
+ */
+public interface PrefSink
+{
+
+ /**
+ * Call the sink with properties.
+ * @param props
+ * @param path
+ * @throws IOException
+ */
+ void sinkProperties(Properties props, List<String> path) throws IOException;
+
+ /**
+ * @throws IOException
+ */
+ void close() throws IOException;
+
+ /**
+ * @throws IOException
+ */
+ void open() throws IOException;
+}
diff --git a/src/de/jandankert/prefs/Prop2Prefs.java b/src/de/jandankert/prefs/Prop2Prefs.java
@@ -0,0 +1,121 @@
+package de.jandankert.prefs;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+import de.jandankert.prefs.unix.PrefsXMLCreator;
+import de.jandankert.prefs.win32.RegistryImport;
+
+/**
+ * Generator for Java-Preferences. Creates from *.properties-files a windows-REG-file and
+ * Unix-prefs.xml-files.
+ *
+ * @author Jan Dankert
+ */
+public class Prop2Prefs
+{
+
+ /**
+ * Define all Property-Sinks.
+ */
+ private static PrefSink[] prefSinks = new PrefSink[]
+ {new PrefsXMLCreator(), new RegistryImport()};
+
+ /**
+ * Main program.
+ *
+ * @param args No args needed for this application
+ */
+ public static void main(String[] args)
+ {
+ try
+ {
+ // Prepare all sinks
+ for (PrefSink sink : prefSinks)
+ sink.open();
+
+ // Recursively traverse through all subdirectories
+ investigateDirectory(new ArrayList<String>());
+
+ // Close all sinks
+ for (PrefSink sink : prefSinks)
+ sink.close();
+ }
+ catch (IOException e)
+ {
+ // Shit happenz
+ e.printStackTrace(System.err);
+
+ System.exit(-4); // Inform the caller about the mess.
+ }
+
+ // all OK :)
+ }
+
+ /**
+ * Investigate a directory. This methode will be recursivly called for all subdirectories.
+ *
+ * @param path Path
+ * @throws IOException
+ */
+ private static void investigateDirectory(List<String> path) throws IOException
+ {
+
+ System.out.println("Investigate directory: " + path.toString());
+ File file = new File("./" + Utils.join(path, "/"));
+ File[] files = file.listFiles(new PropertyFileFilter());
+ for (File propFile : files)
+ {
+ parsePropertyFile(propFile, path);
+ }
+
+ File[] dirs = file.listFiles(new DirectoryFilter());
+ for (File subDir : dirs)
+ {
+ ArrayList<String> newsubDir = new ArrayList<String>(path);
+ newsubDir.add(subDir.getName());
+ investigateDirectory(newsubDir);
+ }
+ }
+
+ /**
+ * @param propFile
+ * @param path
+ * @throws IOException
+ */
+ private static void parsePropertyFile(File propFile, List<String> path) throws IOException
+ {
+ System.out.println("Investigate property-file: " + propFile.getName());
+
+ Properties props = new Properties();
+ props.load(new FileInputStream(propFile));
+
+ // Call all sinks with the loaded properties.
+ for (PrefSink sink : prefSinks)
+ sink.sinkProperties(props, path);
+ }
+
+ private static class DirectoryFilter implements FileFilter
+ {
+
+ public boolean accept(File pathname)
+ {
+ return pathname.isDirectory();
+ }
+ }
+
+ private static class PropertyFileFilter implements FileFilter
+ {
+
+ public boolean accept(File pathname)
+ {
+ return pathname.getName().endsWith(".properties");
+ }
+ }
+
+}
diff --git a/src/de/jandankert/prefs/Utils.java b/src/de/jandankert/prefs/Utils.java
@@ -0,0 +1,20 @@
+package de.jandankert.prefs;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+public final class Utils
+{
+
+ public static <T> String join(final Collection<T> objs, final String delimiter)
+ {
+ if (objs == null || objs.isEmpty())
+ return "";
+ Iterator<T> iter = objs.iterator();
+ StringBuffer buffer = new StringBuffer(iter.next().toString());
+ while (iter.hasNext())
+ buffer.append(delimiter).append(iter.next().toString());
+ return buffer.toString();
+ }
+
+}
diff --git a/src/de/jandankert/prefs/unix/PrefsXMLCreator.java b/src/de/jandankert/prefs/unix/PrefsXMLCreator.java
@@ -0,0 +1,83 @@
+package de.jandankert.prefs.unix;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.List;
+import java.util.Properties;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXTransformerFactory;
+import javax.xml.transform.stream.StreamResult;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import de.jandankert.prefs.PrefSink;
+import de.jandankert.prefs.Utils;
+
+/**
+ * @author Jan Dankert
+ */
+public class PrefsXMLCreator implements PrefSink
+{
+
+ public void open() throws IOException
+ {
+ // nothing to do...
+ // a new file is created on each properties-set.
+ }
+
+ public void close() throws IOException
+ {
+ }
+
+ public void sinkProperties(Properties props, List<String> path) throws IOException
+ {
+ new File("Unix/" + Utils.join(path, "/")).mkdirs();
+
+ FileOutputStream prefsXMLOutputStream = new FileOutputStream("Unix/" + Utils.join(path, "/") + "/prefs.xml");
+ DocumentBuilder builder = null;
+ try
+ {
+ builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ }
+ catch (ParserConfigurationException e)
+ {
+ throw new RuntimeException(e);
+ }
+
+ Document newDocument = builder.newDocument();
+ Element mapElement = newDocument.createElement("map");
+ for (Object key : props.keySet())
+ {
+
+ Object value = props.get(key);
+
+ Element entry = newDocument.createElement("entry");
+ entry.setAttribute("key", key.toString());
+ entry.setAttribute("value", value.toString());
+ mapElement.appendChild(entry);
+ }
+
+ newDocument.appendChild(mapElement);
+ try
+ {
+ Transformer transformer = SAXTransformerFactory.newInstance().newTransformer();
+ transformer.transform(new DOMSource(newDocument), new StreamResult(prefsXMLOutputStream));
+ SAXTransformerFactory.newInstance().newTransformer();
+ }
+ catch (TransformerException e)
+ {
+ e.printStackTrace(System.err);
+ throw new RuntimeException(e);
+ }
+
+ }
+
+}
diff --git a/src/de/jandankert/prefs/win32/RegistryImport.java b/src/de/jandankert/prefs/win32/RegistryImport.java
@@ -0,0 +1,51 @@
+package de.jandankert.prefs.win32;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.List;
+import java.util.Properties;
+
+import de.jandankert.prefs.PrefSink;
+import de.jandankert.prefs.Utils;
+
+/**
+ * @author Jan Dankert
+ */
+public class RegistryImport implements PrefSink
+{
+
+ private static FileOutputStream regedit;
+
+ public RegistryImport()
+ {
+ }
+
+ public void open() throws IOException
+ {
+ new File("Windows").mkdirs();
+ regedit = new FileOutputStream(new File("Windows/prefs.reg"));
+ regedit.write("REGEDIT4\n\n".getBytes());
+ }
+
+ public void close() throws IOException
+ {
+ regedit.close();
+ }
+
+ public void sinkProperties(Properties props, List<String> path) throws IOException
+ {
+ new File("Unix/" + Utils.join(path, "/")).mkdirs();
+
+ regedit.write(("[HKEY_CURRENT_USER\\Software\\JavaSoft\\prefs\\" + Utils.join(path, "\\") + "]\n").getBytes());
+ for (Object key : props.keySet())
+ {
+
+ Object value = props.get(key);
+ regedit.write(("\"" + key + "\"=\"" + value + "\"\n").getBytes());
+
+ }
+ regedit.write("\n".getBytes());
+
+ }
+}