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;
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations