Coverage details for ui.command.IO.ExportSessionListCommand

LineHitsSource
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  
300import javax.xml.parsers.DocumentBuilderFactory;
310import javax.xml.transform.Transformer;
320import javax.xml.transform.TransformerFactory;
33 import javax.xml.transform.dom.DOMSource;
34 import javax.xml.transform.stream.StreamResult;
350import javax.xml.transform.stream.StreamSource;
36  
370import org.w3c.dom.Document;
380import org.w3c.dom.Element;
390 
400import ui.Messages;
410import ui.command.Command;
420import base.ConfigurationManager;
430import base.ICXmlTags;
44 import base.IXMLSaveable;
45 import base.InternetCafe;
46 import base.InternetCafeManager;
470 
480public class ExportSessionListCommand extends Command {
490    
500    private final IXMLSaveable[] data;
510    private InputStream xsltSource = null;
520    private InputStream cssSource = null;
530    private File outputDir = null;
540    
550    public ExportSessionListCommand() {
560        super();
570        try {
580            xsltSource = new FileInputStream(ConfigurationManager.getInstance().getHtmlDirectoryPath()+File.separatorChar+ConfigurationManager.SESSION_LIST_XSL_FILE_NAME);
590            cssSource = new FileInputStream(ConfigurationManager.getInstance().getHtmlDirectoryPath()+File.separatorChar+ConfigurationManager.SESSION_LIST_CSS_FILE_NAME);
600        } catch (FileNotFoundException e) {
610            e.printStackTrace();
620        }
630        this.data = InternetCafeManager.getInstance().getSession();
640    }
65     
660    @Override
670    protected void prologo() {
680        InternetCafe.setStatusBarMessage(Messages.getString("command.exportsessionlistcommand.exportingsessionlist")); //$NON-NLS-1$
690        javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
700        chooser.setDialogTitle(Messages.getString("command.exportsessionlistcommand.message1")); //$NON-NLS-1$
710        chooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
720        chooser.setApproveButtonText(Messages.getString("button.select")); //$NON-NLS-1$
730        chooser.setMultiSelectionEnabled(true);
740        
750        if (chooser.showOpenDialog(null) == javax.swing.JFileChooser.APPROVE_OPTION){
760            outputDir = chooser.getSelectedFile();
770        
780            setStatus(EXECUTE_STATUS);
790        } else
800            setStatus(ABORT_STATUS);
810    }
820    
83     @Override
840    protected void execution() throws Exception {
850        switch (getStatus()) {
860        case ABORT_STATUS:
870            break;
880        case VETOED_STATUS:
890            break;
900        case EXECUTE_STATUS:
910            if (outputDir != null) {
920                Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
930                Element sessionListElement = document.createElement(ICXmlTags.IC_SESSION_LIST_TAG);
940                for (int i = 0; i < data.length; i++)
950                    sessionListElement.appendChild(data[i].toXml(document));
960                document.appendChild(sessionListElement);
970                Transformer transformer = TransformerFactory.newInstance().newTransformer();
980                DOMSource xmlSource = new DOMSource(document);
990    
1000                TransformerFactory factory = TransformerFactory.newInstance();
1010                transformer = factory.newTransformer(new StreamSource(xsltSource));
1020                
1030                OutputStream htmlOutput = new FileOutputStream(""+outputDir+File.separatorChar+ConfigurationManager.SESSION_LIST_HTML_FILE_NAME); //$NON-NLS-1$
1040 
1050                transformer.transform(xmlSource,new StreamResult(htmlOutput));
1060                htmlOutput.close();
1070                xsltSource.close();
1080                
1090                FileOutputStream cssOutput = new FileOutputStream(""+outputDir+File.separatorChar + ConfigurationManager.SESSION_LIST_CSS_FILE_NAME); //$NON-NLS-1$
1100                byte[] buff = new byte[1024];
1110                int l = 0;
1120                while((l = cssSource.read(buff)) > 0){
1130                    cssOutput.write(buff,0,l);
1140                }
1150                cssOutput.close();
1160                cssSource.close();
1170                
1180                setStatus(SUCCESS_STATUS);
1190            } else
1200                setStatus(ERROR_STATUS);
121             break;
1220        }
1230        
1240    }
125 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.