-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathETSLoader.java
More file actions
394 lines (331 loc) · 15.1 KB
/
ETSLoader.java
File metadata and controls
394 lines (331 loc) · 15.1 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package org.knx;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.Unmarshaller.Listener;
import org.knx.xml.BaseClass;
import org.knx.xml.KNX;
import org.knx.xml.KnxApplicationProgramT;
import org.knx.xml.KnxManufacturerDataT;
import org.knx.xml.KnxManufacturerDataT.KnxManufacturer;
import org.knx.xml.KnxManufacturerDataT.KnxManufacturer.KnxApplicationPrograms;
import org.knx.xml.KnxManufacturerDataT.KnxManufacturer.KnxBaggages;
import org.knx.xml.KnxManufacturerDataT.KnxManufacturer.KnxCatalog;
import org.knx.xml.KnxManufacturerDataT.KnxManufacturer.KnxHardware;
import org.knx.xml.KnxProjectT;
import org.knx.xml.KnxProjectT.KnxInstallations;
import org.knx.xml.KnxProjectT.KnxInstallations.KnxInstallation;
import org.slf4j.LoggerFactory;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.io.inputstream.ZipInputStream;
import net.lingala.zip4j.model.FileHeader;
import net.lingala.zip4j.util.FileUtils;
public class ETSLoader
{
protected final org.slf4j.Logger LOG = LoggerFactory.getLogger(this.getClass());
private static final String TEMP_FILE_PREFIX = "knx-project";
private static final String ZIP_EXTENSION = "zip";
private static final String MASTER_DATA_FILE = "knx_master.xml";
private static final String MANUFACTURER_DIR_PREFIX = "M-";
private static final String BAGGAGES_FILE = "Baggages.xml";
private static final String HARDWARE_FILE = "Hardware.xml";
private static final String CATALOG_FILE = "Catalog.xml";
private static final String PROJECT_ZIP_FILE_PREFIX = "P-";
private static final String MAIN_PROJECT_FILE = "project.xml";
public KNX load(final File file, final Optional<String> password) throws ETSLoaderException
{
ZipFile projectZipFile = new ZipFile(file);
LookupIdResolver idResolver = new LookupIdResolver();
LOG.debug("Loading Master Data");
KNX knx = loadMasterData(projectZipFile, idResolver);
LOG.debug("Loading Manufacturer Data");
KnxManufacturerDataT manufacturerData = new KnxManufacturerDataT();
loadManufacturerData(projectZipFile, manufacturerData, idResolver);
reconnect(manufacturerData, knx, KNX::setManufacturerData, idResolver);
LOG.debug("Loading Projects");
loadProjects(password, projectZipFile, knx, idResolver);
return knx;
}
private <T extends BaseClass, P extends BaseClass> void reconnect(final T element, final P newParent,
final BiConsumer<P, T> adder, final LookupIdResolver idResolver)
{
if (element.getParent() != null)
{
idResolver.purgeSuperHierarchy(element.getParent());
}
adder.accept(newParent, element);
element.setParent(newParent);
}
/**
* Load manufacturer data from Zip file
*
* @param projectZipFile
* @param manufacturerData
* @param idLookupMap
* @return
*/
protected void loadManufacturerData(final ZipFile projectZipFile, final KnxManufacturerDataT manufacturerData,
final LookupIdResolver idResolver)
{
try
{
// Group files in Zip by manufacturer directory name
Map<String, List<FileHeader>> directories = projectZipFile.getFileHeaders().stream()
.filter(h -> h.getFileName().indexOf('/') > 0)
.filter(h -> h.getFileName().startsWith(MANUFACTURER_DIR_PREFIX))
.collect(Collectors.groupingBy(h -> h.getFileName().substring(0, h.getFileName().indexOf('/'))));
// Load manufacturers
directories.values().stream()
.forEach(d -> loadManufacturer(projectZipFile, d, manufacturerData, idResolver));
}
catch (ZipException e)
{
throw new ETSLoaderException(e);
}
}
protected void loadManufacturer(final ZipFile projectZipFile, final List<FileHeader> directory,
final KnxManufacturerDataT manufacturerData, final LookupIdResolver idResolver)
{
KnxManufacturer manufacturer = new KnxManufacturer();
// Extract manufacturer ID from directory name
String fileName = directory.get(0).getFileName();
String manufacturerId = fileName.substring(0, fileName.indexOf('/'));
manufacturer.setRefId(manufacturerId);
directory.stream().filter(d -> d.getFileName().endsWith(BAGGAGES_FILE)).findAny()
.ifPresent(h -> loadBaggage(projectZipFile, h, manufacturer, idResolver));
KnxApplicationPrograms knxApplicationPrograms = new KnxApplicationPrograms();
directory.stream()
.filter(d -> d.getFileName().startsWith(MANUFACTURER_DIR_PREFIX, d.getFileName().indexOf('/') + 1))
.forEach(d -> loadApplicationProgram(projectZipFile, d, knxApplicationPrograms, idResolver));
reconnect(knxApplicationPrograms, manufacturer, KnxManufacturer::setApplicationPrograms, idResolver);
directory.stream().filter(d -> d.getFileName().endsWith(HARDWARE_FILE)).findAny()
.ifPresent(h -> loadHardware(projectZipFile, h, manufacturer, idResolver));
directory.stream().filter(d -> d.getFileName().endsWith(CATALOG_FILE)).findAny()
.ifPresent(h -> loadCatalog(projectZipFile, h, manufacturer, idResolver));
reconnect(manufacturer, manufacturerData, (p, e) -> p.getManufacturer().add(e), idResolver);
}
protected void loadBaggage(final ZipFile zipFile, final FileHeader fileHeader, final KnxManufacturer manufacturer,
final LookupIdResolver idResolver)
{
try (InputStream stream = getInputStreamFromZip(zipFile, fileHeader))
{
KNX knx = loadKnxFromInputStream(stream, idResolver, null);
KnxBaggages baggages = knx.getManufacturerData().getManufacturer().get(0).getBaggages();
reconnect(baggages, manufacturer, KnxManufacturer::setBaggages, idResolver);
}
catch (IOException e)
{
throw new ETSLoaderException(e);
}
}
protected void loadApplicationProgram(final ZipFile zipFile, final FileHeader fileHeader,
final KnxApplicationPrograms knxApplicationPrograms, final LookupIdResolver idResolver)
{
try (InputStream stream = getInputStreamFromZip(zipFile, fileHeader))
{
KNX knx = loadKnxFromInputStream(stream, idResolver, null);
KnxApplicationProgramT knxApplicationProgramT = knx.getManufacturerData().getManufacturer().get(0)
.getApplicationPrograms().getApplicationProgram().get(0);
reconnect(knxApplicationProgramT, knxApplicationPrograms, (p, e) -> p.getApplicationProgram().add(e),
idResolver);
}
catch (IOException e)
{
throw new ETSLoaderException(e);
}
}
protected void loadHardware(final ZipFile zipFile, final FileHeader fileHeader, final KnxManufacturer manufacturer,
final LookupIdResolver idResolver)
{
try (InputStream stream = getInputStreamFromZip(zipFile, fileHeader))
{
KNX knx = loadKnxFromInputStream(stream, idResolver, null);
KnxHardware hardware = knx.getManufacturerData().getManufacturer().get(0).getHardware();
reconnect(hardware, manufacturer, KnxManufacturer::setHardware, idResolver);
}
catch (IOException e)
{
throw new ETSLoaderException(e);
}
}
protected void loadCatalog(final ZipFile zipFile, final FileHeader fileHeader, final KnxManufacturer manufacturer,
final LookupIdResolver idResolver)
{
try (InputStream stream = getInputStreamFromZip(zipFile, fileHeader))
{
KNX knx = loadKnxFromInputStream(stream, idResolver, null);
KnxCatalog catalog = knx.getManufacturerData().getManufacturer().get(0).getCatalog();
reconnect(catalog, manufacturer, KnxManufacturer::setCatalog, idResolver);
}
catch (IOException e)
{
throw new ETSLoaderException(e);
}
}
protected void loadProjects(final Optional<String> password, final ZipFile projectZipFile, final KNX knx,
final LookupIdResolver idResolver)
{
try
{
projectZipFile.getFileHeaders().stream().filter(this::isProjectFile)
.forEach(h -> loadProjectFile(projectZipFile, h, password, knx, idResolver));
projectZipFile.getFileHeaders().stream().filter(this::isProjectDir)
.collect(Collectors.groupingBy(f -> f.getFileName().substring(0, f.getFileName().indexOf('/'))))
.forEach((k, e) -> loadProjectDir(projectZipFile, e, knx, idResolver));
}
catch (ZipException e)
{
throw new ETSLoaderException(e);
}
}
protected KNX loadMasterData(final ZipFile projectZipFile, final LookupIdResolver idResolver)
throws ETSLoaderException
{
try
{
FileHeader masterDataFileHeader = projectZipFile.getFileHeader(MASTER_DATA_FILE);
try (InputStream masterDataFileStream = getInputStreamFromZip(projectZipFile, masterDataFileHeader))
{
return loadKnxFromInputStream(masterDataFileStream, idResolver, null);
}
}
catch (IOException e)
{
throw new ETSLoaderException(e);
}
}
protected void loadProjectDir(final ZipFile zipFile, final List<FileHeader> fileHeaders, final KNX knx,
final LookupIdResolver idResolver) throws ETSLoaderException
{
KnxInstallations installations = new KnxInstallations();
fileHeaders.stream().filter(this::isInstallationFile)
.sorted((o1, o2) -> o1.getFileName().compareTo(o2.getFileName()))
.forEach(h -> loadInstallationFile(zipFile, h, installations, idResolver));
FileHeader projectFileHeader = fileHeaders.stream().filter(f -> f.getFileName().endsWith(MAIN_PROJECT_FILE))
.findAny().get();
KNX knxProject;
try (ZipInputStream inputStream = getInputStreamFromZip(zipFile, projectFileHeader))
{
knxProject = loadKnxFromInputStream(inputStream, idResolver, null);
}
catch (IOException e)
{
throw new ETSLoaderException(e);
}
KnxProjectT project = knxProject.getProject().get(0);
reconnect(project, knx, (p, e) -> p.getProject().add(e), idResolver);
reconnect(installations, project, KnxProjectT::setInstallations, idResolver);
}
protected void loadProjectFile(final ZipFile zipFile, final FileHeader fileHeader, final Optional<String> password,
final KNX knx, final LookupIdResolver idResolver) throws ETSLoaderException
{
try (InputStream stream = getInputStreamFromZip(zipFile, fileHeader))
{
Path tempFile = Files.createTempFile(TEMP_FILE_PREFIX, "." + ZIP_EXTENSION);
tempFile.toFile().deleteOnExit();
Files.copy(stream, tempFile, StandardCopyOption.REPLACE_EXISTING);
ZipFile projectZipFile = new ZipFile(tempFile.toFile(), password.map(String::toCharArray).orElse(null));
List<FileHeader> fileHeaders = projectZipFile.getFileHeaders();
loadProjectDir(projectZipFile, fileHeaders, knx, idResolver);
}
catch (IOException e)
{
throw new ETSLoaderException(e);
}
}
protected void loadInstallationFile(final ZipFile zipFile, final FileHeader fileHeader,
final KnxInstallations installations, final LookupIdResolver idResolver)
{
try (InputStream stream = getInputStreamFromZip(zipFile, fileHeader))
{
int installationId = Integer.parseInt(getFileName(fileHeader));
KNX knxFile = loadKnxFromInputStream(stream, idResolver, knx -> knx.getProject().get(0).getInstallations()
.getInstallation().get(0).setInstallationId(installationId));
KnxInstallation installation = knxFile.getProject().get(0).getInstallations().getInstallation().get(0);
reconnect(installation, installations, (p, e) -> p.getInstallation().add(e), idResolver);
}
catch (IOException e)
{
throw new ETSLoaderException(e);
}
}
private String getFileName(final FileHeader fileHeader)
{
String fileName = FileUtils.getFileNameWithoutExtension(fileHeader.getFileName());
if (fileName.indexOf('/') > 0)
{
fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
}
return fileName;
}
protected ZipInputStream getInputStreamFromZip(final ZipFile zipFile, final FileHeader fileHeader)
throws IOException
{
LOG.debug("Processing " + fileHeader.getFileName());
return zipFile.getInputStream(fileHeader);
}
protected KNX loadKnxFromInputStream(final InputStream inputStream, final LookupIdResolver idResolver,
final Consumer<KNX> postProcessing) throws ETSLoaderException
{
try
{
JAXBContext jaxbContext = JAXBContext.newInstance(KNX.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
jaxbUnmarshaller.setProperty(com.sun.xml.bind.IDResolver.class.getName(), idResolver);
jaxbUnmarshaller.setListener(new Listener()
{
@Override
public void afterUnmarshal(final Object target, final Object parent)
{
((BaseClass) target).setParent((BaseClass) parent);
super.afterUnmarshal(target, parent);
}
});
KNX knx = (KNX) jaxbUnmarshaller.unmarshal(inputStream);
if (postProcessing != null)
{
postProcessing.accept(knx);
}
idResolver.resolveProxies(knx);
return knx;
}
catch (JAXBException e)
{
throw new ETSLoaderException(e);
}
}
protected boolean isInstallationFile(final FileHeader h)
{
String fileName = getFileName(h);
try
{
Integer.parseInt(fileName);
return true;
}
catch (@SuppressWarnings("unused") NumberFormatException e)
{
return false;
}
}
protected boolean isProjectFile(final FileHeader p)
{
return p.getFileName().startsWith(PROJECT_ZIP_FILE_PREFIX) && p.getFileName().endsWith("." + ZIP_EXTENSION);
}
protected boolean isProjectDir(final FileHeader p)
{
return p.getFileName().startsWith(PROJECT_ZIP_FILE_PREFIX) && p.getFileName().indexOf('/') > 0;
}
}