KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > core > ie > PackageExport


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.core.ie;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.core.ItemType;
17 import info.magnolia.cms.core.NodeData;
18 import info.magnolia.cms.core.Path;
19
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.zip.ZipEntry JavaDoc;
30 import java.util.zip.ZipOutputStream JavaDoc;
31
32 import javax.jcr.PropertyType;
33 import javax.jcr.RepositoryException;
34
35 import org.apache.log4j.Logger;
36 import org.doomdark.uuid.UUIDGenerator;
37
38
39 /**
40  * Date: May 24, 2005 Time: 10:34:36 AM
41  * @author Sameer Charles $Id :$
42  */

43 public class PackageExport implements ExportHandler {
44
45     /**
46      * Logger.
47      */

48     protected static Logger log = Logger.getLogger(PackageExport.class);
49
50     private static final String JavaDoc START_DIRECTORY = "data"; //$NON-NLS-1$
51

52     public static final String JavaDoc DATA_FILE_NAME = "data.xml"; //$NON-NLS-1$
53

54     /**
55      * fields
56      */

57     private boolean binaryAsLink = true;
58
59     private Map JavaDoc params = new Hashtable JavaDoc();
60
61     /**
62      * this class specific parameters
63      */

64
65     public void setBinaryAsLink(boolean binaryAsLink) {
66         this.binaryAsLink = binaryAsLink;
67     }
68
69     public boolean getBinaryAsLink() {
70         return this.binaryAsLink;
71     }
72
73     public Object JavaDoc exportContent(Content content) throws RepositoryException {
74         String JavaDoc message = "export to object not supported by PackageExport"; //$NON-NLS-1$
75
log.error(message);
76         throw new UnsupportedOperationException JavaDoc(message);
77     }
78
79     public void exportContent(Content content, OutputStream JavaDoc outStream) throws RepositoryException, IOException JavaDoc {
80         ContentZipper zipper = new ContentZipper(content);
81         zipper.run();
82         // write temporary file to the stream and delete after
83
FileInputStream JavaDoc is = new FileInputStream JavaDoc(zipper.getZipFile());
84         byte[] buffer = new byte[8192];
85         int read = 0;
86         while ((read = is.read(buffer)) > 0) {
87             outStream.write(buffer, 0, read);
88         }
89         outStream.flush();
90         outStream.close();
91         is.close();
92         zipper.getZipFile().delete();
93     }
94
95     public void setParameter(String JavaDoc key, Object JavaDoc value) {
96         this.params.put(key, value);
97     }
98
99     public Object JavaDoc getParameter(String JavaDoc key) {
100         return this.params.get(key);
101     }
102
103     /**
104      * This class will be instantiated on each call to PackageExport.export method
105      */

106     class ContentZipper {
107
108         private String JavaDoc zipFileName;
109
110         private File JavaDoc zipFile;
111
112         private ZipOutputStream JavaDoc outputStream;
113
114         private Content content;
115
116         ContentZipper(Content content) {
117             this.content = content;
118         }
119
120         public void run() throws IOException JavaDoc, RepositoryException {
121             try {
122                 this.createTargetFile();
123                 this.outputStream = new ZipOutputStream JavaDoc(new FileOutputStream JavaDoc(this.zipFile));
124                 // set compression method and level
125
this.outputStream.setMethod(ZipOutputStream.DEFLATED);
126                 this.outputStream.setLevel(9);
127                 this.addTextContent();
128                 this.addBinaryContent(this.content);
129                 this.outputStream.close();
130             }
131             catch (IOException JavaDoc e) {
132                 log.error(e.getMessage());
133                 log.error("failed to pack content, deleteting temp file " + this.getZipFileName()); //$NON-NLS-1$
134
if (this.zipFile.exists()) {
135                     this.zipFile.delete();
136                 }
137                 throw e;
138             }
139         }
140
141         public String JavaDoc getZipFileName() {
142             return zipFileName;
143         }
144
145         public void setZipFileName(String JavaDoc zipFileName) {
146             this.zipFileName = zipFileName;
147         }
148
149         public File JavaDoc getZipFile() {
150             return zipFile;
151         }
152
153         public void setZipFile(File JavaDoc zipFile) {
154             this.zipFile = zipFile;
155         }
156
157         public ZipOutputStream JavaDoc getOutputStream() {
158             return outputStream;
159         }
160
161         public void setOutputStream(ZipOutputStream JavaDoc outputStream) {
162             this.outputStream = outputStream;
163         }
164
165         private void createTargetFile() throws IOException JavaDoc {
166             this.zipFileName = UUIDGenerator.getInstance().generateTimeBasedUUID().toString() + ".zip"; //$NON-NLS-1$
167
if (log.isDebugEnabled()) {
168                 log.debug("Generating zip file " + this.zipFileName); //$NON-NLS-1$
169
}
170             this.zipFile = new File JavaDoc(Path.getTempDirectoryPath() + "/" + this.zipFileName); //$NON-NLS-1$
171
this.zipFile.createNewFile();
172         }
173
174         private void addTextContent() throws IOException JavaDoc, RepositoryException {
175             XmlExport xmlExport = new XmlExport();
176             xmlExport.setBinaryAsLink(true);
177             if (log.isDebugEnabled()) {
178                 log.debug("adding a new zip file entry " + START_DIRECTORY + "/" + DATA_FILE_NAME); //$NON-NLS-1$ //$NON-NLS-2$
179
}
180             this.outputStream.putNextEntry(new ZipEntry JavaDoc(START_DIRECTORY + "/" + DATA_FILE_NAME)); //$NON-NLS-1$
181
xmlExport.exportContent(this.content, this.outputStream);
182             this.outputStream.closeEntry();
183         }
184
185         private void addBinaryContent(Content content) throws IOException JavaDoc, RepositoryException {
186             Iterator JavaDoc dataNodes = content.getNodeDataCollection().iterator();
187             while (dataNodes.hasNext()) {
188                 NodeData nodeData = (NodeData) dataNodes.next();
189                 if (nodeData.getType() == PropertyType.BINARY) {
190                     if (log.isDebugEnabled()) {
191                         log.debug("adding a new zip file entry " + START_DIRECTORY + nodeData.getHandle()); //$NON-NLS-1$
192
}
193                     this.outputStream.putNextEntry(new ZipEntry JavaDoc(START_DIRECTORY + nodeData.getHandle()));
194                     InputStream JavaDoc is = nodeData.getStream();
195                     byte[] buffer = new byte[8192];
196                     int read = 0;
197                     while ((read = is.read(buffer)) > 0) {
198                         this.outputStream.write(buffer, 0, read);
199                     }
200                     this.outputStream.closeEntry();
201                 }
202             }
203             Iterator JavaDoc subNodes = content.getChildren(ItemType.NT_BASE).iterator();
204             while (subNodes.hasNext()) {
205                 this.addBinaryContent((Content) subNodes.next());
206             }
207         }
208     }
209
210 }
211
Popular Tags