* @throws Exception
*/
public void runFeatureTest(Object testName, ILayer[] layers, ValidationResults results, IProgressMonitor monitor) throws Exception {
// get the validator from testKey
FeatureValidation validator = null;
// (navigate through the featureLookup until we find an instance of the test)
for (Iterator i = featureLookup.keySet().iterator(); i.hasNext();) {
ArrayList testList = (ArrayList) featureLookup.get(i.next());
// iterate through each item in the list
for (Object thisTest : testList) {
Validation test = (Validation) thisTest;
// this is the matching validation for the given test
if (test.getName().equals(testName)) {
validator = (FeatureValidation) test;
break;
}
}
}
// run the test
if (validator != null) // if we found the test
{
results.setValidation(validator);
//get a list of typeRefs to figure out which layers to run the test on
String[] typeRefs = validator.getTypeRefs();
if (typeRefs == null) {
//unfortunately, ALL typeRefs = null; we'll override that for now
typeRefs = new String[1];
typeRefs[0] = "*"; //$NON-NLS-1$
// } else if (typeRefs.length == 0) {
// return; //TODO: add messageBox "there are no typeRefs!"
}
Set<ILayer> relevantLayers = new HashSet<ILayer>();
for (int i = 0; i < typeRefs.length; i++) {
String typeRef = (String) typeRefs[i];
if (typeRef.equals("") || (typeRef.equals("*"))) { //$NON-NLS-1$//$NON-NLS-2$
//wildcard (I assume); add all layers to the relevantLayers and break
for (int j = 0; j < layers.length; j++) {
ILayer thisLayer = layers[j];
relevantLayers.add(thisLayer);
}
break;
}
//find the layer that matches the typeRef
for (int j = 0; j < layers.length; j++) {
ILayer thisLayer = layers[j];
//make the typeRef
SimpleFeatureType schema = thisLayer.getSchema();
if(schema == null)
continue;
String dataStoreID = schema.getName().getNamespaceURI();
String thisTypeRef = dataStoreID+":"+schema.getName().getLocalPart(); //$NON-NLS-1$
//if the typeRefs match, add the layer to our set
if (thisTypeRef.equals(typeRef)) {
relevantLayers.add(thisLayer);
break;
}
}
}
//for each relevant layer
for (Iterator k = relevantLayers.iterator(); k.hasNext();) {
ILayer thisLayer = (ILayer) k.next();
//get the SimpleFeatureType
SimpleFeatureType type = thisLayer.getSchema();
//create a FeatureReader (collection.reader)
FeatureSource<SimpleFeatureType, SimpleFeature> source;
source = thisLayer.getResource(FeatureSource.class, monitor);
FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures();
//hmm... pretty pictures or efficiency?
int count = collection.size();
monitor.beginTask("", count); //$NON-NLS-1$
FeatureIterator<SimpleFeature> reader = collection.features();
// iterate through each feature and run the test on it
int position = 0;
while (reader.hasNext())
{
//check for the cancel button
if (monitor.isCanceled()) {
reader.close();
break;
}
//validate this feature
monitor.subTask(++position + "/" + count); //$NON-NLS-1$
SimpleFeature feature = (SimpleFeature) reader.next();
try {
validator.validate(feature, type, results);
} catch (Throwable e) {
results.error(feature, e.getMessage());
}
monitor.worked(1);
}