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.bci;
|
10
|
|
11
|
import java.io.IOException;
|
12
|
import java.io.InputStream;
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.List;
|
15
|
|
16
|
import javax.xml.parsers.ParserConfigurationException;
|
17
|
import javax.xml.parsers.SAXParser;
|
18
|
import javax.xml.parsers.SAXParserFactory;
|
19
|
|
20
|
import org.xml.sax.Attributes;
|
21
|
import org.xml.sax.SAXException;
|
22
|
import org.xml.sax.helpers.DefaultHandler;
|
23
|
|
24
|
import eu.etaxonomy.cdm.common.URI;
|
25
|
import eu.etaxonomy.cdm.ext.common.SchemaAdapterBase;
|
26
|
import eu.etaxonomy.cdm.model.agent.Team;
|
27
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
28
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
29
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
30
|
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
|
31
|
|
32
|
/**
|
33
|
* @author a.kohlbecker
|
34
|
* @since 25.08.2010
|
35
|
*/
|
36
|
public class BciSchemaAdapter extends SchemaAdapterBase<Reference>{
|
37
|
|
38
|
static URI identifier = null;
|
39
|
|
40
|
static String nameSpace = "http://purl.org/dc/elements/1.1/";
|
41
|
|
42
|
static {
|
43
|
identifier = URI.create("info:srw/schema/1/dc-v1.1");
|
44
|
}
|
45
|
|
46
|
@Override
|
47
|
public URI getIdentifier() {
|
48
|
return identifier;
|
49
|
}
|
50
|
|
51
|
@Override
|
52
|
public String getShortName() {
|
53
|
return "dc";
|
54
|
}
|
55
|
|
56
|
@Override
|
57
|
public List<Reference> getCmdEntities(InputStream inputStream) {
|
58
|
|
59
|
SAXParserFactory factory = SAXParserFactory.newInstance();
|
60
|
factory.setNamespaceAware(true);
|
61
|
SAXParser parser = null;
|
62
|
try {
|
63
|
parser = factory.newSAXParser();
|
64
|
} catch (ParserConfigurationException e) {
|
65
|
logger.error(e);
|
66
|
} catch (SAXException e) {
|
67
|
logger.error(e);
|
68
|
}
|
69
|
|
70
|
DcSaxHandler handler = new DcSaxHandler();
|
71
|
|
72
|
try {
|
73
|
if(parser != null){
|
74
|
parser.parse(inputStream, handler);
|
75
|
} else {
|
76
|
logger.error("parser is null");
|
77
|
}
|
78
|
} catch (SAXException e) {
|
79
|
logger.error(e);
|
80
|
} catch (IOException e) {
|
81
|
logger.error(e);
|
82
|
}
|
83
|
|
84
|
return handler.referenceList;
|
85
|
}
|
86
|
|
87
|
class DcSaxHandler extends DefaultHandler {
|
88
|
|
89
|
private static final String DC_DC = "dc:dc";
|
90
|
|
91
|
private static final String DC_TITLE = "dc:title";
|
92
|
private static final String DC_CREATOR = "dc:creator";
|
93
|
private static final String DC_PUBLISHER = "dc:publisher";
|
94
|
private static final String DC_DATE = "dc:date";
|
95
|
|
96
|
private List<Reference> referenceList = new ArrayList<>();
|
97
|
|
98
|
private Reference reference = null;
|
99
|
|
100
|
private String dcFieldName = null;
|
101
|
|
102
|
@Override
|
103
|
public void startElement(String uri, String localName,
|
104
|
String qName, Attributes attributes) throws SAXException {
|
105
|
|
106
|
if (uri.equals(nameSpace)) {
|
107
|
logger.debug("Start Element :" + qName + "; " + uri);
|
108
|
|
109
|
if (qName.equals(DC_DC)) {
|
110
|
reference = ReferenceFactory.newGeneric();
|
111
|
} else {
|
112
|
dcFieldName = qName;
|
113
|
}
|
114
|
}
|
115
|
}
|
116
|
|
117
|
@Override
|
118
|
public void endElement(String uri, String localName, String qName)
|
119
|
throws SAXException {
|
120
|
|
121
|
if (uri.equals(nameSpace)) {
|
122
|
if(reference != null) {
|
123
|
logger.debug("End Element :" + qName + "; " + uri);
|
124
|
|
125
|
if (qName.equals(DC_DC)) {
|
126
|
referenceList.add(reference);
|
127
|
reference = null;
|
128
|
} else {
|
129
|
dcFieldName = null;
|
130
|
}
|
131
|
}
|
132
|
}
|
133
|
}
|
134
|
|
135
|
@Override
|
136
|
public void characters(char ch[], int start, int length)
|
137
|
throws SAXException {
|
138
|
|
139
|
if(reference != null && dcFieldName != null){
|
140
|
String text = new String(ch, start, length);
|
141
|
logger.debug("Characters : " + text);
|
142
|
if(dcFieldName.equals(DC_TITLE)){
|
143
|
reference.setTitleCache(text, true);
|
144
|
}
|
145
|
if(dcFieldName.equals(DC_DATE)){
|
146
|
reference.setDatePublished(TimePeriodParser.parseStringVerbatim(text));
|
147
|
}
|
148
|
if(dcFieldName.equals(DC_PUBLISHER)){
|
149
|
reference.setPublisher(text);
|
150
|
}
|
151
|
if(dcFieldName.equals(DC_CREATOR)){
|
152
|
TeamOrPersonBase<?> authorship = new Team();
|
153
|
authorship.setTitleCache(text, true);
|
154
|
reference.setAuthorship(authorship);
|
155
|
}
|
156
|
}
|
157
|
}
|
158
|
}
|
159
|
}
|