Search in sources :

Example 1 with ValueConverterWithValueException

use of org.eclipse.xtext.conversion.ValueConverterWithValueException in project xtext-xtend by eclipse.

the class JavaIDValueConverter method doConvertFromJavaString.

/**
 * Mostly copied from {@link Strings#convertFromJavaString(String, boolean)}
 */
private static String doConvertFromJavaString(String identifier, int firstEscapeSequence, INode node) {
    int off = firstEscapeSequence;
    int len = identifier.length();
    char[] convtBuf = new char[len];
    char aChar;
    char[] out = convtBuf;
    identifier.getChars(0, firstEscapeSequence, out, 0);
    int outLen = firstEscapeSequence;
    int end = len;
    boolean error = false;
    boolean badChar = false;
    while (off < end) {
        aChar = identifier.charAt(off++);
        if (aChar == '\\') {
            if (off < end) {
                aChar = identifier.charAt(off++);
                switch(aChar) {
                    case 'u':
                        {
                            // Read the xxxx
                            int value = 0;
                            if (off + 4 > end || !isHexSequence(identifier, off, 4)) {
                                error = true;
                                out[outLen++] = aChar;
                                break;
                            } else {
                                for (int i = 0; i < 4; i++) {
                                    aChar = identifier.charAt(off++);
                                    switch(aChar) {
                                        case '0':
                                        case '1':
                                        case '2':
                                        case '3':
                                        case '4':
                                        case '5':
                                        case '6':
                                        case '7':
                                        case '8':
                                        case '9':
                                            value = (value << 4) + aChar - '0';
                                            break;
                                        case 'a':
                                        case 'b':
                                        case 'c':
                                        case 'd':
                                        case 'e':
                                        case 'f':
                                            value = (value << 4) + 10 + aChar - 'a';
                                            break;
                                        case 'A':
                                        case 'B':
                                        case 'C':
                                        case 'D':
                                        case 'E':
                                        case 'F':
                                            value = (value << 4) + 10 + aChar - 'A';
                                            break;
                                        default:
                                            throw new IllegalArgumentException("Malformed \\uxxxx encoding.");
                                    }
                                }
                                if (setChar(outLen, out, (char) value)) {
                                    outLen++;
                                } else {
                                    badChar = true;
                                }
                                break;
                            }
                        }
                    default:
                        {
                            if (setChar(outLen, out, aChar)) {
                                outLen++;
                            } else {
                                badChar = true;
                            }
                        }
                }
            } else {
                badChar = true;
            }
        } else {
            if (setChar(outLen, out, aChar)) {
                outLen++;
            } else {
                badChar = true;
            }
        }
    }
    String result = new String(out, 0, outLen);
    if (error) {
        throw new ValueConverterWithValueException("Illegal escape sequence in identifier '" + identifier + "'", node, result, null);
    }
    if (badChar) {
        if (result.length() != 0)
            throw new ValueConverterWithValueException("Illegal character in identifier '" + result + "' (" + identifier + ")", node, result, null);
        else
            throw new ValueConverterWithValueException("Illegal character in identifier '" + identifier + "'", node, null, null);
    }
    return result;
}
Also used : ValueConverterWithValueException(org.eclipse.xtext.conversion.ValueConverterWithValueException)

Example 2 with ValueConverterWithValueException

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

the class DefaultEcoreElementFactory method set.

@Override
public void set(EObject object, String feature, Object value, String ruleName, INode node) throws ValueConverterException {
    final EStructuralFeature structuralFeature = object.eClass().getEStructuralFeature(feature);
    if (structuralFeature == null)
        throw new IllegalArgumentException(object.eClass().getName() + "." + feature + " does not exist");
    try {
        final Object tokenValue = getTokenValue(value, ruleName, node);
        checkNullForPrimitiveFeatures(structuralFeature, tokenValue, node);
        object.eSet(structuralFeature, tokenValue);
    } catch (ValueConverterWithValueException e) {
        final Object tokenValue = e.getValue();
        checkNullForPrimitiveFeatures(structuralFeature, tokenValue, node);
        object.eSet(structuralFeature, tokenValue);
        throw e;
    } catch (ValueConverterException e) {
        throw e;
    } catch (NullPointerException e) {
        log.warn(e.getMessage(), e);
        throw new ValueConverterException("A NullPointerException occured. This indicates a missing value converter or a bug in its implementation.", node, e);
    } catch (Exception e) {
        throw new ValueConverterException(null, node, e);
    }
}
Also used : ValueConverterWithValueException(org.eclipse.xtext.conversion.ValueConverterWithValueException) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) ValueConverterWithValueException(org.eclipse.xtext.conversion.ValueConverterWithValueException) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException)

Example 3 with ValueConverterWithValueException

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

the class DefaultEcoreElementFactory method add.

@Override
@SuppressWarnings("unchecked")
public void add(EObject object, String feature, Object value, String ruleName, INode node) throws ValueConverterException {
    if (value == null)
        return;
    final EStructuralFeature structuralFeature = object.eClass().getEStructuralFeature(feature);
    if (structuralFeature == null)
        throw new IllegalArgumentException(object.eClass().getName() + "." + feature + " does not exist");
    try {
        if (value instanceof EObject) {
            // containment lists are unique per-se and the tokenValue was created just a sec ago
            ((InternalEList<EObject>) object.eGet(structuralFeature)).addUnique((EObject) value);
        } else {
            final Object tokenValue = getTokenValue(value, ruleName, node);
            checkNullForPrimitiveFeatures(structuralFeature, value, node);
            ((Collection<Object>) object.eGet(structuralFeature)).add(tokenValue);
        }
    } catch (ValueConverterWithValueException e) {
        final Object tokenValue = e.getValue();
        checkNullForPrimitiveFeatures(structuralFeature, value, node);
        ((Collection<Object>) object.eGet(structuralFeature)).add(tokenValue);
        throw e;
    } catch (ValueConverterException e) {
        throw e;
    } catch (NullPointerException e) {
        log.error(e.getMessage(), e);
        throw new ValueConverterException("A NullPointerException occured. This indicates a missing value converter or a bug in its implementation.", node, e);
    } catch (Exception e) {
        throw new ValueConverterException(null, node, e);
    }
}
Also used : ValueConverterWithValueException(org.eclipse.xtext.conversion.ValueConverterWithValueException) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) InternalEList(org.eclipse.emf.ecore.util.InternalEList) Collection(java.util.Collection) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) ValueConverterWithValueException(org.eclipse.xtext.conversion.ValueConverterWithValueException) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException)

Example 4 with ValueConverterWithValueException

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

the class LinkingHelper method getCrossRefNodeAsString.

public String getCrossRefNodeAsString(INode node, boolean convert) {
    String convertMe = NodeModelUtils.getTokenText(node);
    if (!convert)
        return convertMe;
    try {
        String ruleName = getRuleNameFrom(node.getGrammarElement());
        if (ruleName == null)
            return convertMe;
        Object result = valueConverter.toValue(convertMe, ruleName, node);
        return result != null ? result.toString() : null;
    } catch (ValueConverterWithValueException ex) {
        Object result = ex.getValue();
        return result != null ? result.toString() : null;
    } catch (ValueConverterException ex) {
        throw new IllegalNodeException(node, ex);
    }
}
Also used : ValueConverterWithValueException(org.eclipse.xtext.conversion.ValueConverterWithValueException) EObject(org.eclipse.emf.ecore.EObject) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException)

Example 5 with ValueConverterWithValueException

use of org.eclipse.xtext.conversion.ValueConverterWithValueException in project xtext-xtend by eclipse.

the class RichTextValueConverterTest method doTestIncompleteRichStringEnd.

protected void doTestIncompleteRichStringEnd(String text, String expectation) {
    RichTextEndValueConverter converter = get(RichTextEndValueConverter.class);
    try {
        converter.toValue(text, null);
        fail("Expected ValueConverterWithValueException");
    } catch (ValueConverterWithValueException e) {
        String value = (String) e.getValue();
        assertEquals(expectation, value);
    }
}
Also used : ValueConverterWithValueException(org.eclipse.xtext.conversion.ValueConverterWithValueException) CommentRichTextEndValueConverter(org.eclipse.xtend.core.conversion.CommentRichTextEndValueConverter) RichTextEndValueConverter(org.eclipse.xtend.core.conversion.RichTextEndValueConverter)

Aggregations

ValueConverterWithValueException (org.eclipse.xtext.conversion.ValueConverterWithValueException)7 EObject (org.eclipse.emf.ecore.EObject)3 ValueConverterException (org.eclipse.xtext.conversion.ValueConverterException)3 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)2 InternalEObject (org.eclipse.emf.ecore.InternalEObject)2 Collection (java.util.Collection)1 InternalEList (org.eclipse.emf.ecore.util.InternalEList)1 CommentRichTextEndValueConverter (org.eclipse.xtend.core.conversion.CommentRichTextEndValueConverter)1 RichTextEndValueConverter (org.eclipse.xtend.core.conversion.RichTextEndValueConverter)1 RichTextValueConverter (org.eclipse.xtend.core.conversion.RichTextValueConverter)1