KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > storage > PropertyFileSnipStorage


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

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 JavaDoc;
34 import java.util.Properties JavaDoc;
35
36 /**
37  * SnipStorage backend that writes the metadata to a property file
38  * and the content to a plain text file
39  *
40  * @author Matthias L. Jugel
41  * @version $Id: PropertyFileSnipStorage.java 1257 2003-12-11 13:36:55Z leo $
42  */

43
44 public class PropertyFileSnipStorage extends TwoFileSnipStorage {
45   private final static String JavaDoc SNIP_FILE_PROPERTIES = "metadata.properties";
46   private final static String JavaDoc SNIP_FILE_CONTENT = "content.txt";
47
48   public static void createStorage() {
49     // there is nothing to create here, all handled dynamically
50
}
51
52   public PropertyFileSnipStorage() {
53   }
54
55   /**
56    * Return a file name for the metadata property file
57    * @return
58    */

59   protected String JavaDoc getMetadataFileName() {
60     return SNIP_FILE_PROPERTIES;
61   }
62
63   /**
64    * Return a filename for the content text file
65    * @return
66    */

67   protected String JavaDoc getContentFileName() {
68     return SNIP_FILE_CONTENT;
69   }
70
71   /**
72    * Store the content of a snip to
73    * the output stream. Content is
74    * stored as plain text
75    *
76    * @param snip
77    * @param out
78    */

79   protected void storeContent(Snip snip, OutputStream out) {
80     PrintWriter snipWriter = null;
81     try {
82       String JavaDoc 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 JavaDoc 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   /**
94    * Store the metadata of a snip to
95    * the output stream. Metadata is
96    * stored as a property file
97    *
98    * @param snip
99    * @param out
100    */

101   protected void storeMetadata(Snip snip, OutputStream out) {
102     // serialize snip, remove content and add application oid
103
Properties JavaDoc snipProps = new Properties JavaDoc();
104     SnipSerializer serializer = SnipSerializer.getInstance();
105     snipProps.putAll(serializer.createSnipMap(snip)); // new String[]{ SNIP_CONTENT });
106
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   /**
116    * Load the metadata of a snip from an InputStream and
117    * store the metadata in a Map. Metadata is in a propery
118    * file.
119    *
120    * @param in Stream to read from
121    * @return
122    * @throws IOException
123    */

124   public Map JavaDoc loadMetadata(InputStream in) throws IOException {
125     Properties JavaDoc snipProps = new Properties JavaDoc();
126     snipProps.load(in);
127     return snipProps;
128   }
129
130   /**
131    * Load the content of a snip from an InputStream.
132    * Content is in a text file.
133    *
134    * @param in Stream to read from
135    * @return
136    * @throws IOException
137    */

138   public String JavaDoc loadContent(InputStream in) throws IOException {
139     StringBuffer JavaDoc content = new StringBuffer JavaDoc();
140     String JavaDoc 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