View Javadoc

1   /*
2    * Copyright 2005 Jukka Zitting <jz@yukatan.fi>
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.exorcist.file;
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  
25  import org.apache.commons.io.IOUtils;
26  import org.apache.xml.serialize.OutputFormat;
27  import org.apache.xml.serialize.XMLSerializer;
28  import org.xml.sax.SAXException;
29  
30  import net.sf.exorcist.api.ContentException;
31  import net.sf.exorcist.api.ContentExporter;
32  import net.sf.exorcist.api.ContentState;
33  import net.sf.exorcist.core.XmlWriter;
34  
35  /***
36   * TODO
37   */
38  public class FileExporter implements ContentExporter {
39  
40      private static final String PREFIX = null;
41  
42      private static final String NSURI = "http://exorcist.sf.net/ns/2005/file";
43  
44      private String directory;
45  
46      private XmlWriter writer;
47  
48      private ContentState state;
49  
50      public void setDirectory(String directory) {
51          this.directory = directory;
52      }
53  
54      private void export(File file) throws SAXException, IOException {
55          if (file.isHidden()) {
56              // do nothing
57          } else if (file.isDirectory()) {
58              exportDirectory(file);
59          } else if (file.isFile()) {
60              exportFile(file);
61          }
62      }
63  
64      private void exportFile(File file) throws SAXException, IOException {
65          writer.startElement("file", null);
66          writer.addElement("name", file.getName());
67          InputStream input = new FileInputStream(file);
68          try {
69              writer.addElement("data", state.addAttachment(input));
70          } finally {
71              IOUtils.closeQuietly(input);
72          }
73          writer.endElement("file");
74      }
75  
76      private void exportDirectory(File directory)
77              throws SAXException, IOException {
78          writer.startElement("directory", null);
79          writer.addElement("name", directory.getName());
80          File[] files = directory.listFiles();
81          for (int i = 0; i < files.length; i++) {
82              export(files[i]);
83          }
84          writer.endElement("directory");
85      }
86      
87      /*** {@inheritDoc} */
88      public void exportContent(ContentState state) throws ContentException {
89          try {
90              File content = File.createTempFile("exorcist", ".xml");
91              content.deleteOnExit();
92  
93              OutputStream output = new FileOutputStream(content);
94              try {
95                  OutputFormat format = new OutputFormat();
96                  format.setIndenting(true);
97                  XMLSerializer serializer = new XMLSerializer(output, format);
98  
99                  writer = new XmlWriter();
100                 writer.setContentHandler(serializer.asContentHandler());
101                 writer.setNamespace(NSURI);
102                 writer.setPrefix(PREFIX);
103 
104                 this.state = state;
105 
106                 writer.startDocument();
107                 writer.startElement("files", null);
108                 File file = new File(directory);
109                 if (file.isDirectory()) {
110                     File[] files = file.listFiles();
111                     for (int i = 0; i < files.length; i++) {
112                         export(files[i]);
113                     }
114                 } else {
115                     export(file);
116                 }
117                 writer.endElement("files");
118                 writer.endDocument();
119             } catch (SAXException e) {
120                 throw new ContentException(e);
121             } finally {
122                 IOUtils.closeQuietly(output);
123             }
124 
125             InputStream input = new FileInputStream(content);
126             try {
127                 state.setContent(input);
128             } finally {
129                 IOUtils.closeQuietly(input);
130             }
131 
132             content.delete();
133         } catch (IOException e) {
134             throw new ContentException(e);
135         }
136     }
137 
138 }