Search in sources :

Example 1 with WritableFileChooser

use of org.zaproxy.zap.view.widgets.WritableFileChooser in project zaproxy by zaproxy.

the class PopupMenuExportURLs method getOutputFile.

protected File getOutputFile() {
    WritableFileChooser chooser = new WritableFileChooser(extension.getModel().getOptionsParam().getUserDirectory());
    FileNameExtensionFilter textFilesFilter = new FileNameExtensionFilter(Constant.messages.getString("file.format.ascii"), "txt");
    FileNameExtensionFilter htmlFilesFilter = new FileNameExtensionFilter(Constant.messages.getString("file.format.html"), "html", "htm");
    chooser.addChoosableFileFilter(textFilesFilter);
    chooser.addChoosableFileFilter(htmlFilesFilter);
    chooser.setFileFilter(textFilesFilter);
    File file = null;
    int rc = chooser.showSaveDialog(extension.getView().getMainFrame());
    if (rc == JFileChooser.APPROVE_OPTION) {
        file = chooser.getSelectedFile();
        if (file == null) {
            return file;
        }
        extension.getModel().getOptionsParam().setUserDirectory(chooser.getCurrentDirectory());
        String fileNameLc = file.getAbsolutePath().toLowerCase();
        if (!fileNameLc.endsWith(".txt") && !fileNameLc.endsWith(".htm") && !fileNameLc.endsWith(".html")) {
            String ext;
            if (htmlFilesFilter.equals(chooser.getFileFilter())) {
                ext = ".html";
            } else {
                ext = ".txt";
            }
            file = new File(file.getAbsolutePath() + ext);
        }
        return file;
    }
    return file;
}
Also used : FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) File(java.io.File) WritableFileChooser(org.zaproxy.zap.view.widgets.WritableFileChooser)

Example 2 with WritableFileChooser

use of org.zaproxy.zap.view.widgets.WritableFileChooser in project zaproxy by zaproxy.

the class ReportLastScan method generateReport.

/** 
     * Generates a report. Defaults to HTML report if reportType is null.
     * @param view
     * @param model
     * @param reportType
     */
public void generateReport(ViewDelegate view, Model model, ReportType reportType) {
    // ZAP: Allow scan report file name to be specified
    final ReportType localReportType;
    if (reportType == null) {
        localReportType = ReportType.HTML;
    } else {
        localReportType = reportType;
    }
    try {
        JFileChooser chooser = new WritableFileChooser(Model.getSingleton().getOptionsParam().getUserDirectory());
        chooser.setFileFilter(new FileFilter() {

            @Override
            public boolean accept(File file) {
                if (file.isDirectory()) {
                    return true;
                } else if (file.isFile()) {
                    String lcFileName = file.getName().toLowerCase(Locale.ROOT);
                    switch(localReportType) {
                        case XML:
                            return lcFileName.endsWith(XML_FILE_EXTENSION);
                        case MD:
                            return lcFileName.endsWith(MD_FILE_EXTENSION);
                        case HTML:
                        default:
                            return (lcFileName.endsWith(HTM_FILE_EXTENSION) || lcFileName.endsWith(HTML_FILE_EXTENSION));
                    }
                }
                return false;
            }

            @Override
            public String getDescription() {
                switch(localReportType) {
                    case XML:
                        return Constant.messages.getString("file.format.xml");
                    case MD:
                        return Constant.messages.getString("file.format.md");
                    case HTML:
                    default:
                        return Constant.messages.getString("file.format.html");
                }
            }
        });
        String reportXSL = "";
        String fileExtension = "";
        switch(localReportType) {
            case XML:
                fileExtension = XML_FILE_EXTENSION;
                // Dont use XSLT
                reportXSL = null;
                break;
            case MD:
                fileExtension = MD_FILE_EXTENSION;
                reportXSL = (Constant.getZapInstall() + "/xml/report.md.xsl");
                break;
            case HTML:
            default:
                fileExtension = HTML_FILE_EXTENSION;
                reportXSL = (Constant.getZapInstall() + "/xml/report.html.xsl");
                break;
        }
        //Default the filename to a reasonable extension;
        chooser.setSelectedFile(new File(fileExtension));
        int rc = chooser.showSaveDialog(View.getSingleton().getMainFrame());
        File file = null;
        if (rc == JFileChooser.APPROVE_OPTION) {
            file = chooser.getSelectedFile();
            File report = generate(file.getAbsolutePath(), model, reportXSL);
            if (report == null) {
                view.showMessageDialog(MessageFormat.format(Constant.messages.getString("report.unknown.error"), new Object[] { file.getAbsolutePath() }));
                return;
            }
            try {
                DesktopUtils.openUrlInBrowser(report.toURI());
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                view.showMessageDialog(MessageFormat.format(Constant.messages.getString("report.complete.warning"), new Object[] { report.getAbsolutePath() }));
            }
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        view.showWarningDialog(Constant.messages.getString("report.unexpected.error"));
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) FileFilter(javax.swing.filechooser.FileFilter) File(java.io.File) WritableFileChooser(org.zaproxy.zap.view.widgets.WritableFileChooser)

Aggregations

File (java.io.File)2 WritableFileChooser (org.zaproxy.zap.view.widgets.WritableFileChooser)2 JFileChooser (javax.swing.JFileChooser)1 FileFilter (javax.swing.filechooser.FileFilter)1 FileNameExtensionFilter (javax.swing.filechooser.FileNameExtensionFilter)1