1
|
package org.bgbm.biovel.drf.checklist;
|
2
|
|
3
|
|
4
|
import java.io.IOException;
|
5
|
import java.io.StringReader;
|
6
|
import java.io.StringWriter;
|
7
|
import java.io.Writer;
|
8
|
import java.net.URI;
|
9
|
import java.util.EnumSet;
|
10
|
import java.util.HashMap;
|
11
|
import java.util.Map;
|
12
|
|
13
|
import javax.xml.bind.JAXBException;
|
14
|
import javax.xml.parsers.DocumentBuilder;
|
15
|
import javax.xml.parsers.DocumentBuilderFactory;
|
16
|
import javax.xml.parsers.ParserConfigurationException;
|
17
|
import javax.xml.transform.Source;
|
18
|
import javax.xml.transform.Transformer;
|
19
|
import javax.xml.transform.TransformerException;
|
20
|
import javax.xml.transform.TransformerFactory;
|
21
|
import javax.xml.transform.stream.StreamResult;
|
22
|
import javax.xml.transform.stream.StreamSource;
|
23
|
import javax.xml.xpath.XPathConstants;
|
24
|
import javax.xml.xpath.XPathExpression;
|
25
|
import javax.xml.xpath.XPathExpressionException;
|
26
|
import javax.xml.xpath.XPathFactory;
|
27
|
|
28
|
import org.apache.http.HttpHost;
|
29
|
import org.bgbm.biovel.drf.client.ServiceProviderInfo;
|
30
|
import org.bgbm.biovel.drf.query.RestClient;
|
31
|
import org.bgbm.biovel.drf.tnr.msg.Query;
|
32
|
import org.bgbm.biovel.drf.tnr.msg.Response;
|
33
|
import org.bgbm.biovel.drf.tnr.msg.TnrMsg;
|
34
|
import org.bgbm.biovel.drf.utils.BiovelUtils;
|
35
|
import org.bgbm.biovel.drf.utils.IdentifierUtils;
|
36
|
import org.bgbm.biovel.drf.utils.TnrMsgUtils;
|
37
|
import org.w3c.dom.Document;
|
38
|
import org.w3c.dom.Node;
|
39
|
import org.xml.sax.InputSource;
|
40
|
import org.xml.sax.SAXException;
|
41
|
|
42
|
|
43
|
public class Species2000ColClient extends BaseChecklistClient<RestClient> {
|
44
|
|
45
|
/**
|
46
|
*
|
47
|
*/
|
48
|
private static final HttpHost HTTP_HOST = new HttpHost("www.catalogueoflife.org",80);
|
49
|
public static final String ID = "species2000col";
|
50
|
public static final String LABEL = "Species2000 - Catalogue Of Life";
|
51
|
public static final String URL = "http://www.catalogueoflife.org";
|
52
|
public static final String DATA_AGR_URL = "http://www.catalogueoflife.org/col/info/copyright";
|
53
|
|
54
|
public static final EnumSet<SearchMode> SEARCH_MODES = EnumSet.of(SearchMode.scientificNameExact);
|
55
|
|
56
|
|
57
|
public Species2000ColClient() {
|
58
|
super();
|
59
|
}
|
60
|
|
61
|
/**
|
62
|
* {@inheritDoc}
|
63
|
*/
|
64
|
@Override
|
65
|
public void initQueryClient() {
|
66
|
queryClient = new RestClient(HTTP_HOST);
|
67
|
|
68
|
}
|
69
|
|
70
|
@Override
|
71
|
public ServiceProviderInfo buildServiceProviderInfo() {
|
72
|
ServiceProviderInfo checklistInfo = new ServiceProviderInfo(ID,LABEL,URL,DATA_AGR_URL, getSearchModes());
|
73
|
return checklistInfo;
|
74
|
}
|
75
|
|
76
|
|
77
|
@Override
|
78
|
public void resolveScientificNamesExact(TnrMsg tnrMsg) throws DRFChecklistException {
|
79
|
|
80
|
Query query = singleQueryFrom(tnrMsg);
|
81
|
|
82
|
//http://www.catalogueoflife.org/col/webservice?response=full&name={sciName}
|
83
|
|
84
|
|
85
|
Map<String, String> paramMap = new HashMap<String, String>();
|
86
|
|
87
|
paramMap.put("response", "full");
|
88
|
|
89
|
URI taxonUri = queryClient.buildUriFromQuery(query,
|
90
|
"/col/webservice",
|
91
|
"name",
|
92
|
paramMap);
|
93
|
|
94
|
String responseBody = queryClient.get(taxonUri);
|
95
|
|
96
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
97
|
DocumentBuilder parser;
|
98
|
try {
|
99
|
parser = factory.newDocumentBuilder();
|
100
|
|
101
|
Document doc = parser.parse(new InputSource(new StringReader(responseBody)));
|
102
|
XPathExpression xp = XPathFactory.newInstance().newXPath().compile("/results/result[1]/name_status");
|
103
|
Node nameStatusNode = (Node) xp.evaluate(doc, XPathConstants.NODE);
|
104
|
|
105
|
if(nameStatusNode != null) {
|
106
|
String nameStatus = nameStatusNode.getTextContent();
|
107
|
if(nameStatus.equals("synonym")) {
|
108
|
xp = XPathFactory.newInstance().newXPath().compile("/results/result[1]/accepted_name/id");
|
109
|
Node synTaxonIdNode = (Node) xp.evaluate(doc, XPathConstants.NODE);
|
110
|
|
111
|
if(synTaxonIdNode != null) {
|
112
|
String synTaxonId = synTaxonIdNode.getTextContent();
|
113
|
//http://www.catalogueoflife.org/col/webservice?response=full&id={sciId}
|
114
|
taxonUri = queryClient.buildUriFromQueryString(synTaxonId,
|
115
|
"/col/webservice",
|
116
|
"id",
|
117
|
paramMap);
|
118
|
|
119
|
responseBody = queryClient.get(taxonUri);
|
120
|
} else {
|
121
|
responseBody = null;
|
122
|
}
|
123
|
}
|
124
|
}
|
125
|
|
126
|
updateQueryWithResponse(query, responseBody);
|
127
|
|
128
|
} catch (ParserConfigurationException e) {
|
129
|
// TODO Auto-generated catch block
|
130
|
e.printStackTrace();
|
131
|
|
132
|
} catch (SAXException e) {
|
133
|
// TODO Auto-generated catch block
|
134
|
e.printStackTrace();
|
135
|
} catch (IOException e) {
|
136
|
// TODO Auto-generated catch block
|
137
|
e.printStackTrace();
|
138
|
} catch (XPathExpressionException e) {
|
139
|
// TODO Auto-generated catch block
|
140
|
e.printStackTrace();
|
141
|
}
|
142
|
|
143
|
}
|
144
|
|
145
|
private void updateQueryWithResponse(Query query, String colXMLResponse) {
|
146
|
|
147
|
Source inSource = new StreamSource(new StringReader(colXMLResponse));
|
148
|
Source xslSource = new StreamSource(new StringReader(BiovelUtils.getResourceAsString("/org/bgbm/biovel/drf/tnr/colres_to_synres.xsl","UTF-8")));
|
149
|
|
150
|
// the factory pattern supports different XSLT processors
|
151
|
// e.g. set the "javax.xml.transform.TransformerFactory" system property
|
152
|
TransformerFactory tnfFact = TransformerFactory.newInstance();
|
153
|
Transformer tnf;
|
154
|
|
155
|
try {
|
156
|
tnf = tnfFact.newTransformer(xslSource);
|
157
|
|
158
|
Writer outputWriter = new StringWriter();
|
159
|
|
160
|
tnf.transform(inSource, new StreamResult(outputWriter));
|
161
|
|
162
|
String tnrResponseXML = outputWriter.toString();
|
163
|
System.out.println(tnrResponseXML);
|
164
|
Response tnrResponse = TnrMsgUtils.convertXMLToTnrResponse(tnrResponseXML);
|
165
|
query.getResponse().add(tnrResponse);
|
166
|
} catch (TransformerException e) {
|
167
|
// TODO Auto-generated catch block
|
168
|
e.printStackTrace();
|
169
|
} catch (JAXBException e) {
|
170
|
// TODO Auto-generated catch block
|
171
|
e.printStackTrace();
|
172
|
} catch (ParserConfigurationException e) {
|
173
|
// TODO Auto-generated catch block
|
174
|
e.printStackTrace();
|
175
|
} catch (SAXException e) {
|
176
|
// TODO Auto-generated catch block
|
177
|
e.printStackTrace();
|
178
|
} catch (IOException e) {
|
179
|
// TODO Auto-generated catch block
|
180
|
e.printStackTrace();
|
181
|
}
|
182
|
|
183
|
|
184
|
}
|
185
|
|
186
|
@Override
|
187
|
public void resolveScientificNamesLike(TnrMsg tnrMsg) throws DRFChecklistException {
|
188
|
// TODO Auto-generated method stub
|
189
|
|
190
|
}
|
191
|
|
192
|
@Override
|
193
|
public void resolveVernacularNamesExact(TnrMsg tnrMsg) throws DRFChecklistException {
|
194
|
// TODO Auto-generated method stub
|
195
|
|
196
|
}
|
197
|
|
198
|
@Override
|
199
|
public EnumSet<SearchMode> getSearchModes() {
|
200
|
return SEARCH_MODES;
|
201
|
}
|
202
|
|
203
|
@Override
|
204
|
public void resolveVernacularNamesLike(TnrMsg tnrMsg) throws DRFChecklistException {
|
205
|
// TODO Auto-generated method stub
|
206
|
|
207
|
}
|
208
|
|
209
|
@Override
|
210
|
public boolean isSupportedIdentifier(String value) {
|
211
|
return IdentifierUtils.checkLSID(value);
|
212
|
}
|
213
|
|
214
|
@Override
|
215
|
public void findByIdentifier(TnrMsg tnrMsg) throws DRFChecklistException {
|
216
|
// TODO Auto-generated method stub
|
217
|
|
218
|
}
|
219
|
|
220
|
|
221
|
}
|
222
|
|
223
|
|