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.xls;
17  
18  import java.io.ByteArrayInputStream;
19  import java.io.ByteArrayOutputStream;
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.OutputStream;
26  
27  import net.sf.exorcist.api.ContentException;
28  import net.sf.exorcist.api.ContentExporter;
29  import net.sf.exorcist.api.ContentState;
30  
31  import org.apache.xml.serialize.OutputFormat;
32  import org.apache.xml.serialize.XMLSerializer;
33  import org.xml.sax.ContentHandler;
34  import org.xml.sax.SAXException;
35  
36  public class ExcelExporter implements ContentExporter {
37  
38      private String filename;
39  
40      public void setFilename(String filename) {
41          this.filename = filename;
42      }
43  
44      public void exportContent(ContentState state) throws ContentException {
45          try {
46              File tmp = File.createTempFile("exorcist", ".xml");
47              tmp.deleteOnExit();
48  
49              OutputStream output = new FileOutputStream(tmp);
50              try {
51                  OutputFormat format = new OutputFormat();
52                  format.setEncoding("ISO-8859-1");
53                  format.setIndenting(true);
54                  XMLSerializer serializer = new XMLSerializer(output, format);
55                  
56                  InputStream xls = new FileInputStream(filename);
57                  try {
58                      ExcelToXmlTransformer transformer =
59                          new ExcelToXmlTransformer();
60                      transformer.setInputStream(xls);
61                      transformer.setContentHandler(serializer.asContentHandler());
62                      transformer.transform();
63                  } finally {
64                      try { xls.close(); } catch (Exception e) {}
65                  }
66              } finally {
67                  try { output.close(); } catch (Exception e) {}
68              }
69  
70              InputStream input = new FileInputStream(tmp);
71              try {
72                  state.setContent(input);
73              } finally {
74                  try { input.close(); } catch (Exception e) {}
75              }
76          } catch (SAXException e) {
77              throw new ContentException(e);
78          } catch (IOException e) {
79              throw new ContentException(e);
80          }
81      }
82  
83      public static void main(String[] args) {
84          try {
85              ExcelExporter exporter = new ExcelExporter();
86              exporter.setFilename(args[0]);
87              exporter.exportContent(null);
88          } catch (Exception e) {
89              e.printStackTrace();
90          }
91      }
92  
93  }