1
|
/**
|
2
|
* Copyright (C) 2018 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.taxeditor.view.webimport.termimport.parser;
|
10
|
|
11
|
import java.util.Collection;
|
12
|
import java.util.LinkedList;
|
13
|
|
14
|
import org.json.JSONArray;
|
15
|
import org.json.JSONException;
|
16
|
import org.json.JSONObject;
|
17
|
|
18
|
import eu.etaxonomy.taxeditor.view.webimport.termimport.wrapper.HierarchyTermWrapper;
|
19
|
|
20
|
/**
|
21
|
* @author pplitzner
|
22
|
* @since May 9, 2018
|
23
|
*
|
24
|
*/
|
25
|
public class HierarchyParser {
|
26
|
public static Collection<HierarchyTermWrapper> parse(String response){
|
27
|
LinkedList<HierarchyTermWrapper> wrapperList = new LinkedList<>();
|
28
|
try {
|
29
|
JSONObject jsonResponse = new JSONObject(response);
|
30
|
JSONArray responseArray = jsonResponse.getJSONArray("results");
|
31
|
HierarchyTermWrapper childTerm = null;
|
32
|
for(int i=0;i<responseArray.length();i++){
|
33
|
JSONObject terminology = responseArray.getJSONObject(i);
|
34
|
String label = terminology.getString("label");
|
35
|
String uri = terminology.getString("uri");
|
36
|
HierarchyTermWrapper hierarchyTermWrapper = new HierarchyTermWrapper(uri, label);
|
37
|
wrapperList.add(hierarchyTermWrapper);
|
38
|
if(childTerm!=null){
|
39
|
childTerm.setParentTerm(hierarchyTermWrapper);
|
40
|
}
|
41
|
childTerm = hierarchyTermWrapper;
|
42
|
}
|
43
|
} catch (JSONException jsonException) {
|
44
|
jsonException.printStackTrace();
|
45
|
}
|
46
|
return wrapperList;
|
47
|
}
|
48
|
|
49
|
}
|