| /** |
| * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * 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: |
| * Florian Pirchner - Initial implementation |
| */ |
| package org.eclipse.osbp.dsl.common.xtext.valueconverter; |
| |
| import org.eclipse.xtext.conversion.IValueConverter; |
| import org.eclipse.xtext.conversion.ValueConverter; |
| import org.eclipse.xtext.conversion.ValueConverterException; |
| import org.eclipse.xtext.conversion.impl.AbstractValueConverter; |
| import org.eclipse.xtext.nodemodel.INode; |
| import org.eclipse.xtext.xbase.conversion.XbaseValueConverterService; |
| |
| import com.google.inject.Inject; |
| import com.google.inject.Singleton; |
| |
| @SuppressWarnings("restriction") |
| @Singleton |
| public class CommonValueConverterService extends XbaseValueConverterService { |
| |
| @Inject |
| private LDecimalValueConverter lDecimalConverter; |
| |
| @Inject |
| private LIntegerValueConverter lIntConverter; |
| |
| @Inject |
| private TypeCrossReferenceValueConverter crossRefConverter; |
| |
| @ValueConverter(rule = "QualifiedNameWithWildCard") |
| public IValueConverter<String> getQualifiedNameWithWildCard() { |
| return getQualifiedNameValueConverter(); |
| } |
| |
| @ValueConverter(rule = "LDecimal") |
| public IValueConverter<Float> getLDecimalConverter() { |
| return lDecimalConverter; |
| } |
| |
| @ValueConverter(rule = "LInt") |
| public IValueConverter<Integer> getLIntConverter() { |
| return lIntConverter; |
| } |
| |
| @ValueConverter(rule = "TYPE_CROSS_REFERENCE") |
| public TypeCrossReferenceValueConverter getTypeCrossReferenceValueConverter() { |
| return crossRefConverter; |
| } |
| |
| public static class LDecimalValueConverter extends |
| AbstractValueConverter<Float> { |
| |
| public Float toValue(String string, INode node) |
| throws ValueConverterException { |
| return Float.valueOf(string); |
| } |
| |
| public String toString(Float value) throws ValueConverterException { |
| return value.toString(); |
| } |
| |
| } |
| |
| public static class LIntegerValueConverter extends |
| AbstractValueConverter<Integer> { |
| |
| public Integer toValue(String string, INode node) |
| throws ValueConverterException { |
| return Integer.valueOf(string); |
| } |
| |
| public String toString(Integer value) throws ValueConverterException { |
| return value.toString(); |
| } |
| |
| } |
| |
| /** |
| * Cuts the String from the last '.' |
| */ |
| public static class TypeCrossReferenceValueConverter extends |
| AbstractValueConverter<String> { |
| |
| @Override |
| public String toString(String value) { |
| if (value.contains(".")) { |
| value = value.substring(value.lastIndexOf(".") + 1, |
| value.length()); |
| } |
| |
| return value; |
| } |
| |
| @Override |
| public String toValue(String string, INode node) |
| throws ValueConverterException { |
| return toString(string); |
| } |
| |
| } |
| } |