| package org.eclipse.osbp.xtext.oxtype.resource.persistence; |
| |
| import java.io.IOException; |
| import java.io.OutputStream; |
| import java.util.ArrayList; |
| import java.util.List; |
| import java.util.function.Predicate; |
| |
| 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.emf.ecore.resource.impl.BinaryResourceImpl.EObjectOutputStream; |
| import org.eclipse.emf.ecore.util.InternalEList; |
| import org.eclipse.xtext.resource.persistence.StorageAwareResource; |
| import org.eclipse.xtext.xbase.lib.CollectionLiterals; |
| import org.eclipse.xtext.xbase.resource.BatchLinkableResourceStorageWritable; |
| |
| @SuppressWarnings("restriction") |
| public class FilterResourceStorageWritable extends BatchLinkableResourceStorageWritable { |
| |
| final Predicate<EObject> filter; |
| |
| public FilterResourceStorageWritable(OutputStream out, boolean storeNodeModel, Predicate<EObject> filter) { |
| super(out, storeNodeModel); |
| this.filter = filter; |
| } |
| |
| protected void writeContents(StorageAwareResource storageAwareResource, OutputStream outputStream) |
| throws IOException { |
| EObjectOutputStream out = new BinaryResourceImpl.EObjectOutputStream(outputStream, |
| CollectionLiterals.emptyMap()) { |
| |
| public void writeURI(URI uri, String fragment) throws IOException { |
| URI fullURI = uri.appendFragment(fragment); |
| URI uriToWrite = storageAwareResource.getPortableURIs().toPortableURI(storageAwareResource, fullURI); |
| if (uriToWrite == null) { |
| uriToWrite = fullURI; |
| } |
| super.writeURI(uriToWrite.trimFragment(), uriToWrite.fragment()); |
| } |
| |
| public void saveEObject(InternalEObject internalEObject, Check check) throws IOException { |
| beforeSaveEObject(internalEObject, this); |
| super.saveEObject(internalEObject, check); |
| handleSaveEObject(internalEObject, this); |
| } |
| |
| public void saveEObjects(InternalEList<? extends InternalEObject> internalEObjects, Check check) |
| throws IOException { |
| |
| List<InternalEObject> targets = new ArrayList<>(); |
| for(InternalEObject internal : internalEObjects) { |
| if(filter.test(internal)) { |
| targets.add(internal); |
| } |
| } |
| |
| int size = targets.size(); |
| InternalEObject[] values = allocateInternalEObjectArray(size); |
| |
| for (int i = 0; i < targets.size(); i++) { |
| InternalEObject internalEObject = targets.get(i); |
| values[i] = internalEObject; |
| } |
| |
| writeCompressedInt(size); |
| for (int i = 0; i < size; ++i) { |
| InternalEObject internalEObject = values[i]; |
| saveEObject(internalEObject, check); |
| } |
| recycle(values); |
| } |
| |
| }; |
| try { |
| out.saveResource(storageAwareResource); |
| } finally { |
| out.flush(); |
| } |
| } |
| |
| } |