1 25 26 package org.snipsnap.snip.storage; 27 28 import org.radeox.util.logging.Logger; 29 import org.snipsnap.snip.Snip; 30 import org.snipsnap.app.Application; 31 32 import java.io.*; 33 import java.util.Map ; 34 import java.util.Properties ; 35 36 43 44 public class PropertyFileSnipStorage extends TwoFileSnipStorage { 45 private final static String SNIP_FILE_PROPERTIES = "metadata.properties"; 46 private final static String SNIP_FILE_CONTENT = "content.txt"; 47 48 public static void createStorage() { 49 } 51 52 public PropertyFileSnipStorage() { 53 } 54 55 59 protected String getMetadataFileName() { 60 return SNIP_FILE_PROPERTIES; 61 } 62 63 67 protected String getContentFileName() { 68 return SNIP_FILE_CONTENT; 69 } 70 71 79 protected void storeContent(Snip snip, OutputStream out) { 80 PrintWriter snipWriter = null; 81 try { 82 String enc = Application.get().getConfiguration().getEncoding(); 83 snipWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out, enc != null ? enc : "UTF-8"))); 84 snipWriter.print(snip.getContent()); 85 } catch (Exception e) { 86 Logger.log("FileSnipStorage: unable to store snip content" + snip.getName(), e); 87 } finally { 88 snipWriter.flush(); 89 snipWriter.close(); 90 } 91 } 92 93 101 protected void storeMetadata(Snip snip, OutputStream out) { 102 Properties snipProps = new Properties (); 104 SnipSerializer serializer = SnipSerializer.getInstance(); 105 snipProps.putAll(serializer.createSnipMap(snip)); snipProps.remove(SnipSerializer.SNIP_CONTENT); 107 108 try { 109 snipProps.store(out, "Properties for " + snip.getName()); 110 } catch (IOException e) { 111 Logger.log("FileSnipStorage: unable to store properties for '" + snip.getName() + "'"); 112 } 113 } 114 115 124 public Map loadMetadata(InputStream in) throws IOException { 125 Properties snipProps = new Properties (); 126 snipProps.load(in); 127 return snipProps; 128 } 129 130 138 public String loadContent(InputStream in) throws IOException { 139 StringBuffer content = new StringBuffer (); 140 String enc = Application.get().getConfiguration().getEncoding(); 141 BufferedReader snipReader = new BufferedReader(new InputStreamReader(in, enc != null ? enc : "UTF-8")); 142 char[] buffer = new char[8192]; 143 int length = 0; 144 while ((length = snipReader.read(buffer)) != -1) { 145 content.append(buffer, 0, length); 146 } 147 return content.toString(); 148 } 149 } 150 | Popular Tags |