3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.ext
.openurl
;
12 import java
.io
.IOException
;
13 import java
.io
.InputStream
;
14 import java
.io
.InputStreamReader
;
15 import java
.io
.Reader
;
17 import java
.net
.URISyntaxException
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
21 import javax
.xml
.parsers
.ParserConfigurationException
;
22 import javax
.xml
.parsers
.SAXParser
;
23 import javax
.xml
.parsers
.SAXParserFactory
;
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
;
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
;
39 * @author a.kohlbecker
42 public class MobotOpenUrlResponseSchemaAdapter
extends SchemaAdapterBase
<Reference
>{
44 static URI identifier
= null;
47 * @see eu.etaxonomy.cdm.ext.common.SchemaAdapterBase#getIdentifier()
50 public URI
getIdentifier() {
55 * @see eu.etaxonomy.cdm.ext.schema.SchemaAdapter#getShortName()
58 public String
getShortName() {
59 return "MOBOT.OpenUrl.Utilities.OpenUrlResponse";
63 * @see eu.etaxonomy.cdm.ext.schema.SchemaAdapter#getCmdEntities(java.io.Reader)
66 public List
<Reference
> getCmdEntities(InputStream inputStream
) throws IOException
{
68 SAXParserFactory factory
= SAXParserFactory
.newInstance();
69 factory
.setNamespaceAware(true);
70 SAXParser parser
= null;
72 parser
= factory
.newSAXParser();
73 } catch (ParserConfigurationException e
) {
75 } catch (SAXException e
) {
80 OpenUrlResponseHandler handler
= new OpenUrlResponseHandler();
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
: ""));
91 logger
.error("parser is null");
93 } catch (SAXException e
) {
98 return handler
.referenceList
;
101 class OpenUrlResponseHandler
extends DefaultHandler
{
104 * Fields of OpenUrlResponse
105 * see http://code.google.com/p/bhl-bits/source/browse/trunk/portal/OpenUrlUtilities/OpenUrlResponse.cs
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";
114 * Fields of OpenUrlResponseCitation
115 * see http://code.google.com/p/bhl-bits/source/browse/trunk/portal/OpenUrlUtilities/OpenUrlResponseCitation.cs
119 * references the specific page in the title
121 private static final String URL
= "Url";
123 * references the according entry in the bibliography
125 private static final String ITEM_URL
= "ItemUrl";
128 * references the specific book or journal, that is to the front page
130 private static final String TITLE_URL
= "TitleUrl";
131 private static final String TITLE
= "Title";
132 private static final String STITLE
= "STitle";
134 * seems to contain the type of the reference : book
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";
155 List
<Reference
> referenceList
= new ArrayList
<Reference
>();
157 OpenUrlReference reference
= null;
159 ResponseStatus status
= null;
160 Team authorTeam
= null;
161 String message
= null;
163 String elementName
= null;
164 private String elementNameToStore
;
165 private StringBuilder textBuffer
= new StringBuilder();
169 public void startElement(String uri
, String localName
,
170 String qName
, Attributes attributes
) throws SAXException
{
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 authorTeam
= Team
.NewInstance();
179 } else if (reference
!= null && qName
.equals(SUBJECTS
)) {
180 //TODO implement, but no equivalent in the cdm model
187 public void endElement(String uri
, String localName
, String qName
) throws SAXException
{
189 if (qName
.equals(OPENURL_RESPONSE
)) {
191 } else if (qName
.equals(OPENURL_RESPONSE_CITATION
)) {
192 referenceList
.add(reference
);
194 } else if (reference
!= null && qName
.equals(AUTHORS
)) {
195 reference
.setAuthorship(authorTeam
);
197 } else if (reference
!= null && qName
.equals(SUBJECTS
)) {
198 //TODO implement, but no equivalent in the cdm model
200 elementNameToStore
= elementName
;
207 public void characters(char ch
[], int start
, int length
)
208 throws SAXException
{
210 if(elementNameToStore
== null){
212 textBuffer
.append(new String(ch
, start
, length
));
216 logger
.debug("Characters [" + elementNameToStore
+ "]: " + textBuffer
);
217 String trimmedText
= textBuffer
.toString().trim();
218 // empty the text buffer
219 textBuffer
.delete(0, textBuffer
.length());
221 // --- Reference --- //
222 if(reference
!= null){
224 if(elementNameToStore
.equals(URL
)){
226 reference
.setUri(new URI(trimmedText
));
227 } catch (URISyntaxException e
) {
228 logger
.warn(e
.getMessage());
231 if(elementNameToStore
.equals(ITEM_URL
)){
233 reference
.setItemUri(new URI(trimmedText
));
234 } catch (URISyntaxException e
) {
235 logger
.warn(e
.getMessage());
238 if(elementNameToStore
.equals(TITLE_URL
)){
240 reference
.setTitleUri(new URI(trimmedText
));
241 } catch (URISyntaxException e
) {
242 logger
.warn(e
.getMessage());
245 if(elementNameToStore
.equals(TITLE
)){
246 reference
.setTitleCache(trimmedText
, true);
248 if(elementNameToStore
.equals(STITLE
)){
249 logger
.debug(elementNameToStore
+ " not yet implemented!");//TODO
251 if(elementNameToStore
.equals(ATITLE
)){
252 logger
.debug(elementNameToStore
+ " not yet implemented!");//TODO
254 if(elementNameToStore
.equals(PUBLISHER_NAME
)){
255 reference
.setPublisher(trimmedText
);
257 if(elementNameToStore
.equals(PUBLISHER_PLACE
)){
258 reference
.setPlacePublished(trimmedText
);
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){
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
);
272 } else if(trimmedText
.length() == 4) {
274 startYear
= Integer
.valueOf(trimmedText
);
275 } catch (NumberFormatException e
) {
276 logger
.error("date can not be parsed: "+ trimmedText
);
278 reference
.setDatePublished(TimePeriod
.NewInstance(startYear
));
281 if(elementNameToStore
.equals(VOLUME
)){
282 reference
.setVolume(trimmedText
);
284 if(elementNameToStore
.equals(EDITION
)){
285 reference
.setEdition(trimmedText
);
287 if(elementNameToStore
.equals(SPAGE
)){
288 reference
.setPages(trimmedText
);
290 if(elementNameToStore
.equals(EPAGE
)){
291 logger
.debug(elementNameToStore
+ " not yet implemented!");//TODO
293 if(elementNameToStore
.equals(PAGES
)){
294 // IGNORE we rather need the start page value SPAGE
296 if(elementNameToStore
.equals(PUBLICATION_FREQUENCY
)){
297 logger
.debug(elementNameToStore
+ " not yet implemented!");//TODO
299 if(elementNameToStore
.equals(LANGUAGE
)){
300 logger
.debug(elementNameToStore
+ " not yet implemented!");//TODO
302 if(elementNameToStore
.equals(OCLC
)){
303 logger
.debug(elementNameToStore
+ " not yet implemented!");//TODO
305 if(elementNameToStore
.equals(LCCN
)){
306 logger
.debug(elementNameToStore
+ " not yet implemented!");//TODO
308 if(elementNameToStore
.equals(ISSN
)){
309 reference
.setIssn(trimmedText
);
313 // --- Reference.authorTeam --- //
314 if(authorTeam
!= null && reference
!= null){
315 if(elementNameToStore
.equals("String")){
316 authorTeam
.addTeamMember(Person
.NewTitledInstance(trimmedText
));
320 // openUrlResponse //
321 if(reference
== null){
322 if(elementNameToStore
.equals(STATUS
)){
323 status
= ResponseStatus
.valueOf(trimmedText
);
327 elementNameToStore
= null;
334 * @see http://code.google.com/p/bhl-bits/source/browse/trunk/portal/OpenUrlUtilities/IOpenUrlResponse.cs
336 public enum ResponseStatus
{
337 Undefined
, // Query not submitted