org.eclipse.emf.ecore.resource.impl.BinaryResourceImpl Java Examples

The following examples show how to use org.eclipse.emf.ecore.resource.impl.BinaryResourceImpl. 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: ResourceStorageLoadable.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
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);
}
 
Example #2
Source File: ResourceStorageWritable.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
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();
	}
}
 
Example #3
Source File: QualifiedNameTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testDeserializeAsLowerCase() throws IOException {
	QualifiedName upperCase = QualifiedName.create("A", "B");
	QualifiedName lowerCase = upperCase.toLowerCase();
	
	ByteArrayOutputStream bos = new ByteArrayOutputStream();
	EObjectOutputStream out = new BinaryResourceImpl.EObjectOutputStream(bos, Collections.emptyMap());
	upperCase.writeToStream(out);
	lowerCase.writeToStream(out);
	out.flush();
	
	EObjectInputStream in = new BinaryResourceImpl.EObjectInputStream(new ByteArrayInputStream(bos.toByteArray()), Collections.emptyMap());
	QualifiedName readUpperCase = QualifiedName.createFromStream(in);
	QualifiedName readLowerCase = QualifiedName.createFromStream(in);
	assertEquals(QualifiedName.class.getName(), readUpperCase.getClass().getName());
	assertEquals(QualifiedName.class.getName() + "$QualifiedNameLowerCase", readLowerCase.getClass().getName());
	assertEquals(upperCase, readUpperCase);
	assertEquals(lowerCase, readLowerCase);
}
 
Example #4
Source File: BatchLinkableResourceStorageWritable.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected Object beforeSaveEObject(final InternalEObject object, final BinaryResourceImpl.EObjectOutputStream writable) throws IOException {
  JvmType _xblockexpression = null;
  {
    super.beforeSaveEObject(object, writable);
    JvmType _xifexpression = null;
    if ((object instanceof XComputedTypeReference)) {
      _xifexpression = ((XComputedTypeReference)object).getType();
    }
    _xblockexpression = _xifexpression;
  }
  return _xblockexpression;
}
 
Example #5
Source File: BatchLinkableResourceStorageWritable.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void handleSaveEObject(final InternalEObject object, final BinaryResourceImpl.EObjectOutputStream out) throws IOException {
  super.handleSaveEObject(object, out);
  DocumentationAdapter documentationAdapter = null;
  JvmIdentifiableMetaData metaDataAdapter = null;
  EList<Adapter> _eAdapters = object.eAdapters();
  for (final Adapter adapter : _eAdapters) {
    {
      if ((adapter instanceof DocumentationAdapter)) {
        documentationAdapter = ((DocumentationAdapter)adapter);
      }
      if ((adapter instanceof JvmIdentifiableMetaData)) {
        metaDataAdapter = ((JvmIdentifiableMetaData)adapter);
      }
    }
  }
  if ((documentationAdapter != null)) {
    out.writeBoolean(true);
    out.writeString(documentationAdapter.getDocumentation());
  } else {
    out.writeBoolean(false);
  }
  if ((metaDataAdapter != null)) {
    out.writeBoolean(true);
    out.writeBoolean(metaDataAdapter.isSynthetic());
  } else {
    out.writeBoolean(false);
  }
}
 
Example #6
Source File: GrammarAccessFragment.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void generate(Grammar grammar, XpandExecutionContext ctx) {
	RuleNames.ensureAdapterInstalled(grammar);
	super.generate(grammar, ctx);

	final ResourceSaveIndicator isSaving = new ResourceSaveIndicator();
	// create a defensive clone
	Grammar copy = deepCopy(grammar, isSaving);
	ResourceSet set = copy.eResource().getResourceSet();

	// save grammar model
	String path;
	if (xmlVersion == null) {
		path = GrammarUtil.getClasspathRelativePathToBinGrammar(copy);
	} else {
		log.warn("'xmlVersion' has been specified for this "
				+ GrammarAccessFragment.class.getSimpleName()
				+ ". Therefore, the grammar is persisted as XMI and not as binary. This can be a performance drawback.");
		path = GrammarUtil.getClasspathRelativePathToXmi(copy);
	}
	URI uri = URI.createURI(ctx.getOutput().getOutlet(Generator.SRC_GEN).getPath() + "/" + path);
	Resource resource = set.createResource(uri, ContentHandler.UNSPECIFIED_CONTENT_TYPE);
	addAllGrammarsToResource(resource, copy, new HashSet<Grammar>());
	isSaving.set(Boolean.TRUE);
	Map<String, Object> saveOptions = Maps.newHashMap();
	if (resource instanceof XMLResource) {
		((XMLResource) resource).setXMLVersion(getXmlVersion());
	} else if (resource instanceof BinaryResourceImpl){
		saveOptions.put(BinaryResourceImpl.OPTION_VERSION, BinaryResourceImpl.BinaryIO.Version.VERSION_1_1);
		saveOptions.put(BinaryResourceImpl.OPTION_STYLE_DATA_CONVERTER, Boolean.TRUE);
	}
	try {
		resource.save(saveOptions);
	} catch (IOException e) {
		log.error(e.getMessage(), e);
	} finally {
		isSaving.set(Boolean.FALSE);
	}
}
 
Example #7
Source File: BinaryGuillemetBugfixTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testEncodeGuillemet() throws IOException {
	String s = "�";
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	new BinaryResourceImpl.EObjectOutputStream(out, null).writeString(s);
	byte[] byteArray = out.toByteArray();
	ByteArrayInputStream in = new ByteArrayInputStream(byteArray);
	String newS = new BinaryResourceImpl.EObjectInputStream(in, null) {
		@Override
		public String readString() throws IOException {
			int length = readCompressedInt();
			if (length == -1) {
				return null;
			} else {
				if (characters == null || characters.length < length) {
					characters = new char[length];
				}
				LOOP: for (int i = 0; i < length; ++i) {
					byte value = readByte();
					if (value == 0) {
						do {
							characters[i] = readChar();
						} while (++i < length);
						break LOOP;
					} else {
						// here's the fix
						char charValue = (char) (value & 0xff);
						characters[i] = charValue;
					}
				}
				return new String(characters, 0, length);
			}
		}

	}.readString();
	assertEquals(s, newS);
}
 
Example #8
Source File: DirectLinkingResourceStorageLoadable.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void readContents(final StorageAwareResource resource, final InputStream inputStream) throws IOException {
  // Implementation is copied over from org.eclipse.xtext.resource.persistence.ResourceStorageLoadable
  // The only difference is that the stream overrides 'loadFeatureValue' to add some error logging
  final BinaryResourceImpl.EObjectInputStream in = 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 {
      final InternalEObject result = super.loadEObject();
      handleLoadEObject(result, this);
      return result;
    }

    @Override
    protected void loadFeatureValue(final InternalEObject internalEObject, final EStructuralFeatureData eStructuralFeatureData) throws IOException {
      try {
        super.loadFeatureValue(internalEObject, eStructuralFeatureData);
        // CHECKSTYLE:OFF
      } catch (Exception e) {
        StringBuilder infoMessage = new StringBuilder(100);
        // CHECKSTYLE:ON
        infoMessage.append("Failed to load feature's value. Owner: ").append(internalEObject.eClass()); //$NON-NLS-1$
        if (eStructuralFeatureData.eStructuralFeature != null) {
          infoMessage.append(", feature name: ").append(eStructuralFeatureData.eStructuralFeature.getName()); //$NON-NLS-1$
        }
        LOG.info(infoMessage);
        throw e;
      }
    }
  };
  in.loadResource(resource);
}
 
Example #9
Source File: ResourceStorageLoadable.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected Object handleLoadEObject(InternalEObject loaded, BinaryResourceImpl.EObjectInputStream input)
		throws IOException {
	return null;
}
 
Example #10
Source File: ResourceStorageWritable.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected Object beforeSaveEObject(InternalEObject object, BinaryResourceImpl.EObjectOutputStream writable)
		throws IOException {
	return null;
}
 
Example #11
Source File: ResourceStorageWritable.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected void handleSaveEObject(InternalEObject object, BinaryResourceImpl.EObjectOutputStream out)
		throws IOException {
}
 
Example #12
Source File: GrammarAccessFragment2.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected void writeGrammar() {
  final Wrapper<Boolean> isSaving = Wrapper.<Boolean>wrap(Boolean.valueOf(false));
  final ResourceSet cloneInto = new ResourceSetImpl();
  Map<String, Object> _extensionToFactoryMap = cloneInto.getResourceFactoryRegistry().getExtensionToFactoryMap();
  FragmentFakingEcoreResource.FactoryImpl _factoryImpl = new FragmentFakingEcoreResource.FactoryImpl(isSaving);
  _extensionToFactoryMap.put(
    FragmentFakingEcoreResource.FactoryImpl.ECORE_SUFFIX, _factoryImpl);
  final ResourceSet resourceSet = EcoreUtil2.<ResourceSet>clone(cloneInto, this.getLanguage().getGrammar().eResource().getResourceSet());
  EObject _head = IterableExtensions.<EObject>head(resourceSet.getResource(this.getLanguage().getGrammar().eResource().getURI(), true).getContents());
  final Grammar copy = ((Grammar) _head);
  String _xifexpression = null;
  if ((this.xmlVersion == null)) {
    _xifexpression = GrammarUtil.getClasspathRelativePathToBinGrammar(copy);
  } else {
    String _xblockexpression = null;
    {
      String _simpleName = GrammarAccessFragment2.class.getSimpleName();
      String _plus = ("The property \'xmlVersion\' has been specified for this " + _simpleName);
      String _plus_1 = (_plus + ". Therefore, the grammar is persisted as XMI and not as binary. This can be a performance drawback.");
      GrammarAccessFragment2.LOG.warn(_plus_1);
      _xblockexpression = GrammarUtil.getClasspathRelativePathToXmi(copy);
    }
    _xifexpression = _xblockexpression;
  }
  final String path = _xifexpression;
  final URI uri = this.getProjectConfig().getRuntime().getSrcGen().getURI(path);
  final Resource resource = resourceSet.createResource(uri, ContentHandler.UNSPECIFIED_CONTENT_TYPE);
  HashSet<Grammar> _hashSet = new HashSet<Grammar>();
  this.addAllGrammarsToResource(resource, copy, _hashSet);
  isSaving.set(Boolean.valueOf(true));
  final Map<String, Object> saveOptions = CollectionLiterals.<String, Object>newHashMap();
  if ((resource instanceof XMLResource)) {
    String _elvis = null;
    if (this.xmlVersion != null) {
      _elvis = this.xmlVersion;
    } else {
      _elvis = "1.0";
    }
    ((XMLResource)resource).setXMLVersion(_elvis);
  } else {
    if ((resource instanceof BinaryResourceImpl)) {
      saveOptions.put(BinaryResourceImpl.OPTION_VERSION, BinaryResourceImpl.BinaryIO.Version.VERSION_1_1);
      saveOptions.put(BinaryResourceImpl.OPTION_STYLE_DATA_CONVERTER, Boolean.valueOf(true));
    }
  }
  try {
    resource.save(saveOptions);
  } catch (final Throwable _t) {
    if (_t instanceof IOException) {
      final IOException e = (IOException)_t;
      GrammarAccessFragment2.LOG.error(e.getMessage(), e);
    } else {
      throw Exceptions.sneakyThrow(_t);
    }
  } finally {
    isSaving.set(Boolean.valueOf(false));
  }
}