org.eclipse.xtext.conversion.ValueConverter Java Examples

The following examples show how to use org.eclipse.xtext.conversion.ValueConverter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DatatypeRulesTestLanguageValueConverters.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@ValueConverter(rule = "Fraction")
public IValueConverter<BigDecimal> Fraction() {
	return new AbstractNullSafeConverter<BigDecimal>(){
		@Override
		protected BigDecimal internalToValue(String string, INode node) {
			String[] splitted = string.split("/");
			if (splitted.length > 1) {
				return new BigDecimal(splitted[0].trim()).divide(new BigDecimal(splitted[1].trim()));
			}
			return new BigDecimal(string);
		}

		@Override
		protected String internalToString(BigDecimal value) {
			BigDecimal bd = value;
			int scale = bd.scale();
			if (scale <= 0) {
				return bd.toPlainString();
			}
			bd = bd.multiply(BigDecimal.valueOf(1, -1 * scale));
			return bd.toPlainString() + '/' + BigDecimal.valueOf(1, -1 * scale).toPlainString();
		}};
}
 
Example #2
Source File: DotFontNameConverters.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * A value converter for the PostScriptAlias datatype rule
 *
 * @return An IValueConverter<PostScriptFontAlias>
 */
@ValueConverter(rule = "PostScriptAlias")
public IValueConverter<PostScriptFontAlias> postScriptFontAlias() {
	return new AbstractNullSafeConverter<PostScriptFontAlias>() {

		@Override
		protected String internalToString(PostScriptFontAlias value) {
			return value.toString();
		}

		@Override
		protected PostScriptFontAlias internalToValue(String string,
				INode node) throws ValueConverterException {
			String postscriptComparator = string.toUpperCase(Locale.ENGLISH)
					.replaceAll("-", "_");
			return PostScriptFontAlias.valueOf(postscriptComparator);
		}
	};
}
 
Example #3
Source File: DotFontNameConverters.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
@ValueConverter(rule = "Style")
public IValueConverter<Style> style() {
	return new AbstractNullSafeConverter<Style>() {

		@Override
		protected String internalToString(Style value) {
			return value.getLiteral();
		}

		@Override
		protected Style internalToValue(String string, INode node)
				throws ValueConverterException {
			switch (string.toLowerCase(Locale.ENGLISH)) {
			case "oblique":
				return Style.OBLIQUE;
			case "italic":
				return Style.ITALIC;
			case "roman":
			default:
				return Style.NORMAL;
			}
		}
	};
}
 
Example #4
Source File: DotFontNameConverters.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
@ValueConverter(rule = "Variant")
public IValueConverter<Variant> variant() {
	return new AbstractNullSafeConverter<Variant>() {

		@Override
		protected String internalToString(Variant value) {
			return value.getLiteral();
		}

		@Override
		protected Variant internalToValue(String string, INode node)
				throws ValueConverterException {
			switch (string.toLowerCase(Locale.ENGLISH)) {
			case "small-caps":
				return Variant.SMALL_CAPS;
			default:
				return Variant.NORMAL;
			}
		}
	};
}
 
Example #5
Source File: FormatterTestValueConverters.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@ValueConverter(rule = "WrappingDataType")
public IValueConverter<String> WrappingDataType() {
	return new AbstractNullSafeConverter<String>() {

		@Override
		protected String internalToString(String value) {
			return value;
		}

		@Override
		protected String internalToValue(String string, INode node) throws ValueConverterException {
			return node.getText().trim();
		}
	};

}
 
Example #6
Source File: DotDoubleOnlyGrammarConverters.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * A value converter for the ID data type rule.
 *
 * @return A {@link DotIDValueConverter}.
 */
@ValueConverter(rule = "DOUBLE")
public IValueConverter<Double> doubleConverter() {
	return new AbstractNullSafeConverter<Double>() {

		@Override
		protected String internalToString(Double value) {
			return value.toString();
		}

		@Override
		protected Double internalToValue(String string, INode node)
				throws ValueConverterException {
			return DotDoubleUtil.parseDotDouble(string);
		}
	};
}
 
Example #7
Source File: FormatterTestValueConverters.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@ValueConverter(rule = "FQN")
public IValueConverter<String> FQN() {
	return new AbstractNullSafeConverter<String>() {
		@Override
		protected String internalToString(String value) {
			return value;
		}

		@Override
		protected String internalToValue(String string, INode node) {
			if (!string.equals(string.trim()))
				throw new RuntimeException();
			StringBuffer b = new StringBuffer();
			for (ILeafNode leaf : node.getLeafNodes()) {
				if (!leaf.isHidden()) {
					b.append(leaf.getText());
				}
			}
			return b.toString();
		}
	};
}
 
Example #8
Source File: Bug362902ValueConverters.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
@ValueConverter(rule = "MyId")
public IValueConverter<String> ID() {
	return new IValueConverter<String>() {

		@Override
		public String toValue(String string, INode node) throws ValueConverterException {
			if (string != null && string.length() > 3) {
				throw new ValueConverterException("ID too long", node, null);
			}
			return string;
		}

		@Override
		public String toString(String value) throws ValueConverterException {
			return value;
		}
	};
}
 
Example #9
Source File: AssignmentFinderTestLanguageRuntimeModule.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@ValueConverter(rule = "DatEnum")
public IValueConverter<TestEnum> DatEnum() {
	return new IValueConverter<TestEnum>() {

		@Override
		public TestEnum toValue(String string, INode node) throws ValueConverterException {
			if ("lit3".equals(string))
				return TestEnum.LIT3;
			else
				throw new ValueConverterException(null, null, null);
		}

		@Override
		public String toString(TestEnum value) throws ValueConverterException {
			if (value == TestEnum.LIT3)
				return TestEnum.LIT3.getName();
			else
				throw new ValueConverterException(null, null, null);
		}
	};
}
 
Example #10
Source File: FormatterTestValueConverters.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
@ValueConverter(rule = "FQN")
// CHECKSTYLE:OFF
public IValueConverter<String> FQN() { // NOPMD
  // CHECKSTYLE:ON
  return new AbstractNullSafeConverter<String>() {
    @Override
    protected String internalToValue(final String string, final INode node) {
      if (!string.equals(string.trim())) {
        throw new RuntimeException(); // NOPMD
      }
      StringBuffer b = new StringBuffer();
      for (ILeafNode l : node.getLeafNodes()) {
        if (!l.isHidden()) {
          b.append(l.getText());
        }
      }
      return b.toString();
    }

    @Override
    protected String internalToString(final String value) {
      return value;
    }
  };
}
 
Example #11
Source File: DotFontNameConverters.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@ValueConverter(rule = "Stretch")
public IValueConverter<Stretch> stretch() {
	return new AbstractNullSafeConverter<Stretch>() {

		@Override
		protected String internalToString(Stretch value) {
			return value.getLiteral();
		}

		@Override
		protected Stretch internalToValue(String string, INode node)
				throws ValueConverterException {
			switch (string.toLowerCase(Locale.ENGLISH)) {
			case "ultra-condensed":
				return Stretch.ULTRA_CONDENSED;
			case "extra-condensed":
				return Stretch.EXTRA_CONDENSED;
			case "condensed":
				return Stretch.CONDENSED;
			case "semi-condensed":
				return Stretch.SEMI_CONDENSED;
			case "semi-expanded":
				return Stretch.SEMI_EXPANDED;
			case "expanded":
				return Stretch.EXPANDED;
			case "extra-expanded":
				return Stretch.EXTRA_EXPANDED;
			case "ultra-expanded":
				return Stretch.ULTRA_EXPANDED;
			default:
				return Stretch.NORMAL;
			}
		}
	};
}
 
Example #12
Source File: ValueConverterForTerminalFragmentsTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@ValueConverter(rule = "ESCAPED_CHAR")
public IValueConverter<String> forFragment() {
	return new AbstractNullSafeConverter<String>() {
		@Override
		protected String internalToValue(String string, INode node) {
			return string;
		}

		@Override
		protected String internalToString(String value) {
			return value;
		}
	};
}
 
Example #13
Source File: DotFontNameConverters.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@ValueConverter(rule = "Gravity")
public IValueConverter<Gravity> gravity() {
	return new AbstractNullSafeConverter<Gravity>() {

		@Override
		protected String internalToString(Gravity value) {
			return value.getLiteral();
		}

		@Override
		protected Gravity internalToValue(String string, INode node)
				throws ValueConverterException {
			switch (string.toLowerCase(Locale.ENGLISH)) {
			case "upside-down":
			case "north":
				return Gravity.NORTH;
			case "rotated-left":
			case "east":
				return Gravity.EAST;
			case "rotated-right":
			case "west":
				return Gravity.WEST;
			case "not-rotated":
			case "south":
			default:
				return Gravity.SOUTH;
			}
		}
	};
}
 
Example #14
Source File: Bug250313RuntimeModule.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@ValueConverter(rule = "NestedDatatype")
public IValueConverter<String> NestedDatatype() {
	return new AbstractNullSafeConverter<String>() {
		@Override
		protected String internalToValue(String string, INode node) {
			return "str";
		}

		@Override
		protected String internalToString(String value) {
			throw new UnsupportedOperationException();
		}
	};
}
 
Example #15
Source File: XtextValueConverters.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@ValueConverter(rule = "LiteralValue")
public IValueConverter<Boolean> LiteralValue() {
	return new AbstractNullSafeConverter<Boolean>() {
		@Override
		protected Boolean internalToValue(String string, INode node) throws ValueConverterException {
			return "+".equals(string);
		}

		@Override
		protected String internalToString(Boolean value) {
			return value.booleanValue() ? "+" : "!";
		}
	};
}
 
Example #16
Source File: AssignmentsTestLanguageRuntimeModule.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@ValueConverter(rule = "StringDatatype")
public IValueConverter<String> StringDatatype() {
	return new AbstractToStringConverter<String>() {
		@Override
		protected String internalToValue(String string, INode node) {
			return "StringDatatype: " + string;
		}
	};
}
 
Example #17
Source File: TerminalRuleTestLanguageConverters.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@ValueConverter(rule = "STRING")
public IValueConverter<String> STRING() {
	return new AbstractNullSafeConverter<String>() {
		@Override
		protected String internalToValue(String string, INode node) {
			return Strings.convertFromJavaString(string.substring(1, string.length() - 1), false);
		}

		@Override
		protected String internalToString(String value) {
			return '"' + Strings.convertToJavaString(value, false) + '"';
		}
	};
}
 
Example #18
Source File: AssignmentsTestLanguageRuntimeModule.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@ValueConverter(rule = "IdDatatype")
public IValueConverter<String> IdDatatype() {
	return new AbstractToStringConverter<String>() {
		@Override
		protected String internalToValue(String string, INode node) {
			return "IdDatatype: " + string;
		}
	};
}
 
Example #19
Source File: Bug250313RuntimeModule.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@ValueConverter(rule = "Datatype")
public IValueConverter<String> Datatype() {
	return new AbstractNullSafeConverter<String>() {
		@Override
		protected String internalToValue(String string, INode node) {
			return "str";
		}

		@Override
		protected String internalToString(String value) {
			throw new UnsupportedOperationException();
		}
	};
}
 
Example #20
Source File: DefaultTerminalConverters.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @since 2.9
 */
@ValueConverter(rule = "org.eclipse.xtext.common.Terminals.ID")
public IValueConverter<String> TerminalsID() {
	return terminalsIdValueConverter;
}
 
Example #21
Source File: DotFontNameConverters.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
@ValueConverter(rule = "Weight")
public IValueConverter<Weight> weight() {
	return new AbstractNullSafeConverter<Weight>() {

		@Override
		protected String internalToString(Weight value) {
			return value.getLiteral();
		}

		@Override
		protected Weight internalToValue(String string, INode node)
				throws ValueConverterException {
			switch (string.toLowerCase(Locale.ENGLISH)) {
			case "thin":
				return Weight.THIN;
			case "ultra-light":
			case "extra-light":
				return Weight.ULTRALIGHT;
			case "light":
				return Weight.LIGHT;
			case "semi-light":
			case "demi-light":
				return Weight.SEMILIGHT;
			case "book":
				return Weight.BOOK;
			case "medium":
				return Weight.MEDIUM;
			case "semi-bold":
			case "demi-bold":
				return Weight.SEMIBOLD;
			case "bold":
				return Weight.BOLD;
			case "ultra-bold":
			case "extra-bold":
				return Weight.ULTRABOLD;
			case "heavy":
			case "black":
				return Weight.HEAVY;
			case "ultra-heavy":
			case "extra-heavy":
			case "ultra-black":
			case "extra-black":
				return Weight.ULTRAHEAVY;
			case "regular":
			default:
				return Weight.NORMAL;
			}
		}
	};
}
 
Example #22
Source File: XtendValueConverterService.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@ValueConverter(rule = "COMMENT_RICH_TEXT_INBETWEEN")
public IValueConverter<String> getCommentRichTextInBetweenValueConverter() {
	return commentRichTextInBetweenValueConverter;
}
 
Example #23
Source File: XbaseValueConverterService.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
@ValueConverter(rule = "INT")
public IValueConverter<Integer> INT() {
	return intUnderscoreValueConverter;
}
 
Example #24
Source File: XbaseValueConverterService.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@ValueConverter(rule = "OpPostfix")
public IValueConverter<String> getOpPostfixConverter() {
	return keywordBasedConverterProvider.get();
}
 
Example #25
Source File: QualifiedNameTerminalConverters.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@ValueConverter(rule = "QualifiedName")
public IValueConverter<String> QualifiedName() {
	return qualifiedNameValueConverter;
}
 
Example #26
Source File: XbaseValueConverterService.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@ValueConverter(rule = "OpMulti")
public IValueConverter<String> getOpMultiConverter() {
	return keywordBasedConverterProvider.get();
}
 
Example #27
Source File: XbaseValueConverterService.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@ValueConverter(rule = "OpAdd")
public IValueConverter<String> getOpAddConverter() {
	return keywordBasedConverterProvider.get();
}
 
Example #28
Source File: XbaseValueConverterService.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@ValueConverter(rule = "OpOther")
public IValueConverter<String> getOpOtherConverter() {
	return otherOperatorsValueConverter;
}
 
Example #29
Source File: XbaseValueConverterService.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@ValueConverter(rule = "OpCompare")
public IValueConverter<String> getOpCompareConverter() {
	return compareOperatorsValueConverter;
}
 
Example #30
Source File: XbaseValueConverterService.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@ValueConverter(rule = "OpEquality")
public IValueConverter<String> getOpEqualityConverter() {
	return keywordBasedConverterProvider.get();
}