Project

General

Profile

1
package eu.dnetlib.data.transform.xml;
2

    
3
import java.lang.reflect.Type;
4
import java.util.List;
5

    
6
import com.google.gson.Gson;
7
import com.google.gson.reflect.TypeToken;
8
import org.apache.commons.lang.StringUtils;
9

    
10
/**
11
 * * <xsl:stylesheet ... xmlns:openTrials="eu.dnetlib.miscutils.functional.xml.OpenTrialsFunctions"> ... </xsl:stylesheet>
12
 * Created by alessia on 13/05/16.
13
 */
14
public class OpenTrialsXsltFunctions {
15

    
16
	/**
17
	 * Parses a json to get the first url item.
18
	 *
19
	 * @param jsonProvList A json string in the following format: [{"url" : "theUrl", "sourceID" : "theSourceId", "sourceName" : "theSourceName"}]
20
	 * @return the url value in the first object in the list
21
	 */
22
	public static String getMainIdentifierURL(String jsonProvList) {
23
		List<JsonProv> provs = getProvs(jsonProvList);
24
		for (JsonProv prov : provs) {
25
			if (StringUtils.isNotBlank(prov.getUrl())) return prov.getUrl();
26
		}
27
		return "";
28
	}
29

    
30

    
31
	public static List<JsonProv> getProvs(String jsonProvList) {
32
		Gson gson = new Gson();
33
		Type type = new TypeToken<List<JsonProv>>() {
34
		}.getType();
35
		return gson.fromJson(jsonProvList, type);
36
	}
37

    
38
	static class JsonProv {
39

    
40
		String url, sourceId, sourceName;
41

    
42
		public String getUrl() {
43
			return url;
44
		}
45

    
46
		public void setUrl(final String url) {
47
			this.url = url;
48
		}
49

    
50
		public String getSourceId() {
51
			return sourceId;
52
		}
53

    
54
		public void setSourceId(final String sourceId) {
55
			this.sourceId = sourceId;
56
		}
57

    
58
		public String getSourceName() {
59
			return sourceName;
60
		}
61

    
62
		public void setSourceName(final String sourceName) {
63
			this.sourceName = sourceName;
64
		}
65
	}
66
}
(8-8/9)