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.dc;
|
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.Team;
|
29
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
30
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
31
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
32
|
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
|
33
|
|
34
|
|
35
|
|
36
|
/**
|
37
|
* @author a.kohlbecker
|
38
|
* @date 25.08.2010
|
39
|
*/
|
40
|
public class DublinCoreSchemaAdapter extends SchemaAdapterBase<Reference>{
|
41
|
|
42
|
|
43
|
|
44
|
static URI identifier = null;
|
45
|
|
46
|
static String nameSpace = "http://purl.org/dc/elements/1.1/";
|
47
|
|
48
|
static {
|
49
|
try {
|
50
|
identifier = new URI("info:srw/schema/1/dc-v1.1");
|
51
|
} catch (URISyntaxException e) {
|
52
|
// should never happen
|
53
|
}
|
54
|
}
|
55
|
|
56
|
/* (non-Javadoc)
|
57
|
* @see eu.etaxonomy.cdm.ext.schema.SchemaAdapter#getIdentifier()
|
58
|
*/
|
59
|
@Override
|
60
|
public URI getIdentifier() {
|
61
|
return identifier;
|
62
|
}
|
63
|
|
64
|
/* (non-Javadoc)
|
65
|
* @see eu.etaxonomy.cdm.ext.schema.SchemaAdapter#getShortName()
|
66
|
*/
|
67
|
@Override
|
68
|
public String getShortName() {
|
69
|
return "dc";
|
70
|
}
|
71
|
|
72
|
/* (non-Javadoc)
|
73
|
* @see eu.etaxonomy.cdm.ext.schema.SchemaAdapter#getCmdEntities(java.io.Reader)
|
74
|
*/
|
75
|
@Override
|
76
|
public List<Reference> getCmdEntities(InputStream inputStream) {
|
77
|
|
78
|
SAXParserFactory factory = SAXParserFactory.newInstance();
|
79
|
factory.setNamespaceAware(true);
|
80
|
SAXParser parser = null;
|
81
|
try {
|
82
|
parser = factory.newSAXParser();
|
83
|
} catch (ParserConfigurationException e) {
|
84
|
logger.error(e);
|
85
|
} catch (SAXException e) {
|
86
|
logger.error(e);
|
87
|
}
|
88
|
|
89
|
|
90
|
DcSaxHandler handler = new DcSaxHandler();
|
91
|
|
92
|
try {
|
93
|
if(parser != null){
|
94
|
parser.parse(inputStream, handler);
|
95
|
} else {
|
96
|
logger.error("parser is null");
|
97
|
}
|
98
|
} catch (SAXException e) {
|
99
|
logger.error(e);
|
100
|
} catch (IOException e) {
|
101
|
logger.error(e);
|
102
|
}
|
103
|
|
104
|
|
105
|
return handler.referenceList;
|
106
|
}
|
107
|
|
108
|
class DcSaxHandler extends DefaultHandler {
|
109
|
|
110
|
private static final String DC_DC = "dc:dc";
|
111
|
|
112
|
private static final String DC_TITLE = "dc:title";
|
113
|
private static final String DC_CREATOR = "dc:creator";
|
114
|
private static final String DC_PUBLISHER = "dc:publisher";
|
115
|
private static final String DC_DATE = "dc:date";
|
116
|
private static final String DC_IDENTIFIER = "dc:identifier"; //TODO map this
|
117
|
|
118
|
List<Reference> referenceList = new ArrayList<Reference>();
|
119
|
|
120
|
Reference reference = null;
|
121
|
|
122
|
String dcFieldName = null;
|
123
|
|
124
|
|
125
|
|
126
|
@Override
|
127
|
public void startElement(String uri, String localName,
|
128
|
String qName, Attributes attributes) throws SAXException {
|
129
|
|
130
|
if (uri.equals(nameSpace)) {
|
131
|
logger.debug("Start Element :" + qName + "; " + uri);
|
132
|
|
133
|
if (qName.equals(DC_DC)) {
|
134
|
reference = ReferenceFactory.newGeneric();
|
135
|
} else {
|
136
|
dcFieldName = qName;
|
137
|
}
|
138
|
}
|
139
|
}
|
140
|
|
141
|
@Override
|
142
|
public void endElement(String uri, String localName, String qName)
|
143
|
throws SAXException {
|
144
|
|
145
|
if (uri.equals(nameSpace)) {
|
146
|
if(reference != null) {
|
147
|
logger.debug("End Element :" + qName + "; " + uri);
|
148
|
|
149
|
if (qName.equals(DC_DC)) {
|
150
|
referenceList.add(reference);
|
151
|
reference = null;
|
152
|
} else {
|
153
|
dcFieldName = null;
|
154
|
}
|
155
|
}
|
156
|
}
|
157
|
|
158
|
}
|
159
|
|
160
|
@Override
|
161
|
public void characters(char ch[], int start, int length)
|
162
|
throws SAXException {
|
163
|
|
164
|
if(reference != null && dcFieldName != null){
|
165
|
String text = new String(ch, start, length);
|
166
|
logger.debug("Characters : " + text);
|
167
|
if(dcFieldName.equals(DC_TITLE)){
|
168
|
reference.setTitleCache(text, true);
|
169
|
}
|
170
|
if(dcFieldName.equals(DC_DATE)){
|
171
|
reference.setDatePublished(TimePeriodParser.parseString(text));
|
172
|
}
|
173
|
if(dcFieldName.equals(DC_PUBLISHER)){
|
174
|
reference.setPublisher(text);
|
175
|
}
|
176
|
if(dcFieldName.equals(DC_CREATOR)){
|
177
|
TeamOrPersonBase authorship = new Team();
|
178
|
authorship.setTitleCache(text, true);
|
179
|
reference.setAuthorship(authorship);
|
180
|
}
|
181
|
|
182
|
}
|
183
|
}
|
184
|
|
185
|
}
|
186
|
|
187
|
}
|