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); }
@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); }
@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; }
@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); } }
@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); } }
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(); } }
@Test public void theTest() throws Exception { EPackage.Registry.INSTANCE.put(EcorePackage.eINSTANCE.getNsURI(), EcorePackage.eINSTANCE); Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("ecore", new ResourceFactoryImpl() { @Override public Resource createResource(URI uri) { return new BinaryResourceImpl(uri); } }); Resource resource = new ResourceSetImpl().createResource(URI.createURI("test.ecore")); EPackage thePkg = EcoreFactory.eINSTANCE.createEPackage(); resource.getContents().add(thePkg); thePkg.setName("ThePkg"); EClass lastClass = null; for (int i = 0; i < 10000; i++) { EClass newClass = EcoreFactory.eINSTANCE.createEClass(); newClass.setName("Class" + i); thePkg.getEClassifiers().add(newClass); if (lastClass != null) { EReference ref = EcoreFactory.eINSTANCE.createEReference(); ref.setName("ref"); ref.setEType(newClass); lastClass.getEStructuralFeatures().add(ref); } lastClass = newClass; } resource.save(null); }
protected Object handleLoadEObject(final InternalEObject loaded, final BinaryResourceImpl.EObjectInputStream input) throws IOException { return null; }
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 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)); } }
@Override public Resource createResource(URI uri) { return new BinaryResourceImpl(uri); }
/** * @since 2.9 */ public BinaryResourceImpl.DataConverter<?> create(EDataType eDataType) { return null; }
/** * Creates a prepared {@link ResourceSet} with some registered factories. * Produced resources by extensions are: * <ul> * <li>.xmi uses {@link XMIResourceImpl}</li> * <li>.bin uses {@link BinaryResourceImpl}</li> * <li>.ecore uses {@link XMIResourceImpl}</li> * <li>.* uses {@link XMIResourceImpl}</li> * </ul> * This method can be override in order to change the basic ResourceSet used * by this wrapper. * * @return a configured ResourceSet */ protected static ResourceSet createResourceSet() { ResourceSet result = new ResourceSetImpl(); result.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl()); result.getResourceFactoryRegistry().getExtensionToFactoryMap().put("bin", new ResourceFactoryImpl() { @Override public Resource createResource(URI uri) { return new BinaryResourceImpl(uri); } }); result.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ecore", new EcoreResourceFactoryImpl()); result.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl()); return result; }