1
|
/**
|
2
|
* Copyright (C) 2009 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.ext.openurl;
|
10
|
|
11
|
import java.io.IOException;
|
12
|
import java.io.InputStream;
|
13
|
import java.io.InputStreamReader;
|
14
|
import java.io.Reader;
|
15
|
import java.net.URI;
|
16
|
import java.net.URISyntaxException;
|
17
|
import java.util.ArrayList;
|
18
|
import java.util.List;
|
19
|
|
20
|
import javax.xml.parsers.ParserConfigurationException;
|
21
|
import javax.xml.parsers.SAXParser;
|
22
|
import javax.xml.parsers.SAXParserFactory;
|
23
|
|
24
|
import org.xml.sax.Attributes;
|
25
|
import org.xml.sax.InputSource;
|
26
|
import org.xml.sax.SAXException;
|
27
|
import org.xml.sax.helpers.DefaultHandler;
|
28
|
|
29
|
import eu.etaxonomy.cdm.ext.common.SchemaAdapterBase;
|
30
|
import eu.etaxonomy.cdm.model.agent.Person;
|
31
|
import eu.etaxonomy.cdm.model.agent.Team;
|
32
|
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
|
33
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
34
|
|
35
|
/**
|
36
|
* @author a.kohlbecker
|
37
|
* @since 25.08.2010
|
38
|
*/
|
39
|
public class MobotOpenUrlResponseSchemaAdapter extends SchemaAdapterBase<Reference>{
|
40
|
|
41
|
static URI identifier = null;
|
42
|
|
43
|
@Override
|
44
|
public URI getIdentifier() {
|
45
|
return identifier;
|
46
|
}
|
47
|
|
48
|
@Override
|
49
|
public String getShortName() {
|
50
|
return "MOBOT.OpenUrl.Utilities.OpenUrlResponse";
|
51
|
}
|
52
|
|
53
|
@Override
|
54
|
public List<Reference> getCmdEntities(InputStream inputStream) throws IOException {
|
55
|
|
56
|
SAXParserFactory factory = SAXParserFactory.newInstance();
|
57
|
factory.setNamespaceAware(true);
|
58
|
SAXParser parser = null;
|
59
|
try {
|
60
|
parser = factory.newSAXParser();
|
61
|
} catch (ParserConfigurationException e) {
|
62
|
logger.error(e);
|
63
|
} catch (SAXException e) {
|
64
|
logger.error(e);
|
65
|
}
|
66
|
|
67
|
OpenUrlResponseHandler handler = new OpenUrlResponseHandler();
|
68
|
|
69
|
try {
|
70
|
if(parser != null){
|
71
|
Reader reader = new InputStreamReader(inputStream, "UTF-8");
|
72
|
InputSource inputSource = new InputSource(reader);
|
73
|
parser.parse(inputSource, handler);
|
74
|
if(handler.status != ResponseStatus.Success){
|
75
|
throw new IOException("MOBOT.OpenUrl.Utilities.OpenUrlResponse - Status:" + handler.status.toString() + (handler.message != null ? handler.message : ""));
|
76
|
}
|
77
|
} else {
|
78
|
logger.error("parser is null");
|
79
|
}
|
80
|
} catch (SAXException e) {
|
81
|
logger.error(e);
|
82
|
}
|
83
|
|
84
|
|
85
|
return handler.referenceList;
|
86
|
}
|
87
|
|
88
|
class OpenUrlResponseHandler extends DefaultHandler {
|
89
|
|
90
|
/*
|
91
|
* Fields of OpenUrlResponse
|
92
|
* see http://code.google.com/p/bhl-bits/source/browse/trunk/portal/OpenUrlUtilities/OpenUrlResponse.cs
|
93
|
*/
|
94
|
private static final String OPENURL_RESPONSE = "OpenUrlResponse";
|
95
|
private static final String STATUS = "Status";
|
96
|
private static final String MESSAGE = "Message";
|
97
|
private static final String CITATIONS = "citations";
|
98
|
private static final String OPENURL_RESPONSE_CITATION = "OpenUrlResponseCitation";
|
99
|
|
100
|
/*
|
101
|
* Fields of OpenUrlResponseCitation
|
102
|
* see http://code.google.com/p/bhl-bits/source/browse/trunk/portal/OpenUrlUtilities/OpenUrlResponseCitation.cs
|
103
|
*/
|
104
|
|
105
|
/**
|
106
|
* references the specific page in the title
|
107
|
*/
|
108
|
private static final String URL = "Url";
|
109
|
/**
|
110
|
* references the according entry in the bibliography
|
111
|
*/
|
112
|
private static final String ITEM_URL = "ItemUrl";
|
113
|
|
114
|
/**
|
115
|
* references the specific book or journal, that is to the front page
|
116
|
*/
|
117
|
private static final String TITLE_URL = "TitleUrl";
|
118
|
private static final String TITLE = "Title";
|
119
|
private static final String STITLE = "STitle";
|
120
|
/**
|
121
|
* seems to contain the type of the reference : book
|
122
|
*/
|
123
|
private static final String GENRE = "Genre";
|
124
|
private static final String AUTHORS = "Authors";
|
125
|
private static final String SUBJECTS = "Subjects";
|
126
|
private static final String PUBLISHER_NAME = "PublisherName";
|
127
|
private static final String PUBLISHER_PLACE = "PublisherPlace";
|
128
|
private static final String DATE = "Date";
|
129
|
private static final String VOLUME = "Volume";
|
130
|
private static final String EDITION = "Edition";
|
131
|
private static final String PUBLICATION_FREQUENCY = "PublicationFrequency";
|
132
|
private static final String LANGUAGE = "Language";
|
133
|
private static final String OCLC = "Oclc";
|
134
|
private static final String LCCN = "Lccn";
|
135
|
private static final String ISSN = "Issn";
|
136
|
private static final String ATITLE = "ATitle";
|
137
|
private static final String SPAGE = "SPage";
|
138
|
private static final String EPAGE = "EPage";
|
139
|
private static final String PAGES = "Pages";
|
140
|
|
141
|
List<Reference> referenceList = new ArrayList<>();
|
142
|
|
143
|
OpenUrlReference reference = null;
|
144
|
|
145
|
ResponseStatus status = null;
|
146
|
Team authorship = null;
|
147
|
String message = null;
|
148
|
|
149
|
String elementName = null;
|
150
|
private String elementNameToStore;
|
151
|
private StringBuilder textBuffer = new StringBuilder();
|
152
|
|
153
|
@Override
|
154
|
public void startElement(String uri, String localName,
|
155
|
String qName, Attributes attributes) throws SAXException {
|
156
|
|
157
|
if (qName.equals(OPENURL_RESPONSE)) {
|
158
|
logger.debug("Start " + OPENURL_RESPONSE + "; ");
|
159
|
status = ResponseStatus.Undefined; // indicates that the OPENURL_RESPONSE element has ben detected
|
160
|
} else if (status != null && qName.equals(OPENURL_RESPONSE_CITATION)) {
|
161
|
reference = new OpenUrlReference();
|
162
|
} else if (reference != null && qName.equals(AUTHORS)) {
|
163
|
authorship = Team.NewInstance();
|
164
|
} else if (reference != null && qName.equals(SUBJECTS)) {
|
165
|
//TODO implement, but no equivalent in the cdm model
|
166
|
} else {
|
167
|
elementName = qName;
|
168
|
}
|
169
|
}
|
170
|
|
171
|
@Override
|
172
|
public void endElement(String uri, String localName, String qName) throws SAXException {
|
173
|
|
174
|
if (qName.equals(OPENURL_RESPONSE)) {
|
175
|
|
176
|
} else if (qName.equals(OPENURL_RESPONSE_CITATION)) {
|
177
|
referenceList.add(reference);
|
178
|
reference = null;
|
179
|
} else if (reference != null && qName.equals(AUTHORS)) {
|
180
|
reference.setAuthorship(authorship);
|
181
|
authorship = null;
|
182
|
} else if (reference != null && qName.equals(SUBJECTS)) {
|
183
|
//TODO implement, but no equivalent in the cdm model
|
184
|
}else {
|
185
|
elementNameToStore = elementName;
|
186
|
elementName = null;
|
187
|
}
|
188
|
|
189
|
}
|
190
|
|
191
|
@Override
|
192
|
public void characters(char ch[], int start, int length)
|
193
|
throws SAXException {
|
194
|
|
195
|
if(elementNameToStore == null){
|
196
|
textBuffer.append(new String(ch, start, length));
|
197
|
} else {
|
198
|
|
199
|
logger.debug("Characters [" + elementNameToStore + "]: " + textBuffer);
|
200
|
String trimmedText = textBuffer.toString().trim();
|
201
|
// empty the text buffer
|
202
|
textBuffer.delete(0, textBuffer.length());
|
203
|
|
204
|
// --- Reference --- //
|
205
|
if(reference != null){
|
206
|
|
207
|
if(elementNameToStore.equals(URL)){
|
208
|
try {
|
209
|
reference.setUri(new URI(trimmedText));
|
210
|
} catch (URISyntaxException e) {
|
211
|
logger.warn(e.getMessage());
|
212
|
}
|
213
|
}
|
214
|
if(elementNameToStore.equals(ITEM_URL)){
|
215
|
try {
|
216
|
reference.setItemUri(new URI(trimmedText));
|
217
|
} catch (URISyntaxException e) {
|
218
|
logger.warn(e.getMessage());
|
219
|
}
|
220
|
}
|
221
|
if(elementNameToStore.equals(TITLE_URL)){
|
222
|
try {
|
223
|
reference.setTitleUri(new URI(trimmedText));
|
224
|
} catch (URISyntaxException e) {
|
225
|
logger.warn(e.getMessage());
|
226
|
}
|
227
|
}
|
228
|
if(elementNameToStore.equals(TITLE)){
|
229
|
reference.setTitleCache(trimmedText, true);
|
230
|
}
|
231
|
if(elementNameToStore.equals(STITLE)){
|
232
|
logger.debug(elementNameToStore + " not yet implemented!");//TODO
|
233
|
}
|
234
|
if(elementNameToStore.equals(ATITLE)){
|
235
|
logger.debug(elementNameToStore + " not yet implemented!");//TODO
|
236
|
}
|
237
|
if(elementNameToStore.equals(PUBLISHER_NAME)){
|
238
|
reference.setPublisher(trimmedText);
|
239
|
}
|
240
|
if(elementNameToStore.equals(PUBLISHER_PLACE)){
|
241
|
reference.setPlacePublished(trimmedText);
|
242
|
}
|
243
|
if(elementNameToStore.equals(DATE)){
|
244
|
/* may be a single year or a range of years 1797-1830 */
|
245
|
Integer startYear = null;
|
246
|
Integer endYear = null;
|
247
|
if(trimmedText.length() == 9 && trimmedText.indexOf("-") == 4){
|
248
|
try {
|
249
|
startYear = Integer.valueOf(trimmedText.substring(0, 4));
|
250
|
endYear = Integer.valueOf(trimmedText.substring(5));
|
251
|
reference.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(startYear, endYear));
|
252
|
} catch (NumberFormatException e) {
|
253
|
logger.error("date can not be parsed: "+ trimmedText);
|
254
|
}
|
255
|
} else if(trimmedText.length() == 4) {
|
256
|
try {
|
257
|
startYear = Integer.valueOf(trimmedText);
|
258
|
} catch (NumberFormatException e) {
|
259
|
logger.error("date can not be parsed: "+ trimmedText);
|
260
|
}
|
261
|
reference.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(startYear));
|
262
|
}
|
263
|
}
|
264
|
if(elementNameToStore.equals(VOLUME)){
|
265
|
reference.setVolume(trimmedText);
|
266
|
}
|
267
|
if(elementNameToStore.equals(EDITION)){
|
268
|
reference.setEdition(trimmedText);
|
269
|
}
|
270
|
if(elementNameToStore.equals(SPAGE)){
|
271
|
reference.setPages(trimmedText);
|
272
|
}
|
273
|
if(elementNameToStore.equals(EPAGE)){
|
274
|
logger.debug(elementNameToStore + " not yet implemented!");//TODO
|
275
|
}
|
276
|
if(elementNameToStore.equals(PAGES)){
|
277
|
// IGNORE we rather need the start page value SPAGE
|
278
|
}
|
279
|
if(elementNameToStore.equals(PUBLICATION_FREQUENCY)){
|
280
|
logger.debug(elementNameToStore + " not yet implemented!");//TODO
|
281
|
}
|
282
|
if(elementNameToStore.equals(LANGUAGE)){
|
283
|
logger.debug(elementNameToStore + " not yet implemented!");//TODO
|
284
|
}
|
285
|
if(elementNameToStore.equals(OCLC)){
|
286
|
logger.debug(elementNameToStore + " not yet implemented!");//TODO
|
287
|
}
|
288
|
if(elementNameToStore.equals(LCCN)){
|
289
|
logger.debug(elementNameToStore + " not yet implemented!");//TODO
|
290
|
}
|
291
|
if(elementNameToStore.equals(ISSN)){
|
292
|
reference.setIssn(trimmedText);
|
293
|
}
|
294
|
}
|
295
|
|
296
|
// --- Reference.authorship --- //
|
297
|
if(authorship != null && reference != null){
|
298
|
if(elementNameToStore.equals("String")){
|
299
|
authorship.addTeamMember(Person.NewTitledInstance(trimmedText));
|
300
|
}
|
301
|
}
|
302
|
|
303
|
// openUrlResponse //
|
304
|
if(reference == null){
|
305
|
if(elementNameToStore.equals(STATUS)){
|
306
|
status = ResponseStatus.valueOf(trimmedText);
|
307
|
}
|
308
|
}
|
309
|
|
310
|
elementNameToStore = null;
|
311
|
}
|
312
|
}
|
313
|
|
314
|
}
|
315
|
|
316
|
/**
|
317
|
* @see http://code.google.com/p/bhl-bits/source/browse/trunk/portal/OpenUrlUtilities/IOpenUrlResponse.cs
|
318
|
*/
|
319
|
public enum ResponseStatus {
|
320
|
Undefined, // Query not submitted
|
321
|
Success, Error
|
322
|
}
|
323
|
|
324
|
|
325
|
}
|