Search in sources :

Example 21 with ValueConverterException

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

the class AbstractRichTextValueConverter method toValue.

@Override
public String toValue(String string, INode node) {
    if (string == null)
        return null;
    try {
        String leadingTerminal = getLeadingTerminal();
        if (string.length() <= leadingTerminal.length()) {
            throw stringLiteralIsNotClosed(node, "");
        }
        String withoutLeadingTerminal = getWithoutLeadingTerminal(string);
        String trailingTerminal = getTrailingTerminal();
        if (withoutLeadingTerminal.endsWith(trailingTerminal)) {
            String result = withoutLeadingTerminal.substring(0, withoutLeadingTerminal.length() - trailingTerminal.length());
            return result;
        }
        List<String> trailingSubsequences = getTrailingSubsequences();
        for (String subsequence : trailingSubsequences) {
            if (withoutLeadingTerminal.endsWith(subsequence)) {
                throw stringLiteralIsNotClosed(node, withoutLeadingTerminal.substring(0, withoutLeadingTerminal.length() - subsequence.length()));
            }
        }
        throw stringLiteralIsNotClosed(node, withoutLeadingTerminal.substring(0, withoutLeadingTerminal.length()));
    } catch (StringIndexOutOfBoundsException e) {
        throw new ValueConverterException(e.getMessage(), node, e);
    }
}
Also used : ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException)

Example 22 with ValueConverterException

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

the class XtextLinkingService method getUsedGrammar.

private List<EObject> getUsedGrammar(Grammar grammar, INode node) {
    try {
        String grammarName = (String) valueConverterService.toValue("", "GrammarID", node);
        if (grammarName != null) {
            final ResourceSet resourceSet = grammar.eResource().getResourceSet();
            List<Resource> resources = resourceSet.getResources();
            for (int i = 0; i < resources.size(); i++) {
                Resource resource = resources.get(i);
                EObject rootElement = null;
                if (resource instanceof XtextResource) {
                    IParseResult parseResult = ((XtextResource) resource).getParseResult();
                    if (parseResult != null)
                        rootElement = parseResult.getRootASTElement();
                } else if (!resource.getContents().isEmpty()) {
                    rootElement = resource.getContents().get(0);
                }
                if (rootElement instanceof Grammar) {
                    Grammar otherGrammar = (Grammar) rootElement;
                    if (grammarName.equals(otherGrammar.getName())) {
                        if (resource instanceof DerivedStateAwareResource)
                            resource.getContents();
                        return Collections.<EObject>singletonList(otherGrammar);
                    }
                }
            }
            URI classpathURI = URI.createURI(ClasspathUriUtil.CLASSPATH_SCHEME + ":/" + grammarName.replace('.', '/') + "." + fileExtension);
            URI normalizedURI = null;
            if (resourceSet instanceof XtextResourceSet) {
                XtextResourceSet set = (XtextResourceSet) resourceSet;
                normalizedURI = set.getClasspathUriResolver().resolve(set.getClasspathURIContext(), classpathURI);
            } else {
                normalizedURI = resourceSet.getURIConverter().normalize(classpathURI);
            }
            final Resource resource = resourceSet.getResource(normalizedURI, true);
            if (!resource.getContents().isEmpty()) {
                final Grammar usedGrammar = (Grammar) resource.getContents().get(0);
                if (grammarName.equals(usedGrammar.getName()))
                    return Collections.<EObject>singletonList(usedGrammar);
            }
        }
        return Collections.emptyList();
    } catch (ClasspathUriResolutionException e) {
        log.debug("Cannot load used grammar.", e);
        return Collections.emptyList();
    } catch (ValueConverterException e) {
        log.debug("Cannot load used grammar.", e);
        return Collections.emptyList();
    }
}
Also used : DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) ClasspathUriResolutionException(org.eclipse.xtext.resource.ClasspathUriResolutionException) DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) XtextResource(org.eclipse.xtext.resource.XtextResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) Grammar(org.eclipse.xtext.Grammar) URI(org.eclipse.emf.common.util.URI) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) EObject(org.eclipse.emf.ecore.EObject) IParseResult(org.eclipse.xtext.parser.IParseResult) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException)

Example 23 with ValueConverterException

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

the class AbstractInternalAntlrParser method handleValueConverterException.

protected void handleValueConverterException(ValueConverterException vce) {
    hadErrors = true;
    Exception cause = (Exception) vce.getCause();
    if (vce != cause) {
        IValueConverterErrorContext errorContext = createValueConverterErrorContext(vce);
        currentError = syntaxErrorProvider.getSyntaxErrorMessage(errorContext);
        if (vce.getNode() == null) {
            appendError(currentNode.getLastChild());
        } else {
            appendError(vce.getNode());
        }
    } else {
        throw new RuntimeException(vce);
    }
}
Also used : IValueConverterErrorContext(org.eclipse.xtext.parser.antlr.ISyntaxErrorMessageProvider.IValueConverterErrorContext) MissingTokenException(org.antlr.runtime.MissingTokenException) RecognitionException(org.antlr.runtime.RecognitionException) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) UnwantedTokenException(org.antlr.runtime.UnwantedTokenException) WrappedException(org.eclipse.emf.common.util.WrappedException) InvocationTargetException(java.lang.reflect.InvocationTargetException) FailedPredicateException(org.antlr.runtime.FailedPredicateException) MismatchedTokenException(org.antlr.runtime.MismatchedTokenException)

Example 24 with ValueConverterException

use of org.eclipse.xtext.conversion.ValueConverterException 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 25 with ValueConverterException

use of org.eclipse.xtext.conversion.ValueConverterException 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)

Aggregations

ValueConverterException (org.eclipse.xtext.conversion.ValueConverterException)54 EObject (org.eclipse.emf.ecore.EObject)37 AntlrDatatypeRuleToken (org.eclipse.xtext.parser.antlr.AntlrDatatypeRuleToken)13 ValueConverterWithValueException (org.eclipse.xtext.conversion.ValueConverterWithValueException)4 Test (org.junit.Test)4 URI (org.eclipse.emf.common.util.URI)3 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)3 Resource (org.eclipse.emf.ecore.resource.Resource)3 ClasspathUriResolutionException (org.eclipse.xtext.resource.ClasspathUriResolutionException)3 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)3 XtextResource (org.eclipse.xtext.resource.XtextResource)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 FailedPredicateException (org.antlr.runtime.FailedPredicateException)2 MismatchedTokenException (org.antlr.runtime.MismatchedTokenException)2 MissingTokenException (org.antlr.runtime.MissingTokenException)2 RecognitionException (org.antlr.runtime.RecognitionException)2 UnwantedTokenException (org.antlr.runtime.UnwantedTokenException)2 WrappedException (org.eclipse.emf.common.util.WrappedException)2 EClass (org.eclipse.emf.ecore.EClass)2 EReference (org.eclipse.emf.ecore.EReference)2