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.Iterator;
44 import java.util.Set;
45
46 import javax.jcr.Node;
47 import javax.jcr.RepositoryException;
48
49 import org.apache.commons.lang.StringUtils;
50
51
52
53
54
55
56
57 public class Rule implements Serializable {
58
59
60
61
62 private static final long serialVersionUID = 222L;
63
64
65
66
67 private Set<String> allowedTypes = new HashSet<String>();
68
69
70
71
72 private boolean reverse = false;
73
74 public Rule() {
75 }
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 public Rule(String allowedTypes, String separator) {
93 String[] types = StringUtils.split(allowedTypes, separator);
94 for (int j = 0; j < types.length; j++) {
95 this.addAllowType(types[j]);
96 }
97 }
98
99
100
101
102
103 public void addAllowType(String nodeType) {
104 if (nodeType != null) {
105 this.allowedTypes.add(nodeType);
106 }
107 }
108
109
110
111
112
113 public void removeAllowType(String nodeType) {
114 if (nodeType != null) {
115 this.allowedTypes.remove(nodeType);
116 }
117 }
118
119
120
121
122
123
124 public boolean isAllowed(String nodeType) {
125 boolean allowed = this.allowedTypes.contains(nodeType);
126 if (this.reverse) {
127 return !allowed;
128 }
129
130 return allowed;
131
132 }
133
134
135
136
137
138 public boolean isAllowed(Node node) throws RepositoryException {
139 boolean allowed = false;
140
141 for(String allowedType:this.allowedTypes){
142 allowed |= NodeUtil.isNodeType(node, allowedType);
143 }
144
145 if (this.reverse) {
146 return !allowed;
147 }
148
149 return allowed;
150 }
151
152
153
154
155
156 @Override
157 public String toString() {
158 StringBuffer buffer = new StringBuffer();
159 Iterator<String> typeIterator = allowedTypes.iterator();
160 while (typeIterator.hasNext()) {
161 buffer.append(typeIterator.next());
162 buffer.append(",");
163 }
164 return buffer.toString();
165 }
166
167
168
169
170 public void reverse() {
171 this.reverse = true;
172 }
173
174 private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
175 final ObjectInputStream.GetField field = ois.readFields();
176 final Object o = field.get("allowedTypes", null);
177 if (o instanceof String[]){
178 Set<String> typesCol = new HashSet<String>();
179 typesCol.addAll(Arrays.asList((String[]) o));
180 this.allowedTypes = typesCol;
181 }
182 if(o instanceof HashSet){
183 this.allowedTypes = (Set<String>) o;
184 }
185 this.reverse = field.get("reverse", false);
186 }
187 }