use of org.eclipse.xtext.conversion.impl.AbstractNullSafeConverter in project xtext-core by eclipse.
the class XtextValueConverters method GrammarID.
@ValueConverter(rule = "GrammarID")
public IValueConverter<String> GrammarID() {
return new AbstractNullSafeConverter<String>() {
@Override
protected String internalToValue(String string, INode node) throws ValueConverterException {
if (node != null) {
StringBuilder result = new StringBuilder();
for (ILeafNode leaf : node.getLeafNodes()) {
if (!leaf.isHidden()) {
if (leaf.getGrammarElement() instanceof Keyword)
result.append(leaf.getText());
else
result.append(ID().toValue(leaf.getText(), leaf));
}
}
return result.toString();
} else {
String[] splitted = string.split("\\.");
StringBuilder result = new StringBuilder(string.length());
for (int i = 0; i < splitted.length; i++) {
if (i != 0)
result.append('.');
result.append(ID().toValue(splitted[i], null));
}
return result.toString();
}
}
@Override
protected String internalToString(String value) {
String[] splitted = value.split("\\.");
StringBuilder result = new StringBuilder(value.length());
for (int i = 0; i < splitted.length; i++) {
if (i != 0)
result.append('.');
result.append(ID().toString(splitted[i]));
}
return result.toString();
}
};
}
use of org.eclipse.xtext.conversion.impl.AbstractNullSafeConverter in project xtext-core by eclipse.
the class XtextValueConverters method RuleID.
@ValueConverter(rule = "RuleID")
public IValueConverter<String> RuleID() {
return new AbstractNullSafeConverter<String>() {
@Override
protected String internalToValue(String string, INode node) throws ValueConverterException {
if (node != null) {
StringBuilder result = new StringBuilder();
for (ILeafNode leaf : node.getLeafNodes()) {
if (!leaf.isHidden()) {
if (leaf.getGrammarElement() instanceof Keyword)
result.append(".");
else
result.append(ID().toValue(leaf.getText(), leaf));
}
}
return result.toString();
} else {
String[] splitted = string.split("\\.");
StringBuilder result = new StringBuilder(string.length());
for (int i = 0; i < splitted.length; i++) {
if (i != 0)
result.append('.');
result.append(ID().toValue(splitted[i], null));
}
return result.toString();
}
}
@Override
protected String internalToString(String value) {
String[] splitted = value.split("\\.");
StringBuilder result = new StringBuilder(value.length());
for (int i = 0; i < splitted.length; i++) {
if (i != 0)
result.append("::");
result.append(ID().toString(splitted[i]));
}
return result.toString();
}
};
}
use of org.eclipse.xtext.conversion.impl.AbstractNullSafeConverter in project xtext-core by eclipse.
the class DatatypeRulesTestLanguageValueConverters method Fraction.
@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();
}
};
}
Aggregations