-
Notifications
You must be signed in to change notification settings - Fork 4
/
Dataset.java
181 lines (155 loc) · 5.75 KB
/
Dataset.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
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
/*
* The MIT License
*
* Copyright 2016 Thibault Debatty.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package info.debatty.java.datasets.dblp;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
/**
*
* @author Thibault Debatty
*/
public class Dataset extends info.debatty.java.datasets.Dataset<Publication> {
private final String file;
public Dataset(String file) {
this.file = file;
}
public Iterator iterator() {
return new DblpIterator(file);
}
@Override
public int hashCode() {
int hash = 7;
hash = 29 * hash + (this.file != null ? this.file.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Dataset other = (Dataset) obj;
if ((this.file == null) ? (other.file != null) : !this.file.equals(other.file)) {
return false;
}
return true;
}
}
class DblpIterator implements Iterator<Publication> {
private static final int BUFFER_SIZE = 20;
private static final String[] PUBLICATIONS_TYPES = new String[] {
"article", "inproceedings", "incollection"};
private static boolean in_publication_types(String localPart) {
return in_array(PUBLICATIONS_TYPES, localPart);
}
private static boolean in_array(String[] haystack, String needle) {
for (String string : haystack) {
if (string.equals(needle)) {
return true;
}
}
return false;
}
XMLEventReader xmlEventReader;
LinkedList<Publication> next_publications = new LinkedList<Publication>();
private boolean in_publication = false;
DblpIterator(String file) {
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
try {
xmlEventReader = xmlInputFactory.createXMLEventReader(new FileInputStream(file));
} catch (FileNotFoundException ex) {
} catch (XMLStreamException ex) {
Logger.getLogger(DblpIterator.class.getName()).log(Level.SEVERE, null, ex);
}
try {
readNext();
} catch (XMLStreamException ex) {
Logger.getLogger(DblpIterator.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void readNext() throws XMLStreamException {
Publication publication = new Publication();
while(xmlEventReader.hasNext()) {
XMLEvent xmlEvent = xmlEventReader.nextEvent();
if (xmlEvent.isStartElement()){
StartElement startElement = xmlEvent.asStartElement();
if(in_publication_types(startElement.getName().getLocalPart())){
publication = new Publication();
publication.type = startElement.getName().getLocalPart();
in_publication = true;
continue;
}
if(in_publication && startElement.getName().getLocalPart().equals("title")){
xmlEvent = xmlEventReader.nextEvent();
publication.title = xmlEvent.toString();
continue;
}
}
if(xmlEvent.isEndElement()){
if(in_publication_types(xmlEvent.asEndElement().getName().getLocalPart())){
if (publication.title != null
&& !publication.title.equals("")) {
next_publications.add(publication);
}
publication = new Publication();
in_publication = false;
if (next_publications.size() >= BUFFER_SIZE) {
break;
}
}
}
}
}
public boolean hasNext() {
return ! next_publications.isEmpty();
}
public Publication next() {
Publication pub = next_publications.pop();
if (next_publications.isEmpty()) {
try {
readNext();
} catch (XMLStreamException ex) {
Logger.getLogger(DblpIterator.class.getName()).log(Level.SEVERE, null, ex);
}
}
return pub;
}
@Override
public void remove() {
throw new UnsupportedOperationException("Not supported yet.");
}
}