Search in sources :

Example 1 with ValueConverterException

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

the class NewFeatureNameUtil method checkNewFeatureName.

public void checkNewFeatureName(String newFeatureName, boolean isLookupInScope, RefactoringStatus status) {
    if (isEmpty(newFeatureName)) {
        status.addFatalError("Choose a name");
        return;
    }
    try {
        Object value = valueConverterService.toValue(newFeatureName, "ValidID", null);
        valueConverterService.toString(value, "ValidID");
    } catch (ValueConverterException exc) {
        status.addFatalError(exc.getMessage());
    }
    if (Character.isUpperCase(newFeatureName.charAt(0)))
        status.addError("Discouraged name '" + newFeatureName + "'. Name should start with a lowercase letter. ");
    if (isKeyword(newFeatureName))
        status.addFatalError("'" + newFeatureName + "' is keyword.");
    @SuppressWarnings("restriction") Class<?> asPrimitive = org.eclipse.xtext.common.types.access.impl.Primitives.forName(newFeatureName);
    if (asPrimitive != null)
        status.addFatalError("'" + newFeatureName + "' is reserved.");
    if (isLookupInScope && featureCallScope != null && isAlreadyDefined(newFeatureName))
        status.addError("The name '" + newFeatureName + "' is already defined in this scope.");
}
Also used : EObject(org.eclipse.emf.ecore.EObject) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException)

Example 2 with ValueConverterException

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

the class DefaultRenameStrategy method validateNewName.

@Override
public RefactoringStatus validateNewName(String newName) {
    RefactoringStatus status = super.validateNewName(newName);
    if (nameRuleName != null) {
        try {
            String value = getNameAsValue(newName);
            String text = getNameAsText(value);
            if (!equal(text, newName)) {
                status.addError("Illegal name: '" + newName + "'. Consider using '" + text + "' instead.");
            }
        } catch (ValueConverterException vce) {
            status.addFatalError("Illegal name: " + notNull(vce.getMessage()));
        }
    }
    return status;
}
Also used : RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException)

Example 3 with ValueConverterException

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

the class RefactoringCrossReferenceSerializer method getCrossRefText.

public String getCrossRefText(EObject owner, CrossReference crossref, EObject target, RefTextEvaluator refTextEvaluator, ITextRegion linkTextRegion, StatusWrapper status) {
    try {
        final EReference ref = GrammarUtil.getReference(crossref, owner.eClass());
        final IScope scope = scopeProvider.getScope(owner, ref);
        if (scope == null) {
            throw new IllegalStateException("Could not create scope for the given cross reference.");
        }
        String ruleName = linkingHelper.getRuleNameFrom(crossref);
        Iterable<IEObjectDescription> descriptionsForCrossRef = scope.getElements(target);
        String bestRefText = null;
        List<String> badNames = new ArrayList<String>();
        for (IEObjectDescription desc : descriptionsForCrossRef) {
            try {
                String unconvertedRefText = qualifiedNameConverter.toString(desc.getName());
                String convertedRefText = valueConverter.toString(unconvertedRefText, ruleName);
                if (refTextEvaluator.isValid(desc) && (bestRefText == null || refTextEvaluator.isBetterThan(convertedRefText, bestRefText)))
                    bestRefText = convertedRefText;
            } catch (ValueConverterException e) {
                // this is a problem only if we don't find any matching value
                badNames.add(desc.getName().toString());
            }
        }
        if (bestRefText == null && !badNames.isEmpty()) {
            status.add(WARNING, "Misconfigured language: New reference text has invalid syntax. Following names are in the scope: " + IterableExtensions.join(badNames, ", "), owner, linkTextRegion);
        }
        return bestRefText;
    } catch (Exception exc) {
        log.error(exc.getMessage(), exc);
        status.add(ERROR, exc.getMessage(), owner, linkTextRegion);
        return null;
    }
}
Also used : ArrayList(java.util.ArrayList) IScope(org.eclipse.xtext.scoping.IScope) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) EReference(org.eclipse.emf.ecore.EReference) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 4 with ValueConverterException

use of org.eclipse.xtext.conversion.ValueConverterException in project dsl-devkit by dsldevkit.

the class ScopeLinkingService method getIncludedModel.

/**
 * Returns the {@link ScopeModel#getIncludes() included scope model} corresponding to the given node from the node model.
 *
 * @param context
 *          context object
 * @param node
 *          parser node corresponding to include reference
 * @return referenced {@link ScopeModel} to include
 */
private List<EObject> getIncludedModel(final ScopeModel context, final INode node) {
    // based on XtextLinkingService#getUsedGrammar()
    try {
        String scopeModelName = (String) valueConverterService.toValue("", grammarAccess.getDottedIDRule().getName(), node);
        if (scopeModelName != null) {
            final ResourceSet resourceSet = context.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 && ((XtextResource) resource).getLanguageName().equals(languageName) && !resource.getContents().isEmpty()) {
                    rootElement = resource.getContents().get(0);
                }
                if (rootElement instanceof ScopeModel) {
                    ScopeModel otherModel = (ScopeModel) rootElement;
                    if (scopeModelName.equals(otherModel.getName())) {
                        return Collections.<EObject>singletonList(otherModel);
                    }
                }
            }
            URI classpathURI = URI.createURI(ClasspathUriUtil.CLASSPATH_SCHEME + ":/" + scopeModelName.replace('.', '/') + "." + fileExtensionProvider.getPrimaryFileExtension());
            final Resource resource = resourceSet.getResource(classpathURI, true);
            if (!resource.getContents().isEmpty()) {
                final ScopeModel otherModel = (ScopeModel) resource.getContents().get(0);
                if (scopeModelName.equals(otherModel.getName())) {
                    return Collections.<EObject>singletonList(otherModel);
                }
            }
        }
        return Collections.emptyList();
    } catch (ClasspathUriResolutionException e) {
        LOG.warn("Cannot load included scope.", e);
        return Collections.emptyList();
    } catch (ValueConverterException e) {
        LOG.warn("Cannot convert included scope name.", e);
        return Collections.emptyList();
    }
}
Also used : ClasspathUriResolutionException(org.eclipse.xtext.resource.ClasspathUriResolutionException) EObject(org.eclipse.emf.ecore.EObject) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) ScopeModel(com.avaloq.tools.ddk.xtext.scope.scope.ScopeModel) XtextResource(org.eclipse.xtext.resource.XtextResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) URI(org.eclipse.emf.common.util.URI)

Example 5 with ValueConverterException

use of org.eclipse.xtext.conversion.ValueConverterException in project dsl-devkit by dsldevkit.

the class AbstractFastLinkingService method getUsedGrammar.

/**
 * Tries to find a grammar.
 *
 * @param resourceSet
 *          to use for loading
 * @param grammarName
 *          qualified grammar name
 * @return A singleton list containing the grammar, or an empty list if not found.
 */
protected List<EObject> getUsedGrammar(final ResourceSet resourceSet, final String grammarName) {
    // copied from XtextLinkingService#getUsedGrammar()
    try {
        if (grammarName != null) {
            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);
                    }
                }
            }
            // $NON-NLS-1$ //$NON-NLS-2$
            URI classpathURI = URI.createURI(ClasspathUriUtil.CLASSPATH_SCHEME + ":/" + grammarName.replace('.', '/') + ".xtext");
            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) {
        return Collections.emptyList();
    } catch (ValueConverterException e) {
        return Collections.emptyList();
    }
}
Also used : DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) ClasspathUriResolutionException(org.eclipse.xtext.resource.ClasspathUriResolutionException) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) EObject(org.eclipse.emf.ecore.EObject) XtextResource(org.eclipse.xtext.resource.XtextResource) DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) IParseResult(org.eclipse.xtext.parser.IParseResult) Grammar(org.eclipse.xtext.Grammar) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) URI(org.eclipse.emf.common.util.URI)

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