/*******************************************************************************
* 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 mandatory attributes
* validation is based on the TMLTagDefinition and does not check for usefully attribute combinations
*
*
*/
public class GenericMandatoryAttributesValidator extends TMLTagValidator {
public void validateAndMark(IFile file, IDocument document, ITypedRegion partition, TMLRegion tmlInfo, TMLTag tag) {
if (wgaVersionIsAtLeast(4, 1) && tmlInfo.getTagName().matches("\\[.*\\]")) {
// this is a short cut for <tml:include ref="xy"> no attributes necessary
return;
}
int charStart = partition.getOffset();
int charEnd = charStart + partition.getLength();
Iterator<TMLTagAttribute> attributes = tag.getRequiredAttributes().iterator();
while (attributes.hasNext()) {
TMLTagAttribute attribute = attributes.next();
if (!tmlInfo.hasAttribute(attribute.getName())) {
MarkerFactory.createErrorMarker(getMarkerID(), file, charStart, charEnd, "Attribute '" + attribute.getName() + "' is mandatory for tml tag '" + tmlInfo.getTagName() + "'.");
}
}
if (tag.getRequiredAttributes().isEmpty() && tag.isAttributeNecessary() && tmlInfo.getAttributeNames().isEmpty()) {
MarkerFactory.createErrorMarker(getMarkerID(), file, charStart, charEnd, "At least one attribute is necessary on tml tag '" + tmlInfo.getTagName() + "'.");
}
}
public String getMarkerID() {
return "de.innovationgate.eclipse.ids.markers.tml.GenericMandatoryAttributes";
}
}