-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
BibtexImportMetadataSourceServiceImpl.java
107 lines (96 loc) · 3.93 KB
/
BibtexImportMetadataSourceServiceImpl.java
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
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.importer.external.bibtex.service;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Resource;
import org.dspace.importer.external.exception.FileSourceException;
import org.dspace.importer.external.service.components.AbstractPlainMetadataSource;
import org.dspace.importer.external.service.components.dto.PlainMetadataKeyValueItem;
import org.dspace.importer.external.service.components.dto.PlainMetadataSourceDto;
import org.jbibtex.BibTeXDatabase;
import org.jbibtex.BibTeXEntry;
import org.jbibtex.BibTeXParser;
import org.jbibtex.Key;
import org.jbibtex.ParseException;
import org.jbibtex.Value;
/**
* Implements a metadata importer for BibTeX files
*
* @author Pasquale Cavallo (pasquale.cavallo at 4science dot it)
*/
public class BibtexImportMetadataSourceServiceImpl extends AbstractPlainMetadataSource {
/**
* The string that identifies this import implementation as
* MetadataSource implementation
*
* @return the identifying uri
*/
@Override
public String getImportSource() {
return "BibTeXMetadataSource";
}
@Override
protected List<PlainMetadataSourceDto> readData (InputStream
inputStream) throws FileSourceException {
List<PlainMetadataSourceDto> list = new ArrayList<>();
BibTeXDatabase database;
try {
database = parseBibTex(inputStream);
} catch (IOException | ParseException e) {
throw new FileSourceException("Unable to parse file with BibTeX parser");
}
if (database == null || database.getEntries() == null) {
throw new FileSourceException("File results in an empty list of metadata");
}
if (database.getEntries() != null) {
for (Entry<Key, BibTeXEntry> entry : database.getEntries().entrySet()) {
PlainMetadataSourceDto item = new PlainMetadataSourceDto();
List<PlainMetadataKeyValueItem> keyValues = new ArrayList<>();
item.setMetadata(keyValues);
PlainMetadataKeyValueItem keyValueItem = new PlainMetadataKeyValueItem();
keyValueItem.setKey(entry.getValue().getType().getValue());
keyValueItem.setValue(entry.getKey().getValue());
keyValues.add(keyValueItem);
if (entry.getValue().getFields() != null) {
for (Entry<Key,Value> subentry : entry.getValue().getFields().entrySet()) {
PlainMetadataKeyValueItem innerItem = new PlainMetadataKeyValueItem();
innerItem.setKey(subentry.getKey().getValue());
innerItem.setValue(subentry.getValue().toUserString());
keyValues.add(innerItem);
}
}
list.add(item);
}
}
return list;
}
private BibTeXDatabase parseBibTex(InputStream inputStream) throws IOException, ParseException {
Reader reader = new InputStreamReader(inputStream);
BibTeXParser bibtexParser = new BibTeXParser();
return bibtexParser.parse(reader);
}
/**
* Retrieve the MetadataFieldMapping containing the mapping between RecordType
* (in this case PlainMetadataSourceDto.class) and Metadata
*
* @return The configured MetadataFieldMapping
*/
@Override
@SuppressWarnings("unchecked")
@Resource(name = "bibtexMetadataFieldMap")
public void setMetadataFieldMap(@SuppressWarnings("rawtypes") Map metadataFieldMap) {
super.setMetadataFieldMap(metadataFieldMap);
}
}