1
|
package eu.dnetlib;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.io.StringWriter;
|
5
|
import java.io.UnsupportedEncodingException;
|
6
|
import java.net.URLEncoder;
|
7
|
import java.time.Instant;
|
8
|
import java.time.LocalDateTime;
|
9
|
import java.time.ZoneId;
|
10
|
import java.time.format.DateTimeFormatter;
|
11
|
import javax.xml.transform.TransformerFactory;
|
12
|
|
13
|
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
|
14
|
import org.dom4j.Document;
|
15
|
import org.dom4j.DocumentException;
|
16
|
import org.dom4j.io.SAXReader;
|
17
|
import org.junit.Test;
|
18
|
import org.w3c.dom.Node;
|
19
|
import org.w3c.dom.NodeList;
|
20
|
|
21
|
import static org.junit.Assert.assertEquals;
|
22
|
|
23
|
/**
|
24
|
* Unit test for simple App.
|
25
|
*/
|
26
|
public class AppTest {
|
27
|
|
28
|
private TransformerFactory tf = TransformerFactory.newInstance();
|
29
|
|
30
|
@Test
|
31
|
public void testXslt() {
|
32
|
final String res = new ApplyXslt(getXslt(), tf).apply(getXml());
|
33
|
System.out.println("******************");
|
34
|
System.out.println(res);
|
35
|
System.out.println("******************");
|
36
|
}
|
37
|
|
38
|
@Test
|
39
|
public void testXpath() throws DocumentException {
|
40
|
final SAXReader reader = new SAXReader();
|
41
|
final Document doc = reader.read(new StringReader("<a><b>test</b></a>"));
|
42
|
assertEquals("", doc.valueOf("//c"));
|
43
|
assertEquals("test", doc.valueOf("//b"));
|
44
|
}
|
45
|
|
46
|
public static String javaMethod(final NodeList list) {
|
47
|
System.out.println("******************");
|
48
|
System.out.println("TYPE : " + list.toString());
|
49
|
System.out.println("LENGTH : " + list.getLength());
|
50
|
for (int i = 0; i < list.getLength(); i++) {
|
51
|
final Node node = list.item(i);
|
52
|
|
53
|
System.out.println("ELEM " + i + ": " + node.getLocalName());
|
54
|
}
|
55
|
|
56
|
System.out.println("******************");
|
57
|
return "ACUS";
|
58
|
}
|
59
|
|
60
|
private String getXml() {
|
61
|
final StringWriter sw = new StringWriter();
|
62
|
sw.append("<?xml version='1.0' encoding='UTF-8'?>\n");
|
63
|
sw.append("<record>\n");
|
64
|
sw.append("<metadata>\n");
|
65
|
sw.append("<a>A text value</a>\n");
|
66
|
sw.append("<b attr='attribute value'/>\n");
|
67
|
sw.append("</metadata>\n");
|
68
|
sw.append("</record>\n");
|
69
|
|
70
|
return sw.toString();
|
71
|
}
|
72
|
|
73
|
private String getXslt() {
|
74
|
final StringWriter sw = new StringWriter();
|
75
|
|
76
|
sw.append("<?xml version='1.0' encoding='UTF-8'?>\n");
|
77
|
sw.append(
|
78
|
"<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:dnet='eu.dnetlib.AppTest' exclude-result-prefixes='xsl dnet'>\n");
|
79
|
sw.append("<xsl:output omit-xml-declaration='yes' indent='yes'/>\n");
|
80
|
sw.append("<xsl:template match='/*'>\n");
|
81
|
sw.append("<xsl:variable name='metadata' select=\"//*[local-name()='metadata']/*\" />\n");
|
82
|
sw.append("<ROWS>\n");
|
83
|
sw.append("<xsl:value-of select='dnet:javaMethod($metadata)'/>\n");
|
84
|
sw.append("</ROWS>\n");
|
85
|
sw.append("</xsl:template>\n");
|
86
|
sw.append("</xsl:stylesheet>\n");
|
87
|
return sw.toString();
|
88
|
}
|
89
|
|
90
|
@Test
|
91
|
public void encodetest() throws UnsupportedEncodingException {
|
92
|
final String theUrl = "http://europepmc.org/GrantLookup/grants.php?&f%5B%5D=WT&range=all&format=xml&output=export";
|
93
|
final String encoded = URLEncoder.encode(theUrl, "UTF-8");
|
94
|
System.out.println(encoded);
|
95
|
}
|
96
|
|
97
|
@Test
|
98
|
public void testDate(){
|
99
|
LocalDateTime zdt = LocalDateTime.ofInstant(Instant.now(), ZoneId.of("Etc/UTC"));
|
100
|
zdt = zdt.minusHours(6);
|
101
|
String x = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(zdt);
|
102
|
System.out.println(x);
|
103
|
}
|
104
|
|
105
|
}
|