All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.xtext.xbase.validation.XbaseSeverityConverter Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (c) 2013 itemis AG (http://www.itemis.eu) and others.
 * 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
 *******************************************************************************/
package org.eclipse.xtext.xbase.validation;

import java.util.List;

import org.eclipse.jdt.core.JavaCore;
import org.eclipse.xtext.diagnostics.Severity;
import org.eclipse.xtext.util.Pair;
import org.eclipse.xtext.util.Strings;
import org.eclipse.xtext.util.Tuples;
import org.eclipse.xtext.validation.SeverityConverter;

/**
 * @author Dennis Huebner - Initial contribution and API
 */
public class XbaseSeverityConverter extends SeverityConverter {

	private static final char DEFAULT_SEVERITY_SEPARATOR = ':';

	@Override
	public Severity stringToSeverity(final String severityAsString) {
		String valueToConvert = severityAsString;
		if (severityAsString.startsWith(JavaCore.PLUGIN_ID)) {
			valueToConvert = delegatedValue(decodeDelegationKey(severityAsString));
		}
		return super.stringToSeverity(valueToConvert);
	}

	/**
	 * 
	 * @param pair
	 *            holds first=delegationKey, second=defaultValue. delegationKey starts with {@link JavaCore#PLUGIN_ID}
	 * @return resolved delegated value
	 */
	protected String delegatedValue(Pair pair) {
		return pair.getSecond();
	}

	/**
	 * @return String delegationKey:defaultSeverity
	 */
	public static String encodeDefaultSeverity(String delegationKey, String defaultSeverity) {
		return new StringBuilder(delegationKey).append(DEFAULT_SEVERITY_SEPARATOR).append(defaultSeverity).toString();
	}

	/**
	 * Returns decoded delegation key or null if encodedValue can not be parsed.
	 * @return {@link Pair} where getFirst() is delegationKey and getSecond() is the defaultSeverity.
	 * @see XbaseSeverityConverter#encodeDefaultSeverity(String, String)
	 */
	public static Pair decodeDelegationKey(String encodedValue) {
		List split = Strings.split(encodedValue, DEFAULT_SEVERITY_SEPARATOR);
		if (split.size() == 2) {
			return Tuples.create(split.get(0), split.get(1));
		} else if (split.size() == 1) {
			return Tuples.create(split.get(0), SeverityConverter.SEVERITY_WARNING);
		} else {
			return null;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy