Project

General

Profile

Download (2.36 KB) Statistics
| Branch: | Tag: | Revision:
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.ArrayList;
12
import java.util.Collection;
13
import java.util.List;
14

    
15
import org.json.JSONArray;
16
import org.json.JSONException;
17
import org.json.JSONObject;
18

    
19
import eu.etaxonomy.taxeditor.view.webimport.termimport.wrapper.OntologyTermWrapper;
20

    
21
/**
22
 * @author pplitzner
23
 * @since May 9, 2018
24
 *
25
 */
26
public class TermParser {
27
    public static OntologyTermWrapper parseSingleTerm(String response){
28
        try {
29
            JSONObject jsonResponse = new JSONObject(response);
30
            JSONArray responseArray = ParserUtil.parseResults(jsonResponse);
31
            for(int i=0;i<responseArray.length();i++){
32
                JSONObject jsonObject = responseArray.getJSONObject(i);
33
                return createWrapper(jsonObject);
34
            }
35
        } catch (JSONException e) {
36
            e.printStackTrace();
37
        }
38
        return null;
39
    }
40

    
41
    public static Collection<OntologyTermWrapper> parse(String response){
42
        List<OntologyTermWrapper> wrapperList = new ArrayList<>();
43
        try {
44
            JSONObject jsonResponse = new JSONObject(response);
45
            JSONArray responseArray = ParserUtil.parseResults(jsonResponse);
46
            for(int i=0;i<responseArray.length();i++){
47
                JSONObject jsonObject = responseArray.getJSONObject(i);
48
                wrapperList.add(createWrapper(jsonObject));
49
            }
50
        } catch (JSONException e) {
51
            e.printStackTrace();
52
        }
53
        return wrapperList;
54
    }
55

    
56
    private static OntologyTermWrapper createWrapper(JSONObject jsonObject) throws JSONException{
57
        String description = ParserUtil.parseDescription(jsonObject);
58
        String label = ParserUtil.parseLabel(jsonObject);
59
        String uri= ParserUtil.parseUri(jsonObject);
60
        String terminology= ParserUtil.parseTerminology(jsonObject);
61
        List<String> synonyms = ParserUtil.parseSynonyms(jsonObject);
62
        OntologyTermWrapper wrapper = new OntologyTermWrapper(uri, label, terminology);
63
        wrapper.setSynonyms(synonyms);
64
        wrapper.setDescription(description);
65
        return wrapper;
66
    }
67

    
68
}
(5-5/6)