Search in sources :

Example 46 with EDataType

use of org.eclipse.emf.ecore.EDataType in project BIMserver by opensourceBIM.

the class HashMapVirtualObject method write.

public ByteBuffer write() throws BimserverDatabaseException {
    int bufferSize = getExactSize(this);
    ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
    byte[] unsetted = new byte[getPackageMetaData().getUnsettedLength(eClass)];
    int fieldCounter = 0;
    for (EStructuralFeature feature : eClass().getEAllStructuralFeatures()) {
        if (getPackageMetaData().useForDatabaseStorage(eClass, feature)) {
            if (useUnsetBit(feature)) {
                unsetted[fieldCounter / 8] |= (1 << (fieldCounter % 8));
            }
            fieldCounter++;
        }
    }
    buffer.put(unsetted);
    EClass eClass = getDatabaseInterface().getEClassForOid(getOid());
    if (!eClass.isSuperTypeOf(eClass())) {
        throw new BimserverDatabaseException("Object with oid " + getOid() + " is a " + eClass().getName() + " but it's cid-part says it's a " + eClass.getName());
    }
    buffer.putLong(getUuid().getMostSignificantBits());
    buffer.putLong(getUuid().getLeastSignificantBits());
    for (EStructuralFeature feature : eClass().getEAllStructuralFeatures()) {
        if (getPackageMetaData().useForDatabaseStorage(eClass, feature)) {
            if (!useUnsetBit(feature)) {
                if (feature.isMany()) {
                    writeList(this, buffer, getPackageMetaData(), feature);
                } else {
                    Object value = eGet(feature);
                    if (feature.getEType() instanceof EEnum) {
                        if (value == null) {
                            buffer.putInt(-1);
                        } else {
                            EEnum eEnum = (EEnum) feature.getEType();
                            EEnumLiteral eEnumLiteral = eEnum.getEEnumLiteralByLiteral(((Enum<?>) value).toString());
                            if (eEnumLiteral != null) {
                                buffer.putInt(eEnumLiteral.getValue());
                            } else {
                                LOGGER.error(((Enum<?>) value).toString() + " not found");
                                buffer.putInt(-1);
                            }
                        }
                    } else if (feature.getEType() instanceof EClass) {
                        if (value == null) {
                            buffer.order(ByteOrder.LITTLE_ENDIAN);
                            buffer.putShort((short) -1);
                            buffer.order(ByteOrder.BIG_ENDIAN);
                        } else if (value instanceof VirtualObject) {
                            writeWrappedValue(getPid(), getRid(), (VirtualObject) value, buffer, getPackageMetaData());
                        } else if (value instanceof WrappedVirtualObject) {
                            writeWrappedValue(getPid(), getRid(), (WrappedVirtualObject) value, buffer, getPackageMetaData());
                        } else {
                            long referencedOid = (Long) value;
                            writeReference(referencedOid, buffer, feature);
                        }
                    } else if (feature.getEType() instanceof EDataType) {
                        writePrimitiveValue(feature, value, buffer);
                    }
                }
            }
        }
    }
    if (buffer.position() != bufferSize) {
        throw new BimserverDatabaseException("Value buffer sizes do not match for " + eClass().getName() + " " + buffer.position() + "/" + bufferSize);
    }
    return buffer;
}
Also used : EEnum(org.eclipse.emf.ecore.EEnum) EDataType(org.eclipse.emf.ecore.EDataType) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) EEnum(org.eclipse.emf.ecore.EEnum) ByteBuffer(java.nio.ByteBuffer) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) EClass(org.eclipse.emf.ecore.EClass) EEnumLiteral(org.eclipse.emf.ecore.EEnumLiteral)

Example 47 with EDataType

use of org.eclipse.emf.ecore.EDataType in project xtext-eclipse by eclipse.

the class XtextGrammarQuickfixProvider method fixExternalImportedPackage.

@Fix(INVALID_PACKAGE_REFERENCE_EXTERNAL)
public void fixExternalImportedPackage(final Issue issue, IssueResolutionAcceptor acceptor) {
    if (issue.getData().length == 1)
        acceptor.accept(issue, "Update the imported package '" + issue.getData()[0] + "'", "Fix the bogus package import\n" + "import '" + issue.getData()[0] + "'", NULL_QUICKFIX_IMAGE, new IModification() {

            @Override
            public void apply(IModificationContext context) throws BadLocationException {
                String replaceString = valueConverterService.toString(issue.getData()[0], "STRING");
                IXtextDocument document = context.getXtextDocument();
                final List<String> importedPackages = document.priorityReadOnly(new IUnitOfWork<List<String>, XtextResource>() {

                    @Override
                    public List<String> exec(XtextResource state) throws Exception {
                        IResourceDescriptions descriptions = resourceDescriptionsProvider.getResourceDescriptions(state);
                        ResourceSet resourceSet = state.getResourceSet();
                        final Map<URI, URI> uriMap = Maps.newHashMap();
                        EPackage ePackage = loadPackageFromIndex(descriptions, resourceSet, uriMap, issue.getData()[0]);
                        if (ePackage != null) {
                            final Map<String, EPackage> packagePerNsURI = Maps.newHashMap();
                            packagePerNsURI.put(ePackage.getNsURI(), ePackage);
                            final Set<URI> updatedReferences = fixReferencesInPackages(ePackage, packagePerNsURI, uriMap, descriptions, resourceSet);
                            if (updatedReferences.isEmpty())
                                return null;
                            Iterator<EPackage> iterator = packagePerNsURI.values().iterator();
                            while (iterator.hasNext()) {
                                EPackage pack = iterator.next();
                                Resource resource = pack.eResource();
                                if (!resource.getURI().isPlatformResource()) {
                                    iterator.remove();
                                }
                            }
                            final List<String> result = Lists.newArrayList();
                            new WorkspaceModifyOperation() {

                                /* workspace lock */
                                @Override
                                protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
                                    try {
                                        for (EPackage pack : packagePerNsURI.values()) {
                                            Resource resource = pack.eResource();
                                            resource.save(Collections.singletonMap(XMLResource.OPTION_URI_HANDLER, new URIHandlerImpl.PlatformSchemeAware() {

                                                @Override
                                                public URI deresolve(URI uri) {
                                                    // replace archive uris with platform:/plugin
                                                    if (!uri.isArchive() || !updatedReferences.contains(uri)) {
                                                        return super.deresolve(uri);
                                                    }
                                                    URI withoutFragment = uri.trimFragment();
                                                    if (uriMap.containsKey(withoutFragment)) {
                                                        withoutFragment = uriMap.get(withoutFragment);
                                                    }
                                                    return super.deresolve(withoutFragment.appendFragment(uri.fragment()));
                                                }
                                            }));
                                            result.add(resource.getURI().toString());
                                        }
                                    } catch (IOException ioe) {
                                        throw new InvocationTargetException(ioe);
                                    }
                                }
                            }.run(new NullProgressMonitor());
                            for (int i = resourceSet.getResources().size() - 1; i >= 0; i--) {
                                Resource resource = resourceSet.getResources().get(i);
                                if (!resource.getContents().isEmpty() && resource.getContents().get(0) instanceof GenModel) {
                                    resourceSet.getResources().remove(i);
                                }
                            }
                            return result;
                        }
                        return null;
                    }

                    private Set<URI> fixReferencesInPackages(EPackage ePackage, Map<String, EPackage> packagePerNsURI, Map<URI, URI> uriMap, IResourceDescriptions descriptions, ResourceSet resourceSet) {
                        Set<URI> result = Sets.newHashSet();
                        Map<EObject, Collection<Setting>> allReferences = EcoreUtil.CrossReferencer.find(Collections.singletonList(ePackage));
                        for (final Setting setting : Iterables.concat(allReferences.values())) {
                            if (setting.getEStructuralFeature().isChangeable()) {
                                final Object referenced = setting.get(true);
                                List<Object> references = null;
                                if (referenced instanceof EObject) {
                                    references = new AbstractList<Object>() {

                                        @Override
                                        public Object set(int index, Object element) {
                                            setting.set(element);
                                            return referenced;
                                        }

                                        @Override
                                        public Object get(int index) {
                                            return referenced;
                                        }

                                        @Override
                                        public int size() {
                                            return 1;
                                        }
                                    };
                                } else {
                                    @SuppressWarnings("unchecked") List<Object> casted = (List<Object>) referenced;
                                    references = casted;
                                }
                                for (int i = 0; i < references.size(); i++) {
                                    if (references.get(i) instanceof EObject) {
                                        EObject referencedEObject = (EObject) references.get(i);
                                        EPackage transitive = EcoreUtil2.getContainerOfType(referencedEObject, EPackage.class);
                                        if (isRegisteredPackage(transitive)) {
                                            if (referencedEObject instanceof EDataType)
                                                continue;
                                            if (referencedEObject == EcorePackage.Literals.EOBJECT)
                                                continue;
                                            EPackage fromWorkspace = packagePerNsURI.get(transitive.getNsURI());
                                            if (fromWorkspace == null && !packagePerNsURI.containsKey(transitive.getNsURI())) {
                                                fromWorkspace = loadPackageFromIndex(descriptions, resourceSet, uriMap, transitive.getNsURI());
                                                packagePerNsURI.put(transitive.getNsURI(), fromWorkspace);
                                            }
                                            if (fromWorkspace != null) {
                                                String fragment = transitive.eResource().getURIFragment(referencedEObject);
                                                EObject replacement = fromWorkspace.eResource().getEObject(fragment);
                                                if (replacement != null) {
                                                    result.add(EcoreUtil.getURI(replacement));
                                                    references.set(i, replacement);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        return result;
                    }

                    private boolean isRegisteredPackage(EPackage ePackage) {
                        return ePackage != null && (ePackage.eResource() == null || ePackage.getNsURI().equals(ePackage.eResource().getURI().toString()));
                    }

                    private EPackage loadPackageFromIndex(IResourceDescriptions descriptions, ResourceSet resourceSet, Map<URI, URI> uriMap, String nsURI) {
                        Iterable<IEObjectDescription> fixUs = descriptions.getExportedObjects(EcorePackage.Literals.EPACKAGE, QualifiedName.create(nsURI), false);
                        for (IEObjectDescription description : fixUs) {
                            if (description.getEObjectURI().isPlatformResource()) {
                                EObject result = resourceSet.getEObject(description.getEObjectURI(), true);
                                if (result instanceof EPackage) {
                                    return (EPackage) result;
                                }
                            }
                        }
                        URI genModelURI = EcorePlugin.getEPackageNsURIToGenModelLocationMap(false).get(nsURI);
                        if (genModelURI != null) {
                            Resource genmodelResource = resourceSet.getResource(genModelURI, true);
                            GenModel genModel = (GenModel) genmodelResource.getContents().get(0);
                            for (GenPackage genPackage : genModel.getGenPackages()) {
                                Object object = genPackage.eGet(GenModelPackage.Literals.GEN_PACKAGE__ECORE_PACKAGE, false);
                                if (object instanceof EObject) {
                                    EObject proxy = (EObject) object;
                                    URI proxyURI = EcoreUtil.getURI(proxy);
                                    URI resolvedProxyURI = proxyURI.resolve(genModelURI);
                                    if (nsURI.equals(genPackage.getEcorePackage().getNsURI())) {
                                        EPackage result = genPackage.getEcorePackage();
                                        uriMap.put(result.eResource().getURI(), resolvedProxyURI.trimFragment());
                                        return result;
                                    }
                                }
                            }
                        }
                        return null;
                    }
                });
                String delimiter = document.get(issue.getOffset(), 1);
                if (!replaceString.startsWith(delimiter)) {
                    replaceString = delimiter + replaceString.substring(1, replaceString.length() - 1) + delimiter;
                }
                document.replace(issue.getOffset(), issue.getLength(), replaceString);
                if (importedPackages != null && !importedPackages.isEmpty()) {
                    final Shell shell = workbench.getActiveWorkbenchWindow().getShell();
                    shell.getDisplay().asyncExec(new Runnable() {

                        @Override
                        public void run() {
                            String title = "Please update the Ecore2XtextDslProjectContributor that generates the language.";
                            String message = "Please make sure that the Ecore2XtextDslProjectContributor that generates the language is up-to date.\n" + "Especially important is the registration of the referenced packages.\n" + "Please refer to the reference documentation for details.";
                            MessageDialog dialog = new MessageDialog(shell, title, null, message, MessageDialog.INFORMATION, new String[] { "Open Documentation", "Close" }, 1);
                            if (dialog.open() == 0) {
                                try {
                                    workbench.getBrowserSupport().getExternalBrowser().openURL(new URL(GRAMMAR_LANG_DOC));
                                } catch (Exception e) {
                                // ignore
                                }
                            }
                        }
                    });
                }
            }
        });
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Set(java.util.Set) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) WorkspaceModifyOperation(org.eclipse.ui.actions.WorkspaceModifyOperation) EDataType(org.eclipse.emf.ecore.EDataType) XtextResource(org.eclipse.xtext.resource.XtextResource) GenPackage(org.eclipse.emf.codegen.ecore.genmodel.GenPackage) URI(org.eclipse.emf.common.util.URI) URL(java.net.URL) EPackage(org.eclipse.emf.ecore.EPackage) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) Shell(org.eclipse.swt.widgets.Shell) EObject(org.eclipse.emf.ecore.EObject) Iterator(java.util.Iterator) AbstractList(java.util.AbstractList) List(java.util.List) MessageDialog(org.eclipse.jface.dialogs.MessageDialog) IModification(org.eclipse.xtext.ui.editor.model.edit.IModification) AbstractList(java.util.AbstractList) Setting(org.eclipse.emf.ecore.EStructuralFeature.Setting) Resource(org.eclipse.emf.ecore.resource.Resource) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource) XtextResource(org.eclipse.xtext.resource.XtextResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IOException(java.io.IOException) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) BadLocationException(org.eclipse.jface.text.BadLocationException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) GenModel(org.eclipse.emf.codegen.ecore.genmodel.GenModel) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) EObject(org.eclipse.emf.ecore.EObject) Map(java.util.Map) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 48 with EDataType

use of org.eclipse.emf.ecore.EDataType in project InformationSystem by ObeoNetwork.

the class EcorebindingModelWizardSpec method explore.

/**
 * Expore the EPackage and init the BPackage.
 * @param ePackage The ecore Package input to read
 * @param bPackage The Binding Package to build
 */
private static void explore(EPackage ePackage, BPackage bPackage) {
    EList<EClassifier> eClassifiers = ePackage.getEClassifiers();
    for (EClassifier eClassifier : eClassifiers) {
        if (eClassifier instanceof EClass) {
            EClass eClass = (EClass) eClassifier;
            // Create BClass
            BClass bClass = EcorebindingFactory.eINSTANCE.createBClass();
            bClass.setEcoreClass(eClass);
            bPackage.getBClasses().add(bClass);
            bPackage.getBClassifiers().add(bClass);
            explore(eClass, bClass);
        } else if (eClassifier instanceof EEnum) {
            EEnum eEnum = (EEnum) eClassifier;
            // Create BEnum
            BEnum bEnum = EcorebindingFactory.eINSTANCE.createBEnum();
            bEnum.setEcoreEnum(eEnum);
            bEnum.setEcoreDataType(eEnum);
            bPackage.getBEnums().add(bEnum);
            bPackage.getBClassifiers().add(bEnum);
            List<EEnumLiteral> eLiterals = eEnum.getELiterals();
            for (EEnumLiteral eLiteral : eLiterals) {
                // Create BEnumLiteral
                BEnumLiteral bEnumLiteral = EcorebindingFactory.eINSTANCE.createBEnumLiteral();
                bEnumLiteral.setBEnum(bEnum);
                bEnumLiteral.setEcoreEnumLiteral(eLiteral);
            }
        } else if (eClassifier instanceof EDataType) {
            EDataType eDataType = (EDataType) eClassifier;
            // Create BDataType
            BDataType bDataType = EcorebindingFactory.eINSTANCE.createBDataType();
            bDataType.setEcoreDataType(eDataType);
            bPackage.getBDataTypes().add(bDataType);
            bPackage.getBClassifiers().add(bDataType);
        }
    }
    List<EPackage> eSubpackages = ePackage.getESubpackages();
    for (EPackage subEPackage : eSubpackages) {
        // Create BPackage
        BPackage subBPackage = EcorebindingFactory.eINSTANCE.createBPackage();
        subBPackage.setEcorePackage(subEPackage);
        bPackage.getNestedBPackages().add(subBPackage);
        explore(subEPackage, subBPackage);
    }
}
Also used : BEnum(org.obeonetwork.dsl.ecorebinding.BEnum) BDataType(org.obeonetwork.dsl.ecorebinding.BDataType) EDataType(org.eclipse.emf.ecore.EDataType) EClassifier(org.eclipse.emf.ecore.EClassifier) EEnum(org.eclipse.emf.ecore.EEnum) EPackage(org.eclipse.emf.ecore.EPackage) BPackage(org.obeonetwork.dsl.ecorebinding.BPackage) EClass(org.eclipse.emf.ecore.EClass) BEnumLiteral(org.obeonetwork.dsl.ecorebinding.BEnumLiteral) BClass(org.obeonetwork.dsl.ecorebinding.BClass) EList(org.eclipse.emf.common.util.EList) List(java.util.List) EEnumLiteral(org.eclipse.emf.ecore.EEnumLiteral)

Aggregations

EDataType (org.eclipse.emf.ecore.EDataType)48 EClass (org.eclipse.emf.ecore.EClass)21 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)15 EEnum (org.eclipse.emf.ecore.EEnum)14 EClassifier (org.eclipse.emf.ecore.EClassifier)13 EObject (org.eclipse.emf.ecore.EObject)13 HashMapVirtualObject (org.bimserver.shared.HashMapVirtualObject)10 Test (org.junit.Test)10 List (java.util.List)7 ArrayList (java.util.ArrayList)6 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)6 IdEObject (org.bimserver.emf.IdEObject)6 VirtualObject (org.bimserver.shared.VirtualObject)6 EEnumLiteral (org.eclipse.emf.ecore.EEnumLiteral)6 EPackage (org.eclipse.emf.ecore.EPackage)6 HashMapWrappedVirtualObject (org.bimserver.shared.HashMapWrappedVirtualObject)4 EAttribute (org.eclipse.emf.ecore.EAttribute)4 EReference (org.eclipse.emf.ecore.EReference)4 AbstractRule (org.eclipse.xtext.AbstractRule)4 ByteBuffer (java.nio.ByteBuffer)3