2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.ext
.openurl
;
11 import java
.io
.IOException
;
12 import java
.io
.InputStream
;
13 import java
.io
.InputStreamReader
;
14 import java
.io
.Reader
;
15 import java
.net
.URISyntaxException
;
16 import java
.util
.ArrayList
;
17 import java
.util
.List
;
19 import javax
.xml
.parsers
.ParserConfigurationException
;
20 import javax
.xml
.parsers
.SAXParser
;
21 import javax
.xml
.parsers
.SAXParserFactory
;
23 import org
.xml
.sax
.Attributes
;
24 import org
.xml
.sax
.InputSource
;
25 import org
.xml
.sax
.SAXException
;
26 import org
.xml
.sax
.helpers
.DefaultHandler
;
28 import eu
.etaxonomy
.cdm
.common
.URI
;
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
;
36 * @author a.kohlbecker
39 public class MobotOpenUrlResponseSchemaAdapter
extends SchemaAdapterBase
<Reference
>{
41 static URI identifier
= null;
44 public URI
getIdentifier() {
49 public String
getShortName() {
50 return "MOBOT.OpenUrl.Utilities.OpenUrlResponse";
54 public List
<Reference
> getCmdEntities(InputStream inputStream
) throws IOException
{
56 SAXParserFactory factory
= SAXParserFactory
.newInstance();
57 factory
.setNamespaceAware(true);
58 SAXParser parser
= null;
60 parser
= factory
.newSAXParser();
61 } catch (ParserConfigurationException e
) {
63 } catch (SAXException e
) {
67 OpenUrlResponseHandler handler
= new OpenUrlResponseHandler();
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
: ""));
78 logger
.error("parser is null");
80 } catch (SAXException e
) {
85 return handler
.referenceList
;
88 class OpenUrlResponseHandler
extends DefaultHandler
{
91 * Fields of OpenUrlResponse
92 * see http://code.google.com/p/bhl-bits/source/browse/trunk/portal/OpenUrlUtilities/OpenUrlResponse.cs
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";
101 * Fields of OpenUrlResponseCitation
102 * see http://code.google.com/p/bhl-bits/source/browse/trunk/portal/OpenUrlUtilities/OpenUrlResponseCitation.cs
106 * references the specific page in the title
108 private static final String URL
= "Url";
110 * references the according entry in the bibliography
112 private static final String ITEM_URL
= "ItemUrl";
115 * references the specific book or journal, that is to the front page
117 private static final String TITLE_URL
= "TitleUrl";
118 private static final String TITLE
= "Title";
119 private static final String STITLE
= "STitle";
121 * seems to contain the type of the reference : book
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";
141 List
<Reference
> referenceList
= new ArrayList
<>();
143 OpenUrlReference reference
= null;
145 ResponseStatus status
= null;
146 Team authorship
= null;
147 String message
= null;
149 String elementName
= null;
150 private String elementNameToStore
;
151 private StringBuilder textBuffer
= new StringBuilder();
154 public void startElement(String uri
, String localName
,
155 String qName
, Attributes attributes
) throws SAXException
{
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
172 public void endElement(String uri
, String localName
, String qName
) throws SAXException
{
174 if (qName
.equals(OPENURL_RESPONSE
)) {
176 } else if (qName
.equals(OPENURL_RESPONSE_CITATION
)) {
177 referenceList
.add(reference
);
179 } else if (reference
!= null && qName
.equals(AUTHORS
)) {
180 reference
.setAuthorship(authorship
);
182 } else if (reference
!= null && qName
.equals(SUBJECTS
)) {
183 //TODO implement, but no equivalent in the cdm model
185 elementNameToStore
= elementName
;
192 public void characters(char ch
[], int start
, int length
)
193 throws SAXException
{
195 if(elementNameToStore
== null){
196 textBuffer
.append(new String(ch
, start
, length
));
199 logger
.debug("Characters [" + elementNameToStore
+ "]: " + textBuffer
);
200 String trimmedText
= textBuffer
.toString().trim();
201 // empty the text buffer
202 textBuffer
.delete(0, textBuffer
.length());
204 // --- Reference --- //
205 if(reference
!= null){
207 if(elementNameToStore
.equals(URL
)){
209 reference
.setUri(new URI(trimmedText
));
210 } catch (URISyntaxException e
) {
211 logger
.warn(e
.getMessage());
214 if(elementNameToStore
.equals(ITEM_URL
)){
216 reference
.setItemUri(new URI(trimmedText
));
217 } catch (URISyntaxException e
) {
218 logger
.warn(e
.getMessage());
221 if(elementNameToStore
.equals(TITLE_URL
)){
223 reference
.setTitleUri(new URI(trimmedText
));
224 } catch (URISyntaxException e
) {
225 logger
.warn(e
.getMessage());
228 if(elementNameToStore
.equals(TITLE
)){
229 reference
.setTitleCache(trimmedText
, true);
231 if(elementNameToStore
.equals(STITLE
)){
232 logger
.debug(elementNameToStore
+ " not yet implemented!");//TODO
234 if(elementNameToStore
.equals(ATITLE
)){
235 logger
.debug(elementNameToStore
+ " not yet implemented!");//TODO
237 if(elementNameToStore
.equals(PUBLISHER_NAME
)){
238 reference
.setPublisher(trimmedText
);
240 if(elementNameToStore
.equals(PUBLISHER_PLACE
)){
241 reference
.setPlacePublished(trimmedText
);
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){
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
);
255 } else if(trimmedText
.length() == 4) {
257 startYear
= Integer
.valueOf(trimmedText
);
258 } catch (NumberFormatException e
) {
259 logger
.error("date can not be parsed: "+ trimmedText
);
261 reference
.setDatePublished(VerbatimTimePeriod
.NewVerbatimInstance(startYear
));
264 if(elementNameToStore
.equals(VOLUME
)){
265 reference
.setVolume(trimmedText
);
267 if(elementNameToStore
.equals(EDITION
)){
268 reference
.setEdition(trimmedText
);
270 if(elementNameToStore
.equals(SPAGE
)){
271 reference
.setPages(trimmedText
);
273 if(elementNameToStore
.equals(EPAGE
)){
274 logger
.debug(elementNameToStore
+ " not yet implemented!");//TODO
276 if(elementNameToStore
.equals(PAGES
)){
277 // IGNORE we rather need the start page value SPAGE
279 if(elementNameToStore
.equals(PUBLICATION_FREQUENCY
)){
280 logger
.debug(elementNameToStore
+ " not yet implemented!");//TODO
282 if(elementNameToStore
.equals(LANGUAGE
)){
283 logger
.debug(elementNameToStore
+ " not yet implemented!");//TODO
285 if(elementNameToStore
.equals(OCLC
)){
286 logger
.debug(elementNameToStore
+ " not yet implemented!");//TODO
288 if(elementNameToStore
.equals(LCCN
)){
289 logger
.debug(elementNameToStore
+ " not yet implemented!");//TODO
291 if(elementNameToStore
.equals(ISSN
)){
292 reference
.setIssn(trimmedText
);
296 // --- Reference.authorship --- //
297 if(authorship
!= null && reference
!= null){
298 if(elementNameToStore
.equals("String")){
299 authorship
.addTeamMember(Person
.NewTitledInstance(trimmedText
));
303 // openUrlResponse //
304 if(reference
== null){
305 if(elementNameToStore
.equals(STATUS
)){
306 status
= ResponseStatus
.valueOf(trimmedText
);
310 elementNameToStore
= null;
317 * @see http://code.google.com/p/bhl-bits/source/browse/trunk/portal/OpenUrlUtilities/IOpenUrlResponse.cs
319 public enum ResponseStatus
{
320 Undefined
, // Query not submitted