/*******************************************************************************
* 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.Iterator;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITypedRegion;
import de.innovationgate.eclipse.editors.config.TMLTag;
import de.innovationgate.eclipse.editors.config.TMLTagAttribute;
import de.innovationgate.eclipse.editors.helpers.MarkerFactory;
import de.innovationgate.eclipse.editors.helpers.TMLTagValidator;
import de.innovationgate.eclipse.editors.tml.TMLRegion;
/**
* generic tml tag validator for attribute values
* validation is based on the TMLTagDefinition
*
*
*/
public class GenericAttributeValueValidator extends TMLTagValidator {
public void validateAndMark(IFile file, IDocument document, ITypedRegion partition, TMLRegion tmlInfo, TMLTag tag) {
Iterator<String> attributes = tmlInfo.getAttributeNames().iterator();
while (attributes.hasNext()) {
String attribute = attributes.next();
String value = tmlInfo.getAttributeValue(attribute);
if (value != null && !value.trim().startsWith("{") && !value.trim().endsWith("}")) {
TMLTagAttribute tagAttribute = tag.getAttribute(attribute);
if (tagAttribute != null && tagAttribute.getValidValues().size() > 0) {
// only defined attribute values allowed - so check
if (!tagAttribute.getValidValues().contains(value)) {
int charStart = tmlInfo.getDocumentRegion().getOffset();
int charEnd = tmlInfo.getDocumentRegion().getOffset() + tmlInfo.getDocumentRegion().getLength();
int valueStart = tmlInfo.getContent().indexOf(value);
if (valueStart != -1) {
charStart = charStart + valueStart;
charEnd = charStart + value.length();
}
MarkerFactory.createErrorMarker(getMarkerID(), file, charStart, charEnd, "Illegal attribute value '" + value + "' for attribute '" + attribute + "' on tml tag '" + tmlInfo.getTagName() + "'.");
}
}
}
}
}
public String getMarkerID() {
return "de.innovationgate.eclipse.ids.markers.tml.GenericAttributeValueValidation";
}
}