-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path164.java
More file actions
82 lines (70 loc) · 2.89 KB
/
Copy path164.java
File metadata and controls
82 lines (70 loc) · 2.89 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
/*
* Copyright 2016 Dmitry Avtonomov.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package umich.ms.fileio.filetypes.gpmdb;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import umich.ms.fileio.exceptions.FileParsingException;
import umich.ms.fileio.filetypes.gpmdb.jaxb.Bioml;
import umich.ms.util.file.AbstractFile;
/**
* Data source for GPMDB XML files with identifications. There is no corresponding data access API
* defined for such data yet.
*
* @author Dmitry Avtonomov
*/
public class GPMDBFile extends AbstractFile {
public GPMDBFile(String path) {
super(path);
}
public Bioml parse() throws FileParsingException {
//ClassLoader classLoaderOrig = Thread.currentThread().getContextClassLoader();
try {
//String filePath = "D:\\projects\\GPMDB\\gpm-xml-parser\\GPM32310000038_indented_cropped.xml";
//String filePath = "D:\\projects\\GPMDB\\gpm-xml-parser\\GPM64510013519.xml";
Path filePath = Paths.get(this.getPath());
FileInputStream fis = new FileInputStream(filePath.toFile());
BufferedInputStream bis = new BufferedInputStream(fis, 1024 * 1024 * 32); // 32 MB buffer
Class<Bioml> clazz = Bioml.class;
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Object unmarshalled = unmarshaller.unmarshal(bis);
Bioml bioml = convertJAXBObjectToDomain(clazz, unmarshalled);
return bioml;
} catch (JAXBException | FileNotFoundException e) {
throw new FileParsingException(e);
} finally {
// Thread.currentThread().setContextClassLoader(classLoaderOrig);
}
}
@SuppressWarnings("unchecked")
protected <T> T convertJAXBObjectToDomain(Class<T> clazz, Object unmarshalled) {
if (unmarshalled == null) {
throw new RuntimeException("Unmarshalled run header object was null");
}
if (!(clazz.isAssignableFrom(unmarshalled.getClass()))) {
throw new RuntimeException(String.format(
"When parsing XML, JAXB object's declared type was wrong. Expected: %s; Found: %s",
clazz.getSimpleName(), unmarshalled.getClass().getSimpleName()));
}
return (T) unmarshalled;
}
}