KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > util > Rule


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.util;
14
15 import java.io.Serializable JavaDoc;
16 import java.util.Iterator JavaDoc;
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 /**
24  * This class defines the rules to be used by the activation content aggregator this is simply a collection of node
25  * types
26  * @author Sameer Charles
27  * @version $Revision: 6341 $ ($Author: philipp $)
28  */

29 public class Rule implements Serializable JavaDoc {
30
31     /**
32      * generated Stable serialVersionUID.
33      */

34     private static final long serialVersionUID = 222L;
35
36     /**
37      * list of node types allowed
38      */

39     private String JavaDoc[] allowedTypes = new String JavaDoc[0];
40
41     /**
42      * reverse rule
43      */

44     private boolean reverse = false;
45
46     /**
47      * Default
48      */

49     public Rule() {
50     }
51
52     /**
53      * generate list from string array
54      * @param allowedTypes
55      */

56     public Rule(String JavaDoc[] allowedTypes) {
57         for (int j = 0; j < allowedTypes.length; j++) {
58             this.addAllowType(allowedTypes[j]);
59         }
60     }
61
62     /**
63      * generate list from the string
64      * @param allowedTypes
65      * @param separator
66      */

67     public Rule(String JavaDoc allowedTypes, String JavaDoc separator) {
68         String JavaDoc[] types = StringUtils.split(allowedTypes, separator);
69         for (int j = 0; j < types.length; j++) {
70             this.addAllowType(types[j]);
71         }
72     }
73
74     /**
75      * add to allow list
76      * @param nodeType
77      */

78     public void addAllowType(String JavaDoc nodeType) {
79         if (nodeType != null) {
80             this.allowedTypes = (String JavaDoc[]) ArrayUtils.add(allowedTypes, nodeType);
81         }
82     }
83
84     /**
85      * remove from allow list
86      * @param nodeType
87      */

88     public void removeAllowType(String JavaDoc nodeType) {
89         if (nodeType != null) {
90             for (int j = 0; j < allowedTypes.length; j++) {
91                 if (nodeType.equals(allowedTypes[j])) {
92                     this.allowedTypes = (String JavaDoc[]) ArrayUtils.remove(allowedTypes, j);
93                     break;
94                 }
95             }
96         }
97     }
98
99     /**
100      * is allowed
101      * @param nodeType
102      * @return true if given nodeType is allowed
103      */

104     public boolean isAllowed(String JavaDoc nodeType) {
105         boolean allowed = ArrayUtils.contains(allowedTypes, nodeType);
106         if (this.reverse) {
107             return !allowed;
108         }
109
110         return allowed;
111
112     }
113
114     /**
115      * get a string representation of this rule
116      * @return string representation
117      */

118     public String JavaDoc toString() {
119         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
120         Iterator JavaDoc typeIterator = IteratorUtils.arrayIterator(allowedTypes);
121         while (typeIterator.hasNext()) {
122             buffer.append((String JavaDoc) typeIterator.next());
123             buffer.append(",");
124         }
125         return new String JavaDoc(buffer);
126     }
127
128     /**
129      * set reverse
130      */

131     public void reverse() {
132         this.reverse = true;
133     }
134
135 }
136
Popular Tags