Merge pull request #1437 from eclipse/ade-refactor-xtend-to-java

[eclipse/xtext#1679] Refactor more Xtend to java.
This commit is contained in:
Christian Dietrich 2020-04-26 17:27:37 +02:00 committed by GitHub
commit 5d8969212e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 933 additions and 1798 deletions

View file

@ -22,37 +22,34 @@ import org.eclipse.xtext.generator.IFileSystemAccessExtension3;
* @since 2.8
*/
public interface IResourceStorageFacade {
/**
* @return whether the given resource should and can be loaded from stored resource state
*/
boolean shouldLoadFromStorage(StorageAwareResource resource);
/**
* @return whether storage data exists for the given URI
*/
boolean hasStorageFor(URI uri);
/**
* Finds or creates a ResourceStorageLoadable for the given resource.
* Clients should first call shouldLoadFromStorage to check whether there exists a storage version
* of the given resource.
*
* @return an IResourceStorageLoadable
*/
ResourceStorageLoadable getOrCreateResourceStorageLoadable(StorageAwareResource resource);
/**
* Saves the resource using the given file system access.
*/
void saveResource(StorageAwareResource resource, IFileSystemAccessExtension3 fsa);
/**
* Creates a fresh ResourceStorageWritable wrapping the given OutputStream
*/
ResourceStorageWritable createResourceStorageWritable(OutputStream outputStream);
/**
* Creates a fresh ResourceStorageLoadable wrapping the given InputStream
*/
ResourceStorageLoadable createResourceStorageLoadable(InputStream inputStream);
/**
* @return whether the given {@link StorageAwareResource resource} should and can be loaded from stored resource state
*/
boolean shouldLoadFromStorage(StorageAwareResource resource);
/**
* @return whether storage data exists for the given {@link URI}
*/
boolean hasStorageFor(URI uri);
/**
* Finds or creates a {@link ResourceStorageLoadable} for the given resource. Clients should first call
* {@link #shouldLoadFromStorage(StorageAwareResource)} to check whether there exists a storage version of the given resource.
*/
ResourceStorageLoadable getOrCreateResourceStorageLoadable(StorageAwareResource resource);
/**
* Saves the resource using the given file system access.
*/
void saveResource(StorageAwareResource resource, IFileSystemAccessExtension3 fsa);
/**
* Creates a fresh {@link ResourceStorageWritable} wrapping the given {@link OutputStream}
*/
ResourceStorageWritable createResourceStorageWritable(OutputStream outputStream);
/**
* Creates a fresh {@link ResourceStorageLoadable} wrapping the given {@link InputStream}
*/
ResourceStorageLoadable createResourceStorageLoadable(InputStream inputStream);
}

View file

@ -0,0 +1,166 @@
/**
* Copyright (c) 2014, 2020 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.resource.persistence;
import static org.eclipse.xtext.xbase.lib.IterableExtensions.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import org.apache.log4j.Logger;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ExtensibleURIConverterImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.generator.AbstractFileSystemAccess2;
import org.eclipse.xtext.generator.IContextualOutputConfigurationProvider;
import org.eclipse.xtext.generator.IFileSystemAccessExtension3;
import org.eclipse.xtext.util.RuntimeIOException;
import com.google.inject.Inject;
import com.google.inject.Provider;
/**
* @author Sven Efftinge - Initial contribution and API
*/
public class ResourceStorageFacade implements IResourceStorageFacade {
private static final Logger LOG = Logger.getLogger(ResourceStorageFacade.class);
@Inject
private IContextualOutputConfigurationProvider outputConfigurationProvider;
@Inject
private Provider<AbstractFileSystemAccess2> fileSystemAccessProvider;
private boolean storeNodeModel = false;
@Override
public boolean shouldLoadFromStorage(StorageAwareResource resource) {
SourceLevelURIsAdapter adapter = SourceLevelURIsAdapter.findInstalledAdapter(resource.getResourceSet());
if (adapter == null || adapter.getSourceLevelURIs().contains(resource.getURI()))
return false;
return doesStorageExist(resource);
}
@Override
public ResourceStorageLoadable getOrCreateResourceStorageLoadable(StorageAwareResource resource) {
try {
ResourceSet resourceSet = resource.getResourceSet();
ResourceStorageProviderAdapter stateProvider = getResourceStorageProviderAdapter(resourceSet);
if (stateProvider != null) {
ResourceStorageLoadable loadable = stateProvider.getResourceStorageLoadable(resource);
if (loadable != null)
return loadable;
}
if (resourceSet.getURIConverter().exists(getBinaryStorageURI(resource.getURI()),
Collections.emptyMap())) {
return createResourceStorageLoadable(resourceSet.getURIConverter()
.createInputStream(getBinaryStorageURI(resource.getURI())));
}
return createResourceStorageLoadable(
getFileSystemAccess(resource).readBinaryFile(computeOutputPath(resource)));
} catch (IOException e) {
throw new RuntimeIOException(e);
}
}
protected boolean doesStorageExist(StorageAwareResource resource) {
ResourceSet resourceSet = resource.getResourceSet();
ResourceStorageProviderAdapter stateProvider = getResourceStorageProviderAdapter(resourceSet);
if (stateProvider != null && stateProvider.getResourceStorageLoadable(resource) != null)
return true;
// check for next to original location, i.e. jars
if (resourceSet.getURIConverter().exists(getBinaryStorageURI(resource.getURI()),
Collections.emptyMap()))
return true;
// if it's an archive URI, we don't need to look up the source folder-output folder scheme
if (resource.getURI().isArchive())
return false;
URI uri = getFileSystemAccess(resource).getURI(computeOutputPath(resource));
return uri != null && resourceSet.getURIConverter().exists(uri, null);
}
@Override
public void saveResource(StorageAwareResource resource, IFileSystemAccessExtension3 fsa) {
MyByteArrayOutputStream bout = new MyByteArrayOutputStream();
try {
createResourceStorageWritable(bout).writeResource(resource);
} catch (IOException e) {
// something went wrong when writing the resource - stream's content is bogus and not written to disk
LOG.warn("Cannot write storage for " + resource.getURI(), e);
return;
}
fsa.generateFile(computeOutputPath(resource), new ByteArrayInputStream(bout.toByteArray(), 0, bout.length()));
}
@Override
public ResourceStorageLoadable createResourceStorageLoadable(InputStream in) {
return new ResourceStorageLoadable(in, isStoreNodeModel());
}
@Override
public ResourceStorageWritable createResourceStorageWritable(OutputStream out) {
return new ResourceStorageWritable(out, isStoreNodeModel());
}
protected ResourceStorageProviderAdapter getResourceStorageProviderAdapter(ResourceSet resourceSet) {
return (ResourceStorageProviderAdapter) EcoreUtil.getExistingAdapter(
resourceSet, ResourceStorageProviderAdapter.class);
}
protected AbstractFileSystemAccess2 getFileSystemAccess(StorageAwareResource resource) {
AbstractFileSystemAccess2 fsa = fileSystemAccessProvider.get();
fsa.setContext(resource);
fsa.setOutputConfigurations(
toMap(outputConfigurationProvider.getOutputConfigurations(resource), it -> it.getName()));
return fsa;
}
protected String computeOutputPath(StorageAwareResource resource) {
return getBinaryStorageURI(resource.getURI()).deresolve(getSourceContainerURI(resource), false, false, true)
.path();
}
protected URI getSourceContainerURI(StorageAwareResource resource) {
return resource.getURI().trimSegments(1).appendSegment("");
}
@Override
public boolean hasStorageFor(URI uri) {
return new ExtensibleURIConverterImpl().exists(getBinaryStorageURI(uri), Collections.emptyMap());
}
protected URI getBinaryStorageURI(URI sourceURI) {
return sourceURI.trimSegments(1).appendSegment("." + sourceURI.lastSegment() + "bin");
}
public boolean isStoreNodeModel() {
return storeNodeModel;
}
public void setStoreNodeModel(boolean storeNodeModel) {
this.storeNodeModel = storeNodeModel;
}
private static class MyByteArrayOutputStream extends ByteArrayOutputStream {
@Override
public synchronized byte[] toByteArray() {
return buf;
}
public int length() {
return count;
}
}
}

View file

@ -1,151 +0,0 @@
/*******************************************************************************
* Copyright (c) 2014, 2017 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.resource.persistence
import com.google.inject.Inject
import com.google.inject.Provider
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.io.OutputStream
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.resource.impl.ExtensibleURIConverterImpl
import org.eclipse.xtext.generator.AbstractFileSystemAccess2
import org.eclipse.xtext.generator.IContextualOutputConfigurationProvider
import org.eclipse.xtext.generator.IFileSystemAccessExtension3
import org.eclipse.xtend.lib.annotations.Accessors
import java.io.IOException
import org.apache.log4j.Logger
/**
* @author Sven Efftinge - Initial contribution and API
*/
class ResourceStorageFacade implements IResourceStorageFacade {
static val Logger LOG = Logger.getLogger(ResourceStorageFacade)
@Inject IContextualOutputConfigurationProvider outputConfigurationProvider
@Inject Provider<AbstractFileSystemAccess2> fileSystemAccessProvider
@Accessors boolean storeNodeModel = false
/**
* @return whether the given resource should be loaded from stored resource state
*/
override boolean shouldLoadFromStorage(StorageAwareResource resource) {
val adapter = SourceLevelURIsAdapter.findInstalledAdapter(resource.resourceSet)
if (adapter === null) {
return false;
} else {
if (adapter.sourceLevelURIs.contains(resource.URI))
return false;
}
return doesStorageExist(resource)
}
/**
* Finds or creates a ResourceStorageLoadable for the given resource.
* Clients should first call shouldLoadFromStorage to check whether there exists a storage version
* of the given resource.
*
* @return an IResourceStorageLoadable
*/
override ResourceStorageLoadable getOrCreateResourceStorageLoadable(StorageAwareResource resource) {
val stateProvider = resource.resourceSet.eAdapters.filter(ResourceStorageProviderAdapter).head
if (stateProvider !== null) {
val inputStream = stateProvider.getResourceStorageLoadable(resource)
if (inputStream !== null)
return inputStream
}
val inputStream = if (resource.resourceSet.URIConverter.exists(resource.URI.getBinaryStorageURI, emptyMap)) {
resource.resourceSet.URIConverter.createInputStream(resource.URI.getBinaryStorageURI)
} else {
val fsa = getFileSystemAccess(resource);
val outputRelativePath = computeOutputPath(resource)
fsa.readBinaryFile(outputRelativePath)
}
return createResourceStorageLoadable(inputStream)
}
override void saveResource(StorageAwareResource resource, IFileSystemAccessExtension3 fsa) {
val path = computeOutputPath(resource)
val bout = new MyByteArrayOutputStream()
val outStream = createResourceStorageWritable(bout)
try {
outStream.writeResource(resource)
} catch(IOException e) {
// something went wrong when writing the resource - stream's content is bogus and not written to disk
LOG.warn("Cannot write storage for " + resource.URI, e)
return;
}
fsa.generateFile(path, new ByteArrayInputStream(bout.toByteArray, 0, bout.length))
}
override ResourceStorageLoadable createResourceStorageLoadable(InputStream in) {
return new ResourceStorageLoadable(in, isStoreNodeModel)
}
override ResourceStorageWritable createResourceStorageWritable(OutputStream out) {
return new ResourceStorageWritable(out, isStoreNodeModel)
}
/**
* @return whether a stored resource state exists for the given resource
*/
protected def doesStorageExist(StorageAwareResource resource) {
val stateProvider = resource.resourceSet.eAdapters.filter(ResourceStorageProviderAdapter).head
if (stateProvider !== null && stateProvider.getResourceStorageLoadable(resource) !== null)
return true;
// check for next to original location, i.e. jars
if (resource.resourceSet.URIConverter.exists(resource.URI.getBinaryStorageURI, emptyMap)) {
return true
}
// if it's an archive URI, we don't need to look up the source folder-output folder scheme
if (resource.URI.isArchive) {
return false
}
// check for source project locations, i.e. use generator config
val fsa = getFileSystemAccess(resource)
val outputRelativePath = computeOutputPath(resource)
val uri = fsa.getURI(outputRelativePath)
return uri !== null && resource.resourceSet.URIConverter.exists(uri, null)
}
protected def getFileSystemAccess(StorageAwareResource resource) {
val fsa = fileSystemAccessProvider.get()
fsa.context = resource
fsa.outputConfigurations = outputConfigurationProvider.getOutputConfigurations(resource).toMap[name]
return fsa
}
protected def computeOutputPath(StorageAwareResource resource) {
val srcContainerURI = getSourceContainerURI(resource)
val uri = resource.URI.getBinaryStorageURI
val outputRelativePath = uri.deresolve(srcContainerURI, false, false, true).path
return outputRelativePath
}
def protected getSourceContainerURI(StorageAwareResource resource) {
return resource.URI.trimSegments(1).appendSegment("")
}
override hasStorageFor(URI uri) {
return new ExtensibleURIConverterImpl().exists(getBinaryStorageURI(uri), emptyMap())
}
protected def getBinaryStorageURI(URI sourceURI) {
return sourceURI.trimSegments(1).appendSegment("."+sourceURI.lastSegment+'bin')
}
private static class MyByteArrayOutputStream extends ByteArrayOutputStream {
override synchronized toByteArray() { buf }
def int length() { count }
}
}

View file

@ -0,0 +1,120 @@
/**
* Copyright (c) 2014, 2020 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.resource.persistence;
import static org.eclipse.xtext.xbase.lib.IterableExtensions.*;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.util.Collections;
import java.util.zip.ZipInputStream;
import org.apache.log4j.Logger;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.resource.impl.BinaryResourceImpl;
import org.eclipse.xtext.nodemodel.impl.SerializableNodeModel;
import org.eclipse.xtext.nodemodel.serialization.DeserializationConversionContext;
import org.eclipse.xtext.parser.ParseResult;
import com.google.common.io.CharStreams;
/**
* @author Sven Efftinge - Initial contribution and API
*/
public class ResourceStorageLoadable {
private static final Logger LOG = Logger.getLogger(ResourceStorageLoadable.class);
private final InputStream in;
private final boolean storeNodeModel;
public ResourceStorageLoadable(InputStream in, boolean storeNodeModel) {
this.in = in;
this.storeNodeModel = storeNodeModel;
}
protected void loadIntoResource(StorageAwareResource resource) throws IOException {
if (!resource.isLoadedFromStorage())
throw new IllegalStateException("Please use StorageAwareResource#load(ResourceStorageLoadable).");
try (ZipInputStream zin = new ZipInputStream(in)) {
loadEntries(resource, zin);
}
}
/**
* Load entries from the storage. Overriding methods should first delegate to super before adding their own entries.
*/
protected void loadEntries(StorageAwareResource resource, ZipInputStream zipIn) throws IOException {
zipIn.getNextEntry();
readContents(resource, new BufferedInputStream(zipIn));
zipIn.getNextEntry();
readResourceDescription(resource, new BufferedInputStream(zipIn));
if (storeNodeModel) {
zipIn.getNextEntry();
readNodeModel(resource, new BufferedInputStream(zipIn));
}
}
protected void readContents(StorageAwareResource resource, InputStream inputStream) throws IOException {
new BinaryResourceImpl.EObjectInputStream(inputStream, Collections.emptyMap()) {
@Override
public int readCompressedInt() throws IOException {
//HACK! null resource set, to avoid usage of resourceSet's package registry
resourceSet = null;
return super.readCompressedInt();
}
@Override
public InternalEObject loadEObject() throws IOException {
InternalEObject result = super.loadEObject();
handleLoadEObject(result, this);
return result;
}
}.loadResource(resource);
}
protected Object handleLoadEObject(InternalEObject loaded, BinaryResourceImpl.EObjectInputStream input)
throws IOException {
return null;
}
protected void readResourceDescription(StorageAwareResource resource, InputStream inputStream) throws IOException {
try {
SerializableResourceDescription description = (SerializableResourceDescription) new ObjectInputStream(
inputStream).readObject();
description.updateResourceURI(resource.getURI());
resource.setResourceDescription(description);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
protected void readNodeModel(StorageAwareResource resource, InputStream inputStream) throws IOException {
SerializableNodeModel serializableNodeModel = new SerializableNodeModel(resource);
// if this is a synthetic resource (i.e. tests or so, don't load the node model)
if (!resource.getResourceSet().getURIConverter().exists(resource.getURI(),
resource.getResourceSet().getLoadOptions())) {
LOG.info("Skipping loading node model for synthetic resource " + resource.getURI());
return;
}
String completeContent = CharStreams.toString(
new InputStreamReader(resource.getResourceSet().getURIConverter().createInputStream(resource.getURI()),
resource.getEncoding()));
DeserializationConversionContext deserializationContext = new DeserializationConversionContext(resource,
completeContent);
serializableNodeModel.readObjectData(new DataInputStream(inputStream), deserializationContext);
resource.setParseResult(new ParseResult(head(resource.getContents()), serializableNodeModel.root,
deserializationContext.hasErrors()));
}
}

View file

@ -1,113 +0,0 @@
/*******************************************************************************
* Copyright (c) 2014, 2017 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.resource.persistence
import com.google.common.io.CharStreams
import java.io.BufferedInputStream
import java.io.DataInputStream
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.io.ObjectInputStream
import java.util.zip.ZipInputStream
import org.apache.log4j.Logger
import org.eclipse.emf.ecore.InternalEObject
import org.eclipse.emf.ecore.resource.impl.BinaryResourceImpl
import org.eclipse.emf.ecore.resource.impl.BinaryResourceImpl.EObjectInputStream
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor
import org.eclipse.xtext.nodemodel.impl.SerializableNodeModel
import org.eclipse.xtext.nodemodel.serialization.DeserializationConversionContext
import org.eclipse.xtext.parser.ParseResult
/**
* @author Sven Efftinge - Initial contribution and API
*/
@FinalFieldsConstructor class ResourceStorageLoadable {
static val LOG = Logger.getLogger(ResourceStorageLoadable)
val InputStream in
val boolean storeNodeModel
protected def void loadIntoResource(StorageAwareResource resource) throws IOException {
if (!resource.isLoadedFromStorage) {
throw new IllegalStateException("Please use StorageAwareResource#load(ResourceStorageLoadable).");
}
val zin = new ZipInputStream(in)
try {
loadEntries(resource, zin)
} finally {
zin.close
}
}
/**
* Load entries from the storage.
* Overriding methods should first delegate to super before adding their own entries.
*/
protected def void loadEntries(StorageAwareResource resource, ZipInputStream zipIn) throws IOException {
zipIn.nextEntry
readContents(resource, new BufferedInputStream(zipIn))
zipIn.nextEntry
readResourceDescription(resource, new BufferedInputStream(zipIn))
if (storeNodeModel) {
zipIn.nextEntry
readNodeModel(resource, new BufferedInputStream(zipIn))
}
}
protected def void readContents(StorageAwareResource resource, InputStream inputStream) throws IOException {
val in = new BinaryResourceImpl.EObjectInputStream(inputStream, emptyMap) {
override readCompressedInt() throws IOException {
//HACK! null resource set, to avoid usage of resourceSet's package registry
resourceSet = null
super.readCompressedInt()
}
override loadEObject() throws IOException {
val result = super.loadEObject()
handleLoadEObject(result, this)
return result
}
}
in.loadResource(resource)
}
protected def handleLoadEObject(InternalEObject loaded, EObjectInputStream input) throws IOException {
}
protected def void readResourceDescription(StorageAwareResource resource, InputStream inputStream) throws IOException {
val objectIn = new ObjectInputStream(inputStream)
val description = objectIn.readObject as SerializableResourceDescription
description.updateResourceURI(resource.URI)
resource.resourceDescription = description
}
protected def void readNodeModel(StorageAwareResource resource, InputStream inputStream) throws IOException {
val serializableNodeModel = new SerializableNodeModel(resource)
// if this is a synthetic resource (i.e. tests or so, don't load the node model)
if (!resource.resourceSet.URIConverter.exists(resource.URI, resource.resourceSet.loadOptions)) {
LOG.info("Skipping loading node model for synthetic resource "+resource.URI)
return;
}
val stream = resource.resourceSet.URIConverter.createInputStream(resource.URI)
val in = new InputStreamReader(stream, resource.encoding)
val completeContent = CharStreams.toString(in)
val deserializationContext = new DeserializationConversionContext(resource, completeContent)
val dataIn = new DataInputStream(inputStream)
serializableNodeModel.readObjectData(dataIn, deserializationContext)
resource.parseResult = new ParseResult(resource.contents.head,serializableNodeModel.root, deserializationContext.hasErrors)
}
}

View file

@ -0,0 +1,148 @@
/**
* Copyright (c) 2014, 2020 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.resource.persistence;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.resource.impl.BinaryResourceImpl;
import org.eclipse.xtext.nodemodel.impl.SerializableNodeModel;
import org.eclipse.xtext.nodemodel.serialization.SerializationConversionContext;
import org.eclipse.xtext.resource.IReferenceDescription;
import org.eclipse.xtext.resource.IResourceDescription;
/**
* @author Sven Efftinge - Initial contribution and API
*/
public class ResourceStorageWritable {
private final OutputStream out;
private final boolean storeNodeModel;
public ResourceStorageWritable(OutputStream out, boolean storeNodeModel) {
this.out = out;
this.storeNodeModel = storeNodeModel;
}
public void writeResource(StorageAwareResource resource) throws IOException {
if (resource.isLoadedFromStorage())
throw new IllegalStateException("cannot write resources loaded from storage. URI was " + resource.getURI());
try (ZipOutputStream zipOut = new ZipOutputStream(out)) {
writeEntries(resource, zipOut);
}
}
/**
* Write entries into the storage. Overriding methods should first delegate to super before adding their own
* entries.
*/
protected void writeEntries(StorageAwareResource resource, ZipOutputStream zipOut) throws IOException {
BufferedOutputStream bufferedOutput = new BufferedOutputStream(zipOut);
zipOut.putNextEntry(new ZipEntry("emf-contents"));
try {
writeContents(resource, bufferedOutput);
} finally {
bufferedOutput.flush();
zipOut.closeEntry();
}
zipOut.putNextEntry(new ZipEntry("resource-description"));
try {
writeResourceDescription(resource, bufferedOutput);
} finally {
bufferedOutput.flush();
zipOut.closeEntry();
}
if (storeNodeModel) {
zipOut.putNextEntry(new ZipEntry("node-model"));
try {
writeNodeModel(resource, bufferedOutput);
} finally {
bufferedOutput.flush();
zipOut.closeEntry();
}
}
}
protected void writeContents(StorageAwareResource storageAwareResource, OutputStream outputStream)
throws IOException {
BinaryResourceImpl.EObjectOutputStream out = new BinaryResourceImpl.EObjectOutputStream(outputStream,
Collections.emptyMap()) {
@Override
public void writeURI(URI uri, String fragment) throws IOException {
URI fullURI = uri.appendFragment(fragment);
URI portableURI = storageAwareResource.getPortableURIs().toPortableURI(storageAwareResource, fullURI);
URI uriToWrite = portableURI == null ? fullURI : portableURI;
super.writeURI(uriToWrite.trimFragment(), uriToWrite.fragment());
}
@Override
public void saveEObject(InternalEObject internalEObject, BinaryResourceImpl.EObjectOutputStream.Check check)
throws IOException {
beforeSaveEObject(internalEObject, this);
super.saveEObject(internalEObject, check);
handleSaveEObject(internalEObject, this);
}
};
try {
out.saveResource(storageAwareResource);
} finally {
out.flush();
}
}
protected Object beforeSaveEObject(InternalEObject object, BinaryResourceImpl.EObjectOutputStream writable)
throws IOException {
return null;
}
protected void handleSaveEObject(InternalEObject object, BinaryResourceImpl.EObjectOutputStream out)
throws IOException {
}
protected void writeResourceDescription(StorageAwareResource resource, OutputStream outputStream)
throws IOException {
IResourceDescription description = resource.getResourceServiceProvider().getResourceDescriptionManager()
.getResourceDescription(resource);
SerializableResourceDescription serializableDescription = SerializableResourceDescription
.createCopy(description);
convertExternalURIsToPortableURIs(serializableDescription, resource);
ObjectOutputStream out = new ObjectOutputStream(outputStream);
try {
out.writeObject(serializableDescription);
} finally {
out.flush();
}
}
protected void convertExternalURIsToPortableURIs(SerializableResourceDescription description,
StorageAwareResource resource) {
for (IReferenceDescription ref : description.getReferenceDescriptions()) {
if (!ref.getTargetEObjectUri().trimFragment().equals(resource.getURI())) {
URI portableURI = resource.getPortableURIs().toPortableURI(resource, ref.getTargetEObjectUri());
((SerializableReferenceDescription) ref)
.setTargetEObjectUri(portableURI != null ? portableURI : ref.getTargetEObjectUri());
}
}
}
protected void writeNodeModel(StorageAwareResource resource, OutputStream outputStream) throws IOException {
DataOutputStream out = new DataOutputStream(outputStream);
new SerializableNodeModel(resource).writeObjectData(out, new SerializationConversionContext(resource));
out.flush();
}
}

View file

@ -1,140 +0,0 @@
/*******************************************************************************
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.resource.persistence
import java.io.BufferedOutputStream
import java.io.DataOutputStream
import java.io.IOException
import java.io.ObjectOutputStream
import java.io.OutputStream
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.InternalEObject
import org.eclipse.emf.ecore.resource.impl.BinaryResourceImpl
import org.eclipse.emf.ecore.resource.impl.BinaryResourceImpl.EObjectOutputStream
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor
import org.eclipse.xtext.nodemodel.impl.SerializableNodeModel
import org.eclipse.xtext.nodemodel.serialization.SerializationConversionContext
/**
* @author Sven Efftinge - Initial contribution and API
*/
@FinalFieldsConstructor class ResourceStorageWritable {
val OutputStream out
val boolean storeNodeModel
def void writeResource(StorageAwareResource resource) throws IOException {
if (resource.isLoadedFromStorage) {
throw new IllegalStateException("cannot write resources loaded from storage. URI was "+resource.URI)
}
val zipOut = new ZipOutputStream(out)
try {
writeEntries(resource, zipOut)
} finally {
zipOut.close
}
}
/**
* Write entries into the storage.
* Overriding methods should first delegate to super before adding their own entries.
*/
protected def void writeEntries(StorageAwareResource resource, ZipOutputStream zipOut) throws IOException {
val bufferedOutput = new BufferedOutputStream(zipOut)
zipOut.putNextEntry(new ZipEntry("emf-contents"))
try {
writeContents(resource, bufferedOutput)
} finally {
bufferedOutput.flush
zipOut.closeEntry
}
zipOut.putNextEntry(new ZipEntry("resource-description"))
try {
writeResourceDescription(resource, bufferedOutput)
} finally {
bufferedOutput.flush
zipOut.closeEntry
}
if (storeNodeModel) {
zipOut.putNextEntry(new ZipEntry("node-model"))
try {
writeNodeModel(resource, bufferedOutput)
} finally {
bufferedOutput.flush
zipOut.closeEntry
}
}
}
protected def void writeContents(StorageAwareResource storageAwareResource, OutputStream outputStream) throws IOException {
val out = new BinaryResourceImpl.EObjectOutputStream(outputStream, emptyMap) {
override writeURI(URI uri, String fragment) throws IOException {
val fullURI = uri.appendFragment(fragment)
val uriToWrite = storageAwareResource.portableURIs.toPortableURI(storageAwareResource, fullURI) ?: fullURI
super.writeURI(uriToWrite.trimFragment, uriToWrite.fragment)
}
override saveEObject(InternalEObject internalEObject, Check check) throws IOException {
beforeSaveEObject(internalEObject, this)
super.saveEObject(internalEObject, check)
handleSaveEObject(internalEObject, this)
}
}
try {
out.saveResource(storageAwareResource)
} finally {
out.flush
}
}
protected def beforeSaveEObject(InternalEObject object, EObjectOutputStream writable) throws IOException {
// do nothing
}
protected def void handleSaveEObject(InternalEObject object, BinaryResourceImpl.EObjectOutputStream out) throws IOException {
// do nothing
}
protected def void writeResourceDescription(StorageAwareResource resource, OutputStream outputStream) throws IOException {
val description = resource.resourceServiceProvider.resourceDescriptionManager.getResourceDescription(resource);
val serializableDescription = SerializableResourceDescription.createCopy(description)
convertExternalURIsToPortableURIs(serializableDescription, resource)
val out = new ObjectOutputStream(outputStream);
try {
out.writeObject(serializableDescription);
} finally {
out.flush
}
}
def protected void convertExternalURIsToPortableURIs(SerializableResourceDescription description, StorageAwareResource resource) {
for (ref : description.referenceDescriptions) {
if (ref.targetEObjectUri.trimFragment != resource.URI) {
(ref as SerializableReferenceDescription).targetEObjectUri = resource.portableURIs.toPortableURI(resource, ref.targetEObjectUri) ?: ref.targetEObjectUri
}
}
}
protected def writeNodeModel(StorageAwareResource resource, OutputStream outputStream) throws IOException {
val out = new DataOutputStream(outputStream);
val serializableNodeModel = new SerializableNodeModel(resource)
val conversionContext = new SerializationConversionContext(resource)
serializableNodeModel.writeObjectData(out, conversionContext)
out.flush
}
}

View file

@ -0,0 +1,123 @@
/**
* Copyright (c) 2014, 2020 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.resource.persistence;
import static org.eclipse.xtext.resource.persistence.SerializationExtensions.*;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.HashMap;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.resource.IEObjectDescription;
/**
* @since 2.8
*/
public class SerializableEObjectDescription
implements IEObjectDescription, Externalizable, SerializableEObjectDescriptionProvider {
protected URI eObjectURI;
protected EClass eClass;
protected QualifiedName qualifiedName;
protected HashMap<String, String> userData;
protected transient EObject eObjectOrProxy;
public void updateResourceURI(URI uri) {
eObjectURI = uri.appendFragment(eObjectURI.fragment());
}
@Override
public EObject getEObjectOrProxy() {
if (eObjectOrProxy == null) {
EObject proxy = EcoreUtil.create(eClass);
((InternalEObject) proxy).eSetProxyURI(eObjectURI);
eObjectOrProxy = proxy;
}
return eObjectOrProxy;
}
@Override
public QualifiedName getName() {
return qualifiedName;
}
@Override
public String getUserData(String key) {
return userData.get(key);
}
@Override
public String[] getUserDataKeys() {
return userData.keySet().toArray(new String[0]);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
eObjectURI = readURI(in);
eClass = readEcoreElement(in);
qualifiedName = readQualifiedName(in);
userData = readCastedObject(in);
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
writeURI(out, eObjectURI);
writeEcoreElement(out, eClass);
writeQualifiedName(out, qualifiedName);
out.writeObject(userData);
}
@Override
public SerializableEObjectDescription toSerializableEObjectDescription() {
return this;
}
public URI getEObjectURI() {
return eObjectURI;
}
public void setEObjectURI(URI eObjectURI) {
this.eObjectURI = eObjectURI;
}
public EClass getEClass() {
return eClass;
}
public void setEClass(EClass eClass) {
this.eClass = eClass;
}
public QualifiedName getQualifiedName() {
return qualifiedName;
}
public void setQualifiedName(QualifiedName qualifiedName) {
this.qualifiedName = qualifiedName;
}
public HashMap<String, String> getUserData() {
return userData;
}
public void setUserData(HashMap<String, String> userData) {
this.userData = userData;
}
}

View file

@ -1,5 +1,5 @@
/**
* Copyright (c) 2014, 2017 itemis AG (http://www.itemis.eu) and others.
* Copyright (c) 2014, 2020 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
@ -8,12 +8,9 @@
*/
package org.eclipse.xtext.resource.persistence;
import org.eclipse.xtext.resource.persistence.SerializableEObjectDescription;
/**
* @since 2.11
*/
@SuppressWarnings("all")
public interface SerializableEObjectDescriptionProvider {
SerializableEObjectDescription toSerializableEObjectDescription();
SerializableEObjectDescription toSerializableEObjectDescription();
}

View file

@ -0,0 +1,99 @@
/**
* Copyright (c) 2014, 2020 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.resource.persistence;
import static org.eclipse.xtext.resource.persistence.SerializationExtensions.*;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.xtext.resource.IReferenceDescription;
/**
* @since 2.8
*/
public class SerializableReferenceDescription implements IReferenceDescription, Externalizable {
private URI sourceEObjectUri;
private URI targetEObjectUri;
private URI containerEObjectURI;
private EReference eReference;
private int indexInList;
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
sourceEObjectUri = readURI(in);
targetEObjectUri = readURI(in);
containerEObjectURI = readURI(in);
eReference = readEcoreElement(in);
indexInList = in.readInt();
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
writeURI(out, sourceEObjectUri);
writeURI(out, targetEObjectUri);
writeURI(out, containerEObjectURI);
writeEcoreElement(out, eReference);
out.writeInt(indexInList);
}
public void updateResourceURI(URI newURI, URI oldURI) {
sourceEObjectUri = newURI.appendFragment(sourceEObjectUri.fragment());
if (targetEObjectUri.trimFragment().equals(oldURI))
targetEObjectUri = newURI.appendFragment(targetEObjectUri.fragment());
}
public URI getSourceEObjectUri() {
return sourceEObjectUri;
}
public void setSourceEObjectUri(URI sourceEObjectUri) {
this.sourceEObjectUri = sourceEObjectUri;
}
public URI getTargetEObjectUri() {
return targetEObjectUri;
}
public void setTargetEObjectUri(URI targetEObjectUri) {
this.targetEObjectUri = targetEObjectUri;
}
public URI getContainerEObjectURI() {
return containerEObjectURI;
}
public void setContainerEObjectURI(URI containerEObjectURI) {
this.containerEObjectURI = containerEObjectURI;
}
public EReference getEReference() {
return eReference;
}
public void setEReference(EReference eReference) {
this.eReference = eReference;
}
public int getIndexInList() {
return indexInList;
}
public void setIndexInList(int indexInList) {
this.indexInList = indexInList;
}
}

View file

@ -0,0 +1,165 @@
/**
* Copyright (c) 2014, 2020 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.resource.persistence;
import static org.eclipse.xtext.resource.persistence.SerializationExtensions.*;
import static org.eclipse.xtext.xbase.lib.IterableExtensions.*;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import org.eclipse.emf.common.util.URI;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.resource.IReferenceDescription;
import org.eclipse.xtext.resource.IResourceDescription;
import org.eclipse.xtext.resource.impl.AbstractResourceDescription;
import com.google.common.collect.Lists;
/**
* @author Sven Efftinge - Initial contribution and API
*
* @since 2.8
*/
public class SerializableResourceDescription extends AbstractResourceDescription implements Externalizable {
private List<SerializableEObjectDescription> descriptions = Collections.emptyList();
private List<SerializableReferenceDescription> references = Collections.emptyList();
private List<QualifiedName> importedNames = Collections.emptyList();
private URI uri;
public void updateResourceURI(URI uri) {
for (SerializableReferenceDescription ref : references)
ref.updateResourceURI(uri, this.uri);
for (SerializableEObjectDescription desc : descriptions)
desc.updateResourceURI(uri);
this.uri = uri;
}
@Override
@SuppressWarnings("unchecked")
protected List<IEObjectDescription> computeExportedObjects() {
return ((List<IEObjectDescription>) ((List<?>) this.descriptions));
}
@Override
public Iterable<QualifiedName> getImportedNames() {
return importedNames;
}
@Override
@SuppressWarnings("unchecked")
public Iterable<IReferenceDescription> getReferenceDescriptions() {
return ((Iterable<IReferenceDescription>) ((Iterable<?>) references));
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
setURI(readURI(in));
int descriptionsSize = in.readInt();
descriptions = new ArrayList<>(descriptionsSize);
for (int i = 0; i < descriptionsSize; i++)
descriptions.add(readCastedObject(in));
int referencesSize = in.readInt();
references = new ArrayList<>(referencesSize);
for (int i = 0; i < referencesSize; i++)
references.add(readCastedObject(in));
int importedNamesSize = in.readInt();
importedNames = new ArrayList<>(importedNamesSize);
for (int i = 0; i < importedNamesSize; i++)
importedNames.add(readQualifiedName(in));
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
writeURI(out, uri);
out.writeInt(descriptions.size());
for (SerializableEObjectDescription desc : descriptions)
out.writeObject(desc);
out.writeInt(references.size());
for (SerializableReferenceDescription ref : references)
out.writeObject(ref);
out.writeInt(importedNames.size());
for (QualifiedName name : this.importedNames)
SerializationExtensions.writeQualifiedName(out, name);
}
public List<SerializableEObjectDescription> getDescriptions() {
return descriptions;
}
public void setDescriptions(List<SerializableEObjectDescription> descriptions) {
this.descriptions = descriptions;
}
public List<SerializableReferenceDescription> getReferences() {
return references;
}
public void setReferences(List<SerializableReferenceDescription> references) {
this.references = references;
}
public void setImportedNames(List<QualifiedName> importedNames) {
this.importedNames = importedNames;
}
public URI getURI() {
return uri;
}
public void setURI(URI uri) {
this.uri = uri;
}
public static SerializableResourceDescription createCopy(IResourceDescription desc) {
SerializableResourceDescription description = new SerializableResourceDescription();
description.setURI(desc.getURI());
description.descriptions = Lists.newArrayList(
map(desc.getExportedObjects(), SerializableResourceDescription::createCopy));
description.references = Lists.newArrayList(
map(desc.getReferenceDescriptions(), SerializableResourceDescription::createCopy));
description.importedNames = Lists.newArrayList(desc.getImportedNames());
return description;
}
private static SerializableEObjectDescription createCopy(IEObjectDescription desc) {
if (desc instanceof SerializableEObjectDescriptionProvider)
return ((SerializableEObjectDescriptionProvider) desc).toSerializableEObjectDescription();
SerializableEObjectDescription result = new SerializableEObjectDescription();
result.setEClass(desc.getEClass());
result.setEObjectURI(desc.getEObjectURI());
result.qualifiedName = desc.getQualifiedName();
result.userData = new HashMap<String, String>(desc.getUserDataKeys().length);
for (String key : desc.getUserDataKeys())
result.userData.put(key, desc.getUserData(key));
return result;
}
private static SerializableReferenceDescription createCopy(IReferenceDescription desc) {
SerializableReferenceDescription result = new SerializableReferenceDescription();
result.setSourceEObjectUri(desc.getSourceEObjectUri());
result.setTargetEObjectUri(desc.getTargetEObjectUri());
result.setEReference(desc.getEReference());
result.setIndexInList(desc.getIndexInList());
result.setContainerEObjectURI(desc.getContainerEObjectURI());
return result;
}
}

View file

@ -1,282 +0,0 @@
/*******************************************************************************
* Copyright (c) 2014, 2017 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.resource.persistence
import java.io.Externalizable
import java.io.IOException
import java.io.ObjectInput
import java.io.ObjectOutput
import java.util.ArrayList
import java.util.HashMap
import java.util.List
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.EClass
import org.eclipse.emf.ecore.ENamedElement
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.EPackage
import org.eclipse.emf.ecore.EReference
import org.eclipse.emf.ecore.InternalEObject
import org.eclipse.emf.ecore.util.EcoreUtil
import org.eclipse.xtend.lib.annotations.Accessors
import org.eclipse.xtext.naming.QualifiedName
import org.eclipse.xtext.resource.IEObjectDescription
import org.eclipse.xtext.resource.IReferenceDescription
import org.eclipse.xtext.resource.IResourceDescription
import org.eclipse.xtext.resource.impl.AbstractResourceDescription
import static extension org.eclipse.xtext.resource.persistence.SerializationExtensions.*
/**
* @author Sven Efftinge - Initial contribution and API
*
* @since 2.8
*/
@Accessors class SerializableResourceDescription extends AbstractResourceDescription implements Externalizable {
def static SerializableResourceDescription createCopy(IResourceDescription desc) {
new SerializableResourceDescription => [
URI = desc.URI
descriptions = desc.exportedObjects.map[createCopy(it)].toList
references = desc.referenceDescriptions.map[createCopy(it)].toList
importedNames = newArrayList(desc.importedNames)
]
}
private def static SerializableEObjectDescription createCopy(IEObjectDescription desc) {
if (desc instanceof SerializableEObjectDescriptionProvider) {
return desc.toSerializableEObjectDescription()
}
return new SerializableEObjectDescription => [
EClass = desc.EClass
EObjectURI = desc.EObjectURI
qualifiedName = desc.qualifiedName
userData = new HashMap(desc.userDataKeys.size)
for (key : desc.userDataKeys) {
userData.put(key, desc.getUserData(key))
}
]
}
private def static SerializableReferenceDescription createCopy(IReferenceDescription desc) {
new SerializableReferenceDescription => [
sourceEObjectUri = desc.sourceEObjectUri
targetEObjectUri = desc.targetEObjectUri
EReference = desc.EReference
indexInList = desc.indexInList
containerEObjectURI = desc.containerEObjectURI
]
}
List<SerializableEObjectDescription> descriptions = emptyList
List<SerializableReferenceDescription> references = emptyList
List<QualifiedName> importedNames = emptyList
URI uRI
def void updateResourceURI(URI uri) {
for (ref : references) {
ref.updateResourceURI(uri, this.uRI)
}
for (desc : descriptions) {
desc.updateResourceURI(uri)
}
this.uRI = uri
}
override protected computeExportedObjects() {
descriptions as List<?> as List<IEObjectDescription>
}
override getImportedNames() {
importedNames
}
override getReferenceDescriptions() {
references as Iterable<?> as Iterable<IReferenceDescription>
}
override readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
URI = in.readURI
val descriptionsSize = in.readInt
descriptions = new ArrayList(descriptionsSize);
for (i : 0 ..< descriptionsSize) {
descriptions.add(in.readCastedObject)
}
val referencesSize = in.readInt
references = new ArrayList(referencesSize);
for (i : 0 ..< referencesSize) {
references.add(in.readCastedObject)
}
val importedNamesSize = in.readInt
importedNames = new ArrayList(importedNamesSize)
for (i : 0 ..< importedNamesSize) {
importedNames.add(in.readQualifiedName)
}
}
override writeExternal(ObjectOutput out) throws IOException {
out.writeURI(uRI)
out.writeInt(descriptions.size)
for (desc : descriptions) {
out.writeObject(desc)
}
out.writeInt(references.size)
for (ref : references) {
out.writeObject(ref)
}
out.writeInt(importedNames.size)
for (name : importedNames) {
out.writeQualifiedName(name)
}
}
}
/**
* @since 2.11
*/
interface SerializableEObjectDescriptionProvider {
def SerializableEObjectDescription toSerializableEObjectDescription();
}
/**
* @since 2.8
*/
@Accessors class SerializableEObjectDescription implements IEObjectDescription, Externalizable, SerializableEObjectDescriptionProvider {
protected URI eObjectURI
protected EClass eClass
protected QualifiedName qualifiedName
protected HashMap<String,String> userData
@Accessors(NONE) protected transient EObject eObjectOrProxy
def void updateResourceURI(URI uri) {
eObjectURI = uri.appendFragment(eObjectURI.fragment)
}
override getEObjectOrProxy() {
if (eObjectOrProxy === null) {
val proxy = EcoreUtil.create(eClass)
(proxy as InternalEObject).eSetProxyURI(eObjectURI)
eObjectOrProxy = proxy
}
return eObjectOrProxy
}
override getName() {
qualifiedName
}
override getUserData(String key) {
userData.get(key)
}
override getUserDataKeys() {
userData.keySet
}
override readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
eObjectURI = in.readURI
eClass = in.readEcoreElement
qualifiedName = in.readQualifiedName
userData = in.readCastedObject
}
override writeExternal(ObjectOutput out) throws IOException {
out.writeURI(eObjectURI)
out.writeEcoreElement(eClass)
out.writeQualifiedName(qualifiedName)
out.writeObject(userData)
}
override toSerializableEObjectDescription() {
return this
}
}
/**
* @since 2.8
*/
@Accessors class SerializableReferenceDescription implements IReferenceDescription, Externalizable {
URI sourceEObjectUri
URI targetEObjectUri
URI containerEObjectURI
EReference eReference
int indexInList
override readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
sourceEObjectUri = in.readURI
targetEObjectUri = in.readURI
containerEObjectURI = in.readURI
eReference = in.readEcoreElement
indexInList = in.readInt
}
override writeExternal(ObjectOutput out) throws IOException {
out.writeURI(sourceEObjectUri)
out.writeURI(targetEObjectUri)
out.writeURI(containerEObjectURI)
out.writeEcoreElement(eReference)
out.writeInt(indexInList)
}
def void updateResourceURI(URI newURI, URI oldURI) {
sourceEObjectUri = newURI.appendFragment(sourceEObjectUri.fragment)
if (targetEObjectUri.trimFragment == oldURI) {
targetEObjectUri = newURI.appendFragment(targetEObjectUri.fragment)
}
}
}
/**
* @since 2.8
*/
class SerializationExtensions {
def static <T extends ENamedElement> T readEcoreElement(ObjectInput in) throws IOException {
val uri = in.readURI
val ePackage = EPackage.Registry.INSTANCE.getEPackage(uri.trimFragment.toString)
return ePackage?.eResource?.getEObject(uri.fragment) as T
}
def static void writeEcoreElement(ObjectOutput out, ENamedElement namedElement) throws IOException {
val uri = EcoreUtil.getURI(namedElement)
out.writeURI(uri)
}
def static <T> T readCastedObject(ObjectInput in) throws IOException {
in.readObject as T
}
def static URI readURI(ObjectInput in) throws IOException {
val stringRep = in.readUTF
if (stringRep == "NULL") {
return null
}
return URI::createURI(stringRep)
}
def static void writeURI(ObjectOutput out, URI uri) throws IOException {
if (uri === null) {
out.writeUTF("NULL")
} else {
out.writeUTF(uri.toString)
}
}
def static QualifiedName readQualifiedName(ObjectInput in) throws IOException {
return QualifiedName.create(in.readObject as ArrayList<String>)
}
def static void writeQualifiedName(ObjectOutput out, QualifiedName name) throws IOException {
out.writeObject(new ArrayList(name.segments))
}
}

View file

@ -0,0 +1,80 @@
/**
* Copyright (c) 2014, 2020 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.resource.persistence;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.naming.QualifiedName;
/**
* @since 2.8
*/
public class SerializationExtensions {
@SuppressWarnings("unchecked")
public static <T extends ENamedElement> T readEcoreElement(ObjectInput in) throws IOException {
URI uri = readURI(in);
EPackage ePackage = EPackage.Registry.INSTANCE.getEPackage(uri.trimFragment().toString());
if (ePackage == null)
return null;
Resource eResource = ePackage.eResource();
if (eResource == null)
return null;
return (T) eResource.getEObject(uri.fragment());
}
public static void writeEcoreElement(ObjectOutput out, ENamedElement namedElement) throws IOException {
writeURI(out, EcoreUtil.getURI(namedElement));
}
@SuppressWarnings("unchecked")
public static <T extends Object> T readCastedObject(ObjectInput in) throws IOException {
try {
return ((T) in.readObject());
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public static URI readURI(ObjectInput in) throws IOException {
String stringRep = in.readUTF();
if ("NULL".equals(stringRep))
return null;
return URI.createURI(stringRep);
}
public static void writeURI(ObjectOutput out, URI uri) throws IOException {
if (uri == null)
out.writeUTF("NULL");
else
out.writeUTF(uri.toString());
}
@SuppressWarnings("unchecked")
public static QualifiedName readQualifiedName(ObjectInput in) throws IOException {
try {
return QualifiedName.create((List<String>) in.readObject());
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public static void writeQualifiedName(ObjectOutput out, QualifiedName name) throws IOException {
out.writeObject(new ArrayList<String>(name.getSegments()));
}
}

View file

@ -1,220 +0,0 @@
/**
* Copyright (c) 2014, 2017 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.resource.persistence;
import com.google.common.collect.Iterables;
import com.google.inject.Inject;
import com.google.inject.Provider;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.log4j.Logger;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.impl.ExtensibleURIConverterImpl;
import org.eclipse.xtend.lib.annotations.Accessors;
import org.eclipse.xtext.generator.AbstractFileSystemAccess2;
import org.eclipse.xtext.generator.IContextualOutputConfigurationProvider;
import org.eclipse.xtext.generator.IFileSystemAccessExtension3;
import org.eclipse.xtext.generator.OutputConfiguration;
import org.eclipse.xtext.resource.persistence.IResourceStorageFacade;
import org.eclipse.xtext.resource.persistence.ResourceStorageLoadable;
import org.eclipse.xtext.resource.persistence.ResourceStorageProviderAdapter;
import org.eclipse.xtext.resource.persistence.ResourceStorageWritable;
import org.eclipse.xtext.resource.persistence.SourceLevelURIsAdapter;
import org.eclipse.xtext.resource.persistence.StorageAwareResource;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.Pure;
/**
* @author Sven Efftinge - Initial contribution and API
*/
@SuppressWarnings("all")
public class ResourceStorageFacade implements IResourceStorageFacade {
private static class MyByteArrayOutputStream extends ByteArrayOutputStream {
@Override
public synchronized byte[] toByteArray() {
return this.buf;
}
public int length() {
return this.count;
}
}
private static final Logger LOG = Logger.getLogger(ResourceStorageFacade.class);
@Inject
private IContextualOutputConfigurationProvider outputConfigurationProvider;
@Inject
private Provider<AbstractFileSystemAccess2> fileSystemAccessProvider;
@Accessors
private boolean storeNodeModel = false;
/**
* @return whether the given resource should be loaded from stored resource state
*/
@Override
public boolean shouldLoadFromStorage(final StorageAwareResource resource) {
final SourceLevelURIsAdapter adapter = SourceLevelURIsAdapter.findInstalledAdapter(resource.getResourceSet());
if ((adapter == null)) {
return false;
} else {
boolean _contains = adapter.getSourceLevelURIs().contains(resource.getURI());
if (_contains) {
return false;
}
}
return this.doesStorageExist(resource);
}
/**
* Finds or creates a ResourceStorageLoadable for the given resource.
* Clients should first call shouldLoadFromStorage to check whether there exists a storage version
* of the given resource.
*
* @return an IResourceStorageLoadable
*/
@Override
public ResourceStorageLoadable getOrCreateResourceStorageLoadable(final StorageAwareResource resource) {
try {
final ResourceStorageProviderAdapter stateProvider = IterableExtensions.<ResourceStorageProviderAdapter>head(Iterables.<ResourceStorageProviderAdapter>filter(resource.getResourceSet().eAdapters(), ResourceStorageProviderAdapter.class));
if ((stateProvider != null)) {
final ResourceStorageLoadable inputStream = stateProvider.getResourceStorageLoadable(resource);
if ((inputStream != null)) {
return inputStream;
}
}
InputStream _xifexpression = null;
boolean _exists = resource.getResourceSet().getURIConverter().exists(this.getBinaryStorageURI(resource.getURI()), CollectionLiterals.<Object, Object>emptyMap());
if (_exists) {
_xifexpression = resource.getResourceSet().getURIConverter().createInputStream(this.getBinaryStorageURI(resource.getURI()));
} else {
InputStream _xblockexpression = null;
{
final AbstractFileSystemAccess2 fsa = this.getFileSystemAccess(resource);
final String outputRelativePath = this.computeOutputPath(resource);
_xblockexpression = fsa.readBinaryFile(outputRelativePath);
}
_xifexpression = _xblockexpression;
}
final InputStream inputStream_1 = _xifexpression;
return this.createResourceStorageLoadable(inputStream_1);
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
@Override
public void saveResource(final StorageAwareResource resource, final IFileSystemAccessExtension3 fsa) {
final String path = this.computeOutputPath(resource);
final ResourceStorageFacade.MyByteArrayOutputStream bout = new ResourceStorageFacade.MyByteArrayOutputStream();
final ResourceStorageWritable outStream = this.createResourceStorageWritable(bout);
try {
outStream.writeResource(resource);
} catch (final Throwable _t) {
if (_t instanceof IOException) {
final IOException e = (IOException)_t;
URI _uRI = resource.getURI();
String _plus = ("Cannot write storage for " + _uRI);
ResourceStorageFacade.LOG.warn(_plus, e);
return;
} else {
throw Exceptions.sneakyThrow(_t);
}
}
byte[] _byteArray = bout.toByteArray();
int _length = bout.length();
ByteArrayInputStream _byteArrayInputStream = new ByteArrayInputStream(_byteArray, 0, _length);
fsa.generateFile(path, _byteArrayInputStream);
}
@Override
public ResourceStorageLoadable createResourceStorageLoadable(final InputStream in) {
boolean _isStoreNodeModel = this.isStoreNodeModel();
return new ResourceStorageLoadable(in, _isStoreNodeModel);
}
@Override
public ResourceStorageWritable createResourceStorageWritable(final OutputStream out) {
boolean _isStoreNodeModel = this.isStoreNodeModel();
return new ResourceStorageWritable(out, _isStoreNodeModel);
}
/**
* @return whether a stored resource state exists for the given resource
*/
protected boolean doesStorageExist(final StorageAwareResource resource) {
final ResourceStorageProviderAdapter stateProvider = IterableExtensions.<ResourceStorageProviderAdapter>head(Iterables.<ResourceStorageProviderAdapter>filter(resource.getResourceSet().eAdapters(), ResourceStorageProviderAdapter.class));
if (((stateProvider != null) && (stateProvider.getResourceStorageLoadable(resource) != null))) {
return true;
}
boolean _exists = resource.getResourceSet().getURIConverter().exists(this.getBinaryStorageURI(resource.getURI()), CollectionLiterals.<Object, Object>emptyMap());
if (_exists) {
return true;
}
boolean _isArchive = resource.getURI().isArchive();
if (_isArchive) {
return false;
}
final AbstractFileSystemAccess2 fsa = this.getFileSystemAccess(resource);
final String outputRelativePath = this.computeOutputPath(resource);
final URI uri = fsa.getURI(outputRelativePath);
return ((uri != null) && resource.getResourceSet().getURIConverter().exists(uri, null));
}
protected AbstractFileSystemAccess2 getFileSystemAccess(final StorageAwareResource resource) {
final AbstractFileSystemAccess2 fsa = this.fileSystemAccessProvider.get();
fsa.setContext(resource);
final Function1<OutputConfiguration, String> _function = (OutputConfiguration it) -> {
return it.getName();
};
fsa.setOutputConfigurations(IterableExtensions.<String, OutputConfiguration>toMap(this.outputConfigurationProvider.getOutputConfigurations(resource), _function));
return fsa;
}
protected String computeOutputPath(final StorageAwareResource resource) {
final URI srcContainerURI = this.getSourceContainerURI(resource);
final URI uri = this.getBinaryStorageURI(resource.getURI());
final String outputRelativePath = uri.deresolve(srcContainerURI, false, false, true).path();
return outputRelativePath;
}
protected URI getSourceContainerURI(final StorageAwareResource resource) {
return resource.getURI().trimSegments(1).appendSegment("");
}
@Override
public boolean hasStorageFor(final URI uri) {
return new ExtensibleURIConverterImpl().exists(this.getBinaryStorageURI(uri), CollectionLiterals.<Object, Object>emptyMap());
}
protected URI getBinaryStorageURI(final URI sourceURI) {
URI _trimSegments = sourceURI.trimSegments(1);
String _lastSegment = sourceURI.lastSegment();
String _plus = ("." + _lastSegment);
String _plus_1 = (_plus + "bin");
return _trimSegments.appendSegment(_plus_1);
}
@Pure
public boolean isStoreNodeModel() {
return this.storeNodeModel;
}
public void setStoreNodeModel(final boolean storeNodeModel) {
this.storeNodeModel = storeNodeModel;
}
}

View file

@ -1,146 +0,0 @@
/**
* Copyright (c) 2014, 2017 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.resource.persistence;
import com.google.common.io.CharStreams;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.util.Map;
import java.util.zip.ZipInputStream;
import org.apache.log4j.Logger;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.resource.impl.BinaryResourceImpl;
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor;
import org.eclipse.xtext.nodemodel.impl.SerializableNodeModel;
import org.eclipse.xtext.nodemodel.serialization.DeserializationConversionContext;
import org.eclipse.xtext.parser.ParseResult;
import org.eclipse.xtext.resource.persistence.SerializableResourceDescription;
import org.eclipse.xtext.resource.persistence.StorageAwareResource;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
/**
* @author Sven Efftinge - Initial contribution and API
*/
@FinalFieldsConstructor
@SuppressWarnings("all")
public class ResourceStorageLoadable {
private static final Logger LOG = Logger.getLogger(ResourceStorageLoadable.class);
private final InputStream in;
private final boolean storeNodeModel;
protected void loadIntoResource(final StorageAwareResource resource) throws IOException {
boolean _isLoadedFromStorage = resource.isLoadedFromStorage();
boolean _not = (!_isLoadedFromStorage);
if (_not) {
throw new IllegalStateException("Please use StorageAwareResource#load(ResourceStorageLoadable).");
}
final ZipInputStream zin = new ZipInputStream(this.in);
try {
this.loadEntries(resource, zin);
} finally {
zin.close();
}
}
/**
* Load entries from the storage.
* Overriding methods should first delegate to super before adding their own entries.
*/
protected void loadEntries(final StorageAwareResource resource, final ZipInputStream zipIn) throws IOException {
zipIn.getNextEntry();
BufferedInputStream _bufferedInputStream = new BufferedInputStream(zipIn);
this.readContents(resource, _bufferedInputStream);
zipIn.getNextEntry();
BufferedInputStream _bufferedInputStream_1 = new BufferedInputStream(zipIn);
this.readResourceDescription(resource, _bufferedInputStream_1);
if (this.storeNodeModel) {
zipIn.getNextEntry();
BufferedInputStream _bufferedInputStream_2 = new BufferedInputStream(zipIn);
this.readNodeModel(resource, _bufferedInputStream_2);
}
}
protected void readContents(final StorageAwareResource resource, final InputStream inputStream) throws IOException {
Map<Object, Object> _emptyMap = CollectionLiterals.<Object, Object>emptyMap();
final BinaryResourceImpl.EObjectInputStream in = new BinaryResourceImpl.EObjectInputStream(inputStream, _emptyMap) {
@Override
public int readCompressedInt() throws IOException {
int _xblockexpression = (int) 0;
{
this.resourceSet = null;
_xblockexpression = super.readCompressedInt();
}
return _xblockexpression;
}
@Override
public InternalEObject loadEObject() throws IOException {
final InternalEObject result = super.loadEObject();
ResourceStorageLoadable.this.handleLoadEObject(result, this);
return result;
}
};
in.loadResource(resource);
}
protected Object handleLoadEObject(final InternalEObject loaded, final BinaryResourceImpl.EObjectInputStream input) throws IOException {
return null;
}
protected void readResourceDescription(final StorageAwareResource resource, final InputStream inputStream) throws IOException {
try {
final ObjectInputStream objectIn = new ObjectInputStream(inputStream);
Object _readObject = objectIn.readObject();
final SerializableResourceDescription description = ((SerializableResourceDescription) _readObject);
description.updateResourceURI(resource.getURI());
resource.setResourceDescription(description);
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
protected void readNodeModel(final StorageAwareResource resource, final InputStream inputStream) throws IOException {
final SerializableNodeModel serializableNodeModel = new SerializableNodeModel(resource);
boolean _exists = resource.getResourceSet().getURIConverter().exists(resource.getURI(), resource.getResourceSet().getLoadOptions());
boolean _not = (!_exists);
if (_not) {
URI _uRI = resource.getURI();
String _plus = ("Skipping loading node model for synthetic resource " + _uRI);
ResourceStorageLoadable.LOG.info(_plus);
return;
}
final InputStream stream = resource.getResourceSet().getURIConverter().createInputStream(resource.getURI());
String _encoding = resource.getEncoding();
final InputStreamReader in = new InputStreamReader(stream, _encoding);
final String completeContent = CharStreams.toString(in);
final DeserializationConversionContext deserializationContext = new DeserializationConversionContext(resource, completeContent);
final DataInputStream dataIn = new DataInputStream(inputStream);
serializableNodeModel.readObjectData(dataIn, deserializationContext);
EObject _head = IterableExtensions.<EObject>head(resource.getContents());
boolean _hasErrors = deserializationContext.hasErrors();
ParseResult _parseResult = new ParseResult(_head, serializableNodeModel.root, _hasErrors);
resource.setParseResult(_parseResult);
}
public ResourceStorageLoadable(final InputStream in, final boolean storeNodeModel) {
super();
this.in = in;
this.storeNodeModel = storeNodeModel;
}
}

View file

@ -1,175 +0,0 @@
/**
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.resource.persistence;
import com.google.common.base.Objects;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.resource.impl.BinaryResourceImpl;
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor;
import org.eclipse.xtext.nodemodel.impl.SerializableNodeModel;
import org.eclipse.xtext.nodemodel.serialization.SerializationConversionContext;
import org.eclipse.xtext.resource.IReferenceDescription;
import org.eclipse.xtext.resource.IResourceDescription;
import org.eclipse.xtext.resource.persistence.SerializableReferenceDescription;
import org.eclipse.xtext.resource.persistence.SerializableResourceDescription;
import org.eclipse.xtext.resource.persistence.StorageAwareResource;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
/**
* @author Sven Efftinge - Initial contribution and API
*/
@FinalFieldsConstructor
@SuppressWarnings("all")
public class ResourceStorageWritable {
private final OutputStream out;
private final boolean storeNodeModel;
public void writeResource(final StorageAwareResource resource) throws IOException {
boolean _isLoadedFromStorage = resource.isLoadedFromStorage();
if (_isLoadedFromStorage) {
URI _uRI = resource.getURI();
String _plus = ("cannot write resources loaded from storage. URI was " + _uRI);
throw new IllegalStateException(_plus);
}
final ZipOutputStream zipOut = new ZipOutputStream(this.out);
try {
this.writeEntries(resource, zipOut);
} finally {
zipOut.close();
}
}
/**
* Write entries into the storage.
* Overriding methods should first delegate to super before adding their own entries.
*/
protected void writeEntries(final StorageAwareResource resource, final ZipOutputStream zipOut) throws IOException {
final BufferedOutputStream bufferedOutput = new BufferedOutputStream(zipOut);
ZipEntry _zipEntry = new ZipEntry("emf-contents");
zipOut.putNextEntry(_zipEntry);
try {
this.writeContents(resource, bufferedOutput);
} finally {
bufferedOutput.flush();
zipOut.closeEntry();
}
ZipEntry _zipEntry_1 = new ZipEntry("resource-description");
zipOut.putNextEntry(_zipEntry_1);
try {
this.writeResourceDescription(resource, bufferedOutput);
} finally {
bufferedOutput.flush();
zipOut.closeEntry();
}
if (this.storeNodeModel) {
ZipEntry _zipEntry_2 = new ZipEntry("node-model");
zipOut.putNextEntry(_zipEntry_2);
try {
this.writeNodeModel(resource, bufferedOutput);
} finally {
bufferedOutput.flush();
zipOut.closeEntry();
}
}
}
protected void writeContents(final StorageAwareResource storageAwareResource, final OutputStream outputStream) throws IOException {
Map<Object, Object> _emptyMap = CollectionLiterals.<Object, Object>emptyMap();
final BinaryResourceImpl.EObjectOutputStream out = new BinaryResourceImpl.EObjectOutputStream(outputStream, _emptyMap) {
@Override
public void writeURI(final URI uri, final String fragment) throws IOException {
final URI fullURI = uri.appendFragment(fragment);
URI _elvis = null;
URI _portableURI = storageAwareResource.getPortableURIs().toPortableURI(storageAwareResource, fullURI);
if (_portableURI != null) {
_elvis = _portableURI;
} else {
_elvis = fullURI;
}
final URI uriToWrite = _elvis;
super.writeURI(uriToWrite.trimFragment(), uriToWrite.fragment());
}
@Override
public void saveEObject(final InternalEObject internalEObject, final BinaryResourceImpl.EObjectOutputStream.Check check) throws IOException {
ResourceStorageWritable.this.beforeSaveEObject(internalEObject, this);
super.saveEObject(internalEObject, check);
ResourceStorageWritable.this.handleSaveEObject(internalEObject, this);
}
};
try {
out.saveResource(storageAwareResource);
} finally {
out.flush();
}
}
protected Object beforeSaveEObject(final InternalEObject object, final BinaryResourceImpl.EObjectOutputStream writable) throws IOException {
return null;
}
protected void handleSaveEObject(final InternalEObject object, final BinaryResourceImpl.EObjectOutputStream out) throws IOException {
}
protected void writeResourceDescription(final StorageAwareResource resource, final OutputStream outputStream) throws IOException {
final IResourceDescription description = resource.getResourceServiceProvider().getResourceDescriptionManager().getResourceDescription(resource);
final SerializableResourceDescription serializableDescription = SerializableResourceDescription.createCopy(description);
this.convertExternalURIsToPortableURIs(serializableDescription, resource);
final ObjectOutputStream out = new ObjectOutputStream(outputStream);
try {
out.writeObject(serializableDescription);
} finally {
out.flush();
}
}
protected void convertExternalURIsToPortableURIs(final SerializableResourceDescription description, final StorageAwareResource resource) {
Iterable<IReferenceDescription> _referenceDescriptions = description.getReferenceDescriptions();
for (final IReferenceDescription ref : _referenceDescriptions) {
URI _trimFragment = ref.getTargetEObjectUri().trimFragment();
URI _uRI = resource.getURI();
boolean _notEquals = (!Objects.equal(_trimFragment, _uRI));
if (_notEquals) {
URI _elvis = null;
URI _portableURI = resource.getPortableURIs().toPortableURI(resource, ref.getTargetEObjectUri());
if (_portableURI != null) {
_elvis = _portableURI;
} else {
URI _targetEObjectUri = ref.getTargetEObjectUri();
_elvis = _targetEObjectUri;
}
((SerializableReferenceDescription) ref).setTargetEObjectUri(_elvis);
}
}
}
protected void writeNodeModel(final StorageAwareResource resource, final OutputStream outputStream) throws IOException {
final DataOutputStream out = new DataOutputStream(outputStream);
final SerializableNodeModel serializableNodeModel = new SerializableNodeModel(resource);
final SerializationConversionContext conversionContext = new SerializationConversionContext(resource);
serializableNodeModel.writeObjectData(out, conversionContext);
out.flush();
}
public ResourceStorageWritable(final OutputStream out, final boolean storeNodeModel) {
super();
this.out = out;
this.storeNodeModel = storeNodeModel;
}
}

View file

@ -1,132 +0,0 @@
/**
* Copyright (c) 2014, 2017 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.resource.persistence;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.HashMap;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtend.lib.annotations.AccessorType;
import org.eclipse.xtend.lib.annotations.Accessors;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.resource.persistence.SerializableEObjectDescriptionProvider;
import org.eclipse.xtext.resource.persistence.SerializationExtensions;
import org.eclipse.xtext.xbase.lib.Conversions;
import org.eclipse.xtext.xbase.lib.Pure;
/**
* @since 2.8
*/
@Accessors
@SuppressWarnings("all")
public class SerializableEObjectDescription implements IEObjectDescription, Externalizable, SerializableEObjectDescriptionProvider {
protected URI eObjectURI;
protected EClass eClass;
protected QualifiedName qualifiedName;
protected HashMap<String, String> userData;
@Accessors(AccessorType.NONE)
protected transient EObject eObjectOrProxy;
public void updateResourceURI(final URI uri) {
this.eObjectURI = uri.appendFragment(this.eObjectURI.fragment());
}
@Override
public EObject getEObjectOrProxy() {
if ((this.eObjectOrProxy == null)) {
final EObject proxy = EcoreUtil.create(this.eClass);
((InternalEObject) proxy).eSetProxyURI(this.eObjectURI);
this.eObjectOrProxy = proxy;
}
return this.eObjectOrProxy;
}
@Override
public QualifiedName getName() {
return this.qualifiedName;
}
@Override
public String getUserData(final String key) {
return this.userData.get(key);
}
@Override
public String[] getUserDataKeys() {
return ((String[])Conversions.unwrapArray(this.userData.keySet(), String.class));
}
@Override
public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
this.eObjectURI = SerializationExtensions.readURI(in);
this.eClass = SerializationExtensions.<EClass>readEcoreElement(in);
this.qualifiedName = SerializationExtensions.readQualifiedName(in);
this.userData = SerializationExtensions.<HashMap<String, String>>readCastedObject(in);
}
@Override
public void writeExternal(final ObjectOutput out) throws IOException {
SerializationExtensions.writeURI(out, this.eObjectURI);
SerializationExtensions.writeEcoreElement(out, this.eClass);
SerializationExtensions.writeQualifiedName(out, this.qualifiedName);
out.writeObject(this.userData);
}
@Override
public SerializableEObjectDescription toSerializableEObjectDescription() {
return this;
}
@Pure
public URI getEObjectURI() {
return this.eObjectURI;
}
public void setEObjectURI(final URI eObjectURI) {
this.eObjectURI = eObjectURI;
}
@Pure
public EClass getEClass() {
return this.eClass;
}
public void setEClass(final EClass eClass) {
this.eClass = eClass;
}
@Pure
public QualifiedName getQualifiedName() {
return this.qualifiedName;
}
public void setQualifiedName(final QualifiedName qualifiedName) {
this.qualifiedName = qualifiedName;
}
@Pure
public HashMap<String, String> getUserData() {
return this.userData;
}
public void setUserData(final HashMap<String, String> userData) {
this.userData = userData;
}
}

View file

@ -1,110 +0,0 @@
/**
* Copyright (c) 2014, 2017 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.resource.persistence;
import com.google.common.base.Objects;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.xtend.lib.annotations.Accessors;
import org.eclipse.xtext.resource.IReferenceDescription;
import org.eclipse.xtext.resource.persistence.SerializationExtensions;
import org.eclipse.xtext.xbase.lib.Pure;
/**
* @since 2.8
*/
@Accessors
@SuppressWarnings("all")
public class SerializableReferenceDescription implements IReferenceDescription, Externalizable {
private URI sourceEObjectUri;
private URI targetEObjectUri;
private URI containerEObjectURI;
private EReference eReference;
private int indexInList;
@Override
public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
this.sourceEObjectUri = SerializationExtensions.readURI(in);
this.targetEObjectUri = SerializationExtensions.readURI(in);
this.containerEObjectURI = SerializationExtensions.readURI(in);
this.eReference = SerializationExtensions.<EReference>readEcoreElement(in);
this.indexInList = in.readInt();
}
@Override
public void writeExternal(final ObjectOutput out) throws IOException {
SerializationExtensions.writeURI(out, this.sourceEObjectUri);
SerializationExtensions.writeURI(out, this.targetEObjectUri);
SerializationExtensions.writeURI(out, this.containerEObjectURI);
SerializationExtensions.writeEcoreElement(out, this.eReference);
out.writeInt(this.indexInList);
}
public void updateResourceURI(final URI newURI, final URI oldURI) {
this.sourceEObjectUri = newURI.appendFragment(this.sourceEObjectUri.fragment());
URI _trimFragment = this.targetEObjectUri.trimFragment();
boolean _equals = Objects.equal(_trimFragment, oldURI);
if (_equals) {
this.targetEObjectUri = newURI.appendFragment(this.targetEObjectUri.fragment());
}
}
@Pure
public URI getSourceEObjectUri() {
return this.sourceEObjectUri;
}
public void setSourceEObjectUri(final URI sourceEObjectUri) {
this.sourceEObjectUri = sourceEObjectUri;
}
@Pure
public URI getTargetEObjectUri() {
return this.targetEObjectUri;
}
public void setTargetEObjectUri(final URI targetEObjectUri) {
this.targetEObjectUri = targetEObjectUri;
}
@Pure
public URI getContainerEObjectURI() {
return this.containerEObjectURI;
}
public void setContainerEObjectURI(final URI containerEObjectURI) {
this.containerEObjectURI = containerEObjectURI;
}
@Pure
public EReference getEReference() {
return this.eReference;
}
public void setEReference(final EReference eReference) {
this.eReference = eReference;
}
@Pure
public int getIndexInList() {
return this.indexInList;
}
public void setIndexInList(final int indexInList) {
this.indexInList = indexInList;
}
}

View file

@ -1,201 +0,0 @@
/**
* Copyright (c) 2014, 2017 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.resource.persistence;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.eclipse.emf.common.util.URI;
import org.eclipse.xtend.lib.annotations.Accessors;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.resource.IReferenceDescription;
import org.eclipse.xtext.resource.IResourceDescription;
import org.eclipse.xtext.resource.impl.AbstractResourceDescription;
import org.eclipse.xtext.resource.persistence.SerializableEObjectDescription;
import org.eclipse.xtext.resource.persistence.SerializableEObjectDescriptionProvider;
import org.eclipse.xtext.resource.persistence.SerializableReferenceDescription;
import org.eclipse.xtext.resource.persistence.SerializationExtensions;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Conversions;
import org.eclipse.xtext.xbase.lib.ExclusiveRange;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.eclipse.xtext.xbase.lib.Pure;
/**
* @author Sven Efftinge - Initial contribution and API
*
* @since 2.8
*/
@Accessors
@SuppressWarnings("all")
public class SerializableResourceDescription extends AbstractResourceDescription implements Externalizable {
public static SerializableResourceDescription createCopy(final IResourceDescription desc) {
SerializableResourceDescription _serializableResourceDescription = new SerializableResourceDescription();
final Procedure1<SerializableResourceDescription> _function = (SerializableResourceDescription it) -> {
it.setURI(desc.getURI());
final Function1<IEObjectDescription, SerializableEObjectDescription> _function_1 = (IEObjectDescription it_1) -> {
return SerializableResourceDescription.createCopy(it_1);
};
it.descriptions = IterableExtensions.<SerializableEObjectDescription>toList(IterableExtensions.<IEObjectDescription, SerializableEObjectDescription>map(desc.getExportedObjects(), _function_1));
final Function1<IReferenceDescription, SerializableReferenceDescription> _function_2 = (IReferenceDescription it_1) -> {
return SerializableResourceDescription.createCopy(it_1);
};
it.references = IterableExtensions.<SerializableReferenceDescription>toList(IterableExtensions.<IReferenceDescription, SerializableReferenceDescription>map(desc.getReferenceDescriptions(), _function_2));
it.importedNames = CollectionLiterals.<QualifiedName>newArrayList(((QualifiedName[])Conversions.unwrapArray(desc.getImportedNames(), QualifiedName.class)));
};
return ObjectExtensions.<SerializableResourceDescription>operator_doubleArrow(_serializableResourceDescription, _function);
}
private static SerializableEObjectDescription createCopy(final IEObjectDescription desc) {
if ((desc instanceof SerializableEObjectDescriptionProvider)) {
return ((SerializableEObjectDescriptionProvider)desc).toSerializableEObjectDescription();
}
SerializableEObjectDescription _serializableEObjectDescription = new SerializableEObjectDescription();
final Procedure1<SerializableEObjectDescription> _function = (SerializableEObjectDescription it) -> {
it.setEClass(desc.getEClass());
it.setEObjectURI(desc.getEObjectURI());
it.qualifiedName = desc.getQualifiedName();
int _size = ((List<String>)Conversions.doWrapArray(desc.getUserDataKeys())).size();
HashMap<String, String> _hashMap = new HashMap<String, String>(_size);
it.userData = _hashMap;
String[] _userDataKeys = desc.getUserDataKeys();
for (final String key : _userDataKeys) {
it.userData.put(key, desc.getUserData(key));
}
};
return ObjectExtensions.<SerializableEObjectDescription>operator_doubleArrow(_serializableEObjectDescription, _function);
}
private static SerializableReferenceDescription createCopy(final IReferenceDescription desc) {
SerializableReferenceDescription _serializableReferenceDescription = new SerializableReferenceDescription();
final Procedure1<SerializableReferenceDescription> _function = (SerializableReferenceDescription it) -> {
it.setSourceEObjectUri(desc.getSourceEObjectUri());
it.setTargetEObjectUri(desc.getTargetEObjectUri());
it.setEReference(desc.getEReference());
it.setIndexInList(desc.getIndexInList());
it.setContainerEObjectURI(desc.getContainerEObjectURI());
};
return ObjectExtensions.<SerializableReferenceDescription>operator_doubleArrow(_serializableReferenceDescription, _function);
}
private List<SerializableEObjectDescription> descriptions = CollectionLiterals.<SerializableEObjectDescription>emptyList();
private List<SerializableReferenceDescription> references = CollectionLiterals.<SerializableReferenceDescription>emptyList();
private List<QualifiedName> importedNames = CollectionLiterals.<QualifiedName>emptyList();
private URI uRI;
public void updateResourceURI(final URI uri) {
for (final SerializableReferenceDescription ref : this.references) {
ref.updateResourceURI(uri, this.uRI);
}
for (final SerializableEObjectDescription desc : this.descriptions) {
desc.updateResourceURI(uri);
}
this.uRI = uri;
}
@Override
protected List<IEObjectDescription> computeExportedObjects() {
return ((List<IEObjectDescription>) ((List<?>) this.descriptions));
}
@Override
public Iterable<QualifiedName> getImportedNames() {
return this.importedNames;
}
@Override
public Iterable<IReferenceDescription> getReferenceDescriptions() {
return ((Iterable<IReferenceDescription>) ((Iterable<?>) this.references));
}
@Override
public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
this.setURI(SerializationExtensions.readURI(in));
final int descriptionsSize = in.readInt();
ArrayList<SerializableEObjectDescription> _arrayList = new ArrayList<SerializableEObjectDescription>(descriptionsSize);
this.descriptions = _arrayList;
ExclusiveRange _doubleDotLessThan = new ExclusiveRange(0, descriptionsSize, true);
for (final Integer i : _doubleDotLessThan) {
this.descriptions.add(SerializationExtensions.<SerializableEObjectDescription>readCastedObject(in));
}
final int referencesSize = in.readInt();
ArrayList<SerializableReferenceDescription> _arrayList_1 = new ArrayList<SerializableReferenceDescription>(referencesSize);
this.references = _arrayList_1;
ExclusiveRange _doubleDotLessThan_1 = new ExclusiveRange(0, referencesSize, true);
for (final Integer i_1 : _doubleDotLessThan_1) {
this.references.add(SerializationExtensions.<SerializableReferenceDescription>readCastedObject(in));
}
final int importedNamesSize = in.readInt();
ArrayList<QualifiedName> _arrayList_2 = new ArrayList<QualifiedName>(importedNamesSize);
this.importedNames = _arrayList_2;
ExclusiveRange _doubleDotLessThan_2 = new ExclusiveRange(0, importedNamesSize, true);
for (final Integer i_2 : _doubleDotLessThan_2) {
this.importedNames.add(SerializationExtensions.readQualifiedName(in));
}
}
@Override
public void writeExternal(final ObjectOutput out) throws IOException {
SerializationExtensions.writeURI(out, this.uRI);
out.writeInt(this.descriptions.size());
for (final SerializableEObjectDescription desc : this.descriptions) {
out.writeObject(desc);
}
out.writeInt(this.references.size());
for (final SerializableReferenceDescription ref : this.references) {
out.writeObject(ref);
}
out.writeInt(this.importedNames.size());
for (final QualifiedName name : this.importedNames) {
SerializationExtensions.writeQualifiedName(out, name);
}
}
@Pure
public List<SerializableEObjectDescription> getDescriptions() {
return this.descriptions;
}
public void setDescriptions(final List<SerializableEObjectDescription> descriptions) {
this.descriptions = descriptions;
}
@Pure
public List<SerializableReferenceDescription> getReferences() {
return this.references;
}
public void setReferences(final List<SerializableReferenceDescription> references) {
this.references = references;
}
public void setImportedNames(final List<QualifiedName> importedNames) {
this.importedNames = importedNames;
}
@Pure
public URI getURI() {
return this.uRI;
}
public void setURI(final URI uRI) {
this.uRI = uRI;
}
}

View file

@ -1,90 +0,0 @@
/**
* Copyright (c) 2014, 2017 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.resource.persistence;
import com.google.common.base.Objects;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.xbase.lib.Exceptions;
/**
* @since 2.8
*/
@SuppressWarnings("all")
public class SerializationExtensions {
public static <T extends ENamedElement> T readEcoreElement(final ObjectInput in) throws IOException {
final URI uri = SerializationExtensions.readURI(in);
final EPackage ePackage = EPackage.Registry.INSTANCE.getEPackage(uri.trimFragment().toString());
Resource _eResource = null;
if (ePackage!=null) {
_eResource=ePackage.eResource();
}
EObject _eObject = null;
if (_eResource!=null) {
_eObject=_eResource.getEObject(uri.fragment());
}
return ((T) _eObject);
}
public static void writeEcoreElement(final ObjectOutput out, final ENamedElement namedElement) throws IOException {
final URI uri = EcoreUtil.getURI(namedElement);
SerializationExtensions.writeURI(out, uri);
}
public static <T extends Object> T readCastedObject(final ObjectInput in) throws IOException {
try {
Object _readObject = in.readObject();
return ((T) _readObject);
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
public static URI readURI(final ObjectInput in) throws IOException {
final String stringRep = in.readUTF();
boolean _equals = Objects.equal(stringRep, "NULL");
if (_equals) {
return null;
}
return URI.createURI(stringRep);
}
public static void writeURI(final ObjectOutput out, final URI uri) throws IOException {
if ((uri == null)) {
out.writeUTF("NULL");
} else {
out.writeUTF(uri.toString());
}
}
public static QualifiedName readQualifiedName(final ObjectInput in) throws IOException {
try {
Object _readObject = in.readObject();
return QualifiedName.create(((ArrayList<String>) _readObject));
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
public static void writeQualifiedName(final ObjectOutput out, final QualifiedName name) throws IOException {
List<String> _segments = name.getSegments();
ArrayList<String> _arrayList = new ArrayList<String>(_segments);
out.writeObject(_arrayList);
}
}