KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > XMLSnipExport


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 package org.snipsnap.snip;
26
27 import org.dom4j.DocumentHelper;
28 import org.dom4j.Element;
29 import org.dom4j.io.OutputFormat;
30 import org.dom4j.io.XMLWriter;
31 import org.radeox.util.logging.Logger;
32 import org.snipsnap.container.Components;
33 import org.snipsnap.jdbc.IntHolder;
34 import org.snipsnap.snip.storage.SnipSerializer;
35 import org.snipsnap.snip.storage.UserSerializer;
36 import org.snipsnap.user.User;
37 import org.snipsnap.versioning.VersionInfo;
38 import org.snipsnap.versioning.VersionManager;
39 import org.apache.commons.codec.binary.Base64;
40
41 import java.io.BufferedInputStream JavaDoc;
42 import java.io.ByteArrayOutputStream JavaDoc;
43 import java.io.File JavaDoc;
44 import java.io.FileInputStream JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.io.OutputStream JavaDoc;
47 import java.util.Iterator JavaDoc;
48 import java.util.List JavaDoc;
49
50 /**
51  * Helper class for exporting Snips and users as XML document.
52  * @author Matthias L. Jugel
53  * @version $Id: XMLSnipExport.java 1835 2005-10-05 12:57:31Z leo $
54  */

55 public class XMLSnipExport {
56
57   private static ThreadLocal JavaDoc instance = new ThreadLocal JavaDoc() {
58     protected synchronized Object JavaDoc initialValue() {
59       return new IntHolder(0);
60     }
61   };
62
63   public static IntHolder getStatus() {
64     return (IntHolder) instance.get();
65   }
66
67   public static void store(OutputStream JavaDoc out, List JavaDoc snips, List JavaDoc users, String JavaDoc filter, List JavaDoc ignoreElements, File JavaDoc fileStore) {
68     try {
69       OutputFormat outputFormat = new OutputFormat();
70       outputFormat.setEncoding("UTF-8");
71       outputFormat.setNewlines(true);
72       XMLWriter xmlWriter = new XMLWriter(out, outputFormat);
73       Element root = DocumentHelper.createElement("snipspace");
74       xmlWriter.writeOpen(root);
75       storeUsers(xmlWriter, users);
76       storeSnips(xmlWriter, snips, filter, ignoreElements, fileStore);
77       xmlWriter.writeClose(root);
78       xmlWriter.flush();
79     } catch (Exception JavaDoc e) {
80       e.printStackTrace();
81     }
82   }
83
84   private static void storeUsers(XMLWriter xmlWriter, List JavaDoc users) throws IOException JavaDoc {
85     if (users != null && users.size() > 0) {
86       UserSerializer userSerializer = UserSerializer.getInstance();
87       Iterator JavaDoc userListIterator = users.iterator();
88       while (userListIterator.hasNext()) {
89         User user = (User) userListIterator.next();
90         xmlWriter.write(userSerializer.serialize(user));
91         getStatus().inc();
92       }
93     }
94   }
95
96   private static void storeSnips(XMLWriter xmlWriter, List JavaDoc snips, String JavaDoc filter, List JavaDoc ignoreElements, File JavaDoc fileStore)
97     throws IOException JavaDoc {
98     if (snips != null && snips.size() > 0) {
99       SnipSerializer snipSerializer = SnipSerializer.getInstance();
100
101       Iterator JavaDoc snipListIterator = snips.iterator();
102       while (snipListIterator.hasNext()) {
103         Snip snip = (Snip) snipListIterator.next();
104         if (filter == null || !snip.getName().matches(filter)) {
105           Element snipEl = snipSerializer.serialize(snip);
106           if (null != ignoreElements) {
107             Iterator JavaDoc filterIt = ignoreElements.iterator();
108             while (filterIt.hasNext()) {
109               String JavaDoc el = (String JavaDoc) filterIt.next();
110               if (snipEl.element(el) != null) {
111                 snipEl.remove(snipEl.element(el));
112               }
113             }
114           }
115           storeAttachments(snipEl, fileStore);
116           storeVersions(snipEl, snip);
117           xmlWriter.write(snipEl);
118         }
119         getStatus().inc();
120       }
121     }
122   }
123
124   private static void storeVersions(Element snipEl, Snip snip) {
125     SnipSerializer serializer = SnipSerializer.getInstance();
126
127     VersionManager versionManager = (VersionManager) Components.getComponent(VersionManager.class);
128     List JavaDoc snipVersions = versionManager.getHistory(snip);
129
130     Element versionsElement = snipEl.addElement("versions");
131     Iterator JavaDoc versionsIt = snipVersions.iterator();
132     while (versionsIt.hasNext()) {
133       VersionInfo versionInfo = (VersionInfo) versionsIt.next();
134       Snip versionSnip = versionManager.loadVersion(snip, versionInfo.getVersion());
135       Element versionSnipEl = serializer.serialize(versionSnip);
136 // versionSnipEl.addAttribute("version", "" + versionInfo.getVersion());
137
versionsElement.add(versionSnipEl);
138     }
139   }
140
141   private static void storeAttachments(Element snipEl, File JavaDoc attRoot) {
142     Element attachmentsEl = snipEl.element("attachments");
143     Iterator JavaDoc attIt = attachmentsEl.elements("attachment").iterator();
144     while (attIt.hasNext()) {
145       Element att = (Element) attIt.next();
146       try {
147         addAttachmentFile(att, new File JavaDoc(attRoot, att.elementText("location")));
148       } catch (Exception JavaDoc e) {
149         Logger.fatal("unable to export attachment: " + e);
150         e.printStackTrace();
151       }
152     }
153   }
154
155   public static void addAttachmentFile(Element att, File JavaDoc attFile) throws IOException JavaDoc {
156     ByteArrayOutputStream JavaDoc data = new ByteArrayOutputStream JavaDoc();
157     BufferedInputStream JavaDoc fileIs = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(attFile));
158     int count = 0;
159     byte[] buffer = new byte[8192];
160     while ((count = fileIs.read(buffer)) != -1) {
161       data.write(buffer, 0, count);
162     }
163     data.close();
164     att.addElement("data").addText(new String JavaDoc(Base64.encodeBase64(data.toByteArray()), "UTF-8"));
165   }
166 }
167
Popular Tags