Java Code Examples for org.eclipse.xtext.conversion.IValueConverter

The following examples show how to use org.eclipse.xtext.conversion.IValueConverter. These examples are extracted from open source projects. 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
@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
private IValueConverter<Object> getDelegateConverter() {
	if (delegateConverter != null) {
		return delegateConverter;
	}
	if (delegateService instanceof IValueConverterService.Introspectable) {
		return delegateConverter = ((IValueConverterService.Introspectable) delegateService).getConverter(delegateRule.getName());
	} else {
		final String ruleName = delegateRule.getName();
		return delegateConverter = new IValueConverter<Object>() {

			@Override
			public Object toValue(String string, INode node) throws ValueConverterException {
				return delegateService.toValue(string, ruleName, node);
			}

			@Override
			public String toString(Object value) throws ValueConverterException {
				return delegateService.toString(value, ruleName);
			}
			
		};
	}
}
 
Example 3
@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 4
Source Project: openhab-core   Source File: SitemapConverters.java    License: 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 5
@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 6
public RewritableImportSection(XtextResource resource, IImportsConfiguration importsConfiguration, XImportSection originalImportSection,
		String lineSeparator, ImportSectionRegionUtil regionUtil, IValueConverter<String> nameConverter) {
	this.importsConfiguration = importsConfiguration;
	this.resource = resource;
	this.lineSeparator = lineSeparator;
	this.regionUtil = regionUtil;
	this.nameValueConverter = nameConverter;
	this.implicitlyImportedPackages = importsConfiguration.getImplicitlyImportedPackages(resource);
	this.importRegion = regionUtil.computeRegion(resource);
	if (originalImportSection != null) {
		for (XImportDeclaration originalImportDeclaration : originalImportSection.getImportDeclarations()) {
			this.originalImportDeclarations.add(originalImportDeclaration);
			JvmDeclaredType importedType = originalImportDeclaration.getImportedType();
			if (originalImportDeclaration.isStatic()) {
				String memberName = originalImportDeclaration.getMemberName();
				if (originalImportDeclaration.isExtension()) {
					Maps2.putIntoSetMap(importedType, memberName, staticExtensionImports);
				} else {
					Maps2.putIntoSetMap(importedType, memberName, staticImports);
				}
			} else if (importedType != null) {
				Maps2.putIntoListMap(importedType.getSimpleName(), importedType, plainImports);
			}
		}
	}
}
 
Example 7
@Override
public void validateParameters(Variable variable, ValidationMessageAcceptor validationMessageAcceptor) {
	if (variable.getParameters().isEmpty()) {
		validationMessageAcceptor.acceptError(getType() + "-variables have mandatory parameters.", variable,
				TemplatesPackage.Literals.VARIABLE__TYPE, ValidationMessageAcceptor.INSIGNIFICANT_INDEX, null);
	} else {
		EList<String> parameters = variable.getParameters();
		for (int i = 0; i < parameters.size(); i++) {
			String param = parameters.get(i);
			try {
				IValueConverter<String> converter = ((XbaseValueConverterService) valueConverterService)
						.getQualifiedNameWithWildCardValueConverter();
				converter.toString(param);
			} catch (ValueConverterException e) {
				validationMessageAcceptor.acceptError(getType() + " - parameter " + param
						+ " is not a valid qualifier.", variable, TemplatesPackage.Literals.VARIABLE__PARAMETERS,
						i, null);
			}
		}
	}
}
 
Example 8
Source Project: openhab-core   Source File: SitemapConverters.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@ValueConverter(rule = "Icon")
public IValueConverter<String> Icon() {

    return new IValueConverter<String>() {

        @Override
        public String toValue(String string, INode node) throws ValueConverterException {
            if (string != null && string.startsWith("\"")) {
                return string.substring(1, string.length() - 1);
            }
            return string;
        }

        @Override
        public String toString(String value) throws ValueConverterException {
            if (containsWhiteSpace(value)) {
                return "\"" + value + "\"";
            }
            return value;
        }

    };
}
 
Example 9
@ValueConverter(rule = "NULL_STRING")
public IValueConverter<String> NULL_STRING() {
	return new IValueConverter<String>() {
		@Override
		public String toString(String value) throws ValueConverterException {
			if (value == null)
				return "''";
			return "'" + value + "'";
		}

		@Override
		public String toValue(String string, INode node) throws ValueConverterException {
			if (string.length() <= 2) {
				return null;
			}
			return string.substring(1, string.length() - 1);
		}
	};
}
 
Example 10
Source Project: smarthome   Source File: SitemapConverters.java    License: 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 11
Source Project: gef   Source File: DotFontNameConverters.java    License: 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 12
Source Project: gef   Source File: DotFontNameConverters.java    License: 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 Project: gef   Source File: DotFontNameConverters.java    License: 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 14
/**
 * 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 15
@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 16
@Inject
private void setValueConverter(IValueConverterService service, N4JSGrammarAccess grammarAccess) {
	@SuppressWarnings({ "unchecked", "rawtypes" })
	IValueConverter<String> converter = (IValueConverter) ((IValueConverterService.Introspectable) service)
			.getConverter(grammarAccess
					.getTypeReferenceNameRule()
					.getName());
	this.valueConverter = converter;
}
 
Example 17
Source Project: xtext-core   Source File: Bug250313RuntimeModule.java    License: 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 18
@ValueConverter(rule = "StringDatatype")
public IValueConverter<String> StringDatatype() {
	return new AbstractToStringConverter<String>() {
		@Override
		protected String internalToValue(String string, INode node) {
			return "StringDatatype: " + string;
		}
	};
}
 
Example 19
private IValueConverter<Object> registerIfMissing(String name, AbstractRule rule, Method method,
		Map<String, IValueConverter<Object>> converters) throws IllegalAccessException, InvocationTargetException {
	if (!converters.containsKey(name)) {
		IValueConverter<Object> valueConverter = reflectiveGetConverter(method, rule);
		converters.put(name, valueConverter);
		return valueConverter;
	}
	return null;
}
 
Example 20
private void registerIfMissing(String name, ParserRule parserRule,
		Map<String, IValueConverter<Object>> converters) {
	if (!converters.containsKey(name)) {
		EDataType datatype = (EDataType) parserRule.getType().getClassifier();
		converters.put(name, new EFactoryValueConverter(datatype));
	}
}
 
Example 21
/**
 * @since 2.4
 * @nooverride This method is not intended to be re-implemented or extended by clients.
 * @noreference This method is not intended to be referenced by clients.
 */
protected void registerEFactoryConverters(Map<String, IValueConverter<Object>> converters) {
	for (ParserRule parserRule : allParserRules(getGrammar())) {
		if (isDatatypeRule(parserRule)) {
			registerIfMissing(parserRule.getName(), parserRule, converters);
			registerIfMissing(ruleNames.getQualifiedName(parserRule), parserRule, converters);
		}
	}
	for (TerminalRule terminalRule : allTerminalRules(getGrammar())) {
		if (!terminalRule.isFragment()) {
			registerIfMissing(terminalRule.getName(), terminalRule, converters);
			registerIfMissing(ruleNames.getQualifiedName(terminalRule), terminalRule, converters);
		}
	}
}
 
Example 22
@SuppressWarnings("unchecked")
private IValueConverter<Object> reflectiveGetConverter(Method method, AbstractRule rule)
		throws IllegalAccessException, InvocationTargetException {
	IValueConverter<Object> valueConverter = (IValueConverter<Object>) method.invoke(this);
	if (valueConverter instanceof IValueConverter.RuleSpecific)
		((IValueConverter.RuleSpecific) valueConverter).setRule(rule);
	return valueConverter;
}
 
Example 23
public void createTypeProposals(IJavaProject project, ICompletionProposalFactory proposalFactory, ContentAssistContext context,
		EReference typeReference, Filter filter, IValueConverter<String> valueConverter, ICompletionProposalAcceptor acceptor) {
	try {
		IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(new IJavaElement[] { project });
		searchAndCreateProposals(searchScope, proposalFactory, context, typeReference, filter, valueConverter, acceptor);
	}
	catch (JavaModelException e) {
		// ignore
	}
}
 
Example 24
Source Project: gef   Source File: DotFontNameConverters.java    License: 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 25
Source Project: gef   Source File: DotFontNameConverters.java    License: 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 26
@Override
protected ConfigurableCompletionProposal.IReplacementTextApplier createTextApplier(final ContentAssistContext context, final IScope typeScope, final IQualifiedNameConverter qualifiedNameConverter, final IValueConverter<String> valueConverter) {
  final Predicate<IEObjectDescription> _function = (IEObjectDescription it) -> {
    QualifiedName _name = it.getName();
    return (!Objects.equal(_name, XtendImportedNamespaceScopeProvider.OLD_DATA_ANNOTATION));
  };
  final FilteringScope scope = new FilteringScope(typeScope, _function);
  return super.createTextApplier(context, scope, qualifiedNameConverter, valueConverter);
}
 
Example 27
/**
 * @since 2.9
 */
@ValueConverter(rule = "org.eclipse.xtext.common.Terminals.STRING")
public IValueConverter<String> TerminalsSTRING() {
	return terminalsStringValueConverter;
}
 
Example 28
/**
 * @since 2.9
 */
@ValueConverter(rule = "org.eclipse.xtext.common.Terminals.INT")
public IValueConverter<Integer> TerminalsINT() {
	return terminalsIntValueConverter;
}
 
Example 29
Source Project: ghidra   Source File: SleighValueConverter.java    License: Apache License 2.0 4 votes vote down vote up
@ValueConverter(rule = "BINVAL")
public IValueConverter<BigInteger> BINVAL() {
	return binValueConverter;
}
 
Example 30
Source Project: n4js   Source File: TypesValueConverterService.java    License: Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Registers the value converter for the rule {@code TypesIdentifier}.
 */
@ValueConverter(rule = "TypesIdentifier")
public IValueConverter<String> TypesIdentifier() {
	return identifierDelegateValueConverter;
}