Project

General

Profile

Download (10.8 KB) Statistics
| Branch: | Tag: | Revision:
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

    
37
/**
38
 * @author a.kohlbecker
39
 * @since 25.08.2010
40
 */
41
public class MobotOpenUrlResponseSchemaAdapter extends SchemaAdapterBase<Reference>{
42

    
43
	static URI identifier = null;
44

    
45
	/* (non-Javadoc)
46
	 * @see eu.etaxonomy.cdm.ext.common.SchemaAdapterBase#getIdentifier()
47
	 */
48
	@Override
49
	public URI getIdentifier() {
50
		return identifier;
51
	}
52

    
53
	/* (non-Javadoc)
54
	 * @see eu.etaxonomy.cdm.ext.schema.SchemaAdapter#getShortName()
55
	 */
56
	@Override
57
	public String getShortName() {
58
		return "MOBOT.OpenUrl.Utilities.OpenUrlResponse";
59
	}
60

    
61
	/* (non-Javadoc)
62
	 * @see eu.etaxonomy.cdm.ext.schema.SchemaAdapter#getCmdEntities(java.io.Reader)
63
	 */
64
	@Override
65
	public List<Reference> getCmdEntities(InputStream inputStream) throws IOException {
66

    
67
		SAXParserFactory factory = SAXParserFactory.newInstance();
68
	    factory.setNamespaceAware(true);
69
	    SAXParser parser = null;
70
		try {
71
			parser = factory.newSAXParser();
72
		} catch (ParserConfigurationException e) {
73
			logger.error(e);
74
		} catch (SAXException e) {
75
			logger.error(e);
76
		}
77

    
78

    
79
		OpenUrlResponseHandler handler = new OpenUrlResponseHandler();
80

    
81
	    try {
82
	    	if(parser != null){
83
	    		Reader reader = new InputStreamReader(inputStream, "UTF-8");
84
	    		InputSource inputSource = new InputSource(reader);
85
	    		parser.parse(inputSource, handler);
86
	    		if(handler.status != ResponseStatus.Success){
87
	    			throw new IOException("MOBOT.OpenUrl.Utilities.OpenUrlResponse - Status:" + handler.status.toString() + (handler.message != null ? handler.message : ""));
88
	    		}
89
	    	} else {
90
	    		logger.error("parser is null");
91
	    	}
92
		} catch (SAXException e) {
93
			logger.error(e);
94
		}
95

    
96

    
97
		return handler.referenceList;
98
	}
99

    
100
	class OpenUrlResponseHandler extends DefaultHandler {
101

    
102
		/*
103
		 * Fields of OpenUrlResponse
104
		 *  see http://code.google.com/p/bhl-bits/source/browse/trunk/portal/OpenUrlUtilities/OpenUrlResponse.cs
105
		 */
106
		private static final String OPENURL_RESPONSE = "OpenUrlResponse";
107
		private static final String STATUS = "Status";
108
		private static final String MESSAGE = "Message";
109
		private static final String CITATIONS = "citations";
110
		private static final String OPENURL_RESPONSE_CITATION = "OpenUrlResponseCitation";
111

    
112
		/*
113
		 * Fields of OpenUrlResponseCitation
114
		 *  see http://code.google.com/p/bhl-bits/source/browse/trunk/portal/OpenUrlUtilities/OpenUrlResponseCitation.cs
115
		 */
116

    
117
		/**
118
		 * references the specific page in the title
119
		 */
120
		private static final String URL = "Url";
121
		/**
122
		 * references the according entry in the bibliography
123
		 */
124
		private static final String ITEM_URL = "ItemUrl";
125

    
126
		/**
127
		 * references the specific book or journal, that is to the front page
128
		 */
129
		private static final String TITLE_URL = "TitleUrl";
130
		private static final String TITLE = "Title";
131
		private static final String STITLE = "STitle";
132
		/**
133
		 * seems to contain the type of the reference : book
134
		 */
135
		private static final String GENRE = "Genre";
136
		private static final String AUTHORS = "Authors";
137
		private static final String SUBJECTS = "Subjects";
138
		private static final String PUBLISHER_NAME = "PublisherName";
139
		private static final String PUBLISHER_PLACE = "PublisherPlace";
140
		private static final String DATE = "Date";
141
		private static final String VOLUME = "Volume";
142
		private static final String EDITION = "Edition";
143
		private static final String PUBLICATION_FREQUENCY = "PublicationFrequency";
144
		private static final String LANGUAGE = "Language";
145
		private static final String OCLC = "Oclc";
146
		private static final String LCCN = "Lccn";
147
		private static final String ISSN = "Issn";
148
		private static final String ATITLE = "ATitle";
149
		private static final String SPAGE = "SPage";
150
		private static final String EPAGE = "EPage";
151
		private static final String PAGES = "Pages";
152

    
153

    
154
		List<Reference> referenceList = new ArrayList<Reference>();
155

    
156
		OpenUrlReference reference = null;
157

    
158
		ResponseStatus status = null;
159
		Team authorship = null;
160
		String message = null;
161

    
162
		String elementName = null;
163
		private String elementNameToStore;
164
		private StringBuilder textBuffer = new StringBuilder();
165

    
166

    
167
		@Override
168
		public void startElement(String uri, String localName,
169
				String qName, Attributes attributes) throws SAXException {
170

    
171
			if (qName.equals(OPENURL_RESPONSE)) {
172
				logger.debug("Start " + OPENURL_RESPONSE + "; ");
173
				status = ResponseStatus.Undefined; // indicates that the OPENURL_RESPONSE element has ben detected
174
			} else if (status != null && qName.equals(OPENURL_RESPONSE_CITATION)) {
175
				reference = new OpenUrlReference();
176
			} else if (reference != null && qName.equals(AUTHORS)) {
177
				authorship = Team.NewInstance();
178
			} else if (reference != null && qName.equals(SUBJECTS)) {
179
				//TODO implement, but no equivalent in the cdm model
180
			} else {
181
				elementName = qName;
182
			}
183
		}
184

    
185
		@Override
186
		public void endElement(String uri, String localName, String qName) throws SAXException {
187

    
188
			if (qName.equals(OPENURL_RESPONSE)) {
189

    
190
			} else if (qName.equals(OPENURL_RESPONSE_CITATION)) {
191
				referenceList.add(reference);
192
				reference = null;
193
			} else if (reference != null && qName.equals(AUTHORS)) {
194
				reference.setAuthorship(authorship);
195
				authorship = null;
196
			} else if (reference != null && qName.equals(SUBJECTS)) {
197
				//TODO implement, but no equivalent in the cdm model
198
			}else {
199
				elementNameToStore = elementName;
200
				elementName = null;
201
			}
202

    
203
		}
204

    
205
		@Override
206
		public void characters(char ch[], int start, int length)
207
				throws SAXException {
208

    
209
			if(elementNameToStore  == null){
210

    
211
				textBuffer.append(new String(ch, start, length));
212

    
213
			} else {
214

    
215
				logger.debug("Characters [" + elementNameToStore + "]: " + textBuffer);
216
				String trimmedText = textBuffer.toString().trim();
217
				// empty the text buffer
218
				textBuffer.delete(0, textBuffer.length());
219

    
220
				// --- Reference --- //
221
				if(reference != null){
222

    
223
					if(elementNameToStore.equals(URL)){
224
						try {
225
							reference.setUri(new URI(trimmedText));
226
						} catch (URISyntaxException e) {
227
							logger.warn(e.getMessage());
228
						}
229
					}
230
					if(elementNameToStore.equals(ITEM_URL)){
231
						try {
232
							reference.setItemUri(new URI(trimmedText));
233
						} catch (URISyntaxException e) {
234
							logger.warn(e.getMessage());
235
						}
236
					}
237
					if(elementNameToStore.equals(TITLE_URL)){
238
						try {
239
							reference.setTitleUri(new URI(trimmedText));
240
						} catch (URISyntaxException e) {
241
							logger.warn(e.getMessage());
242
						}
243
					}
244
					if(elementNameToStore.equals(TITLE)){
245
						reference.setTitleCache(trimmedText, true);
246
					}
247
					if(elementNameToStore.equals(STITLE)){
248
						logger.debug(elementNameToStore + " not yet implemented!");//TODO
249
					}
250
					if(elementNameToStore.equals(ATITLE)){
251
						logger.debug(elementNameToStore + " not yet implemented!");//TODO
252
					}
253
					if(elementNameToStore.equals(PUBLISHER_NAME)){
254
						reference.setPublisher(trimmedText);
255
					}
256
					if(elementNameToStore.equals(PUBLISHER_PLACE)){
257
						reference.setPlacePublished(trimmedText);
258
					}
259
					if(elementNameToStore.equals(DATE)){
260
						/* may be a single year or a range of years 1797-1830 */
261
						Integer startYear = null;
262
						Integer endYear = null;
263
						if(trimmedText.length() == 9 && trimmedText.indexOf("-") == 4){
264
							try {
265
								startYear = Integer.valueOf(trimmedText.substring(0, 4));
266
								endYear = Integer.valueOf(trimmedText.substring(5));
267
								reference.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(startYear, endYear));
268
							} catch (NumberFormatException e) {
269
								logger.error("date can not be parsed: "+ trimmedText);
270
							}
271
						} else if(trimmedText.length() == 4) {
272
							try {
273
								startYear = Integer.valueOf(trimmedText);
274
							} catch (NumberFormatException e) {
275
								logger.error("date can not be parsed: "+ trimmedText);
276
							}
277
							reference.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(startYear));
278
						}
279
					}
280
					if(elementNameToStore.equals(VOLUME)){
281
						reference.setVolume(trimmedText);
282
					}
283
					if(elementNameToStore.equals(EDITION)){
284
						reference.setEdition(trimmedText);
285
					}
286
					if(elementNameToStore.equals(SPAGE)){
287
						reference.setPages(trimmedText);
288
					}
289
					if(elementNameToStore.equals(EPAGE)){
290
						logger.debug(elementNameToStore + " not yet implemented!");//TODO
291
					}
292
					if(elementNameToStore.equals(PAGES)){
293
						// IGNORE we rather need the start page value SPAGE
294
					}
295
					if(elementNameToStore.equals(PUBLICATION_FREQUENCY)){
296
						logger.debug(elementNameToStore + " not yet implemented!");//TODO
297
					}
298
					if(elementNameToStore.equals(LANGUAGE)){
299
						logger.debug(elementNameToStore + " not yet implemented!");//TODO
300
					}
301
					if(elementNameToStore.equals(OCLC)){
302
						logger.debug(elementNameToStore + " not yet implemented!");//TODO
303
					}
304
					if(elementNameToStore.equals(LCCN)){
305
						logger.debug(elementNameToStore + " not yet implemented!");//TODO
306
					}
307
					if(elementNameToStore.equals(ISSN)){
308
						reference.setIssn(trimmedText);
309
					}
310
				}
311

    
312
				// --- Reference.authorship --- //
313
				if(authorship != null && reference != null){
314
					if(elementNameToStore.equals("String")){
315
						authorship.addTeamMember(Person.NewTitledInstance(trimmedText));
316
					}
317
				}
318

    
319
				// openUrlResponse //
320
				if(reference == null){
321
					if(elementNameToStore.equals(STATUS)){
322
						status = ResponseStatus.valueOf(trimmedText);
323
					}
324
				}
325

    
326
				elementNameToStore = null;
327
			}
328
		}
329

    
330
	}
331

    
332
	 /**
333
	 * @see http://code.google.com/p/bhl-bits/source/browse/trunk/portal/OpenUrlUtilities/IOpenUrlResponse.cs
334
	 */
335
	public enum ResponseStatus {
336
		Undefined, // Query not submitted
337
		Success, Error
338
	}
339

    
340

    
341
}
(2-2/4)