1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
/*******************************************************************************
* Copyright (c) 2001, 2008 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.wst.validation.internal;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.wst.validation.internal.core.Message;
import org.eclipse.wst.validation.internal.operations.WorkbenchReporter;
import org.eclipse.wst.validation.internal.plugin.ValidationPlugin;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;
public final class InternalValidatorManager {
private static InternalValidatorManager _inst = null;
private static final String OP_GROUP = "ValidationOperation"; //$NON-NLS-1$ // when the ValidationOperation
// adds a message to the task
// list; e.g. cancel, or internal
// error, this group name is used
// to distinguish between the
// messages that the validator
// itself put, and the ones which
// the validator owns, but the
// operation put. //$NON-NLS-1$
private InternalValidatorManager() {
//default
}
public static InternalValidatorManager getManager() {
if (_inst == null) {
_inst = new InternalValidatorManager();
}
return _inst;
}
/**
* Return a new Set that contains all of the elements from the array.
*/
public static Set<ValidatorMetaData> wrapInSet(ValidatorMetaData[] vmds) {
Set<ValidatorMetaData> result = new HashSet<ValidatorMetaData>();
if ((vmds == null) || (vmds.length == 0))return result;
for (ValidatorMetaData vmd : vmds)result.add(vmd);
return result;
}
/**
* If the current validator throws a Throwable, log the internal error to the task list.
*
* This method is for use by the validation framework only.
*/
public void addInternalErrorTask(IProject project, ValidatorMetaData vmd, Throwable exc) {
addOperationTask(project, vmd, ResourceConstants.VBF_EXC_INTERNAL, new String[]{project.getName(), vmd.getValidatorDisplayName(), ((exc.getMessage() == null) ? "" : exc.getMessage())}); //$NON-NLS-1$
}
/**
* If the user is cancelling validation on the current project/resource, Add an information task
* to the task list informing the user that validation has not been run on the current project.
*
* If the current validator throws a Throwable, log the internal error to the task list.
*/
public void addOperationTask(IProject project, ValidatorMetaData vmd, String messageId, String[] parms) {
Message message = ValidationPlugin.getMessage();
message.setSeverity(IMessage.LOW_SEVERITY);
message.setId(messageId);
message.setParams(parms);
message.setGroupName(OP_GROUP);
// Although the message is owned by the validator, the string of the message has to be
// loaded by this class' ClassLoader
WorkbenchReporter.addMessage(project, vmd.getValidatorUniqueName(), getClass().getClassLoader(), message);
}
/**
* If the user cancelled the previous validation with this validator, or if there was a
* Throwable caught during the last execution of this validator, and the validator is in the
* process of validating now, remove the former information task messages.
*/
public void removeOperationTasks(IProject project, ValidatorMetaData vmd) {
WorkbenchReporter.removeMessageSubset(project, vmd.getValidatorUniqueName(), OP_GROUP);
}
/**
* Return an array of the fully-qualified names of the validator classes.
*/
public String[] getValidatorNames(ValidatorMetaData[] vmds) {
Set<String> temp = new HashSet<String>();
for (ValidatorMetaData vmd : vmds) {
for (String name : vmd.getValidatorNames()) {
temp.add(name);
}
}
String[] vmdNames = new String[temp.size()];
temp.toArray(vmdNames);
return vmdNames;
}
/**
* Return an array of the fully-qualified names of the validator classes.
*/
public String[] getValidatorNames(Collection<ValidatorMetaData> vmds) {
Set<String> temp = new HashSet<String>();
for (ValidatorMetaData vmd : vmds) {
for (String name : vmd.getValidatorNames()) {
temp.add(name);
}
}
String[] vmdNames = new String[temp.size()];
temp.toArray(vmdNames);
return vmdNames;
}
/**
* Return a list of validators that validate files with the given extension.
*/
public ValidatorMetaData[] getValidatorsForExtension(IProject project, String fileExtension) {
try {
ProjectConfiguration prjp = ConfigurationManager.getManager().getProjectConfiguration(project);
// Get all of the validators configured on the project for the given file extension
ValidatorMetaData[] vmds = prjp.getValidators();
// Construct a fake IFile type to represent a file with this extension.
StringBuffer buffer = new StringBuffer(project.getName());
buffer.append(IPath.SEPARATOR);
buffer.append(fileExtension);
IPath path = new Path(buffer.toString());
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
ValidatorMetaData[] temp = new ValidatorMetaData[vmds.length];
int count = 0;
for (int i = 0; i < vmds.length; i++) {
ValidatorMetaData vmd = vmds[i];
if (vmd.isApplicableTo(file)) {
temp[count++] = vmd;
}
}
ValidatorMetaData[] result = new ValidatorMetaData[count];
System.arraycopy(temp, 0, result, 0, count);
return result;
} catch (InvocationTargetException e) {
ValidationPlugin.getPlugin().handleException(e);
if (e.getTargetException() != null)
ValidationPlugin.getPlugin().handleException(e.getTargetException());
return new ValidatorMetaData[0];
}
}
/**
* Return a list of validator names that validate files with the given extension.
*/
public String[] getValidatorNamesForExtension(IProject project, String fileExtension) {
ValidatorMetaData[] vmds = getValidatorsForExtension(project, fileExtension);
String[] names = new String[vmds.length];
for (int i = 0; i < names.length; i++) {
names[i] = vmds[i].getValidatorUniqueName();
}
return names;
}
}
|