forked from JabRef/jabref
/
BibTeXMLHandler.java
107 lines (89 loc) · 3.57 KB
/
BibTeXMLHandler.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
/* Copyright (C) 2003-2016 JabRef contributors.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 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 General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package net.sf.jabref.logic.importer.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.IdGenerator;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
/**
* Reader for the BibTeXML format. See
* <a href="http://bibtexml.sourceforge.net/">bibtexml.sf.net</a>.
*
* @author Egon Willighagen
*/
public class BibTeXMLHandler extends DefaultHandler {
private static final String BIBTEXML_URI = "http://bibtexml.sf.net/";
private List<BibEntry> bibitems;
private BibEntry b; // the entry being read
// XML parsing stuff
private String currentChars;
public List<BibEntry> getItems() {
if (bibitems == null) {
return Collections.emptyList();
}
return bibitems;
}
// SAX parsing methods
@Override
public void startDocument() {
bibitems = new ArrayList<>();
}
@Override
public void characters(char[] ch, int start, int length) {
String s = new String(ch, start, length).trim();
currentChars += s;
}
@Override
public void startElement(String uri, String local, String raw, Attributes atts) {
if (BIBTEXML_URI.equals(uri)) {
if ("entry".equals(local)) {
b = new BibEntry(IdGenerator.next());
// Determine and-set bibtex key
String bibtexKey = null;
for (int i = 0; i < atts.getLength(); i++) {
String attrURI = atts.getURI(i);
if ((BIBTEXML_URI.equals(attrURI) || "".equals(attrURI)) && "id".equals(atts.getLocalName(i))) {
bibtexKey = atts.getValue(i);
}
}
if (bibtexKey != null) {
b.setCiteKey(bibtexKey);
}
} else if ("article".equals(local) || "inbook".equals(local) || "book".equals(local)
|| "booklet".equals(local) || "incollection".equals(local) || "inproceedings".equals(local)
|| "proceedings".equals(local) || "manual".equals(local) || "mastersthesis".equals(local)
|| "phdthesis".equals(local) || "techreport".equals(local) || "unpublished".equals(local)
|| "misc".equals(local)) {
b.setType(local);
}
}
currentChars = "";
}
@Override
public void endElement(String uri, String local, String raw) {
if (BIBTEXML_URI.equals(uri)) {
if ("entry".equals(local)) {
bibitems.add(b);
} else {
if (!currentChars.trim().isEmpty()) {
b.setField(local, currentChars);
}
}
}
currentChars = "";
}
}