/*******************************************************************************
* 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.tml.markers;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITypedRegion;
import de.innovationgate.eclipse.editors.ResourceIDs;
import de.innovationgate.eclipse.editors.config.TMLTag;
import de.innovationgate.eclipse.editors.helpers.LabelFileLookup;
import de.innovationgate.eclipse.editors.helpers.MarkerFactory;
import de.innovationgate.eclipse.editors.helpers.TMLTagValidator;
import de.innovationgate.eclipse.editors.tml.TMLRegion;
import de.innovationgate.eclipse.utils.wga.WGADesignStructureHelper;
public class TMLLabelTagValidator extends TMLTagValidator {
public static final String ATTRIBUTE_LABELKEY = "labelKey";
public static final String ATTRIBUTE_LABELFILENAME = "labelFilename";
public String getMarkerID() {
return ResourceIDs.MARKER_TML_LABEL;
}
public void validateAndMark(IFile file, IDocument document, ITypedRegion partition, TMLRegion tmlInfo, TMLTag tag) {
int charStart = partition.getOffset();
int charEnd = charStart + partition.getLength();
if (tag.getName().equals("label") && tmlInfo.hasAttribute("key") && !tmlInfo.isDynamicAttributeValue("key")) {
String labelFilename = LabelFileLookup.determineLabelFileName(tmlInfo);
if (labelFilename != null) {
String labelKey = tmlInfo.getAttributeValue("key");
Map<String,Object> markerAttributes = new HashMap<String,Object>();
markerAttributes.put(ATTRIBUTE_LABELFILENAME, labelFilename);
markerAttributes.put(ATTRIBUTE_LABELKEY, labelKey);
WGADesignStructureHelper designHelper= new WGADesignStructureHelper(file);
if (!labelFilename.equalsIgnoreCase("general")) {
// check if labelfile exists
try {
IFile labelFile = designHelper.getLabelFile(labelFilename);
if (labelFile == null || !labelFile.exists()) {
try {
IRegion attributeRegion = tmlInfo.getAttributeValueRegion("file");
if (attributeRegion != null) {
charStart = attributeRegion.getOffset();
charEnd = charStart + attributeRegion.getLength();
}
} catch (BadLocationException e) {
}
MarkerFactory.createErrorMarker(getMarkerID(), file, charStart, charEnd, "Labelfile '" + labelFilename + "' does not exist.", markerAttributes);
}
} catch (CoreException e1) {
}
}
Set<String> labelKeys = designHelper.getLabelKeys(labelFilename);
if (!labelKeys.contains(labelKey)) {
try {
IRegion attributeRegion = tmlInfo.getAttributeValueRegion("key");
if (attributeRegion != null) {
charStart = attributeRegion.getOffset();
charEnd = charStart + attributeRegion.getLength();
}
} catch (BadLocationException e) {
}
if (labelFilename.equalsIgnoreCase("general")) {
MarkerFactory.createWarningMarker(getMarkerID(), file, charStart, charEnd, "Cannot resolve label for key '" + labelKey + "'.", markerAttributes);
} else {
MarkerFactory.createErrorMarker(getMarkerID(), file, charStart, charEnd, "Missing label for key '" + labelKey + "'.", markerAttributes);
}
}
}
}
}
}