Search in sources :

Example 1 with ResourceStorageWritable

use of org.eclipse.xtext.resource.persistence.ResourceStorageWritable in project xtext-core by eclipse.

the class PortableURIsTest method testPortableReferenceDescriptions.

@Test
public void testPortableReferenceDescriptions() {
    try {
        final XtextResourceSet resourceSet = this.<XtextResourceSet>get(XtextResourceSet.class);
        Resource _createResource = resourceSet.createResource(URI.createURI("hubba:/bubba.langatestlanguage"));
        final StorageAwareResource resourceA = ((StorageAwareResource) _createResource);
        Resource _createResource_1 = resourceSet.createResource(URI.createURI("hubba:/bubba2.langatestlanguage"));
        final StorageAwareResource resourceB = ((StorageAwareResource) _createResource_1);
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("type B");
        _builder.newLine();
        resourceB.load(this.getAsStream(_builder.toString()), null);
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("import \'hubba:/bubba2.langatestlanguage\'");
        _builder_1.newLine();
        _builder_1.newLine();
        _builder_1.append("type A extends B");
        _builder_1.newLine();
        resourceA.load(this.getAsStream(_builder_1.toString()), null);
        final ByteArrayOutputStream bout = new ByteArrayOutputStream();
        final ResourceStorageWritable writable = resourceA.getResourceStorageFacade().createResourceStorageWritable(bout);
        writable.writeResource(resourceA);
        byte[] _byteArray = bout.toByteArray();
        ByteArrayInputStream _byteArrayInputStream = new ByteArrayInputStream(_byteArray);
        final ResourceStorageLoadable loadable = resourceA.getResourceStorageFacade().createResourceStorageLoadable(_byteArrayInputStream);
        Resource _createResource_2 = resourceSet.createResource(URI.createURI("hubba:/bubba3.langatestlanguage"));
        final StorageAwareResource resourceC = ((StorageAwareResource) _createResource_2);
        resourceC.loadFromStorage(loadable);
        final IReferenceDescription refDesc = IterableExtensions.<IReferenceDescription>head(resourceC.getResourceDescription().getReferenceDescriptions());
        EObject _head = IterableExtensions.<EObject>head(resourceB.getContents());
        Assert.assertSame(IterableExtensions.<Type>head(((Main) _head).getTypes()), resourceSet.getEObject(refDesc.getTargetEObjectUri(), false));
        EObject _head_1 = IterableExtensions.<EObject>head(resourceC.getContents());
        Assert.assertSame(IterableExtensions.<Type>head(((Main) _head_1).getTypes()), resourceSet.getEObject(refDesc.getSourceEObjectUri(), false));
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : Resource(org.eclipse.emf.ecore.resource.Resource) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ResourceStorageWritable(org.eclipse.xtext.resource.persistence.ResourceStorageWritable) ResourceStorageLoadable(org.eclipse.xtext.resource.persistence.ResourceStorageLoadable) ByteArrayInputStream(java.io.ByteArrayInputStream) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Main(org.eclipse.xtext.linking.langATestLanguage.Main) IReferenceDescription(org.eclipse.xtext.resource.IReferenceDescription) Test(org.junit.Test)

Example 2 with ResourceStorageWritable

use of org.eclipse.xtext.resource.persistence.ResourceStorageWritable in project xtext-core by eclipse.

the class ResourceStorageFacade method saveResource.

@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);
}
Also used : IResourceStorageFacade(org.eclipse.xtext.resource.persistence.IResourceStorageFacade) ByteArrayInputStream(java.io.ByteArrayInputStream) ResourceStorageWritable(org.eclipse.xtext.resource.persistence.ResourceStorageWritable) IOException(java.io.IOException) URI(org.eclipse.emf.common.util.URI)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 ResourceStorageWritable (org.eclipse.xtext.resource.persistence.ResourceStorageWritable)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 URI (org.eclipse.emf.common.util.URI)1 EObject (org.eclipse.emf.ecore.EObject)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)1 Main (org.eclipse.xtext.linking.langATestLanguage.Main)1 IReferenceDescription (org.eclipse.xtext.resource.IReferenceDescription)1 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)1 IResourceStorageFacade (org.eclipse.xtext.resource.persistence.IResourceStorageFacade)1 ResourceStorageLoadable (org.eclipse.xtext.resource.persistence.ResourceStorageLoadable)1 StorageAwareResource (org.eclipse.xtext.resource.persistence.StorageAwareResource)1 Test (org.junit.Test)1