File src/de/jandankert/prefs/unix/PrefsXMLCreator.java
Last commit: Fri Jan 6 00:04:37 2012 +0100 Jan Dankert let's start...
1 package de.jandankert.prefs.unix; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.util.List; 7 import java.util.Properties; 8 9 import javax.xml.parsers.DocumentBuilder; 10 import javax.xml.parsers.DocumentBuilderFactory; 11 import javax.xml.parsers.ParserConfigurationException; 12 import javax.xml.transform.Transformer; 13 import javax.xml.transform.TransformerException; 14 import javax.xml.transform.dom.DOMSource; 15 import javax.xml.transform.sax.SAXTransformerFactory; 16 import javax.xml.transform.stream.StreamResult; 17 18 import org.w3c.dom.Document; 19 import org.w3c.dom.Element; 20 21 import de.jandankert.prefs.PrefSink; 22 import de.jandankert.prefs.Utils; 23 24 /** 25 * @author Jan Dankert 26 */ 27 public class PrefsXMLCreator implements PrefSink 28 { 29 30 public void open() throws IOException 31 { 32 // nothing to do... 33 // a new file is created on each properties-set. 34 } 35 36 public void close() throws IOException 37 { 38 } 39 40 public void sinkProperties(Properties props, List<String> path) throws IOException 41 { 42 new File("Unix/" + Utils.join(path, "/")).mkdirs(); 43 44 FileOutputStream prefsXMLOutputStream = new FileOutputStream("Unix/" + Utils.join(path, "/") + "/prefs.xml"); 45 DocumentBuilder builder = null; 46 try 47 { 48 builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 49 } 50 catch (ParserConfigurationException e) 51 { 52 throw new RuntimeException(e); 53 } 54 55 Document newDocument = builder.newDocument(); 56 Element mapElement = newDocument.createElement("map"); 57 for (Object key : props.keySet()) 58 { 59 60 Object value = props.get(key); 61 62 Element entry = newDocument.createElement("entry"); 63 entry.setAttribute("key", key.toString()); 64 entry.setAttribute("value", value.toString()); 65 mapElement.appendChild(entry); 66 } 67 68 newDocument.appendChild(mapElement); 69 try 70 { 71 Transformer transformer = SAXTransformerFactory.newInstance().newTransformer(); 72 transformer.transform(new DOMSource(newDocument), new StreamResult(prefsXMLOutputStream)); 73 SAXTransformerFactory.newInstance().newTransformer(); 74 } 75 catch (TransformerException e) 76 { 77 e.printStackTrace(System.err); 78 throw new RuntimeException(e); 79 } 80 81 } 82 83 }
Downloadsrc/de/jandankert/prefs/unix/PrefsXMLCreator.java
History Fri, 6 Jan 2012 00:04:37 +0100 Jan Dankert let's start...