Applying Command and CommandProcessor patterns

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@3038 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Philippe Herlin
2004-11-21 21:39:45 +00:00
parent b1f6e9ae94
commit 990150d236
16 changed files with 2059 additions and 0 deletions

View File

@ -0,0 +1,89 @@
/*
* Created on 21 nov. 2004
*
* Copyright (c) 2004, PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.sourceforge.pmd.eclipse.cmd;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
/**
* Rebuild a project to force PMD to be run on that project.
*
* @author Philippe Herlin
* @version $Revision$
*
* $Log$
* Revision 1.1 2004/11/21 21:39:45 phherlin
* Applying Command and CommandProcessor patterns
*
*
*/
public class BuildProjectCommand extends JobCommand {
private IProject project;
/**
* @param name
*/
public BuildProjectCommand() {
super("Building project");
setReadOnly(false);
setOutputData(false);
setName("BuildProject");
setDescription("Rebuild a project.");
}
/**
* @see net.sourceforge.pmd.eclipse.cmd.JobCommand#execute()
*/
protected IStatus execute() throws CommandException {
try {
this.project.build(IncrementalProjectBuilder.FULL_BUILD, this.getMonitor());
} catch (CoreException e) {
throw new CommandException(e);
}
return this.getMonitor().isCanceled() ? Status.CANCEL_STATUS : Status.OK_STATUS;
}
/**
* @param project The project to set.
*/
public void setProject(IProject project) {
this.project = project;
}
}

View File

@ -0,0 +1,77 @@
/*
* Created on 20 nov. 2004
*
* Copyright (c) 2004, PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.sourceforge.pmd.eclipse.cmd;
/**
* This the abstraction of a Command
*
* @author Philippe Herlin
* @version $Revision$
*
* $Log$
* Revision 1.1 2004/11/21 21:39:45 phherlin
* Applying Command and CommandProcessor patterns
*
*
*/
public interface Command {
/**
* To execute a command
* @throws CommandException if a technical or internal error occurred
*/
void performExecute() throws CommandException;
/**
* @return the name of the Command
*/
String getName();
/**
* @return the description of the Command
*/
String getDescription();
/**
*
* @return whether the command apply some modifications or not
*/
boolean isReadOnly();
/**
* @return whether the command has a result or output data
*/
boolean hasOutputData();
}

View File

@ -0,0 +1,81 @@
/*
* Created on 20 nov. 2004
*
* Copyright (c) 2004, PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.sourceforge.pmd.eclipse.cmd;
/**
* This is the default exception thrown when a technical error occurred during
* the exception of a command
*
* @author Philippe Herlin
* @version $Revision$
*
* $Log$
* Revision 1.1 2004/11/21 21:39:45 phherlin
* Applying Command and CommandProcessor patterns
*
*
*/
public class CommandException extends Exception {
/**
* Default constructor
*/
public CommandException() {
super();
}
/**
* Constructor with a message
* @param message
*/
public CommandException(String message) {
super(message);
}
/**
* Constructor whith a message and a root exception
* @param message
* @param cause
*/
public CommandException(String message, Throwable cause) {
super(message, cause);
}
/**
* Construction with only a root exception
* @param cause
*/
public CommandException(Throwable cause) {
super(cause);
}
}

View File

@ -0,0 +1,142 @@
/*
* Created on 20 nov. 2004
*
* Copyright (c) 2004, PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.sourceforge.pmd.eclipse.cmd;
import net.sourceforge.pmd.eclipse.PMDConstants;
import net.sourceforge.pmd.eclipse.PMDPlugin;
import net.sourceforge.pmd.eclipse.PMDPluginConstants;
/**
* This is a base implementation for a command.
* This could be used as a root implementation for the simplest a command
* which is a command that executes whitout any Command Processor.
* Or this could be used as a base implementation for more complex form such as
* Job Command.
*
* @author Philippe Herlin
* @version $Revision$
*
* $Log$
* Revision 1.1 2004/11/21 21:39:45 phherlin
* Applying Command and CommandProcessor patterns
*
*
*/
public abstract class DefaultCommand implements Command, PMDConstants, PMDPluginConstants {
private boolean readOnly;
private String description;
private String name;
private boolean outputData;
/**
* @return Returns the readOnly.
*/
public boolean isReadOnly() {
return readOnly;
}
/**
* @param readOnly The readOnly to set.
*/
public void setReadOnly(boolean readOnly) {
this.readOnly = readOnly;
}
/**
* @return Returns the description.
*/
public String getDescription() {
return description;
}
/**
* @param description The description to set.
*/
public void setDescription(String description) {
this.description = description;
}
/**
* @return Returns the outputData.
*/
public boolean hasOutputData() {
return outputData;
}
/**
* @param outputData The outputData to set.
*/
public void setOutputData(boolean hasOutputData) {
this.outputData = hasOutputData;
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}
/**
* By default a command is executed without a Command Processor
* @throws CommandException if a internal technical error occurred
*/
public void performExecute() throws CommandException {
execute();
}
/**
* The implementation of the command logic
* @throws CommandException if a internal technical error occurred
*/
protected abstract void execute() throws CommandException;
/**
* Helper method to shorten message access
* @param key a message key
* @return requested message
*/
protected String getMessage(String key) {
return PMDPlugin.getDefault().getMessage(key);
}
}

View File

@ -0,0 +1,183 @@
/*
* Created on 21 nov. 2004
*
* Copyright (c) 2004, PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.sourceforge.pmd.eclipse.cmd;
import net.sourceforge.pmd.eclipse.PMDConstants;
import net.sourceforge.pmd.eclipse.PMDPluginConstants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
/**
* This class is a default implementation for job commands. Job commands are
* standard Eclipse Jobs and, therefore are executed under the control of the
* Eclipse Job Processor which acts as a Command Processor.
* Job commands are used for long running commands such as a buildin a project,
* runing PMD against a project, or commands that perfoms ressource updates.
*
* @author Philippe Herlin
* @version $Revision$
*
* $Log$
* Revision 1.1 2004/11/21 21:39:45 phherlin
* Applying Command and CommandProcessor patterns
*
*
*/
public abstract class JobCommand extends Job implements Command, PMDConstants, PMDPluginConstants {
private static final Log log = LogFactory.getLog("net.sourceforge.pmd.eclipse.cmd.JobCommand");
private IProgressMonitor monitor;
private boolean readOnly;
private String description;
private String name;
private boolean outputData;
/**
* Default constructor for a job
* @param name
*/
public JobCommand(String name) {
super(name);
}
/**
* @return Returns the readOnly.
*/
public boolean isReadOnly() {
return readOnly;
}
/**
* @param readOnly The readOnly to set.
*/
public void setReadOnly(boolean readOnly) {
this.readOnly = readOnly;
}
/**
* @return Returns the description.
*/
public String getDescription() {
return description;
}
/**
* @param description The description to set.
*/
public void setDescription(String description) {
this.description = description;
}
/**
* @return Returns the outputData.
*/
public boolean hasOutputData() {
return outputData;
}
/**
* @param outputData The outputData to set.
*/
public void setOutputData(boolean hasOutputData) {
this.outputData = hasOutputData;
}
/**
* @return the command default priority. The default is Job.SHORT.
* Any concrete command may override this method to set a different priority.
*/
public int getDefaultPriority() {
return Job.SHORT;
}
/**
* @see net.sourceforge.pmd.eclipse.cmd.Command#performExecute()
*/
public void performExecute() throws CommandException {
log.debug("Begining a job command");
try {
this.setPriority(this.getDefaultPriority());
this.schedule();
this.join();
} catch (InterruptedException e) {
throw new CommandException(e);
} finally {
log.debug("Ending a job command");
}
}
/**
* Execute the command in the specified delay.
*/
public void performExecuteWithDelay(long delay) throws CommandException {
this.setPriority(this.getDefaultPriority());
this.schedule(delay);
}
/**
* @return Returns the monitor.
*/
protected IProgressMonitor getMonitor() {
return this.monitor;
}
/**
* @see org.eclipse.core.internal.jobs.InternalJob#run(org.eclipse.core.runtime.IProgressMonitor)
*/
protected IStatus run(IProgressMonitor monitor) {
this.monitor = monitor;
IStatus executionStatus = null;
try {
executionStatus = this.execute();
} catch (CommandException e) {
executionStatus = new Status(Status.ERROR, PLUGIN_ID, 0, e.getMessage(), e);
}
return executionStatus;
}
/**
* Method that implements the actual command logic. Each concrete command
* must implement this method.
* @return
*/
protected abstract IStatus execute() throws CommandException;
}

View File

@ -0,0 +1,61 @@
/*
* Created on 20 nov. 2004
*
* Copyright (c) 2004, PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.sourceforge.pmd.eclipse.cmd;
/**
* Exception thrown when a command is invoked when some mandatory parameters are
* missing (are null). The message contains the name of the missing parameter.
*
* @author Philippe Herlin
* @version $Revision$
*
* $Log$
* Revision 1.1 2004/11/21 21:39:45 phherlin
* Applying Command and CommandProcessor patterns
*
*
*/
public class MandatoryInputParameterMissingException extends CommandException {
/**
* Constructor with a message
* @param message should be the name of the missing parameter
*/
public MandatoryInputParameterMissingException(String message) {
super(message);
}
}

View File

@ -0,0 +1,99 @@
/*
* Created on 20 nov. 2004
*
* Copyright (c) 2004, PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.sourceforge.pmd.eclipse.cmd;
import net.sourceforge.pmd.eclipse.builder.PMDNature;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
/**
* Query whether PMD is enabled for a project
*
* @author Philippe Herlin
* @version $Revision$
*
* $Log$
* Revision 1.1 2004/11/21 21:39:45 phherlin
* Applying Command and CommandProcessor patterns
*
*
*/
public class QueryPmdEnabledPropertyCmd extends DefaultCommand {
private IProject project;
private boolean pmdEnabled;
/**
* Default constructor. Initializes command attributes
*
*/
public QueryPmdEnabledPropertyCmd() {
setReadOnly(true);
setOutputData(true);
setName("QueryPmdEnabledProperty");
setDescription("Query whether PMD is enabled for a project.");
}
/**
* @see net.sourceforge.pmd.eclipse.cmd.DefaultCommand#execute()
*/
protected void execute() throws CommandException {
if (this.project == null ) throw new MandatoryInputParameterMissingException("project");
boolean fEnabled = false;
try {
fEnabled = project.hasNature(PMDNature.PMD_NATURE);
} catch (CoreException e) {
throw new CommandException(getMessage(MSGKEY_ERROR_CORE_EXCEPTION), e);
}
this.pmdEnabled = fEnabled;
}
/**
* @return Returns the pmdEnabled.
*/
public boolean isPmdEnabled() {
return pmdEnabled;
}
/**
* @param project The project to set.
*/
public void setProject(IProject project) {
this.project = project;
}
}

View File

@ -0,0 +1,143 @@
/*
* Created on 20 nov. 2004
*
* Copyright (c) 2004, PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.sourceforge.pmd.eclipse.cmd;
import net.sourceforge.pmd.RuleSet;
import net.sourceforge.pmd.eclipse.PMDPlugin;
import org.eclipse.core.resources.IProject;
import org.eclipse.ui.IWorkingSet;
/**
* This command retrieves the PMD specific properties of a particular
* workbench project. This is a composite basic command.
*
* @author Philippe Herlin
* @version $Revision$
*
* $Log$
* Revision 1.1 2004/11/21 21:39:45 phherlin
* Applying Command and CommandProcessor patterns
*
*
*/
public class QueryProjectPropertiesCmd extends DefaultCommand {
private IProject project;
private boolean pmdEnabled;
private IWorkingSet projectWorkingSet;
private RuleSet projectRuleSet;
private boolean ruleSetStoredInProject;
/**
* Default constructor. Initializes command attributes
*
*/
public QueryProjectPropertiesCmd() {
setReadOnly(true);
setOutputData(true);
setName("QueryProjectProperties");
setDescription("Query the PMD specific properties of a particular workbench project.");
}
/**
* @see net.sourceforge.pmd.eclipse.cmd.Command#performExecute()
*/
protected void execute() throws CommandException {
if (this.project == null) throw new MandatoryInputParameterMissingException("project");
QueryPmdEnabledPropertyCmd queryPmdEnabledCmd = new QueryPmdEnabledPropertyCmd();
queryPmdEnabledCmd.setProject(this.project);
queryPmdEnabledCmd.performExecute();
this.pmdEnabled = queryPmdEnabledCmd.isPmdEnabled();
QueryProjectWorkingSetCmd queryProjectWorkingSetCmd = new QueryProjectWorkingSetCmd();
queryProjectWorkingSetCmd.setProject(this.project);
queryProjectWorkingSetCmd.performExecute();
this.projectWorkingSet = queryProjectWorkingSetCmd.getProjectWorkingSet();
QueryProjectRuleSetCmd queryProjectRuleSetCmd = new QueryProjectRuleSetCmd();
queryProjectRuleSetCmd.setProject(this.project);
queryProjectRuleSetCmd.setFromProperties(true);
queryProjectRuleSetCmd.performExecute();
this.projectRuleSet = queryProjectRuleSetCmd.getProjectRuleSet();
if (this.projectRuleSet == null) {
this.projectRuleSet = PMDPlugin.getDefault().getRuleSet();
}
QueryRuleSetStoredInProjectPropertyCmd queryRuleSetStoredInProjectCmd = new QueryRuleSetStoredInProjectPropertyCmd();
queryRuleSetStoredInProjectCmd.setProject(this.project);
queryRuleSetStoredInProjectCmd.performExecute();
this.ruleSetStoredInProject = queryRuleSetStoredInProjectCmd.isRuleSetStoredInProject();
}
/**
* Mandatory input parameter.
* Specify the project to query.
* @param project
*/
public void setProject(IProject project) {
this.project = project;
}
/**
* @return whether PMD is activated for that project
*/
public boolean isPMDEnabled() {
return this.pmdEnabled;
}
/**
* @return the project working set
*/
public IWorkingSet getProjectWorkingSet() {
return this.projectWorkingSet;
}
/**
* @return the project rule set
*/
public RuleSet getProjectRuleSet() {
return this.projectRuleSet;
}
/**
* @return whether the project rule set is stored inside the project
* instead of the plugin properties store
*/
public boolean isRuleSetStoredInProject() {
return this.ruleSetStoredInProject;
}
}

View File

@ -0,0 +1,201 @@
/*
* Created on 20 nov. 2004
*
* Copyright (c) 2004, PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.sourceforge.pmd.eclipse.cmd;
import java.util.Iterator;
import java.util.StringTokenizer;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleSet;
import net.sourceforge.pmd.RuleSetFactory;
import net.sourceforge.pmd.eclipse.PMDConstants;
import net.sourceforge.pmd.eclipse.PMDPlugin;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
/**
* Query the rule set configured for a specific project.
*
* @author Philippe Herlin
* @version $Revision$
*
* $Log$
* Revision 1.1 2004/11/21 21:39:45 phherlin
* Applying Command and CommandProcessor patterns
*
*
*/
public class QueryProjectRuleSetCmd extends DefaultCommand {
private static final Log log = LogFactory.getLog("net.sourceforge.pmd.eclipse.cmd.QueryProjectRuleSetCmd");
private IProject project;
private boolean fromProperties;
private RuleSet projectRuleSet;
/**
* @see net.sourceforge.pmd.eclipse.cmd.DefaultCommand#execute()
*/
protected void execute() throws CommandException {
if (this.project == null) throw new MandatoryInputParameterMissingException("project");
QueryRuleSetStoredInProjectPropertyCmd queryRuleSetStoredInProjectCmd = new QueryRuleSetStoredInProjectPropertyCmd();
queryRuleSetStoredInProjectCmd.setProject(this.project);
queryRuleSetStoredInProjectCmd.performExecute();
boolean ruleSetStoredInProject = queryRuleSetStoredInProjectCmd.isRuleSetStoredInProject();
if ((this.fromProperties) || (!ruleSetStoredInProject)) {
getRuleSetFromProperties();
} else {
getRuleSetFromProject();
}
}
/**
* @param project The project to set.
*/
public void setProject(IProject project) {
this.project = project;
}
/**
* @param fromProperties The fromProperties to set.
*/
public void setFromProperties(boolean fromProperties) {
this.fromProperties = fromProperties;
}
/**
* @return Returns the projectRuleSet.
*/
public RuleSet getProjectRuleSet() {
return this.projectRuleSet;
}
/**
* Get the rulset configured for the project
*/
private void getRuleSetFromProperties() throws CommandException {
log.debug("Searching a ruleset for project " + this.project.getName() + " in properties");
boolean flSaveInSession = false;
RuleSet configuredRuleSet = PMDPlugin.getDefault().getRuleSet();
try {
// Try to load the ruleset from the session properties
// If not available query the rules list from presistent store and
// ask the instantiation of a RuleSet object from that list
this.projectRuleSet = (RuleSet) this.project.getSessionProperty(SESSION_PROPERTY_ACTIVE_RULESET);
if (this.projectRuleSet == null) {
String activeRulesList = this.project.getPersistentProperty(PERSISTENT_PROPERTY_ACTIVE_RULESET);
if (activeRulesList != null) {
this.projectRuleSet = getRuleSetFromRuleList(activeRulesList);
flSaveInSession = true;
}
}
// If meanwhile, rules have been deleted from preferences
// delete them also from the project ruleset
if ((this.projectRuleSet != null) && (this.projectRuleSet != configuredRuleSet)) {
Iterator i = this.projectRuleSet.getRules().iterator();
while (i.hasNext()) {
Object rule = i.next();
if (!configuredRuleSet.getRules().contains(rule)) {
i.remove();
flSaveInSession = true;
}
}
}
// If needed store modified ruleset in session properties
if (flSaveInSession) {
this.project.setSessionProperty(SESSION_PROPERTY_ACTIVE_RULESET, this.projectRuleSet);
}
} catch (CoreException e) {
throw new CommandException("Error when searching for project ruleset. Using the full ruleset.", e);
}
}
/**
* Retrieve a project ruleset from a ruleset file in the project instead of
* the plugin properties/preferences
*/
private void getRuleSetFromProject() throws CommandException {
log.debug("Searching a ruleset for project " + this.project.getName() + " in the project file");
IFile ruleSetFile = this.project.getFile(".ruleset");
if (ruleSetFile.exists()) {
try {
this.projectRuleSet = (RuleSet) this.project.getSessionProperty(SESSION_PROPERTY_ACTIVE_RULESET);
Long oldModificationStamp = (Long) this.project.getSessionProperty(SESSION_PROPERTY_RULESET_MODIFICATION_STAMP);
long newModificationStamp = ruleSetFile.getModificationStamp();
if ((oldModificationStamp == null) || (oldModificationStamp.longValue() != newModificationStamp)) {
RuleSetFactory ruleSetFactory = new RuleSetFactory();
this.projectRuleSet = ruleSetFactory.createRuleSet(ruleSetFile.getContents());
this.project.setSessionProperty(SESSION_PROPERTY_ACTIVE_RULESET, this.projectRuleSet);
this.project.setSessionProperty(SESSION_PROPERTY_RULESET_MODIFICATION_STAMP, new Long(newModificationStamp));
}
} catch (Exception e) {
throw new CommandException(getMessage(PMDConstants.MSGKEY_ERROR_LOADING_RULESET), e);
}
}
}
/**
* Get a sub ruleset from a rule list
* @param ruleList a list composed of rule names seperated by a delimiter
* @return a rule set composed only of the rules imported in the plugin
*/
private RuleSet getRuleSetFromRuleList(String ruleList) {
RuleSet subRuleSet = new RuleSet();
RuleSet ruleSet = PMDPlugin.getDefault().getRuleSet();
StringTokenizer st = new StringTokenizer(ruleList, LIST_DELIMITER);
while (st.hasMoreTokens()) {
try {
Rule rule = ruleSet.getRuleByName(st.nextToken());
if (rule != null) {
subRuleSet.addRule(rule);
}
} catch (RuntimeException e) {
PMDPlugin.getDefault().logError("Ignored runtime exception from PMD : ", e);
}
}
return subRuleSet;
}
}

View File

@ -0,0 +1,111 @@
/*
* Created on 20 nov. 2004
*
* Copyright (c) 2004, PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.sourceforge.pmd.eclipse.cmd;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.IWorkingSetManager;
import org.eclipse.ui.PlatformUI;
/**
* Query the working set associated with a project
*
* @author Philippe Herlin
* @version $Revision$
*
* $Log$
* Revision 1.1 2004/11/21 21:39:45 phherlin
* Applying Command and CommandProcessor patterns
*
*
*/
public class QueryProjectWorkingSetCmd extends DefaultCommand {
private IProject project;
private IWorkingSet projectWorkingSet;
/**
* Default constructor. Initializes command attributes
*
*/
public QueryProjectWorkingSetCmd() {
setReadOnly(true);
setOutputData(true);
setName("QueryProjectWorkingSet");
setDescription("Query the working set associated with a project.");
}
/**
* @see net.sourceforge.pmd.eclipse.cmd.DefaultCommand#execute()
*/
protected void execute() throws CommandException {
if (project == null) throw new MandatoryInputParameterMissingException("project");
IWorkingSet workingSet = null;
try {
workingSet = (IWorkingSet) project.getSessionProperty(SESSION_PROPERTY_WORKINGSET);
if (workingSet == null) {
String workingSetName = project.getPersistentProperty(PERSISTENT_PROPERTY_WORKINGSET);
if (workingSetName != null) {
IWorkingSetManager workingSetManager = PlatformUI.getWorkbench().getWorkingSetManager();
workingSet = workingSetManager.getWorkingSet(workingSetName);
if (workingSet != null) {
project.setSessionProperty(SESSION_PROPERTY_WORKINGSET, workingSet);
}
}
}
} catch (CoreException e) {
throw new CommandException("Exception when retreiving a project working set", e);
}
this.projectWorkingSet = workingSet;
}
/**
* @return Returns the projectWorkingSet.
*/
public IWorkingSet getProjectWorkingSet() {
return projectWorkingSet;
}
/**
* @param project The project to set.
*/
public void setProject(IProject project) {
this.project = project;
}
}

View File

@ -0,0 +1,113 @@
/*
* Created on 20 nov. 2004
*
* Copyright (c) 2004, PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.sourceforge.pmd.eclipse.cmd;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
/**
* Query whether a project ruleset is stored in the project instead of the
* plugin properties store
*
* @author Philippe Herlin
* @version $Revision$
*
* $Log$
* Revision 1.1 2004/11/21 21:39:45 phherlin
* Applying Command and CommandProcessor patterns
*
*
*/
public class QueryRuleSetStoredInProjectPropertyCmd extends DefaultCommand {
private IProject project;
private boolean ruleSetStoredInProject;
/**
* Default constructor. Initializes command attributes
*
*/
public QueryRuleSetStoredInProjectPropertyCmd() {
setReadOnly(true);
setOutputData(true);
setName("QueryRuleSetStoredInProjectProperty");
setDescription("Query whether a project rule set is stored in a project instead of the plugin properties store.");
}
/**
* @see net.sourceforge.pmd.eclipse.cmd.DefaultCommand#execute()
*/
protected void execute() throws CommandException {
if (this.project == null) throw new MandatoryInputParameterMissingException("project");
Boolean booleanProperty = Boolean.FALSE;
boolean flSaveInSession = false;
try {
booleanProperty = (Boolean) this.project.getSessionProperty(SESSION_PROPERTY_STORE_RULESET_PROJECT);
if (booleanProperty == null) {
String stringProperty = this.project.getPersistentProperty(PERSISTENT_PROPERTY_STORE_RULESET_PROJECT);
if (stringProperty != null) {
booleanProperty = new Boolean(stringProperty);
flSaveInSession = true;
}
}
// If needed store modified ruleset
if (flSaveInSession) {
this.project.setSessionProperty(SESSION_PROPERTY_STORE_RULESET_PROJECT, booleanProperty);
}
this.ruleSetStoredInProject = booleanProperty == null ? false : booleanProperty.booleanValue();
} catch (CoreException e) {
throw new CommandException("Error when searching for the store_ruleset_project property. Assuming the project doesn't store it's own ruleset", e);
}
}
/**
* @return Returns the ruleSetStoredInProject.
*/
public boolean isRuleSetStoredInProject() {
return ruleSetStoredInProject;
}
/**
* @param project The project to set.
*/
public void setProject(IProject project) {
this.project = project;
}
}

View File

@ -0,0 +1,156 @@
/*
* Created on 21 nov. 2004
*
* Copyright (c) 2004, PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.sourceforge.pmd.eclipse.cmd;
import net.sourceforge.pmd.eclipse.builder.PMDNature;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
/**
* Update whether PMD is enabled for a project or not.
*
* @author Philippe Herlin
* @version $Revision$
*
* $Log$
* Revision 1.1 2004/11/21 21:39:45 phherlin
* Applying Command and CommandProcessor patterns
*
*
*/
public class UpdatePmdEnabledPropertyCmd extends DefaultCommand {
private static final Log log = LogFactory.getLog("net.sourceforge.pmd.eclipse.cmd.UpdatePmdEnabledPropertyCmd");
private IProject project;
private boolean pmdEnabled;
private boolean needRebuild;
/**
* Default constructor. Initializes command attributes
*
*/
public UpdatePmdEnabledPropertyCmd() {
setReadOnly(false);
setOutputData(true);
setName("UpdatePmdEnabledProperty");
setDescription("Update whether PMD is enabled for a project or not.");
}
/**
* @see net.sourceforge.pmd.eclipse.cmd.DefaultCommand#execute()
*/
protected void execute() throws CommandException {
if (this.project == null) throw new CommandException("project");
if (this.pmdEnabled) {
addNature();
} else {
removeNature();
}
}
/**
* @param pmdEnabled The pmdEnabled to set.
*/
public void setPmdEnabled(boolean pmdEnabled) {
this.pmdEnabled = pmdEnabled;
}
/**
* @param project The project to set.
*/
public void setProject(IProject project) {
this.project = project;
}
/**
* @return Returns the needRebuild.
*/
public boolean isNeedRebuild() {
return needRebuild;
}
/**
* Add a PMD nature to the project
* @throws CommandException
*/
private void addNature() throws CommandException {
try {
if (!this.project.hasNature(PMDNature.PMD_NATURE)) {
log.info("Adding PMD nature to the project " + this.project.getName());
IProjectDescription description = this.project.getDescription();
String[] natureIds = description.getNatureIds();
String[] newNatureIds = new String[natureIds.length + 1];
System.arraycopy(natureIds, 0, newNatureIds, 0, natureIds.length);
newNatureIds[natureIds.length] = PMDNature.PMD_NATURE;
description.setNatureIds(newNatureIds);
this.project.setDescription(description, null);
this.needRebuild = true;
}
} catch (CoreException e) {
throw new CommandException(getMessage(MSGKEY_ERROR_CORE_EXCEPTION), e);
}
}
/**
* Remove a PMD nature from the project
* @throws CommandException
*/
private void removeNature() throws CommandException {
try {
if (this.project.hasNature(PMDNature.PMD_NATURE)) {
IProjectDescription description = this.project.getDescription();
String[] natureIds = description.getNatureIds();
String[] newNatureIds = new String[natureIds.length - 1];
for (int i = 0, j = 0; i < natureIds.length; i++) {
if (!natureIds[i].equals(PMDNature.PMD_NATURE)) {
newNatureIds[j++] = natureIds[i];
}
}
description.setNatureIds(newNatureIds);
this.project.setDescription(description, null);
this.project.deleteMarkers(PMD_MARKER, true, IResource.DEPTH_INFINITE);
}
} catch (CoreException e) {
throw new CommandException(getMessage(MSGKEY_ERROR_CORE_EXCEPTION), e);
}
}
}

View File

@ -0,0 +1,186 @@
/*
* Created on 21 nov. 2004
*
* Copyright (c) 2004, PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.sourceforge.pmd.eclipse.cmd;
import net.sourceforge.pmd.RuleSet;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.IWorkingSet;
/**
* Save updated project properties. This is a composite command.
*
* @author Philippe Herlin
* @version $Revision$
*
* $Log$
* Revision 1.1 2004/11/21 21:39:45 phherlin
* Applying Command and CommandProcessor patterns
*
*
*/
public class UpdateProjectPropertiesCmd extends JobCommand {
private IProject project;
private boolean pmdEnabled;
private IWorkingSet projectWorkingSet;
private RuleSet projectRuleSet;
private boolean ruleSetStoredInProject;
private boolean needRebuild;
private boolean ruleSetFileNotFound;
/**
* Default constructor. Initializes command attributes
*
*/
public UpdateProjectPropertiesCmd() {
super("Updating project properties");
setReadOnly(false);
setOutputData(true);
setName("UpdateProjectProperties");
setDescription("Update a project PMD specific properties.");
}
/**
* @see net.sourceforge.pmd.eclipse.cmd.DefaultCommand#execute()
*/
protected IStatus execute() throws CommandException {
if (this.project == null) throw new MandatoryInputParameterMissingException("project");
if (this.projectRuleSet == null) throw new MandatoryInputParameterMissingException("projectRuleSet");
this.getMonitor().beginTask("Updating project properties", 4);
UpdatePmdEnabledPropertyCmd pmdEnabledCmd = new UpdatePmdEnabledPropertyCmd();
if (!this.getMonitor().isCanceled()) {
this.getMonitor().subTask("Updating PMD enabling state");
pmdEnabledCmd.setProject(this.project);
pmdEnabledCmd.setPmdEnabled(this.pmdEnabled);
pmdEnabledCmd.execute();
this.getMonitor().worked(1);
}
UpdateProjectRuleSetCmd projectRuleSetCmd = new UpdateProjectRuleSetCmd();
if (!this.getMonitor().isCanceled()) {
this.getMonitor().subTask("Updating project rule set");
projectRuleSetCmd.setProject(this.project);
projectRuleSetCmd.setProjectRuleSet(this.projectRuleSet);
projectRuleSetCmd.execute();
this.getMonitor().worked(1);
}
UpdateProjectWorkingSetCmd projectWorkingSetCmd = new UpdateProjectWorkingSetCmd();
if (!this.getMonitor().isCanceled()) {
this.getMonitor().subTask("Updating project working set");
projectWorkingSetCmd.setProject(this.project);
projectWorkingSetCmd.setProjectWorkingSet(this.projectWorkingSet);
projectWorkingSetCmd.execute();
this.getMonitor().worked(1);
}
UpdateRuleSetStoredInProjectPropertyCmd ruleSetStoredInProjectCmd = new UpdateRuleSetStoredInProjectPropertyCmd();
if (!this.getMonitor().isCanceled()) {
this.getMonitor().subTask("Updating rule set location state");
ruleSetStoredInProjectCmd.setProject(this.project);
ruleSetStoredInProjectCmd.setRuleSetStoredInProject(this.ruleSetStoredInProject);
ruleSetStoredInProjectCmd.execute();
this.ruleSetFileNotFound = ruleSetStoredInProjectCmd.isRuleSetFileNotFound();
this.getMonitor().worked(1);
}
if (!this.getMonitor().isCanceled()) {
this.needRebuild = ruleSetStoredInProjectCmd.isNeedRebuild();
if (this.pmdEnabled) {
if (!this.ruleSetStoredInProject) {
this.needRebuild |= projectRuleSetCmd.isNeedRebuild();
}
this.needRebuild |= pmdEnabledCmd.isNeedRebuild();
this.needRebuild |= projectWorkingSetCmd.isNeedRebuild();
}
}
this.getMonitor().done();
return this.getMonitor().isCanceled() ? Status.CANCEL_STATUS : Status.OK_STATUS;
}
/**
* @param project The project to set.
*/
public void setProject(IProject project) {
this.project = project;
}
/**
* @param pmdEnabled The pmdEnabled to set.
*/
public void setPmdEnabled(boolean pmdEnabled) {
this.pmdEnabled = pmdEnabled;
}
/**
* @param projectRuleSet The projectRuleSet to set.
*/
public void setProjectRuleSet(RuleSet projectRuleSet) {
this.projectRuleSet = projectRuleSet;
}
/**
* @param projectWorkingSet The projectWorkingSet to set.
*/
public void setProjectWorkingSet(IWorkingSet projectWorkingSet) {
this.projectWorkingSet = projectWorkingSet;
}
/**
* @param ruleSetStoredInProject The ruleSetStoredInProject to set.
*/
public void setRuleSetStoredInProject(boolean ruleSetStoredInProject) {
this.ruleSetStoredInProject = ruleSetStoredInProject;
}
/**
* @return Returns the needRebuild.
*/
public boolean isNeedRebuild() {
return needRebuild;
}
/**
* @return Returns the ruleSetFileNotFound.
*/
public boolean isRuleSetFileNotFound() {
return ruleSetFileNotFound;
}
}

View File

@ -0,0 +1,158 @@
/*
* Created on 21 nov. 2004
*
* Copyright (c) 2004, PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.sourceforge.pmd.eclipse.cmd;
import java.util.Iterator;
import java.util.Set;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleSet;
import net.sourceforge.pmd.eclipse.PMDConstants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
/**
* Update a project ruleset. The ruleset are stored in the plugin properties
* store.
*
* @author Philippe Herlin
* @version $Revision$
*
* $Log$
* Revision 1.1 2004/11/21 21:39:45 phherlin
* Applying Command and CommandProcessor patterns
*
*
*/
public class UpdateProjectRuleSetCmd extends DefaultCommand {
private static final Log log = LogFactory.getLog("net.sourceforge.pmd.eclipse.cmd.UpdateProjectRuleSetCmd");
private IProject project;
private RuleSet projectRuleSet;
private boolean needRebuild;
/**
* Default constructor. Initializes command attributes
*
*/
public UpdateProjectRuleSetCmd() {
setReadOnly(false);
setOutputData(true);
setName("UpdateProjectRuleSet");
setDescription("Update a project ruleset.");
}
/**
* @see net.sourceforge.pmd.eclipse.cmd.DefaultCommand#execute()
*/
protected void execute() throws CommandException {
if (this.project == null) throw new MandatoryInputParameterMissingException("project");
if (this.projectRuleSet == null) throw new MandatoryInputParameterMissingException("projectRuleSet");
// Before updating, query the current ruleset
QueryProjectRuleSetCmd queryCmd = new QueryProjectRuleSetCmd();
queryCmd.setProject(this.project);
queryCmd.setFromProperties(true);
queryCmd.execute();
// Now store the ruleset
try {
StringBuffer ruleSelectionList = new StringBuffer();
Iterator i = this.projectRuleSet.getRules().iterator();
while (i.hasNext()) {
Rule rule = (Rule) i.next();
ruleSelectionList.append(rule.getName()).append(LIST_DELIMITER);
}
log.debug("Storing ruleset for project " + this.project.getName());
this.project.setPersistentProperty(PERSISTENT_PROPERTY_ACTIVE_RULESET, ruleSelectionList.toString());
log.debug(" list : " + ruleSelectionList.toString());
this.project.setSessionProperty(SESSION_PROPERTY_ACTIVE_RULESET, this.projectRuleSet);
} catch (CoreException e) {
throw new CommandException(getMessage(PMDConstants.MSGKEY_ERROR_STORING_PROPERTY), e);
}
// Finally, compare to the initial list whether the ruleset has really changed
if (queryCmd.getProjectRuleSet() != null) {
// 1-if a rule has been deselected
Iterator i = queryCmd.getProjectRuleSet().getRules().iterator();
Set selectedRules = this.projectRuleSet.getRules();
while ((i.hasNext()) && (!this.needRebuild)) {
Rule rule = (Rule) i.next();
if (!selectedRules.contains(rule)) {
this.needRebuild = true;
}
}
// 1-if a rule has been selected
i = this.projectRuleSet.getRules().iterator();
Set previousRules = queryCmd.getProjectRuleSet().getRules();
while ((i.hasNext()) && (!this.needRebuild)) {
Rule rule = (Rule) i.next();
if (!previousRules.contains(rule)) {
this.needRebuild = true;
}
}
} else {
this.needRebuild = true;
}
}
/**
* @param project The project to set.
*/
public void setProject(IProject project) {
this.project = project;
}
/**
* @param projectRuleSet The projectRuleSet to set.
*/
public void setProjectRuleSet(RuleSet projectRuleSet) {
this.projectRuleSet = projectRuleSet;
}
/**
* @return Returns the needRebuild.
*/
public boolean isNeedRebuild() {
return needRebuild;
}
}

View File

@ -0,0 +1,129 @@
/*
* Created on 21 nov. 2004
*
* Copyright (c) 2004, PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.sourceforge.pmd.eclipse.cmd;
import net.sourceforge.pmd.eclipse.PMDConstants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.ui.IWorkingSet;
/**
* Update a project working set.
*
* @author Philippe Herlin
* @version $Revision$
*
* $Log$
* Revision 1.1 2004/11/21 21:39:45 phherlin
* Applying Command and CommandProcessor patterns
*
*
*/
public class UpdateProjectWorkingSetCmd extends DefaultCommand {
private static final Log log = LogFactory.getLog("net.sourceforge.pmd.eclipse.cmd.UpdateProjectWorkingSetCmd");
private IProject project;
private IWorkingSet projectWorkingSet;
private boolean needRebuild;
/**
* Default constructor. Initialize command attributes.
*/
public UpdateProjectWorkingSetCmd() {
setReadOnly(false);
setOutputData(true);
setName("UpdateProjectWorkingSet");
setDescription("Update a project working set.");
}
/**
* @see net.sourceforge.pmd.eclipse.cmd.DefaultCommand#execute()
*/
protected void execute() throws CommandException {
if (this.project == null) throw new MandatoryInputParameterMissingException("project");
log.debug("Set the working set " + this.projectWorkingSet + " for project " + this.project.getName());
// First query the previous active working set
QueryProjectWorkingSetCmd queryCmd = new QueryProjectWorkingSetCmd();
queryCmd.setProject(this.project);
queryCmd.execute();
// Then, store the property
try {
this.project.setPersistentProperty(PERSISTENT_PROPERTY_WORKINGSET, this.projectWorkingSet == null ? null : this.projectWorkingSet.getName());
this.project.setSessionProperty(SESSION_PROPERTY_WORKINGSET, this.projectWorkingSet);
} catch (CoreException e) {
throw new CommandException(getMessage(PMDConstants.MSGKEY_ERROR_CORE_EXCEPTION), e);
}
// Now, check whether the property has changed to know if rebuild is necessary
boolean bothNotNull = (queryCmd.getProjectWorkingSet() != null) && (this.projectWorkingSet != null);
log.debug("both working set are not null : " + bothNotNull);
boolean fl1 = (queryCmd.getProjectWorkingSet() == null) && (this.projectWorkingSet !=null);
log.debug("no previous working set, new one selected : " + fl1);
boolean fl2 = (queryCmd.getProjectWorkingSet() != null) && (this.projectWorkingSet ==null);
log.debug("previous working set selected, no working set selected now : " + fl2);
this.needRebuild = fl1 | fl2 | (bothNotNull && (!queryCmd.getProjectWorkingSet().getName().equals(this.projectWorkingSet.getName())));
log.debug("project need to be rebuild : " + this.needRebuild);
}
/**
* @return Returns the needRebuild.
*/
public boolean isNeedRebuild() {
return needRebuild;
}
/**
* @param project The project to set.
*/
public void setProject(IProject project) {
this.project = project;
}
/**
* @param projectWorkingSet The projectWorkingSet to set.
*/
public void setProjectWorkingSet(IWorkingSet projectWorkingSet) {
this.projectWorkingSet = projectWorkingSet;
}
}

View File

@ -0,0 +1,130 @@
/*
* Created on 21 nov. 2004
*
* Copyright (c) 2004, PMD for Eclipse Development Team
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed in part by support from
* the Defense Advanced Research Project Agency (DARPA)"
* * Neither the name of "PMD for Eclipse Development Team" nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.sourceforge.pmd.eclipse.cmd;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
/**
* Update whether a project rule set is stored in a project file instead of
* inside the plugin properties store.
*
* @author Philippe Herlin
* @version $Revision$
*
* $Log$
* Revision 1.1 2004/11/21 21:39:45 phherlin
* Applying Command and CommandProcessor patterns
*
*
*/
public class UpdateRuleSetStoredInProjectPropertyCmd extends DefaultCommand {
private IProject project;
private boolean ruleSetStoredInProject;
private boolean ruleSetFileNotFound;
private boolean needRebuild;
/**
* Default constructor. Initializes command attributes
*
*/
public UpdateRuleSetStoredInProjectPropertyCmd() {
setReadOnly(false);
setOutputData(true);
setName("UpdateRuleSetStoredInProjectProperty");
setDescription("Update whether a project rule set is stored in a project file.");
}
/**
* @see net.sourceforge.pmd.eclipse.cmd.DefaultCommand#execute()
*/
protected void execute() throws CommandException {
if (this.project == null) throw new MandatoryInputParameterMissingException("project");
// First query the current value
QueryRuleSetStoredInProjectPropertyCmd queryCmd = new QueryRuleSetStoredInProjectPropertyCmd();
queryCmd.setProject(this.project);
queryCmd.execute();
// Then, update the property
try {
Boolean property = new Boolean(this.ruleSetStoredInProject);
this.project.setPersistentProperty(PERSISTENT_PROPERTY_STORE_RULESET_PROJECT, property.toString());
this.project.setSessionProperty(SESSION_PROPERTY_STORE_RULESET_PROJECT, property);
if (this.ruleSetStoredInProject) {
this.project.setSessionProperty(SESSION_PROPERTY_RULESET_MODIFICATION_STAMP, null);
}
IFile ruleSetFile = this.project.getFile(".ruleset");
this.ruleSetFileNotFound = !ruleSetFile.exists();
} catch (CoreException e) {
throw new CommandException(getMessage(MSGKEY_ERROR_CORE_EXCEPTION), e);
}
// Now, check if a rebuild is necessary
this.needRebuild = queryCmd.isRuleSetStoredInProject() != this.ruleSetStoredInProject;
}
/**
* @return Returns the ruleSetFileNotFound.
*/
public boolean isRuleSetFileNotFound() {
return ruleSetFileNotFound;
}
/**
* @return Returns the needRebuild.
*/
public boolean isNeedRebuild() {
return needRebuild;
}
/**
* @param project The project to set.
*/
public void setProject(IProject project) {
this.project = project;
}
/**
* @param ruleSetStoredInProject The ruleSetStoredInProject to set.
*/
public void setRuleSetStoredInProject(boolean ruleSetStoredInProject) {
this.ruleSetStoredInProject = ruleSetStoredInProject;
}
}