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;
}
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
}
}
}
});
}
}
});
}
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);
}
}
Aggregations