1 25 26 package org.snipsnap.snip.storage; 27 28 import org.dom4j.Document; 29 import org.dom4j.DocumentException; 30 import org.dom4j.DocumentHelper; 31 import org.dom4j.io.OutputFormat; 32 import org.dom4j.io.SAXReader; 33 import org.dom4j.io.XMLWriter; 34 import org.radeox.util.logging.Logger; 35 import org.snipsnap.snip.Snip; 36 37 import java.io.IOException ; 38 import java.io.InputStream ; 39 import java.io.OutputStream ; 40 import java.util.Map ; 41 42 50 51 public class XMLFileSnipStorage extends OneFileSnipStorage { 52 public final static String SNIP_XML = "snip.xml"; 53 54 private SnipSerializer serializer = SnipSerializer.getInstance(); 55 56 public static void createStorage() { 57 } 58 59 protected String getFileName() { 60 return SNIP_XML; 61 } 62 63 public XMLFileSnipStorage() { 64 } 65 66 protected Map loadSnip(InputStream in) throws IOException { 67 SAXReader saxReader = new SAXReader(); 68 69 try { 70 Document snipDocument = saxReader.read(in); 71 return serializer.getElementMap(snipDocument.getRootElement()); 72 } catch (DocumentException e) { 73 Logger.log("XMLFileSnipStorage: unable to parse snip", e); 74 } 75 76 return null; 77 } 78 79 protected void storeSnip(Snip snip, OutputStream out) { 80 Document snipDocument = DocumentHelper.createDocument(); 81 snipDocument.add(serializer.serialize(snip)); 82 83 try { 84 OutputFormat outputFormat = new OutputFormat(); 85 outputFormat.setEncoding("UTF-8"); 86 XMLWriter xmlWriter = new XMLWriter(out, outputFormat); 87 xmlWriter.write(snipDocument); 88 xmlWriter.flush(); 89 } catch (Exception e) { 90 e.printStackTrace(); 91 } 92 } 93 } 94 | Popular Tags |