Search in sources :

Example 11 with AbstractMetamodelDeclaration

use of org.eclipse.xtext.AbstractMetamodelDeclaration in project xtext-core by eclipse.

the class GrammarAccessFragment2 method addAllGrammarsToResource.

protected void addAllGrammarsToResource(final Resource resource, final Grammar grammar, final Set<Grammar> visitedGrammars) {
    boolean _add = visitedGrammars.add(grammar);
    boolean _not = (!_add);
    if (_not) {
        return;
    }
    resource.getContents().add(grammar);
    EList<AbstractMetamodelDeclaration> _metamodelDeclarations = grammar.getMetamodelDeclarations();
    for (final AbstractMetamodelDeclaration metamodelDecl : _metamodelDeclarations) {
        {
            final EPackage pack = metamodelDecl.getEPackage();
            final Resource packResource = pack.eResource();
            String _string = packResource.getURI().toString();
            String _nsURI = pack.getNsURI();
            boolean _notEquals = (!Objects.equal(_string, _nsURI));
            if (_notEquals) {
                final ResourceSet packResourceSet = packResource.getResourceSet();
                if ((packResourceSet != null)) {
                    EPackage topMost = pack;
                    while (((topMost.getESuperPackage() != null) && (topMost.getESuperPackage().eResource() == topMost.eResource()))) {
                        topMost = topMost.getESuperPackage();
                    }
                    if ((packResource.getContents().contains(topMost) && (packResource.getContents().size() == 1))) {
                        boolean _isEmpty = topMost.getEClassifiers().isEmpty();
                        boolean _not_1 = (!_isEmpty);
                        if (_not_1) {
                            packResource.setURI(URI.createURI(topMost.getNsURI()));
                        } else {
                            this.moveSubpackagesToNewResource(topMost, resource.getResourceSet());
                        }
                    }
                    boolean _equals = topMost.eResource().getURI().toString().equals(topMost.getNsURI());
                    boolean _not_2 = (!_equals);
                    if (_not_2) {
                        this.movePackageToNewResource(topMost, resource.getResourceSet());
                    }
                }
            }
        }
    }
    EList<Grammar> _usedGrammars = grammar.getUsedGrammars();
    for (final Grammar usedGrammar : _usedGrammars) {
        this.addAllGrammarsToResource(resource, usedGrammar, visitedGrammars);
    }
}
Also used : Resource(org.eclipse.emf.ecore.resource.Resource) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource) FragmentFakingEcoreResource(org.eclipse.xtext.xtext.generator.grammarAccess.FragmentFakingEcoreResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) Grammar(org.eclipse.xtext.Grammar) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) EPackage(org.eclipse.emf.ecore.EPackage)

Example 12 with AbstractMetamodelDeclaration

use of org.eclipse.xtext.AbstractMetamodelDeclaration in project xtext-core by eclipse.

the class TypeRefImpl method setMetamodel.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public void setMetamodel(AbstractMetamodelDeclaration newMetamodel) {
    AbstractMetamodelDeclaration oldMetamodel = metamodel;
    metamodel = newMetamodel;
    if (eNotificationRequired())
        eNotify(new ENotificationImpl(this, Notification.SET, XtextPackage.TYPE_REF__METAMODEL, oldMetamodel, metamodel));
}
Also used : ENotificationImpl(org.eclipse.emf.ecore.impl.ENotificationImpl) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration)

Example 13 with AbstractMetamodelDeclaration

use of org.eclipse.xtext.AbstractMetamodelDeclaration in project xtext-core by eclipse.

the class Xtext2EcoreTransformer method collectEPackages.

private void collectEPackages() {
    final List<AbstractMetamodelDeclaration> metamodelDeclarations = grammar.getMetamodelDeclarations();
    final Map<String, GeneratedMetamodel> generateUs = Maps.newLinkedHashMap();
    for (AbstractMetamodelDeclaration metamodelDeclaration : metamodelDeclarations) {
        try {
            if (metamodelDeclaration instanceof ReferencedMetamodel) {
                // load imported metamodel
                ReferencedMetamodel referencedMetamodel = (ReferencedMetamodel) metamodelDeclaration;
                EPackage referencedEPackage = referencedMetamodel.getEPackage();
                if (referencedEPackage != null)
                    collectClassInfosOf(referencedEPackage, referencedMetamodel);
            } else if (metamodelDeclaration instanceof GeneratedMetamodel) {
                String alias = Strings.emptyIfNull(metamodelDeclaration.getAlias());
                if (generateUs.containsKey(alias)) {
                    GeneratedMetamodel prev = generateUs.get(alias);
                    if (prev != null) {
                        if (prev.getEPackage() != null) {
                            prev.getEPackage().eResource().getResourceSet().getResources().remove(prev.getEPackage().eResource());
                        }
                        prev.setEPackage(null);
                    }
                    generateUs.put(alias, null);
                    EPackage pack = metamodelDeclaration.getEPackage();
                    if (pack != null)
                        pack.eResource().getResourceSet().getResources().remove(pack.eResource());
                    metamodelDeclaration.setEPackage(null);
                    throw new TransformationException(TransformationErrorCode.AliasForMetamodelAlreadyExists, "Alias '" + alias + "' registered more than once.", metamodelDeclaration);
                }
                generateUs.put(alias, (GeneratedMetamodel) metamodelDeclaration);
            } else
                throw new IllegalStateException("unknown metamodelDeclaraton " + metamodelDeclaration);
        } catch (TransformationException e) {
            reportError(e);
        }
    }
    for (GeneratedMetamodel metamodel : generateUs.values()) {
        try {
            if (metamodel != null)
                addGeneratedEPackage(metamodel);
        } catch (TransformationException e) {
            reportError(e);
        }
    }
}
Also used : GeneratedMetamodel(org.eclipse.xtext.GeneratedMetamodel) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) ReferencedMetamodel(org.eclipse.xtext.ReferencedMetamodel) EPackage(org.eclipse.emf.ecore.EPackage)

Example 14 with AbstractMetamodelDeclaration

use of org.eclipse.xtext.AbstractMetamodelDeclaration in project xtext-core by eclipse.

the class Xtext2EcoreTransformer method getGeneratedPackages.

public List<EPackage> getGeneratedPackages() {
    final List<EPackage> result = new ArrayList<EPackage>();
    final ResourceSet resourceSet = grammar.eResource().getResourceSet();
    if (resourceSet == null)
        throw new NullPointerException("resourceSet may not be null");
    Iterables.addAll(result, Iterables.filter(Iterables.transform(Iterables.filter(grammar.getMetamodelDeclarations(), GeneratedMetamodel.class), new Function<AbstractMetamodelDeclaration, EPackage>() {

        @Override
        public EPackage apply(AbstractMetamodelDeclaration param) {
            EPackage pack = (EPackage) param.eGet(XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE, false);
            if (pack != null && !pack.eIsProxy()) {
                return pack;
            }
            return null;
        }
    }), Predicates.notNull()));
    return getPackagesSortedByName(result);
}
Also used : GeneratedMetamodel(org.eclipse.xtext.GeneratedMetamodel) ArrayList(java.util.ArrayList) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) EPackage(org.eclipse.emf.ecore.EPackage)

Example 15 with AbstractMetamodelDeclaration

use of org.eclipse.xtext.AbstractMetamodelDeclaration in project xtext-core by eclipse.

the class Xtext2EcoreTransformer method findMetamodel.

public AbstractMetamodelDeclaration findMetamodel(Grammar grammar, String alias, String containedClassifier) {
    final List<AbstractMetamodelDeclaration> declarations = grammar.getMetamodelDeclarations();
    AbstractMetamodelDeclaration result = null;
    for (AbstractMetamodelDeclaration decl : declarations) {
        if (isSameAlias(decl.getAlias(), alias)) {
            EPackage pack = decl.getEPackage();
            if (pack != null && pack.getEClassifier(containedClassifier) != null) {
                if (result != null)
                    return null;
                result = decl;
            }
        }
    }
    return result;
}
Also used : AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) EPackage(org.eclipse.emf.ecore.EPackage)

Aggregations

AbstractMetamodelDeclaration (org.eclipse.xtext.AbstractMetamodelDeclaration)33 Grammar (org.eclipse.xtext.Grammar)21 Test (org.junit.Test)16 EObject (org.eclipse.emf.ecore.EObject)11 EPackage (org.eclipse.emf.ecore.EPackage)9 AbstractRule (org.eclipse.xtext.AbstractRule)9 GeneratedMetamodel (org.eclipse.xtext.GeneratedMetamodel)8 XtextResource (org.eclipse.xtext.resource.XtextResource)8 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)7 TypeRef (org.eclipse.xtext.TypeRef)7 ReferencedMetamodel (org.eclipse.xtext.ReferencedMetamodel)6 EClass (org.eclipse.emf.ecore.EClass)4 EClassifier (org.eclipse.emf.ecore.EClassifier)4 Resource (org.eclipse.emf.ecore.resource.Resource)4 ParserRule (org.eclipse.xtext.ParserRule)4 RuleCall (org.eclipse.xtext.RuleCall)4 CompoundElement (org.eclipse.xtext.CompoundElement)3 EnumRule (org.eclipse.xtext.EnumRule)3 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)2 InternalEObject (org.eclipse.emf.ecore.InternalEObject)2