Search in sources :

Example 6 with ValueConverterWithValueException

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

the class RichTextValueConverterTest method doTestIncompleteRichString.

protected void doTestIncompleteRichString(String text, String expectation) {
    RichTextValueConverter converter = get(RichTextValueConverter.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) RichTextValueConverter(org.eclipse.xtend.core.conversion.RichTextValueConverter)

Example 7 with ValueConverterWithValueException

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

the class OldJavaIDValueConverter method convertFromJavaIdentifier.

/**
 * Mostly copied from {@link Strings#convertFromJavaString(String, boolean)}
 */
public static String convertFromJavaIdentifier(String identifier, INode node) {
    char[] in = identifier.toCharArray();
    int off = 0;
    int len = identifier.length();
    char[] convtBuf = new char[len];
    char aChar;
    char[] out = convtBuf;
    int outLen = 0;
    int end = off + len;
    boolean error = false;
    boolean badChar = false;
    while (off < end) {
        aChar = in[off++];
        if (aChar == '\\') {
            if (off < end) {
                aChar = in[off++];
                switch(aChar) {
                    case 'u':
                        {
                            // Read the xxxx
                            int value = 0;
                            if (off + 4 > end || !isHexSequence(in, off, 4)) {
                                error = true;
                                out[outLen++] = aChar;
                                break;
                            } else {
                                for (int i = 0; i < 4; i++) {
                                    aChar = in[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)

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