Skip to main content
summaryrefslogtreecommitdiffstats
blob: 02ad58c5f238d039882881fbf356604ff3660f2c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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();
		}
	}

}

Back to the top