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 @Deprecated
137 public boolean isAllowed(String nodeType) {
138 boolean allowed = this.allowedTypes.contains(nodeType);
139 if (this.reverse) {
140 return !allowed;
141 }
142
143 return allowed;
144
145 }
146
147
148
149
150 public boolean isAllowed(Node node) throws RepositoryException {
151 boolean allowed = false;
152
153 for (String allowedType : this.allowedTypes) {
154 allowed |= NodeUtil.isNodeType(node, allowedType);
155 }
156
157 if (this.reverse) {
158 return !allowed;
159 }
160
161 return allowed;
162 }
163
164
165
166
167
168
169
170 @Override
171 public String toString() {
172 final StringBuilder builder = new StringBuilder();
173 builder.append(allowedTypes.toString());
174 builder.append("; ");
175 builder.append(REVERSE_LABEL);
176 builder.append(reverse);
177 return builder.toString();
178 }
179
180
181
182
183 public void reverse() {
184 this.reverse = true;
185 }
186
187 private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
188 final ObjectInputStream.GetField field = ois.readFields();
189 final Object o = field.get("allowedTypes", null);
190 if (o instanceof String[]) {
191 Set<String> typesCol = new HashSet<String>();
192 typesCol.addAll(Arrays.asList((String[]) o));
193 this.allowedTypes = typesCol;
194 }
195 if (o instanceof HashSet) {
196 this.allowedTypes = (Set<String>) o;
197 }
198 this.reverse = field.get("reverse", false);
199 }
200 }