1 13 package info.magnolia.cms.util; 14 15 import java.io.Serializable ; 16 import java.util.Iterator ; 17 18 import org.apache.commons.collections.IteratorUtils; 19 import org.apache.commons.lang.ArrayUtils; 20 import org.apache.commons.lang.StringUtils; 21 22 23 29 public class Rule implements Serializable { 30 31 34 private static final long serialVersionUID = 222L; 35 36 39 private String [] allowedTypes = new String [0]; 40 41 44 private boolean reverse = false; 45 46 49 public Rule() { 50 } 51 52 56 public Rule(String [] allowedTypes) { 57 for (int j = 0; j < allowedTypes.length; j++) { 58 this.addAllowType(allowedTypes[j]); 59 } 60 } 61 62 67 public Rule(String allowedTypes, String separator) { 68 String [] types = StringUtils.split(allowedTypes, separator); 69 for (int j = 0; j < types.length; j++) { 70 this.addAllowType(types[j]); 71 } 72 } 73 74 78 public void addAllowType(String nodeType) { 79 if (nodeType != null) { 80 this.allowedTypes = (String []) ArrayUtils.add(allowedTypes, nodeType); 81 } 82 } 83 84 88 public void removeAllowType(String nodeType) { 89 if (nodeType != null) { 90 for (int j = 0; j < allowedTypes.length; j++) { 91 if (nodeType.equals(allowedTypes[j])) { 92 this.allowedTypes = (String []) ArrayUtils.remove(allowedTypes, j); 93 break; 94 } 95 } 96 } 97 } 98 99 104 public boolean isAllowed(String nodeType) { 105 boolean allowed = ArrayUtils.contains(allowedTypes, nodeType); 106 if (this.reverse) { 107 return !allowed; 108 } 109 110 return allowed; 111 112 } 113 114 118 public String toString() { 119 StringBuffer buffer = new StringBuffer (); 120 Iterator typeIterator = IteratorUtils.arrayIterator(allowedTypes); 121 while (typeIterator.hasNext()) { 122 buffer.append((String ) typeIterator.next()); 123 buffer.append(","); 124 } 125 return new String (buffer); 126 } 127 128 131 public void reverse() { 132 this.reverse = true; 133 } 134 135 } 136 | Popular Tags |