Search in sources :

Example 1 with IValueConverter

use of org.eclipse.xtext.conversion.IValueConverter 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();
        }
    };
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) Keyword(org.eclipse.xtext.Keyword) AbstractNullSafeConverter(org.eclipse.xtext.conversion.impl.AbstractNullSafeConverter) IValueConverter(org.eclipse.xtext.conversion.IValueConverter) ValueConverter(org.eclipse.xtext.conversion.ValueConverter)

Example 2 with IValueConverter

use of org.eclipse.xtext.conversion.IValueConverter 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();
        }
    };
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) Keyword(org.eclipse.xtext.Keyword) AbstractNullSafeConverter(org.eclipse.xtext.conversion.impl.AbstractNullSafeConverter) IValueConverter(org.eclipse.xtext.conversion.IValueConverter) ValueConverter(org.eclipse.xtext.conversion.ValueConverter)

Example 3 with IValueConverter

use of org.eclipse.xtext.conversion.IValueConverter 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();
        }
    };
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) BigDecimal(java.math.BigDecimal) AbstractNullSafeConverter(org.eclipse.xtext.conversion.impl.AbstractNullSafeConverter) IValueConverter(org.eclipse.xtext.conversion.IValueConverter) ValueConverter(org.eclipse.xtext.conversion.ValueConverter)

Example 4 with IValueConverter

use of org.eclipse.xtext.conversion.IValueConverter in project xtext-core by eclipse.

the class AbstractDeclarativeValueConverterService method recursiveRegisterForClass.

private void recursiveRegisterForClass(Method[] methods, Set<String> thisConverters, boolean onlyExplicitConverters, Map<String, IValueConverter<Object>> converters) {
    for (Method method : methods) {
        if (isConfigurationMethod(method)) {
            try {
                String ruleName = method.getAnnotation(ValueConverter.class).rule();
                if (onlyExplicitConverters ^ ruleName.indexOf('.') < 0) {
                    AbstractRule rule = GrammarUtil.findRuleForName(getGrammar(), ruleName);
                    if (rule != null) {
                        if (rule instanceof TerminalRule) {
                            if (((TerminalRule) rule).isFragment())
                                throw new IllegalStateException("Tried to register a value converter for a fragment terminal rule: '" + ruleName + "'");
                        }
                        if (!thisConverters.add(ruleName)) {
                            throw new IllegalStateException("Tried to register two value converters for rule '" + ruleName + "'");
                        }
                        IValueConverter<Object> converter = registerIfMissing(ruleName, rule, method, converters);
                        String qualifiedName = ruleNames.getQualifiedName(rule);
                        registerIfMissing(qualifiedName, rule, converter, method, converters);
                    } else {
                        log.trace("Tried to register value converter for rule '" + ruleName + "' which is not available in the grammar.");
                    }
                }
            } catch (IllegalStateException e) {
                throw e;
            } catch (IllegalArgumentException e) {
                log.warn(e.getMessage(), e);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    }
}
Also used : Method(java.lang.reflect.Method) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule) IValueConverter(org.eclipse.xtext.conversion.IValueConverter) ValueConverter(org.eclipse.xtext.conversion.ValueConverter) InvocationTargetException(java.lang.reflect.InvocationTargetException) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException)

Aggregations

IValueConverter (org.eclipse.xtext.conversion.IValueConverter)4 ValueConverter (org.eclipse.xtext.conversion.ValueConverter)4 AbstractNullSafeConverter (org.eclipse.xtext.conversion.impl.AbstractNullSafeConverter)3 INode (org.eclipse.xtext.nodemodel.INode)3 Keyword (org.eclipse.xtext.Keyword)2 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 BigDecimal (java.math.BigDecimal)1 AbstractRule (org.eclipse.xtext.AbstractRule)1 TerminalRule (org.eclipse.xtext.TerminalRule)1 ValueConverterException (org.eclipse.xtext.conversion.ValueConverterException)1