/
QualifierModelImpl.java
97 lines (78 loc) · 3.06 KB
/
QualifierModelImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package org.drools.kproject.models;
import java.util.HashMap;
import java.util.Map;
import org.drools.core.util.AbstractXStreamConverter;
import org.kie.builder.QualifierModel;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
public class QualifierModelImpl implements QualifierModel {
private String type;
private String value;
private Map<String, String> arguments = new HashMap<String, String>();
public QualifierModelImpl() { }
public QualifierModelImpl(String type) {
this.type = type;
}
public String getType() {
return type;
}
private void setType(String type) {
this.type = type;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public QualifierModel addArgument(String key, String value) {
arguments.put(key, value);
return this;
}
public Map<String, String> getArguments() {
return arguments;
}
boolean isSimple() {
return value == null && arguments.isEmpty();
}
public static class QualifierConverter extends AbstractXStreamConverter {
public QualifierConverter() {
super(QualifierModelImpl.class);
}
public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
QualifierModelImpl qualifier = (QualifierModelImpl) value;
writer.addAttribute("type", qualifier.getType());
if (qualifier.getValue() != null) {
writer.addAttribute("value", qualifier.getValue());
} else {
for (Map.Entry<String, String> entry : qualifier.getArguments().entrySet()) {
writer.startNode("arg");
writer.addAttribute("key", entry.getKey());
writer.addAttribute("value", entry.getValue());
writer.endNode();
}
}
}
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
final QualifierModelImpl qualifier = new QualifierModelImpl();
qualifier.setType(reader.getAttribute("type"));
String value = reader.getAttribute("value");
if (value != null) {
qualifier.setValue(value);
} else {
readNodes( reader, new AbstractXStreamConverter.NodeReader() {
public void onNode(HierarchicalStreamReader reader,
String name,
String value) {
if ( "arg".equals( name ) ) {
qualifier.addArgument(reader.getAttribute("key"), reader.getAttribute("value"));
}
}
} );
}
return qualifier;
}
}
}