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