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