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
;
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
.SAXException
;
25 import org
.xml
.sax
.helpers
.DefaultHandler
;
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
;
36 * @author a.kohlbecker
39 public class MobotOpenUrlResponseSchemaAdapter
extends SchemaAdapterBase
<Reference
>{
41 static URI identifier
= null;
44 * @see eu.etaxonomy.cdm.ext.common.SchemaAdapterBase#getIdentifier()
47 public URI
getIdentifier() {
52 * @see eu.etaxonomy.cdm.ext.schema.SchemaAdapter#getShortName()
55 public String
getShortName() {
56 return "MOBOT.OpenUrl.Utilities.OpenUrlResponse";
60 * @see eu.etaxonomy.cdm.ext.schema.SchemaAdapter#getCmdEntities(java.io.Reader)
63 public List
<Reference
> getCmdEntities(InputStream inputStream
) throws IOException
{
65 SAXParserFactory factory
= SAXParserFactory
.newInstance();
66 factory
.setNamespaceAware(true);
67 SAXParser parser
= null;
69 parser
= factory
.newSAXParser();
70 } catch (ParserConfigurationException e
) {
72 } catch (SAXException e
) {
77 OpenUrlResponseHandler handler
= new OpenUrlResponseHandler();
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
: ""));
86 logger
.error("parser is null");
88 } catch (SAXException e
) {
93 return handler
.referenceList
;
96 class OpenUrlResponseHandler
extends DefaultHandler
{
99 * Fields of OpenUrlResponse
100 * see http://code.google.com/p/bhl-bits/source/browse/trunk/portal/OpenUrlUtilities/OpenUrlResponse.cs
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";
109 * Fields of OpenUrlResponseCitation
110 * see http://code.google.com/p/bhl-bits/source/browse/trunk/portal/OpenUrlUtilities/OpenUrlResponseCitation.cs
114 * references the specific page in the title
116 private static final String URL
= "Url";
118 * references the according entry in the bibliography
120 private static final String ITEM_URL
= "ItemUrl";
123 * references the specific book or journal, that is to the front page
125 private static final String TITLE_URL
= "TitleUrl";
126 private static final String TITLE
= "Title";
127 private static final String STITLE
= "STitle";
129 * seems to contain the type of the reference : book
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";
150 List
<Reference
> referenceList
= new ArrayList
<Reference
>();
152 OpenUrlReference reference
= null;
154 ResponseStatus status
= null;
155 Team authorTeam
= null;
156 String message
= null;
158 String elementName
= null;
159 private String elementNameToStore
;
160 private StringBuilder textBuffer
= new StringBuilder();
164 public void startElement(String uri
, String localName
,
165 String qName
, Attributes attributes
) throws SAXException
{
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
182 public void endElement(String uri
, String localName
, String qName
) throws SAXException
{
184 if (qName
.equals(OPENURL_RESPONSE
)) {
186 } else if (qName
.equals(OPENURL_RESPONSE_CITATION
)) {
187 referenceList
.add(reference
);
189 } else if (reference
!= null && qName
.equals(AUTHORS
)) {
190 reference
.setAuthorTeam(authorTeam
);
192 } else if (reference
!= null && qName
.equals(SUBJECTS
)) {
193 //TODO implement, but no equivalent in the cdm model
195 elementNameToStore
= elementName
;
202 public void characters(char ch
[], int start
, int length
)
203 throws SAXException
{
205 if(elementNameToStore
== null){
207 textBuffer
.append(new String(ch
, start
, length
));
211 logger
.debug("Characters [" + elementNameToStore
+ "]: " + textBuffer
);
212 String trimmedText
= textBuffer
.toString().trim();
213 // empty the text buffer
214 textBuffer
.delete(0, textBuffer
.length());
216 // --- Reference --- //
217 if(reference
!= null){
219 if(elementNameToStore
.equals(URL
)){
221 reference
.setUri(new URI(trimmedText
));
222 } catch (URISyntaxException e
) {
223 logger
.warn(e
.getMessage());
226 if(elementNameToStore
.equals(ITEM_URL
)){
228 reference
.setItemUri(new URI(trimmedText
));
229 } catch (URISyntaxException e
) {
230 logger
.warn(e
.getMessage());
233 if(elementNameToStore
.equals(TITLE_URL
)){
235 reference
.setTitleUri(new URI(trimmedText
));
236 } catch (URISyntaxException e
) {
237 logger
.warn(e
.getMessage());
240 if(elementNameToStore
.equals(TITLE
)){
241 reference
.setTitleCache(trimmedText
, true);
243 if(elementNameToStore
.equals(STITLE
)){
244 logger
.info(elementNameToStore
+ " not yet implemented!");//TODO
246 if(elementNameToStore
.equals(ATITLE
)){
247 logger
.info(elementNameToStore
+ " not yet implemented!");//TODO
249 if(elementNameToStore
.equals(PUBLISHER_NAME
)){
250 reference
.setPublisher(trimmedText
);
252 if(elementNameToStore
.equals(PUBLISHER_PLACE
)){
253 reference
.setPlacePublished(trimmedText
);
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){
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
);
267 } else if(trimmedText
.length() == 4) {
269 startYear
= Integer
.valueOf(trimmedText
);
270 } catch (NumberFormatException e
) {
271 logger
.error("date can not be parsed: "+ trimmedText
);
273 reference
.setDatePublished(TimePeriod
.NewInstance(startYear
));
276 if(elementNameToStore
.equals(VOLUME
)){
277 reference
.setVolume(trimmedText
);
279 if(elementNameToStore
.equals(EDITION
)){
280 reference
.setEdition(trimmedText
);
282 if(elementNameToStore
.equals(SPAGE
)){
283 reference
.setPages(trimmedText
);
285 if(elementNameToStore
.equals(EPAGE
)){
286 logger
.info(elementNameToStore
+ " not yet implemented!");//TODO
288 if(elementNameToStore
.equals(PAGES
)){
289 // IGNORE we rather need the start page value SPAGE
291 if(elementNameToStore
.equals(PUBLICATION_FREQUENCY
)){
292 logger
.info(elementNameToStore
+ " not yet implemented!");//TODO
294 if(elementNameToStore
.equals(LANGUAGE
)){
295 logger
.info(elementNameToStore
+ " not yet implemented!");//TODO
297 if(elementNameToStore
.equals(OCLC
)){
298 logger
.info(elementNameToStore
+ " not yet implemented!");//TODO
300 if(elementNameToStore
.equals(LCCN
)){
301 logger
.info(elementNameToStore
+ " not yet implemented!");//TODO
303 if(elementNameToStore
.equals(ISSN
)){
304 reference
.setIssn(trimmedText
);
308 // --- Reference.authorTeam --- //
309 if(authorTeam
!= null && reference
!= null){
310 if(elementNameToStore
.equals("String")){
311 authorTeam
.addTeamMember(Person
.NewTitledInstance(trimmedText
));
315 // openUrlResponse //
316 if(reference
== null){
317 if(elementNameToStore
.equals(STATUS
)){
318 status
= ResponseStatus
.valueOf(trimmedText
);
322 elementNameToStore
= null;
329 * @see http://code.google.com/p/bhl-bits/source/browse/trunk/portal/OpenUrlUtilities/IOpenUrlResponse.cs
331 public enum ResponseStatus
{
332 Undefined
, // Query not submitted