1
|
package eu.dnetlib.actionmanager.actions;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.List;
|
5
|
import javax.xml.transform.Transformer;
|
6
|
import javax.xml.transform.TransformerException;
|
7
|
|
8
|
import com.google.common.base.Predicate;
|
9
|
import com.google.common.collect.Iterables;
|
10
|
import com.google.common.collect.Lists;
|
11
|
import eu.dnetlib.actionmanager.ActionManagerConstants.ACTION_TYPE;
|
12
|
import eu.dnetlib.actionmanager.common.Agent;
|
13
|
import eu.dnetlib.actionmanager.common.Operation;
|
14
|
import eu.dnetlib.actionmanager.common.Provenance;
|
15
|
import org.apache.commons.codec.binary.Base64;
|
16
|
import org.apache.commons.lang.StringUtils;
|
17
|
import org.apache.commons.logging.Log;
|
18
|
import org.apache.commons.logging.LogFactory;
|
19
|
import org.dom4j.Document;
|
20
|
import org.dom4j.DocumentException;
|
21
|
import org.dom4j.Element;
|
22
|
import org.dom4j.io.DocumentResult;
|
23
|
import org.dom4j.io.DocumentSource;
|
24
|
import org.dom4j.io.SAXReader;
|
25
|
|
26
|
public class XsltInfoPackageAction extends AbstractAction {
|
27
|
|
28
|
private static final Log log = LogFactory.getLog(XsltInfoPackageAction.class); // NOPMD by marko on 11/24/08 5:02 PM
|
29
|
|
30
|
private Operation operation;
|
31
|
private String infoPackage;
|
32
|
private Provenance provenance;
|
33
|
private String trust;
|
34
|
private ActionFactory actionFactory;
|
35
|
private Transformer transformer;
|
36
|
|
37
|
public XsltInfoPackageAction(final String rawSet, final Agent agent, final Operation operation, final String infoPackage, final Provenance provenance,
|
38
|
final String trust, final Transformer transformer, final ActionFactory actionFactory) {
|
39
|
super(ACTION_TYPE.pkg, rawSet, agent);
|
40
|
|
41
|
this.transformer = transformer;
|
42
|
this.operation = operation;
|
43
|
this.infoPackage = infoPackage;
|
44
|
this.provenance = provenance;
|
45
|
this.trust = trust;
|
46
|
this.actionFactory = actionFactory;
|
47
|
this.transformer = transformer;
|
48
|
}
|
49
|
|
50
|
public List<AtomicAction> asAtomicActions() throws TransformerException, DocumentException {
|
51
|
return Lists.newArrayList(Iterables.filter(calculateAtomicActions(), new Predicate<AtomicAction>() {
|
52
|
@Override
|
53
|
public boolean apply(final AtomicAction action) {
|
54
|
return action != null && action.isValid();
|
55
|
}
|
56
|
}));
|
57
|
}
|
58
|
|
59
|
private Document applyXslt(final String xml, final Provenance provenance, final String trust) throws DocumentException,
|
60
|
TransformerException {
|
61
|
final Document doc = (new SAXReader()).read(new StringReader(xml));
|
62
|
|
63
|
final DocumentResult result = new DocumentResult();
|
64
|
|
65
|
transformer.setParameter("trust", trust);
|
66
|
transformer.setParameter("provenance", provenance.toString());
|
67
|
|
68
|
transformer.transform(new DocumentSource(doc), result);
|
69
|
|
70
|
return result.getDocument();
|
71
|
}
|
72
|
|
73
|
protected List<AtomicAction> calculateAtomicActions() throws TransformerException, DocumentException {
|
74
|
final List<AtomicAction> list = Lists.newArrayList();
|
75
|
|
76
|
final Document doc = applyXslt(getInfoPackage(), provenance, trust);
|
77
|
for (Object o : doc.selectNodes("//ROW")) {
|
78
|
list.add(createAtomicAction((Element) o));
|
79
|
}
|
80
|
|
81
|
return list;
|
82
|
}
|
83
|
|
84
|
private AtomicAction createAtomicAction(final Element elem) {
|
85
|
String key = elem.valueOf("./@key");
|
86
|
String colFamily = elem.valueOf("./@columnFamily");
|
87
|
String col = elem.valueOf("./QUALIFIER/@name");
|
88
|
final String text = elem.valueOf("./QUALIFIER");
|
89
|
byte[] value = StringUtils.isBlank(text) ? null : Base64.decodeBase64(text.trim());
|
90
|
|
91
|
return getActionFactory().createAtomicAction(getRawSet(), getAgent(), key, colFamily, col, value);
|
92
|
}
|
93
|
|
94
|
public Operation getOperation() {
|
95
|
return operation;
|
96
|
}
|
97
|
|
98
|
public void setOperation(final Operation operation) {
|
99
|
this.operation = operation;
|
100
|
}
|
101
|
|
102
|
public ActionFactory getActionFactory() {
|
103
|
return actionFactory;
|
104
|
}
|
105
|
|
106
|
public void setActionFactory(final ActionFactory actionFactory) {
|
107
|
this.actionFactory = actionFactory;
|
108
|
}
|
109
|
|
110
|
protected byte[] getBytesContent(final String content) {
|
111
|
return content.getBytes();
|
112
|
}
|
113
|
|
114
|
public String getInfoPackage() {
|
115
|
return infoPackage;
|
116
|
}
|
117
|
|
118
|
public void setInfoPackage(final String infoPackage) {
|
119
|
this.infoPackage = infoPackage;
|
120
|
}
|
121
|
|
122
|
public Provenance getProvenance() {
|
123
|
return provenance;
|
124
|
}
|
125
|
|
126
|
public void setProvenance(final Provenance provenance) {
|
127
|
this.provenance = provenance;
|
128
|
}
|
129
|
|
130
|
public String getTrust() {
|
131
|
return trust;
|
132
|
}
|
133
|
|
134
|
public void setTrust(final String trust) {
|
135
|
this.trust = trust;
|
136
|
}
|
137
|
|
138
|
public Transformer getTransformer() {
|
139
|
return transformer;
|
140
|
}
|
141
|
|
142
|
public void setTransformer(final Transformer transformer) {
|
143
|
this.transformer = transformer;
|
144
|
}
|
145
|
|
146
|
}
|