org.eclipse.xtext.resource.persistence.ResourceStorageLoadable Java Examples

The following examples show how to use org.eclipse.xtext.resource.persistence.ResourceStorageLoadable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ResourceForIEditorInputFactory.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected void configureResourceSet(ResourceSet resourceSet, URI primaryURI) {
	// TODO: Filter external content - primary resource should not use dirty state
	externalContentSupport.configureResourceSet(resourceSet, externalContentProvider);
	if (!(resourceServiceProvider instanceof IResourceServiceProviderExtension) 
			|| ((IResourceServiceProviderExtension)resourceServiceProvider).isSource(primaryURI)) {
		SourceLevelURIsAdapter.setSourceLevelUris(resourceSet, Collections.singleton(primaryURI));
		resourceSet.eAdapters().add(new ResourceStorageProviderAdapter() {
			
			@Override
			public ResourceStorageLoadable getResourceStorageLoadable(StorageAwareResource resource) {
				if (!dirtyStateManager.hasContent(resource.getURI())) {
					return null;
				}
				return ((DirtyStateManager)dirtyStateManager).getResourceStorageLoadable(resource.getURI());
			}
		});
	}
}
 
Example #2
Source File: DocumentBasedDirtyResource.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.8
 * @nooverride This method is not intended to be re-implemented or extended by clients.
 * @noreference This method is not intended to be referenced by clients.
 */
@Override
public ResourceStorageLoadable getResourceStorageLoadable() {
	Provider<ResourceStorageLoadable> provider = this.storageAwareResourceInputStreamProvider;
	if (provider == null)
		return null;
	return provider.get();
}
 
Example #3
Source File: DirtyStateManager.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.8
 * @noreference This method is not intended to be referenced by clients.
 * @nooverride This method is not intended to be re-implemented or extended by clients.
 */
public ResourceStorageLoadable getResourceStorageLoadable(URI uri) {
	IDirtyResource dirtyResource = findDirtyResourcebyURIorNormalizedURI(uri);
	if (dirtyResource instanceof IDirtyResource.ICurrentStateProvidingExtension) {
		return ((IDirtyResource.ICurrentStateProvidingExtension)dirtyResource).getResourceStorageLoadable();
	}
	return null;
}
 
Example #4
Source File: ResourceStorageTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testConstantValueIsPersisted() {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class C {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("static val CONSTANT = \'a\' + \'b\' + 0");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final String contents = _builder.toString();
    final XtendFile file = this.file(contents);
    final ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ((ResourceStorageFacade) this.resourceStorageFacade).setStoreNodeModel(true);
    Resource _eResource = file.eResource();
    this.resourceStorageFacade.createResourceStorageWritable(bout).writeResource(((StorageAwareResource) _eResource));
    byte[] _byteArray = bout.toByteArray();
    ByteArrayInputStream _byteArrayInputStream = new ByteArrayInputStream(_byteArray);
    final ResourceStorageLoadable in = this.resourceStorageFacade.createResourceStorageLoadable(_byteArrayInputStream);
    Resource _createResource = file.eResource().getResourceSet().createResource(URI.createURI("synthetic:/test/MyClass.xtend"));
    final StorageAwareResource resource = ((StorageAwareResource) _createResource);
    final InMemoryURIConverter converter = new InMemoryURIConverter();
    converter.addModel(resource.getURI().toString(), contents);
    ResourceSet _resourceSet = resource.getResourceSet();
    _resourceSet.setURIConverter(converter);
    EList<Resource> _resources = file.eResource().getResourceSet().getResources();
    _resources.add(resource);
    resource.loadFromStorage(in);
    EObject _get = resource.getContents().get(1);
    final JvmGenericType jvmClass = ((JvmGenericType) _get);
    JvmMember _last = IterableExtensions.<JvmMember>last(jvmClass.getMembers());
    final JvmField field = ((JvmField) _last);
    Assert.assertTrue(field.isConstant());
    Assert.assertTrue(field.isSetConstant());
    Assert.assertEquals("ab0", field.getConstantValue());
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #5
Source File: ResourceStorageTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testConstantValueIsPersisted_01() {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class C {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("static val CONSTANT = Object");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final String contents = _builder.toString();
    final XtendFile file = this.file(contents);
    final ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ((ResourceStorageFacade) this.resourceStorageFacade).setStoreNodeModel(true);
    Resource _eResource = file.eResource();
    this.resourceStorageFacade.createResourceStorageWritable(bout).writeResource(((StorageAwareResource) _eResource));
    byte[] _byteArray = bout.toByteArray();
    ByteArrayInputStream _byteArrayInputStream = new ByteArrayInputStream(_byteArray);
    final ResourceStorageLoadable in = this.resourceStorageFacade.createResourceStorageLoadable(_byteArrayInputStream);
    Resource _createResource = file.eResource().getResourceSet().createResource(URI.createURI("synthetic:/test/MyClass.xtend"));
    final StorageAwareResource resource = ((StorageAwareResource) _createResource);
    final InMemoryURIConverter converter = new InMemoryURIConverter();
    converter.addModel(resource.getURI().toString(), contents);
    ResourceSet _resourceSet = resource.getResourceSet();
    _resourceSet.setURIConverter(converter);
    EList<Resource> _resources = file.eResource().getResourceSet().getResources();
    _resources.add(resource);
    resource.loadFromStorage(in);
    EObject _get = resource.getContents().get(1);
    final JvmGenericType jvmClass = ((JvmGenericType) _get);
    JvmMember _last = IterableExtensions.<JvmMember>last(jvmClass.getMembers());
    final JvmField field = ((JvmField) _last);
    Assert.assertFalse(field.isConstant());
    Assert.assertTrue(field.isSetConstant());
    Assert.assertNull(field.getConstantValue());
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #6
Source File: PortableURIsTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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);
    IResourceStorageFacade _resourceStorageFacade = resourceA.getResourceStorageFacade();
    byte[] _byteArray = bout.toByteArray();
    ByteArrayInputStream _byteArrayInputStream = new ByteArrayInputStream(_byteArray);
    final ResourceStorageLoadable loadable = _resourceStorageFacade.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);
  }
}
 
Example #7
Source File: BatchLinkableResourceStorageFacade.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public ResourceStorageLoadable createResourceStorageLoadable(InputStream in) {
	return new BatchLinkableResourceStorageLoadable(in, isStoreNodeModel());
}
 
Example #8
Source File: ResourceStorageTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testWriteAndLoad() {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("{");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("var x = \"Hello\"");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("var y = \"\"");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("val it = x");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("y = length.toString");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("println(x)");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("y = length.toString");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("println(x)");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("y = length.toString");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("println(x)");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final String contents = _builder.toString();
    final XExpression file = this.expression(contents);
    final ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ((ResourceStorageFacade) this.resourceStorageFacade).setStoreNodeModel(true);
    Resource _eResource = file.eResource();
    this.resourceStorageFacade.createResourceStorageWritable(bout).writeResource(((StorageAwareResource) _eResource));
    byte[] _byteArray = bout.toByteArray();
    ByteArrayInputStream _byteArrayInputStream = new ByteArrayInputStream(_byteArray);
    final ResourceStorageLoadable in = this.resourceStorageFacade.createResourceStorageLoadable(_byteArrayInputStream);
    Resource _createResource = file.eResource().getResourceSet().createResource(URI.createURI("synthetic:/Test.___xbase"));
    final StorageAwareResource resource = ((StorageAwareResource) _createResource);
    final ResourceStorageTest.InMemoryURIConverter converter = new ResourceStorageTest.InMemoryURIConverter();
    converter.addModel(resource.getURI().toString(), contents);
    ResourceSet _resourceSet = resource.getResourceSet();
    _resourceSet.setURIConverter(converter);
    EList<Resource> _resources = file.eResource().getResourceSet().getResources();
    _resources.add(resource);
    resource.loadFromStorage(in);
    EObject _get = resource.getContents().get(0);
    final XExpression root = ((XExpression) _get);
    Assert.assertTrue((root instanceof XBlockExpression));
    final BidiTreeIterator<INode> restoredNodes = NodeModelUtils.findActualNodeFor(IterableExtensions.<EObject>head(resource.getContents())).getAsTreeIterable().iterator();
    final BidiTreeIterator<INode> originalNodes = NodeModelUtils.findActualNodeFor(file).getAsTreeIterable().iterator();
    while (restoredNodes.hasNext()) {
      {
        final INode rest = restoredNodes.next();
        final INode orig = originalNodes.next();
        Assert.assertEquals(orig.getStartLine(), rest.getStartLine());
        Assert.assertEquals(orig.getEndLine(), rest.getEndLine());
        Assert.assertEquals(orig.getOffset(), rest.getOffset());
        Assert.assertEquals(orig.getEndOffset(), rest.getEndOffset());
        Assert.assertEquals(orig.getLength(), rest.getLength());
        Assert.assertEquals(orig.getTotalStartLine(), rest.getTotalStartLine());
        Assert.assertEquals(orig.getTotalEndLine(), rest.getTotalEndLine());
        Assert.assertEquals(orig.getTotalOffset(), rest.getTotalOffset());
        Assert.assertEquals(orig.getTotalEndOffset(), rest.getTotalEndOffset());
        Assert.assertEquals(orig.getTotalLength(), rest.getTotalLength());
        Assert.assertSame(orig.getGrammarElement(), rest.getGrammarElement());
        Assert.assertTrue((((orig.getSemanticElement() != null) && (rest.getSemanticElement() != null)) || ((orig.getSemanticElement() == null) && (rest.getSemanticElement() == null))));
        EObject _semanticElement = orig.getSemanticElement();
        boolean _tripleNotEquals = (_semanticElement != null);
        if (_tripleNotEquals) {
          Assert.assertEquals(file.eResource().getURIFragment(orig.getSemanticElement()), resource.getURIFragment(rest.getSemanticElement()));
        }
        Assert.assertEquals(orig.getText(), rest.getText());
      }
    }
    Assert.assertFalse(originalNodes.hasNext());
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #9
Source File: DocumentBasedDirtyResource.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @since 2.8
 */
public void setResourceStorageLoadableProvider(Provider<ResourceStorageLoadable> provider) {
	this.storageAwareResourceInputStreamProvider = provider;
}
 
Example #10
Source File: DirtyStateEditorSupport.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public ResourceStorageLoadable getResourceStorageLoadable() {
	return dirtyResource.getResourceStorageLoadable();
}
 
Example #11
Source File: DirtyStateEditorSupport.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @since 2.7
 */
public void announceDirtyState(XtextResource resource) {
	if (resource == null || !dirtyResource.isInitialized())
		return;
	ClientAwareDirtyResource clientAwareResource = delegatingClientAwareResource;
	if (state != State.CLEAN || ((!resource.isTrackingModification() || resource.isModified()) && clientAwareResource.isDirty() && dirtyStateManager.manageDirtyState(clientAwareResource))) {
		synchronized (dirtyStateManager) {
			Manager resourceDescriptionManager = getResourceDescriptionManagerIfOwnLanguage(resource);
			if (resourceDescriptionManager != null) {
				final IResourceDescription newDescription = resourceDescriptionManager.getResourceDescription(resource);
				if (state == State.SHOULD_UPDATE || haveEObjectDescriptionsChanged(newDescription, resourceDescriptionManager)) {
					if(state == State.SHOULD_UPDATE) {
						if(clientAwareResource.isDirty())
							state = State.DIRTY;
						else 
							state = State.CLEAN;
					}
					IResourceDescription oldDescription = dirtyResource.getDescription();
					dirtyResource.copyState(newDescription);
					if (resoureStorageFacade != null && (resource instanceof StorageAwareResource)) {
						try {
							StorageAwareResource storageAwareResource = (StorageAwareResource) resource;
							class MyByteArrayOutputStream extends ByteArrayOutputStream {
								@Override
								public synchronized byte[] toByteArray() {
									return buf;
								}
								public int length() { 
									return count;
								}
							}
							final MyByteArrayOutputStream bout = new MyByteArrayOutputStream();
							ResourceStorageWritable resourceOutputStream = resoureStorageFacade.createResourceStorageWritable(bout);
							resourceOutputStream.writeResource(storageAwareResource);
							dirtyResource.setResourceStorageLoadableProvider(new Provider<ResourceStorageLoadable>() {
								@Override
								public ResourceStorageLoadable get() {
									return resoureStorageFacade.createResourceStorageLoadable(new ByteArrayInputStream(bout.toByteArray(), 0 , bout.length()));
								}
							});
						} catch(IOException e) {
							// something went wrong when writing the resource - stream's content is bogus and not attached to the dirty resource info
							LOG.warn("Cannot persist storage for " + resource.getURI(), e);
						}
					}
					if (dirtyStateManager instanceof DirtyStateManager) {
						((DirtyStateManager)dirtyStateManager).announceDirtyStateChanged(oldDescription, clientAwareResource);
					} else {
						dirtyStateManager.announceDirtyStateChanged(clientAwareResource);
					}
				}
			}
		}
	}
}
 
Example #12
Source File: ResourceStorageTest.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testWriteAndLoad() {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package foo");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class Bar {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def dispatch myMethod(String s) {}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("* Hello myMethod ");
    _builder.newLine();
    _builder.append("\t ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def dispatch myMethod(CharSequence cs) {}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.append("class Foo {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def dispatch other(String it) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("var x = \"\"");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("x = length.toString");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("println(x)");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final String contents = _builder.toString();
    final XtendFile file = this.file(contents);
    final ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ((ResourceStorageFacade) this.resourceStorageFacade).setStoreNodeModel(true);
    Resource _eResource = file.eResource();
    this.resourceStorageFacade.createResourceStorageWritable(bout).writeResource(((StorageAwareResource) _eResource));
    byte[] _byteArray = bout.toByteArray();
    ByteArrayInputStream _byteArrayInputStream = new ByteArrayInputStream(_byteArray);
    final ResourceStorageLoadable in = this.resourceStorageFacade.createResourceStorageLoadable(_byteArrayInputStream);
    Resource _createResource = file.eResource().getResourceSet().createResource(URI.createURI("synthetic:/test/MyClass.xtend"));
    final StorageAwareResource resource = ((StorageAwareResource) _createResource);
    final InMemoryURIConverter converter = new InMemoryURIConverter();
    converter.addModel(resource.getURI().toString(), contents);
    ResourceSet _resourceSet = resource.getResourceSet();
    _resourceSet.setURIConverter(converter);
    EList<Resource> _resources = file.eResource().getResourceSet().getResources();
    _resources.add(resource);
    resource.loadFromStorage(in);
    EObject _get = resource.getContents().get(1);
    final JvmGenericType jvmClass = ((JvmGenericType) _get);
    Assert.assertEquals("java.lang.CharSequence", IterableExtensions.<JvmFormalParameter>head((((JvmOperation[])Conversions.unwrapArray(jvmClass.getDeclaredOperations(), JvmOperation.class))[2]).getParameters()).getParameterType().getQualifiedName());
    Assert.assertEquals("java.lang.Object", (((JvmOperation[])Conversions.unwrapArray(jvmClass.getDeclaredOperations(), JvmOperation.class))[2]).getReturnType().getQualifiedName());
    Assert.assertEquals("Hello myMethod", IterableExtensions.<DocumentationAdapter>head(Iterables.<DocumentationAdapter>filter((((JvmOperation[])Conversions.unwrapArray(jvmClass.getDeclaredOperations(), JvmOperation.class))[1]).eAdapters(), DocumentationAdapter.class)).getDocumentation());
    Assert.assertEquals(resource.getURI(), resource.getResourceDescription().getURI());
    Assert.assertEquals(2, IterableExtensions.size(resource.getResourceDescription().getExportedObjects()));
    Assert.assertEquals("foo.Bar", IterableExtensions.<IEObjectDescription>head(resource.getResourceDescription().getExportedObjects()).getQualifiedName().toString());
    final BidiTreeIterator<INode> restoredNodes = NodeModelUtils.findActualNodeFor(IterableExtensions.<EObject>head(resource.getContents())).getAsTreeIterable().iterator();
    final BidiTreeIterator<INode> originalNodes = NodeModelUtils.findActualNodeFor(file).getAsTreeIterable().iterator();
    while (restoredNodes.hasNext()) {
      {
        final INode rest = restoredNodes.next();
        final INode orig = originalNodes.next();
        Assert.assertEquals(orig.getStartLine(), rest.getStartLine());
        Assert.assertEquals(orig.getEndLine(), rest.getEndLine());
        Assert.assertEquals(orig.getOffset(), rest.getOffset());
        Assert.assertEquals(orig.getEndOffset(), rest.getEndOffset());
        Assert.assertEquals(orig.getLength(), rest.getLength());
        Assert.assertEquals(orig.getTotalStartLine(), rest.getTotalStartLine());
        Assert.assertEquals(orig.getTotalEndLine(), rest.getTotalEndLine());
        Assert.assertEquals(orig.getTotalOffset(), rest.getTotalOffset());
        Assert.assertEquals(orig.getTotalEndOffset(), rest.getTotalEndOffset());
        Assert.assertEquals(orig.getTotalLength(), rest.getTotalLength());
        Assert.assertSame(orig.getGrammarElement(), rest.getGrammarElement());
        Assert.assertEquals(file.eResource().getURIFragment(orig.getSemanticElement()), resource.getURIFragment(rest.getSemanticElement()));
        Assert.assertEquals(orig.getText(), rest.getText());
      }
    }
    Assert.assertFalse(originalNodes.hasNext());
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #13
Source File: DirectLinkingResourceStorageFacade.java    From dsl-devkit with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public ResourceStorageLoadable createResourceStorageLoadable(final InputStream in) {
  return new DirectLinkingResourceStorageLoadable(in, isStoreNodeModel(), traceSet);
}
 
Example #14
Source File: IDirtyResource.java    From xtext-eclipse with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * @noreference This method is not intended to be referenced by clients.
 * @nooverride This default method is not intended to be re-implemented or extended by clients.
 */
public ResourceStorageLoadable getResourceStorageLoadable();