Project

General

Profile

1
package eu.dnetlib.data.collector.plugins.schemaorg;
2

    
3
import org.json.JSONArray;
4
import org.json.JSONObject;
5
import org.w3c.dom.Document;
6
import org.w3c.dom.NodeList;
7
import org.xml.sax.InputSource;
8

    
9
import javax.xml.parsers.DocumentBuilder;
10
import javax.xml.parsers.DocumentBuilderFactory;
11
import javax.xml.xpath.XPath;
12
import javax.xml.xpath.XPathConstants;
13
import javax.xml.xpath.XPathExpression;
14
import javax.xml.xpath.XPathFactory;
15
import java.io.File;
16
import java.io.FileInputStream;
17
import java.io.FileOutputStream;
18
import java.io.StringReader;
19
import java.nio.charset.Charset;
20
import java.nio.charset.UnsupportedCharsetException;
21
import java.util.ArrayList;
22
import java.util.EnumSet;
23
import java.util.HashMap;
24
import java.util.List;
25
import java.util.zip.GZIPInputStream;
26

    
27
public class Utils {
28

    
29
	public static List<String> collectAsStrings(String xml, String xpath) throws Exception{
30
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
31
		DocumentBuilder builder = factory.newDocumentBuilder();
32
		Document doc = builder.parse(new InputSource(new StringReader(xml)));
33
		return Utils.collectAsStrings(doc, xpath);
34
	}
35

    
36
	public static List<String> collectAsStrings(File file, String xpath) throws Exception{
37
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
38
		DocumentBuilder builder = factory.newDocumentBuilder();
39
		Document doc = builder.parse(file);
40
		return Utils.collectAsStrings(doc, xpath);
41
	}
42

    
43
	public static List<String> collectAsStrings(Document doc, String xpath) throws Exception{
44
		XPathFactory xPathfactory = XPathFactory.newInstance();
45
		XPath path = xPathfactory.newXPath();
46
		XPathExpression expr = path.compile(xpath);
47
		NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
48

    
49
		List<String> values = new ArrayList<>();
50

    
51
		for (int i = 0; i < nodes.getLength(); i++)
52
			values.add(nodes.item(i).getNodeValue());
53

    
54
		return values;
55
	}
56

    
57
	public static void decompressGZipTo(File input, File output) throws Exception {
58
		try (GZIPInputStream in = new GZIPInputStream(new FileInputStream(input))){
59
			try (FileOutputStream out = new FileOutputStream(output)){
60
				byte[] buffer = new byte[1024];
61
				int len;
62
				while((len = in.read(buffer)) != -1){
63
					out.write(buffer, 0, len);
64
				}
65
			}
66
		}
67
	}
68

    
69
	public static String getAsString(HashMap<String,String> map, String key, String defaultValue)
70
	{
71
		String value = map.get(key);
72
		if(value == null) return defaultValue;
73
		return value;
74
	}
75

    
76
	public static List<String> getAsStringCsv(HashMap<String,String> map, String key, List<String> defaultValue)
77
	{
78
		String value = map.get(key);
79
		if(value == null) return defaultValue;
80
		String[] splits = value.split(",");
81
		List<String> curated = new ArrayList<>();
82
		for(String item : splits){
83
			if(item == null || item.trim().length() == 0) continue;
84
			curated.add(item.trim());
85
		}
86
		return curated;
87
	}
88

    
89
	public static int getAsInt(HashMap<String,String> map, String key, int defaultValue)
90
	{
91
		String value = map.get(key);
92
		if(value == null) return defaultValue;
93
		try {
94
			return Integer.parseInt(value);
95
		} catch (NumberFormatException e) {
96
			return defaultValue;
97
		}
98
	}
99

    
100
	public static long getAsLong(HashMap<String,String> map, String key, long defaultValue)
101
	{
102
		String value = map.get(key);
103
		if(value == null) return defaultValue;
104
		try {
105
			return Long.parseLong(value);
106
		} catch (NumberFormatException e) {
107
			return defaultValue;
108
		}
109
	}
110

    
111
	public static <E extends Enum<E>> E getAsEnum(HashMap<String,String> map, String key, E defaultValue, Class<E> clazz) {
112
		//EnumSet<E> values = EnumSet.allOf(defaultValue.getClass());
113
		EnumSet<E> values = EnumSet.allOf(clazz);
114
		String value = map.get(key);
115
		if (value == null) return defaultValue;
116
		for(E val : values){
117
			if(!val.name().equalsIgnoreCase(value)) continue;
118
			return val;
119
		}
120
		return defaultValue;
121
	}
122

    
123
	public static Boolean getAsBoolean(HashMap<String,String> map, String key, Boolean defaultValue) {
124
		String value = map.get(key);
125
		if (value == null) return defaultValue;
126
		return Boolean.parseBoolean(value);
127
	}
128

    
129
	public static Charset getAsCharset(HashMap<String,String> map, String key, Charset defaultValue)
130
	{
131
		String value = map.get(key);
132
		if(value == null) return defaultValue;
133
		try {
134
			return Charset.forName(value);
135
		} catch (UnsupportedCharsetException e) {
136
			return defaultValue;
137
		}
138
	}
139

    
140
}
(11-11/11)