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 package info.magnolia.cms.util;
35
36 import info.magnolia.jcr.util.NodeUtil;
37
38 import java.io.IOException;
39 import java.io.ObjectInputStream;
40 import java.io.Serializable;
41 import java.util.Arrays;
42 import java.util.HashSet;
43 import java.util.LinkedHashSet;
44 import java.util.Set;
45
46 import javax.jcr.Node;
47 import javax.jcr.RepositoryException;
48
49 import org.apache.commons.lang3.StringUtils;
50
51
52
53
54
55 public class Rule implements Serializable {
56
57
58
59
60
61 private static final long serialVersionUID = 222L;
62
63
64
65
66 private Set<String> allowedTypes = new LinkedHashSet<String>();
67
68
69
70
71 private boolean reverse = false;
72
73 private final String REVERSE_LABEL = "Reverse:";
74
75 public Rule() {
76 }
77
78
79
80
81 public Rule(String[] allowedTypes) {
82 for (int j = 0; j < allowedTypes.length; j++) {
83 this.addAllowType(allowedTypes[j]);
84 }
85 }
86
87
88
89
90
91
92
93 public Rule(String ruleStringRepresentation, String separator) {
94 String nodeTypes = ruleStringRepresentation;
95 if (StringUtils.contains(ruleStringRepresentation, "[") && StringUtils.contains(ruleStringRepresentation, "]")) {
96 nodeTypes = StringUtils.substringBetween(ruleStringRepresentation, "[", "]").replaceAll("\\s", "");
97 reverse = Boolean.parseBoolean(StringUtils.substringAfter(ruleStringRepresentation, REVERSE_LABEL));
98 }
99
100 String[] types = StringUtils.split(nodeTypes, separator);
101 for (int j = 0; j < types.length; j++) {
102 this.addAllowType(types[j]);
103 }
104 }
105
106 public void setAllowedTypes(String[] types) {
107 for (String t : types) {
108 this.addAllowType(t);
109 }
110 }
111
112
113
114
115 public void addAllowType(String nodeType) {
116 if (nodeType != null) {
117 this.allowedTypes.add(nodeType);
118 }
119 }
120
121
122
123
124 public void removeAllowType(String nodeType) {
125 if (nodeType != null) {
126 this.allowedTypes.remove(nodeType);
127 }
128 }
129
130
131
132
133
134
135
136 public boolean isAllowed(String nodeType) {
137 boolean allowed = this.allowedTypes.contains(nodeType);
138 if (this.reverse) {
139 return !allowed;
140 }
141
142 return allowed;
143
144 }
145
146
147
148
149 public boolean isAllowed(Node node) throws RepositoryException {
150 boolean allowed = false;
151
152 for (String allowedType : this.allowedTypes) {
153 allowed |= NodeUtil.isNodeType(node, allowedType);
154 }
155
156 if (this.reverse) {
157 return !allowed;
158 }
159
160 return allowed;
161 }
162
163
164
165
166
167
168
169 @Override
170 public String toString() {
171 final StringBuilder builder = new StringBuilder();
172 builder.append(allowedTypes.toString());
173 builder.append("; ");
174 builder.append(REVERSE_LABEL);
175 builder.append(reverse);
176 return builder.toString();
177 }
178
179
180
181
182 public void reverse() {
183 this.reverse = true;
184 }
185
186 private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
187 final ObjectInputStream.GetField field = ois.readFields();
188 final Object o = field.get("allowedTypes", null);
189 if (o instanceof String[]) {
190 Set<String> typesCol = new HashSet<String>();
191 typesCol.addAll(Arrays.asList((String[]) o));
192 this.allowedTypes = typesCol;
193 }
194 if (o instanceof HashSet) {
195 this.allowedTypes = (Set<String>) o;
196 }
197 this.reverse = field.get("reverse", false);
198 }
199 }