/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.editors.all;
import java.util.Map;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentDescription;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.texteditor.IDocumentProvider;
import de.innovationgate.eclipse.editors.Plugin;
import de.innovationgate.eclipse.editors.ResourceIDs;
import de.innovationgate.eclipse.editors.tml.markers.TMLFileValidator;
import de.innovationgate.eclipse.utils.wga.WGADesignStructureHelper;
import de.innovationgate.wga.model.VersionCompliance;
/**
* validator for generic WGA design files
* - provides methods for validating generic WGA design files
* - dispatches to other validators if necessary
* - extends incremental project builder to perform revalidation on resource changes
*
*
*/
public class WGADesignResourceValidator extends IncrementalProjectBuilder {
public static void validate(IFile file) {
if (shouldBeValidated(file)) {
doGenericValidation(file);
if (file.getFileExtension().equalsIgnoreCase("tml")) {
TMLFileValidator.validateTMLFile(file);
}
}
}
public static void validate(IContainer container) {
if (shouldBeValidated(container)) {
doGenericValidation(container);
}
}
public static void doGenericValidation(IFile file) {
try {
IDocumentProvider provider = new WGADesignFileDocumentProvider();
IFileEditorInput input = new FileEditorInput(file);
provider.connect(input);
VersionCompliance versionCompliance = WGADesignStructureHelper.getWGAVersionCompliance(file);
InvalidEncodingMarkingHandler handler = new InvalidEncodingMarkingHandler();
handler.setWGAVersionCompliance(versionCompliance);
handler.setDocumentProvider(provider);
handler.createMarkers(file, provider.getDocument(input));
provider.disconnect(input);
} catch (CoreException e) {
Plugin.getDefault().logError("Unable to validate generic design file '" + file.getLocation().toString() + "'.", e);
}
}
public static void doGenericValidation(IContainer container) {
if(WGADesignStructureHelper.isMediaKeyContainer(container)){
InvalidMediaKeyMarkingHandler handler = new InvalidMediaKeyMarkingHandler();
handler.setWGAVersionCompliance(WGADesignStructureHelper.getWGAVersionCompliance(container));
try {
handler.createMarkers(container);
} catch (CoreException e) {
Plugin.getDefault().logError("Unable to validate generic container '" + container.getLocation().toString() + "'.", e);
}
}
}
public static boolean shouldBeValidated(IFile file) {
if (!WGADesignStructureHelper.isWGADesignResource(file)) {
return false;
} else {
WGADesignStructureHelper helper = new WGADesignStructureHelper(file);
if (helper.getFileContainerRoot().getLocation().isPrefixOf(file.getLocation())) {
// only validate labelfiles in file containers
if (!file.getFileExtension().equalsIgnoreCase("properties")) {
return false;
}
}
// all other textual resources should be validated
try {
IContentDescription desc = file.getContentDescription();
if (desc != null) {
IContentType type = desc.getContentType();
if (type != null && desc.getContentType().isKindOf(Platform.getContentTypeManager().getContentType("org.eclipse.core.runtime.text"))) {
return true;
}
}
} catch (CoreException e) {
}
return false;
}
}
public static boolean shouldBeValidated(IContainer container) {
if (!WGADesignStructureHelper.isWGADesignResource(container)) {
return false;
} else {
if (WGADesignStructureHelper.isMediaKeyContainer(container)){
return true;
}
}
return false;
}
@SuppressWarnings("unchecked")
@Override
protected IProject[] build(int kind, Map args, IProgressMonitor monitor) throws CoreException {
IProject project = getProject();
if (project != null && project.isAccessible()) {
IMarker[] tmlmarkers = project.findMarkers(ResourceIDs.MARKER_TML_REFERENCES, false, IResource.DEPTH_INFINITE);
for (IMarker marker : tmlmarkers) {
if (marker.getResource() != null && marker.getResource() instanceof IFile) {
TMLFileValidator.validateTMLFile((IFile)marker.getResource());
}
}
project.accept(new IResourceVisitor() {
public boolean visit(IResource resource) throws CoreException {
if(resource instanceof IContainer){
WGADesignResourceValidator.validate((IContainer)resource);
return true;
}
return false;
}
});
}
return null;
}
}