Search in sources :

Example 1 with WriterException

use of net.sourceforge.pmd.eclipse.runtime.writer.WriterException 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 2 with WriterException

use of net.sourceforge.pmd.eclipse.runtime.writer.WriterException 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 3 with WriterException

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

the class AstWriterImpl method write.

/**
 * @see net.sourceforge.pmd.eclipse.runtime.writer.IAstWriter#write(java.io.Writer,
 *      net.sourceforge.pmd.ast.ASTCompilationUnit)
 */
public void write(OutputStream outputStream, ASTCompilationUnit compilationUnit) throws WriterException {
    try {
        Document doc = compilationUnit.getAsDocument();
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(doc), new StreamResult(outputStream));
    } catch (DOMException e) {
        throw new WriterException(e);
    } catch (FactoryConfigurationError e) {
        throw new WriterException(e);
    } catch (TransformerException e) {
        throw new WriterException(e);
    }
}
Also used : DOMException(org.w3c.dom.DOMException) DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Document(org.w3c.dom.Document) FactoryConfigurationError(javax.xml.parsers.FactoryConfigurationError) WriterException(net.sourceforge.pmd.eclipse.runtime.writer.WriterException) TransformerException(javax.xml.transform.TransformerException)

Example 4 with WriterException

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

the class PMDGenerateASTAction method generateAST.

/**
 * Generate a AST for a file
 *
 * @param file
 *            a file
 */
private void generateAST(IFile file) {
    LOG.info("Generating AST for file " + file.getName());
    ByteArrayOutputStream byteArrayOutputStream = null;
    ByteArrayInputStream astInputStream = null;
    try {
        JavaParser parser = new JavaParser(new JavaCharStream(file.getContents()));
        parser.setJdkVersion(Integer.MAX_VALUE);
        ASTCompilationUnit compilationUnit = parser.CompilationUnit();
        byteArrayOutputStream = new ByteArrayOutputStream();
        IAstWriter astWriter = PMDPlugin.getDefault().getAstWriter();
        astWriter.write(byteArrayOutputStream, compilationUnit);
        byteArrayOutputStream.flush();
        IFile astFile = createASTFile(file);
        if (astFile != null) {
            if (astFile.exists()) {
                astFile.delete(false, null);
            }
            astInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            astFile.create(astInputStream, false, null);
        }
    } catch (CoreException e) {
        showErrorById(StringKeys.ERROR_CORE_EXCEPTION, e);
    } catch (ParseException e) {
        showErrorById(StringKeys.ERROR_PMD_EXCEPTION, e);
    } catch (WriterException e) {
        showErrorById(StringKeys.ERROR_PMD_EXCEPTION, e);
    } catch (IOException e) {
        showErrorById(StringKeys.ERROR_IO_EXCEPTION, e);
    } finally {
        IOUtil.closeQuietly(byteArrayOutputStream);
        IOUtil.closeQuietly(astInputStream);
    }
}
Also used : JavaParser(net.sourceforge.pmd.lang.java.ast.JavaParser) IFile(org.eclipse.core.resources.IFile) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) IAstWriter(net.sourceforge.pmd.eclipse.runtime.writer.IAstWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ParseException(net.sourceforge.pmd.lang.java.ast.ParseException) IOException(java.io.IOException) JavaCharStream(net.sourceforge.pmd.lang.ast.JavaCharStream) WriterException(net.sourceforge.pmd.eclipse.runtime.writer.WriterException)

Example 5 with WriterException

use of net.sourceforge.pmd.eclipse.runtime.writer.WriterException 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

WriterException (net.sourceforge.pmd.eclipse.runtime.writer.WriterException)6 IOException (java.io.IOException)4 IRuleSetWriter (net.sourceforge.pmd.eclipse.runtime.writer.IRuleSetWriter)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 IFile (org.eclipse.core.resources.IFile)2 CoreException (org.eclipse.core.runtime.CoreException)2 File (java.io.File)1 FactoryConfigurationError (javax.xml.parsers.FactoryConfigurationError)1 Transformer (javax.xml.transform.Transformer)1 TransformerException (javax.xml.transform.TransformerException)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 RuleSetWriter (net.sourceforge.pmd.RuleSetWriter)1 PMDPlugin (net.sourceforge.pmd.eclipse.plugin.PMDPlugin)1 PropertiesException (net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException)1 IAstWriter (net.sourceforge.pmd.eclipse.runtime.writer.IAstWriter)1 JavaCharStream (net.sourceforge.pmd.lang.ast.JavaCharStream)1