1
|
package eu.dnetlib.utils.parser;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.text.ParseException;
|
5
|
import java.text.SimpleDateFormat;
|
6
|
import java.util.Date;
|
7
|
|
8
|
import javax.xml.xpath.XPath;
|
9
|
import javax.xml.xpath.XPathConstants;
|
10
|
import javax.xml.xpath.XPathExpression;
|
11
|
import javax.xml.xpath.XPathExpressionException;
|
12
|
import javax.xml.xpath.XPathFactory;
|
13
|
|
14
|
import org.apache.log4j.Logger;
|
15
|
import org.w3c.dom.NodeList;
|
16
|
import org.xml.sax.InputSource;
|
17
|
|
18
|
public class Parser {
|
19
|
private String xml = null;
|
20
|
private XPathFactory factory = XPathFactory.newInstance();
|
21
|
private XPath xPath = factory.newXPath();
|
22
|
|
23
|
private Logger logger = Logger.getLogger(Parser.class);
|
24
|
|
25
|
public Parser(String xml) {
|
26
|
this.xml = xml;
|
27
|
}
|
28
|
|
29
|
public String getValue(String field) {
|
30
|
NodeList nodes = extractXPath(field);
|
31
|
if (nodes != null && nodes.getLength() > 0) {
|
32
|
return nodes.item(0).getAttributes().getNamedItem("value").getNodeValue();
|
33
|
}
|
34
|
|
35
|
return null;
|
36
|
}
|
37
|
|
38
|
public String getValue(String field, String[] values) {
|
39
|
if (field.split("\\.").length <= values.length) {
|
40
|
logger.debug("Wrong value request");
|
41
|
return null;
|
42
|
}
|
43
|
|
44
|
NodeList nodes = extractXPath(field, values);
|
45
|
if (nodes != null && nodes.getLength() > 0) {
|
46
|
return nodes.item(0).getAttributes().getNamedItem("value").getNodeValue();
|
47
|
}
|
48
|
|
49
|
return null;
|
50
|
}
|
51
|
|
52
|
public Boolean getBoolean(String field) {
|
53
|
NodeList nodes = extractXPath(field);
|
54
|
if (nodes != null && nodes.getLength() > 0) {
|
55
|
return Boolean.parseBoolean(nodes.item(0).getAttributes().getNamedItem("value").getNodeValue());
|
56
|
}
|
57
|
|
58
|
return null;
|
59
|
}
|
60
|
|
61
|
public Date getDate(String field, String format) {
|
62
|
NodeList nodes = extractXPath(field);
|
63
|
try {
|
64
|
if (nodes != null && nodes.getLength() > 0) {
|
65
|
return new SimpleDateFormat(format).parse(nodes.item(0).getAttributes().getNamedItem("value").getNodeValue());
|
66
|
}
|
67
|
} catch (ParseException pe) {
|
68
|
logger.debug("Parsing of date failed. ", pe);
|
69
|
|
70
|
} catch (IllegalArgumentException iae) {
|
71
|
logger.debug("Parsing of date failed. ", iae);
|
72
|
}
|
73
|
|
74
|
return null;
|
75
|
}
|
76
|
|
77
|
public Integer getInteger(String field) {
|
78
|
NodeList nodes = extractXPath(field);
|
79
|
try {
|
80
|
if (nodes != null && nodes.getLength() > 0) {
|
81
|
return Integer.parseInt(nodes.item(0).getAttributes().getNamedItem("value").getNodeValue());
|
82
|
}
|
83
|
|
84
|
} catch (NumberFormatException pe) {
|
85
|
logger.debug("Parsing integer failed. " + pe);
|
86
|
}
|
87
|
|
88
|
return null;
|
89
|
}
|
90
|
|
91
|
public String[] getValues(String field) {
|
92
|
NodeList nodes = extractXPath(field);
|
93
|
|
94
|
if (nodes != null && nodes.getLength() > 0) {
|
95
|
String[] values = new String[nodes.getLength()];
|
96
|
for (int i = 0; i < nodes.getLength(); i++) {
|
97
|
values[i] = nodes.item(i).getAttributes().getNamedItem("value").getNodeValue();
|
98
|
}
|
99
|
return values;
|
100
|
}
|
101
|
|
102
|
return null;
|
103
|
}
|
104
|
|
105
|
public String[] getValues(String field, String[] values) {
|
106
|
if (field.split("\\.").length <= values.length) {
|
107
|
logger.debug("Wrong value request");
|
108
|
return null;
|
109
|
}
|
110
|
|
111
|
NodeList nodes = extractXPath(field, values);
|
112
|
if (nodes != null && nodes.getLength() > 0) {
|
113
|
String[] fieldvalues = new String[nodes.getLength()];
|
114
|
for (int i = 0; i < nodes.getLength(); i++) {
|
115
|
fieldvalues[i] = nodes.item(i).getAttributes().getNamedItem("value").getNodeValue();
|
116
|
}
|
117
|
return fieldvalues;
|
118
|
}
|
119
|
return null;
|
120
|
}
|
121
|
|
122
|
|
123
|
public Integer[] getIntegers(String field) {
|
124
|
NodeList nodes = extractXPath(field);
|
125
|
if (nodes != null && nodes.getLength() > 0) {
|
126
|
Integer[] values = new Integer[nodes.getLength()];
|
127
|
for (int i = 0; i < nodes.getLength(); i++) {
|
128
|
try {
|
129
|
values[i] = Integer.parseInt(nodes.item(i).getAttributes().getNamedItem("value").getNodeValue());
|
130
|
} catch (NumberFormatException pe) {
|
131
|
values[i] = null;
|
132
|
logger.debug("Parsing integer failed. " + pe);
|
133
|
}
|
134
|
}
|
135
|
return values;
|
136
|
}
|
137
|
|
138
|
return null;
|
139
|
}
|
140
|
|
141
|
private NodeList extractXPath(String field) {
|
142
|
String expression = "/result";
|
143
|
String[] fieldnames = field.split("\\.");
|
144
|
|
145
|
for ( int i=0; i < fieldnames.length; i++ ) {
|
146
|
expression += "/field[@name=\"" + fieldnames[i] +"\"]";
|
147
|
}
|
148
|
|
149
|
try {
|
150
|
XPathExpression xPathExpression = xPath.compile(expression);
|
151
|
NodeList nodes = (NodeList) xPathExpression.evaluate(new InputSource(new StringReader(xml)), XPathConstants.NODESET);
|
152
|
return nodes;
|
153
|
|
154
|
} catch (XPathExpressionException e) {
|
155
|
return null;
|
156
|
}
|
157
|
}
|
158
|
|
159
|
private NodeList extractXPath(String field, Object values) {
|
160
|
String expression = "/result";
|
161
|
String[] fieldnames = field.split("\\.");
|
162
|
String[] fieldvalues = (String[]) values;
|
163
|
|
164
|
for ( int i=0; i < fieldnames.length; i++ ) {
|
165
|
if (i < fieldnames.length-1) {
|
166
|
expression += "/field[@name=\"" + fieldnames[i] +"\"]" + "[@value=\"" + fieldvalues[i] + "\"]";
|
167
|
} else {
|
168
|
expression += "/field[@name=\"" + fieldnames[i] +"\"]";
|
169
|
}
|
170
|
}
|
171
|
|
172
|
try {
|
173
|
XPathExpression xPathExpression = xPath.compile(expression);
|
174
|
NodeList nodes = (NodeList) xPathExpression.evaluate(new InputSource(new StringReader(xml)), XPathConstants.NODESET);
|
175
|
if(nodes.getLength() == 0) return null;
|
176
|
|
177
|
return nodes;
|
178
|
|
179
|
} catch (XPathExpressionException e) {
|
180
|
return null;
|
181
|
}
|
182
|
}
|
183
|
|
184
|
|
185
|
}
|