-
Notifications
You must be signed in to change notification settings - Fork 642
Expand file tree
/
Copy pathEmfSerializer.java
More file actions
139 lines (119 loc) · 4.26 KB
/
EmfSerializer.java
File metadata and controls
139 lines (119 loc) · 4.26 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package org.bimserver.plugins.serializers;
/******************************************************************************
* Copyright (C) 2009-2019 BIMserver.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see {@literal<http://www.gnu.org/licenses/>}.
*****************************************************************************/
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import org.bimserver.emf.IdEObject;
import org.bimserver.emf.IdEObjectImpl;
import org.bimserver.emf.IfcModelInterface;
import org.bimserver.emf.PackageMetaData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class EmfSerializer implements Serializer, StreamingReader {
private static final Logger LOGGER = LoggerFactory.getLogger(EmfSerializer.class);
protected IfcModelInterface model;
private Mode mode = Mode.HEADER;
private ProjectInfo projectInfo;
private boolean normalizeOids;
private long expressIdCounter = 1;
protected static enum Mode {
HEADER, BODY, FOOTER, FINISHED
}
public void init(IfcModelInterface model, ProjectInfo projectInfo, boolean normalizeOids) throws SerializerException {
this.model = model;
this.projectInfo = projectInfo;
this.normalizeOids = normalizeOids;
}
public PackageMetaData getPackageMetaData() {
return model.getPackageMetaData();
}
public ProjectInfo getProjectInfo() {
return projectInfo;
}
protected Mode getMode() {
return mode;
}
public boolean isNormalizeOids() {
return normalizeOids;
}
protected void setMode(Mode mode) {
this.mode = mode;
}
protected long getExpressId(IdEObject object) {
if (normalizeOids && object.getExpressId() == -1) {
((IdEObjectImpl)object).setExpressId(expressIdCounter ++);
}
return object.getExpressId();
}
public byte[] getBytes() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
writeToOutputStream(outputStream, null);
} catch (SerializerException e) {
LOGGER.error("", e);
}
return outputStream.toByteArray();
}
public InputStream getInputStream() throws IOException {
return new SerializerInputstream(this);
}
/*
* The serializer must implement this method and write data to the
* outputstream. This call can be called multiple times by the BIMserver.
* The implementation must return true when data has been written, or false
* when no data has been written (this will stop the serialization).
*/
protected abstract boolean write(OutputStream outputStream, ProgressReporter progressReporter) throws SerializerException;
public void writeToOutputStream(OutputStream outputStream, ProgressReporter progressReporter) throws SerializerException {
boolean result = write(outputStream, progressReporter);
while (result) {
result = write(outputStream, progressReporter);
}
if(progressReporter!=null) progressReporter.update(1, 1);
}
public void writeToFile(Path file, ProgressReporter progressReporter) throws SerializerException {
try {
OutputStream outputStream = Files.newOutputStream(file);
try {
writeToOutputStream(outputStream, progressReporter);
} finally {
outputStream.close();
}
} catch (FileNotFoundException e) {
LOGGER.error("", e);
} catch (IOException e) {
LOGGER.error("", e);
}
}
public IfcModelInterface getModel() {
return model;
}
@Override
public boolean write(OutputStream out) {
try {
return write(out, null);
} catch (SerializerException e) {
LOGGER.error("", e);
}
return false;
}
}