Jelajahi Sumber

Initial Contribution of a QIDValueConverter (#1465)

* Initial Contribution of a QIDValudConverter

* Formatting

* Refactoring

* Update QIDValueConverter.java

* Using QualifiedName instead of String splitting

* Refactoring

* Minor Refactoring
jthoene 8 tahun lalu
induk
melakukan
b855171799

+ 13 - 5
plugins/org.yakindu.base.expressions/src/org/yakindu/base/expressions/terminals/ExpressionsValueConverterService.java

@@ -25,11 +25,19 @@ import com.google.inject.Inject;
 public class ExpressionsValueConverterService extends DefaultTerminalConverters {
 	
 	@Inject
-	private AbstractIDValueConverter idValueConverter;
+	protected AbstractIDValueConverter idValueConverter;
 	
-	protected BoolValueConverter boolConverter = new BoolValueConverter();
-	protected HexValueConverter hexConverter = new HexValueConverter();
-	protected BinaryValueConverter binaryConverter = new BinaryValueConverter();
+	@Inject
+	protected BoolValueConverter boolConverter;
+	
+	@Inject
+	protected HexValueConverter hexConverter;
+	
+	@Inject
+	protected BinaryValueConverter binaryConverter;
+	
+	@Inject
+	protected QIDValueConverter qidConverter;
 	
 	@ValueConverter(rule = "BOOL")
 	public IValueConverter<Boolean> BOOL() {
@@ -48,7 +56,7 @@ public class ExpressionsValueConverterService extends DefaultTerminalConverters
 	
 	@ValueConverter(rule = "QID")
 	public IValueConverter<String> QID() {
-		return  idValueConverter;
+		return qidConverter;
 	}
 
 

+ 55 - 0
plugins/org.yakindu.base.expressions/src/org/yakindu/base/expressions/terminals/QIDValueConverter.java

@@ -0,0 +1,55 @@
+/** 
+ * Copyright (c) 2017 committers of YAKINDU 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 
+ * Contributors:
+ * 
+ * Jonathan Thoene - initial API and implementation
+ *
+*/
+package org.yakindu.base.expressions.terminals;
+
+import org.eclipse.xtext.conversion.ValueConverterException;
+import org.eclipse.xtext.conversion.impl.AbstractIDValueConverter;
+import org.eclipse.xtext.conversion.impl.AbstractValueConverter;
+import org.eclipse.xtext.naming.IQualifiedNameConverter;
+import org.eclipse.xtext.naming.QualifiedName;
+import org.eclipse.xtext.nodemodel.INode;
+
+import com.google.inject.Inject;
+
+/**
+ * 
+ * @author jonathan thoene - Initial contribution and API
+ * 
+ */
+
+public class QIDValueConverter extends AbstractValueConverter<String> {
+
+	@Inject
+	protected AbstractIDValueConverter idValueConverter;
+	@Inject
+	protected IQualifiedNameConverter converter;
+
+	@Override
+	public String toValue(String string, INode node) throws ValueConverterException {
+		QualifiedName name = converter.toQualifiedName(string);
+		QualifiedName result = QualifiedName.EMPTY;
+		for(String str : name.getSegments()){
+			result = result.append(idValueConverter.toValue(str, node));
+		}
+		return result.toString();
+	}
+
+	@Override
+	public String toString(String value) throws ValueConverterException {
+		QualifiedName name = converter.toQualifiedName(value);
+		QualifiedName result = QualifiedName.EMPTY;
+		for(String str : name.getSegments()){
+			result = result.append(idValueConverter.toString(str));
+		}
+		return result.toString();
+	}
+}