1 /********************************************************************************
2 * InternetCafe is a software solution that helps the management of Cybercafes
3 * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4 * Copyright (C) 2006 Guido Angelo Ingenito
5
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *******************************************************************************/
20
21 package ui.command.IO;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.FileOutputStream;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29
30 import javax.xml.parsers.DocumentBuilderFactory;
31 import javax.xml.transform.Transformer;
32 import javax.xml.transform.TransformerFactory;
33 import javax.xml.transform.dom.DOMSource;
34 import javax.xml.transform.stream.StreamResult;
35 import javax.xml.transform.stream.StreamSource;
36
37 import org.w3c.dom.Document;
38 import org.w3c.dom.Element;
39
40 import ui.Messages;
41 import ui.command.Command;
42 import base.ConfigurationManager;
43 import base.ICXmlTags;
44 import base.IXMLSaveable;
45 import base.InternetCafe;
46 import base.InternetCafeManager;
47
48 public class ExportSessionListCommand extends Command {
49
50 private final IXMLSaveable[] data;
51 private InputStream xsltSource = null;
52 private InputStream cssSource = null;
53 private File outputDir = null;
54
55 public ExportSessionListCommand() {
56 super();
57 try {
58 xsltSource = new FileInputStream(ConfigurationManager.getInstance().getHtmlDirectoryPath()+File.separatorChar+ConfigurationManager.SESSION_LIST_XSL_FILE_NAME);
59 cssSource = new FileInputStream(ConfigurationManager.getInstance().getHtmlDirectoryPath()+File.separatorChar+ConfigurationManager.SESSION_LIST_CSS_FILE_NAME);
60 } catch (FileNotFoundException e) {
61 e.printStackTrace();
62 }
63 this.data = InternetCafeManager.getInstance().getSession();
64 }
65
66 @Override
67 protected void prologo() {
68 InternetCafe.setStatusBarMessage(Messages.getString("command.exportsessionlistcommand.exportingsessionlist"));
69 javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
70 chooser.setDialogTitle(Messages.getString("command.exportsessionlistcommand.message1"));
71 chooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
72 chooser.setApproveButtonText(Messages.getString("button.select"));
73 chooser.setMultiSelectionEnabled(true);
74
75 if (chooser.showOpenDialog(null) == javax.swing.JFileChooser.APPROVE_OPTION){
76 outputDir = chooser.getSelectedFile();
77
78 setStatus(EXECUTE_STATUS);
79 } else
80 setStatus(ABORT_STATUS);
81 }
82
83 @Override
84 protected void execution() throws Exception {
85 switch (getStatus()) {
86 case ABORT_STATUS:
87 break;
88 case VETOED_STATUS:
89 break;
90 case EXECUTE_STATUS:
91 if (outputDir != null) {
92 Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
93 Element sessionListElement = document.createElement(ICXmlTags.IC_SESSION_LIST_TAG);
94 for (int i = 0; i < data.length; i++)
95 sessionListElement.appendChild(data[i].toXml(document));
96 document.appendChild(sessionListElement);
97 Transformer transformer = TransformerFactory.newInstance().newTransformer();
98 DOMSource xmlSource = new DOMSource(document);
99
100 TransformerFactory factory = TransformerFactory.newInstance();
101 transformer = factory.newTransformer(new StreamSource(xsltSource));
102
103 OutputStream htmlOutput = new FileOutputStream(""+outputDir+File.separatorChar+ConfigurationManager.SESSION_LIST_HTML_FILE_NAME);
104
105 transformer.transform(xmlSource,new StreamResult(htmlOutput));
106 htmlOutput.close();
107 xsltSource.close();
108
109 FileOutputStream cssOutput = new FileOutputStream(""+outputDir+File.separatorChar + ConfigurationManager.SESSION_LIST_CSS_FILE_NAME);
110 byte[] buff = new byte[1024];
111 int l = 0;
112 while((l = cssSource.read(buff)) > 0){
113 cssOutput.write(buff,0,l);
114 }
115 cssOutput.close();
116 cssSource.close();
117
118 setStatus(SUCCESS_STATUS);
119 } else
120 setStatus(ERROR_STATUS);
121 break;
122 }
123
124 }
125 }