org.eclipse.xtext.conversion.impl.AbstractNullSafeConverter Java Examples

The following examples show how to use org.eclipse.xtext.conversion.impl.AbstractNullSafeConverter. 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: TerminalRuleTestLanguageConverters.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@ValueConverter(rule = "ID")
public IValueConverter<String> ID() {
	return new AbstractNullSafeConverter<String>() {
		@Override
		protected String internalToValue(String string, INode node) {
			return string.startsWith("^") ? string.substring(1) : string;
		}

		@Override
		protected String internalToString(String value) {
			if (GrammarUtil.getAllKeywords(getGrammar()).contains(value)) {
				return "^"+value;
			}
			return value;
		}
	};
}
 
Example #2
Source File: SitemapConverters.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
@ValueConverter(rule = "Command")
public IValueConverter<String> Command() {
    return new AbstractNullSafeConverter<String>() {
        @Override
        protected String internalToValue(String string, INode node) {
            if ((string.startsWith("'") && string.endsWith("'"))
                    || (string.startsWith("\"") && string.endsWith("\""))) {
                return STRING().toValue(string, node);
            }
            return ID().toValue(string, node);
        }

        @Override
        protected String internalToString(String value) {
            if (ID_PATTERN.matcher(value).matches()) {
                return ID().toString(value);
            } else {
                return STRING().toString(value);
            }
        }
    };
}
 
Example #3
Source File: SitemapConverters.java    From openhab-core with Eclipse Public License 2.0 6 votes vote down vote up
@ValueConverter(rule = "Command")
public IValueConverter<String> Command() {
    return new AbstractNullSafeConverter<String>() {
        @Override
        protected String internalToValue(String string, INode node) {
            if ((string.startsWith("'") && string.endsWith("'"))
                    || (string.startsWith("\"") && string.endsWith("\""))) {
                return STRING().toValue(string, node);
            }
            return ID().toValue(string, node);
        }

        @Override
        protected String internalToString(String value) {
            if (ID_PATTERN.matcher(value).matches()) {
                return ID().toString(value);
            } else {
                return STRING().toString(value);
            }
        }
    };
}
 
Example #4
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 #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: 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 #7
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 #8
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 #9
Source File: Ecore2XtextTerminalConverters.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@ValueConverter(rule = "EString")
public IValueConverter<String> EString() {
	return new AbstractNullSafeConverter<String>() {
		@Override
		protected String internalToValue(String string, INode node) {
			if((string.startsWith("'") && string.endsWith("'"))||(string.startsWith("\"") && string.endsWith("\""))) {
				return STRING().toValue(string, node);
			}
			return ID().toValue(string, node);
		}

		@Override
		protected String internalToString(String value) {
			if(ID_PATTERN.matcher(value).matches()) {
				return ID().toString(value);
			} else {
				return STRING().toString(value);
			}
		}
	};
}
 
Example #10
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 #11
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 #12
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 #13
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 #14
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 #15
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 #16
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 #17
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 #18
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 #19
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 #20
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;
			}
		}
	};
}