1
|
package eu.dnetlib.enabling.tools;
|
2
|
|
3
|
import java.io.StringWriter;
|
4
|
import java.util.Date;
|
5
|
|
6
|
import javax.xml.transform.Transformer;
|
7
|
import javax.xml.transform.TransformerConfigurationException;
|
8
|
import javax.xml.transform.TransformerException;
|
9
|
import javax.xml.transform.TransformerFactory;
|
10
|
import javax.xml.transform.TransformerFactoryConfigurationError;
|
11
|
import javax.xml.transform.dom.DOMSource;
|
12
|
import javax.xml.transform.stream.StreamResult;
|
13
|
import javax.xml.xpath.XPath;
|
14
|
import javax.xml.xpath.XPathConstants;
|
15
|
import javax.xml.xpath.XPathExpressionException;
|
16
|
import javax.xml.xpath.XPathFactory;
|
17
|
|
18
|
import org.apache.commons.logging.Log;
|
19
|
import org.apache.commons.logging.LogFactory;
|
20
|
import org.w3c.dom.Document;
|
21
|
import org.w3c.dom.Element;
|
22
|
|
23
|
import eu.dnetlib.miscutils.datetime.DateUtils;
|
24
|
|
25
|
/**
|
26
|
* OpaqueResource holding a plain old DOM document.
|
27
|
*
|
28
|
* @author marko
|
29
|
*
|
30
|
*/
|
31
|
public class DOMOpaqueResource implements OpaqueResource {
|
32
|
|
33
|
/**
|
34
|
* xpath expression error message.
|
35
|
*/
|
36
|
private static final String XPATH_ERROR = "cannot compile xpath expression";
|
37
|
|
38
|
/**
|
39
|
* value attribute.
|
40
|
*/
|
41
|
private static final String VALUE_ATTR = "value";
|
42
|
|
43
|
/**
|
44
|
* logger.
|
45
|
*/
|
46
|
private static final Log log = LogFactory.getLog(DOMOpaqueResource.class); // NOPMD by marko on 11/24/08 5:02 PM
|
47
|
|
48
|
/**
|
49
|
* resource identifier.
|
50
|
*/
|
51
|
private String resourceId;
|
52
|
|
53
|
/**
|
54
|
* resource type.
|
55
|
*/
|
56
|
private String resourceType;
|
57
|
|
58
|
/**
|
59
|
* resource kind.
|
60
|
*/
|
61
|
private String resourceKind;
|
62
|
|
63
|
/**
|
64
|
* resource uri.
|
65
|
*/
|
66
|
private String resourceUri;
|
67
|
|
68
|
/**
|
69
|
* modification time stamp.
|
70
|
*/
|
71
|
private Date modificationDate;
|
72
|
|
73
|
/**
|
74
|
* original document DOM.
|
75
|
*/
|
76
|
private Document dom;
|
77
|
|
78
|
/**
|
79
|
* xslt transformer instance.
|
80
|
*/
|
81
|
private Transformer transformer;
|
82
|
|
83
|
/**
|
84
|
* construct a DOMOpaqueInstance from a W3C DOM document.
|
85
|
*
|
86
|
* @param dom
|
87
|
* DOM document
|
88
|
* @throws XPathExpressionException
|
89
|
* happens
|
90
|
*/
|
91
|
public DOMOpaqueResource(final Document dom) throws XPathExpressionException {
|
92
|
this.dom = dom;
|
93
|
|
94
|
try {
|
95
|
this.transformer = TransformerFactory.newInstance().newTransformer();
|
96
|
} catch (final TransformerConfigurationException e) {
|
97
|
throw new IllegalStateException("transformer configuration", e);
|
98
|
} catch (final TransformerFactoryConfigurationError e) {
|
99
|
throw new IllegalStateException("transformer configuration", e);
|
100
|
}
|
101
|
|
102
|
final XPath xpath = XPathFactory.newInstance().newXPath();
|
103
|
this.resourceId = xpath.evaluate("/RESOURCE_PROFILE/HEADER/RESOURCE_IDENTIFIER/@value", dom);
|
104
|
this.resourceType = xpath.evaluate("/RESOURCE_PROFILE/HEADER/RESOURCE_TYPE/@value", dom);
|
105
|
this.resourceKind = xpath.evaluate("/RESOURCE_PROFILE/HEADER/RESOURCE_KIND/@value", dom);
|
106
|
this.resourceUri = xpath.evaluate("/RESOURCE_PROFILE/HEADER/RESOURCE_URI/@value", dom);
|
107
|
|
108
|
final String modificationDateSource = xpath.evaluate("/RESOURCE_PROFILE/HEADER/DATE_OF_CREATION/@value", dom);
|
109
|
try {
|
110
|
this.modificationDate = new DateUtils().parse(modificationDateSource);
|
111
|
} catch (final IllegalStateException e) {
|
112
|
log.debug("invalid date '" + modificationDateSource + "'", e);
|
113
|
}
|
114
|
}
|
115
|
|
116
|
/**
|
117
|
* {@inheritDoc}
|
118
|
*
|
119
|
* @see eu.dnetlib.enabling.tools.OpaqueResource#asDom()
|
120
|
*/
|
121
|
@Override
|
122
|
public Document asDom() {
|
123
|
return getDom();
|
124
|
}
|
125
|
|
126
|
/**
|
127
|
* {@inheritDoc}
|
128
|
*
|
129
|
* @see eu.dnetlib.enabling.tools.OpaqueResource#asString()
|
130
|
*/
|
131
|
@Override
|
132
|
public String asString() {
|
133
|
final StringWriter writer = new StringWriter();
|
134
|
|
135
|
try {
|
136
|
this.transformer.transform(new DOMSource(getDom()), new StreamResult(writer));
|
137
|
} catch (final TransformerException e) {
|
138
|
log.fatal("cannot serialize document", e);
|
139
|
return null;
|
140
|
}
|
141
|
return writer.toString();
|
142
|
}
|
143
|
|
144
|
public Document getDom() {
|
145
|
return this.dom;
|
146
|
}
|
147
|
|
148
|
public void setDom(final Document dom) {
|
149
|
this.dom = dom;
|
150
|
}
|
151
|
|
152
|
@Override
|
153
|
public String getResourceId() {
|
154
|
return this.resourceId;
|
155
|
}
|
156
|
|
157
|
/**
|
158
|
* {@inheritDoc}
|
159
|
*
|
160
|
* @see eu.dnetlib.enabling.tools.OpaqueResource#setResourceId(java.lang.String)
|
161
|
*/
|
162
|
@Override
|
163
|
public void setResourceId(final String identifier) {
|
164
|
try {
|
165
|
final XPath xpath = XPathFactory.newInstance().newXPath();
|
166
|
final Element idEl = (Element) xpath.evaluate("/RESOURCE_PROFILE/HEADER/RESOURCE_IDENTIFIER", asDom(), XPathConstants.NODE);
|
167
|
idEl.setAttribute(VALUE_ATTR, identifier);
|
168
|
this.resourceId = identifier;
|
169
|
} catch (final XPathExpressionException e) {
|
170
|
throw new IllegalStateException(XPATH_ERROR, e);
|
171
|
}
|
172
|
}
|
173
|
|
174
|
@Override
|
175
|
public String getResourceType() {
|
176
|
return this.resourceType;
|
177
|
}
|
178
|
|
179
|
public void setResourceType(final String resourceType) {
|
180
|
this.resourceType = resourceType;
|
181
|
}
|
182
|
|
183
|
@Override
|
184
|
public String getResourceKind() {
|
185
|
return this.resourceKind;
|
186
|
}
|
187
|
|
188
|
/**
|
189
|
* {@inheritDoc}
|
190
|
*
|
191
|
* @see eu.dnetlib.enabling.tools.OpaqueResource#setResourceKind(java.lang.String)
|
192
|
*/
|
193
|
@Override
|
194
|
public void setResourceKind(final String resourceKind) {
|
195
|
try {
|
196
|
final XPath xpath = XPathFactory.newInstance().newXPath();
|
197
|
final Element kindEl = (Element) xpath.evaluate("/RESOURCE_PROFILE/HEADER/RESOURCE_KIND", asDom(), XPathConstants.NODE);
|
198
|
kindEl.setAttribute(VALUE_ATTR, resourceKind);
|
199
|
this.resourceKind = resourceKind;
|
200
|
} catch (final XPathExpressionException e) {
|
201
|
throw new IllegalStateException(XPATH_ERROR, e);
|
202
|
}
|
203
|
|
204
|
}
|
205
|
|
206
|
@Override
|
207
|
public Date getModificationDate() {
|
208
|
return this.modificationDate;
|
209
|
}
|
210
|
|
211
|
/**
|
212
|
* {@inheritDoc}
|
213
|
*
|
214
|
* @see eu.dnetlib.enabling.tools.OpaqueResource#setModificationDate(java.util.Date)
|
215
|
*/
|
216
|
@Override
|
217
|
public void setModificationDate(final Date modificationDate) {
|
218
|
try {
|
219
|
final XPath xpath = XPathFactory.newInstance().newXPath();
|
220
|
final Element idEl = (Element) xpath.evaluate("/RESOURCE_PROFILE/HEADER/DATE_OF_CREATION", asDom(), XPathConstants.NODE);
|
221
|
if (idEl == null) {
|
222
|
log.warn("resource with type " + getResourceType() + " has no date of creation element");
|
223
|
return;
|
224
|
}
|
225
|
idEl.setAttribute(VALUE_ATTR, new DateUtils(modificationDate).getDateAsISO8601String());
|
226
|
this.modificationDate = modificationDate;
|
227
|
} catch (final XPathExpressionException e) {
|
228
|
throw new IllegalStateException(XPATH_ERROR, e);
|
229
|
}
|
230
|
|
231
|
}
|
232
|
|
233
|
@Override
|
234
|
public String getResourceUri() {
|
235
|
return this.resourceUri;
|
236
|
}
|
237
|
|
238
|
/**
|
239
|
* {@inheritDoc}
|
240
|
*
|
241
|
* @see eu.dnetlib.enabling.tools.OpaqueResource#setResourceUri(java.lang.String)
|
242
|
*/
|
243
|
@Override
|
244
|
public void setResourceUri(final String resourceUri) {
|
245
|
try {
|
246
|
final XPath xpath = XPathFactory.newInstance().newXPath();
|
247
|
final Element uriEl = (Element) xpath.evaluate("/RESOURCE_PROFILE/HEADER/RESOURCE_URI", asDom(), XPathConstants.NODE);
|
248
|
uriEl.setAttribute(VALUE_ATTR, resourceUri);
|
249
|
this.resourceUri = resourceUri;
|
250
|
} catch (final XPathExpressionException e) {
|
251
|
throw new IllegalStateException(XPATH_ERROR, e);
|
252
|
}
|
253
|
}
|
254
|
|
255
|
public Transformer getTransformer() {
|
256
|
return this.transformer;
|
257
|
}
|
258
|
|
259
|
public void setTransformer(final Transformer transformer) {
|
260
|
this.transformer = transformer;
|
261
|
}
|
262
|
|
263
|
}
|