Coverage details for ui.command.IO.ExportUserListCommand

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 
40 import ui.Messages;
410import ui.command.Command;
420import base.ConfigurationManager;
43 import base.ICXmlTags;
440import base.IXMLSaveable;
450import base.InternetCafe;
46 import base.InternetCafeManager;
47  
48 public class ExportUserListCommand 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 ExportUserListCommand() {
560        super();
570        try {
580            xsltSource = new FileInputStream(ConfigurationManager.getInstance().getHtmlDirectoryPath()+File.separatorChar+ConfigurationManager.USER_LIST_XSL_FILE_NAME);
590            cssSource = new FileInputStream(ConfigurationManager.getInstance().getHtmlDirectoryPath()+File.separatorChar+ConfigurationManager.USER_LIST_CSS_FILE_NAME);
600        } catch (FileNotFoundException e) {
610            // TODO Auto-generated catch block
620            e.printStackTrace();
630        }
640        
650        this.data = InternetCafeManager.getInstance().getUser();
660    }
670    
68     @Override
690    protected void prologo() {
700        InternetCafe.setStatusBarMessage(Messages.getString("command.exportuserlistcommand.exportinguserlist")); //$NON-NLS-1$
710        
720        javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
730        chooser.setDialogTitle(Messages.getString("command.exportuserlistcommand.message1")); //$NON-NLS-1$
740        chooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
750        chooser.setApproveButtonText(Messages.getString("button.select")); //$NON-NLS-1$
760        chooser.setMultiSelectionEnabled(true);
770        
780        if (chooser.showOpenDialog(null) == javax.swing.JFileChooser.APPROVE_OPTION){
790            outputDir = chooser.getSelectedFile();
800        
810            setStatus(EXECUTE_STATUS);
820        } else
830            setStatus(ABORT_STATUS);
840    }
850    
86     @Override
870    protected void execution() throws Exception {
880        switch (getStatus()) {
890        case ABORT_STATUS:
900            break;
910        case VETOED_STATUS:
920            break;
930        case EXECUTE_STATUS:
940            if (outputDir != null) {
950                Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
960                Element userListElement = document.createElement(ICXmlTags.IC_USER_LIST_TAG);
970                for (int i = 0; i < data.length; i++)
980                    userListElement.appendChild(data[i].toXml(document));
990                document.appendChild(userListElement);
1000                Transformer transformer = TransformerFactory.newInstance().newTransformer();
1010                DOMSource xmlSource = new DOMSource(document);
1020    
1030                TransformerFactory factory = TransformerFactory.newInstance();
1040                transformer = factory.newTransformer(new StreamSource(xsltSource));
1050                
1060                OutputStream htmlOutput = new FileOutputStream(""+outputDir+File.separatorChar+ConfigurationManager.USER_LIST_HTML_FILE_NAME); //$NON-NLS-1$
1070 
1080                transformer.transform(xmlSource,new StreamResult(htmlOutput));
1090                htmlOutput.close();
1100                xsltSource.close();
1110                
1120                FileOutputStream cssOutput = new FileOutputStream(""+outputDir+File.separatorChar + ConfigurationManager.USER_LIST_CSS_FILE_NAME); //$NON-NLS-1$
1130                byte[] buff = new byte[1024];
1140                int l = 0;
1150                while((l = cssSource.read(buff)) > 0){
1160                    cssOutput.write(buff,0,l);
1170                }
1180                cssOutput.close();
1190                cssSource.close();
1200                
1210                setStatus(SUCCESS_STATUS);
1220            } else
1230                setStatus(ERROR_STATUS);
124             break;
1250        }
1260        
1270    }
128 }

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.