Search in sources :

Example 1 with IRuleSetWriter

use of net.sourceforge.pmd.eclipse.runtime.writer.IRuleSetWriter in project pmd-eclipse-plugin by pmd.

the class RuleTableManager method exportTo.

private void exportTo(String fileName, Shell shell) throws FileNotFoundException, WriterException, IOException {
    File file = new File(fileName);
    boolean flContinue = true;
    if (file.exists()) {
        flContinue = MessageDialog.openConfirm(shell, getMessage(StringKeys.CONFIRM_TITLE), getMessage(StringKeys.CONFIRM_RULESET_EXISTS));
    }
    InputDialog input = null;
    RuleSet ruleSet = null;
    if (flContinue) {
        ruleSet = ruleSelectionAsRuleSet();
        input = new InputDialog(shell, getMessage(StringKeys.PREF_RULESET_DIALOG_TITLE), getMessage(StringKeys.PREF_RULESET_DIALOG_RULESET_DESCRIPTION), ruleSet.getDescription() == null ? "" : ruleSet.getDescription().trim(), null);
        flContinue = input.open() == Window.OK;
    }
    if (flContinue) {
        OutputStream out = null;
        try {
            ruleSet = RuleSetUtil.setNameDescription(ruleSet, FileUtil.getFileNameWithoutExtension(file.getName()), input.getValue());
            out = new FileOutputStream(fileName);
            IRuleSetWriter writer = plugin.getRuleSetWriter();
            writer.write(out, ruleSet);
        } finally {
            IOUtil.closeQuietly(out);
        }
        MessageDialog.openInformation(shell, getMessage(StringKeys.INFORMATION_TITLE), getMessage(StringKeys.INFORMATION_RULESET_EXPORTED));
    }
}
Also used : RuleSet(net.sourceforge.pmd.RuleSet) InputDialog(org.eclipse.jface.dialogs.InputDialog) IRuleSetWriter(net.sourceforge.pmd.eclipse.runtime.writer.IRuleSetWriter) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 2 with IRuleSetWriter

use of net.sourceforge.pmd.eclipse.runtime.writer.IRuleSetWriter in project pmd-eclipse-plugin by pmd.

the class PMDPreferencePage method buildExportRuleSetButton.

/**
 * Build the export rule set button
 */
private Button buildExportRuleSetButton(Composite parent) {
    Button button = new Button(parent, SWT.PUSH | SWT.LEFT);
    button.setText(getMessage(StringKeys.PREF_RULESET_BUTTON_EXPORTRULESET));
    button.setEnabled(true);
    button.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent event) {
            FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
            String fileName = dialog.open();
            if (fileName != null) {
                try {
                    File file = new File(fileName);
                    boolean flContinue = true;
                    if (file.exists()) {
                        flContinue = MessageDialog.openConfirm(getShell(), getMessage(StringKeys.CONFIRM_TITLE), getMessage(StringKeys.CONFIRM_RULESET_EXISTS));
                    }
                    InputDialog input = null;
                    if (flContinue) {
                        input = new InputDialog(getShell(), getMessage(StringKeys.PREF_RULESET_DIALOG_TITLE), getMessage(StringKeys.PREF_RULESET_DIALOG_RULESET_DESCRIPTION), ruleSet.getDescription() == null ? "" : ruleSet.getDescription().trim(), null);
                        flContinue = input.open() == InputDialog.OK;
                    }
                    if (flContinue) {
                        ruleSet = RuleSetUtil.setNameDescription(ruleSet, getFileNameWithoutExtension(file.getName()), input.getValue());
                        OutputStream out = new FileOutputStream(fileName);
                        IRuleSetWriter writer = PMDPlugin.getDefault().getRuleSetWriter();
                        writer.write(out, ruleSet);
                        out.close();
                        MessageDialog.openInformation(getShell(), getMessage(StringKeys.INFORMATION_TITLE), getMessage(StringKeys.INFORMATION_RULESET_EXPORTED));
                    }
                } catch (IOException e) {
                    PMDPlugin.getDefault().showError(getMessage(StringKeys.ERROR_EXPORTING_RULESET), e);
                } catch (WriterException e) {
                    PMDPlugin.getDefault().showError(getMessage(StringKeys.ERROR_EXPORTING_RULESET), e);
                }
            }
        }
    });
    return button;
}
Also used : InputDialog(org.eclipse.jface.dialogs.InputDialog) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) IRuleSetWriter(net.sourceforge.pmd.eclipse.runtime.writer.IRuleSetWriter) Button(org.eclipse.swt.widgets.Button) FileOutputStream(java.io.FileOutputStream) SelectionEvent(org.eclipse.swt.events.SelectionEvent) FileDialog(org.eclipse.swt.widgets.FileDialog) File(java.io.File) WriterException(net.sourceforge.pmd.eclipse.runtime.writer.WriterException)

Example 3 with IRuleSetWriter

use of net.sourceforge.pmd.eclipse.runtime.writer.IRuleSetWriter in project pmd-eclipse-plugin by pmd.

the class ProjectPropertiesImpl method createDefaultRuleSetFile.

/**
 * Create a project ruleset file from the current configured rules
 */
public void createDefaultRuleSetFile() throws PropertiesException {
    LOG.info("Create a default rule set file for project " + this.project.getName());
    ByteArrayOutputStream baos = null;
    ByteArrayInputStream bais = null;
    try {
        IRuleSetWriter writer = PMDPlugin.getDefault().getRuleSetWriter();
        baos = new ByteArrayOutputStream();
        writer.write(baos, projectRuleSet);
        final IFile file = project.getFile(PROJECT_RULESET_FILE);
        if (file.exists() && file.isAccessible()) {
            throw new PropertiesException("Project ruleset file already exists");
        } else {
            bais = new ByteArrayInputStream(baos.toByteArray());
            file.create(bais, true, null);
        }
    } catch (WriterException e) {
        throw new PropertiesException(e);
    } catch (CoreException e) {
        throw new PropertiesException(e);
    } finally {
        IOUtil.closeQuietly(baos);
        IOUtil.closeQuietly(bais);
    }
}
Also used : IRuleSetWriter(net.sourceforge.pmd.eclipse.runtime.writer.IRuleSetWriter) IFile(org.eclipse.core.resources.IFile) PropertiesException(net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WriterException(net.sourceforge.pmd.eclipse.runtime.writer.WriterException)

Example 4 with IRuleSetWriter

use of net.sourceforge.pmd.eclipse.runtime.writer.IRuleSetWriter in project pmd-eclipse-plugin by pmd.

the class PreferencesManagerImpl method storeRuleSetInStateLocation.

/**
 * Store the rule set in preference store
 */
private void storeRuleSetInStateLocation(RuleSet ruleSet) {
    OutputStream out = null;
    PMDPlugin plugin = PMDPlugin.getDefault();
    try {
        IPath ruleSetLocation = plugin.getStateLocation().append(PREFERENCE_RULESET_FILE);
        out = new FileOutputStream(ruleSetLocation.toOSString());
        IRuleSetWriter writer = plugin.getRuleSetWriter();
        writer.write(out, ruleSet);
        out.flush();
    } catch (IOException e) {
        plugin.logError("IO Exception when storing ruleset in state location", e);
    } catch (WriterException e) {
        plugin.logError("General PMD Eclipse Exception when storing ruleset in state location", e);
    } finally {
        IOUtil.closeQuietly(out);
    }
}
Also used : IRuleSetWriter(net.sourceforge.pmd.eclipse.runtime.writer.IRuleSetWriter) IPath(org.eclipse.core.runtime.IPath) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) PMDPlugin(net.sourceforge.pmd.eclipse.plugin.PMDPlugin) IOException(java.io.IOException) WriterException(net.sourceforge.pmd.eclipse.runtime.writer.WriterException)

Aggregations

IRuleSetWriter (net.sourceforge.pmd.eclipse.runtime.writer.IRuleSetWriter)4 FileOutputStream (java.io.FileOutputStream)3 OutputStream (java.io.OutputStream)3 WriterException (net.sourceforge.pmd.eclipse.runtime.writer.WriterException)3 File (java.io.File)2 IOException (java.io.IOException)2 InputDialog (org.eclipse.jface.dialogs.InputDialog)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 RuleSet (net.sourceforge.pmd.RuleSet)1 PMDPlugin (net.sourceforge.pmd.eclipse.plugin.PMDPlugin)1 PropertiesException (net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException)1 IFile (org.eclipse.core.resources.IFile)1 CoreException (org.eclipse.core.runtime.CoreException)1 IPath (org.eclipse.core.runtime.IPath)1 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1 Button (org.eclipse.swt.widgets.Button)1 FileDialog (org.eclipse.swt.widgets.FileDialog)1