1 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 ; 42 import java.io.ByteArrayOutputStream ; 43 import java.io.File ; 44 import java.io.FileInputStream ; 45 import java.io.IOException ; 46 import java.io.OutputStream ; 47 import java.util.Iterator ; 48 import java.util.List ; 49 50 55 public class XMLSnipExport { 56 57 private static ThreadLocal instance = new ThreadLocal () { 58 protected synchronized Object 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 out, List snips, List users, String filter, List ignoreElements, File 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 e) { 80 e.printStackTrace(); 81 } 82 } 83 84 private static void storeUsers(XMLWriter xmlWriter, List users) throws IOException { 85 if (users != null && users.size() > 0) { 86 UserSerializer userSerializer = UserSerializer.getInstance(); 87 Iterator 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 snips, String filter, List ignoreElements, File fileStore) 97 throws IOException { 98 if (snips != null && snips.size() > 0) { 99 SnipSerializer snipSerializer = SnipSerializer.getInstance(); 100 101 Iterator 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 filterIt = ignoreElements.iterator(); 108 while (filterIt.hasNext()) { 109 String el = (String ) 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 snipVersions = versionManager.getHistory(snip); 129 130 Element versionsElement = snipEl.addElement("versions"); 131 Iterator 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 versionsElement.add(versionSnipEl); 138 } 139 } 140 141 private static void storeAttachments(Element snipEl, File attRoot) { 142 Element attachmentsEl = snipEl.element("attachments"); 143 Iterator attIt = attachmentsEl.elements("attachment").iterator(); 144 while (attIt.hasNext()) { 145 Element att = (Element) attIt.next(); 146 try { 147 addAttachmentFile(att, new File (attRoot, att.elementText("location"))); 148 } catch (Exception e) { 149 Logger.fatal("unable to export attachment: " + e); 150 e.printStackTrace(); 151 } 152 } 153 } 154 155 public static void addAttachmentFile(Element att, File attFile) throws IOException { 156 ByteArrayOutputStream data = new ByteArrayOutputStream (); 157 BufferedInputStream fileIs = new BufferedInputStream (new FileInputStream (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 (Base64.encodeBase64(data.toByteArray()), "UTF-8")); 165 } 166 } 167 | Popular Tags |