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