KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
38 import java.io.InputStream JavaDoc;
39 import java.io.OutputStream JavaDoc;
40 import java.util.Map JavaDoc;
41
42 /**
43  * SnipStorage backend that uses files for persisting data. This storage
44  * has limitations in the snip name length and possibly characters as well
45  * since not all filesystems can store UTF-8 file names.
46  *
47  * @author Matthias L. Jugel
48  * @version $Id: XMLFileSnipStorage.java 1606 2004-05-17 10:56:18Z leo $
49  */

50
51 public class XMLFileSnipStorage extends OneFileSnipStorage {
52   public final static String JavaDoc SNIP_XML = "snip.xml";
53
54   private SnipSerializer serializer = SnipSerializer.getInstance();
55
56   public static void createStorage() {
57   }
58
59   protected String JavaDoc getFileName() {
60     return SNIP_XML;
61   }
62
63   public XMLFileSnipStorage() {
64   }
65
66   protected Map JavaDoc loadSnip(InputStream JavaDoc in) throws IOException JavaDoc {
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 JavaDoc 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 JavaDoc e) {
90       e.printStackTrace();
91     }
92   }
93 }
94
Popular Tags