View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * This class defines the rules to be used by the activation content aggregator this is simply a collection of node
53   * types.
54   * @author Sameer Charles
55   * @version $Revision$ ($Author$)
56   */
57  public class Rule implements Serializable {
58  
59      /**
60       * generated Stable serialVersionUID.
61       */
62      private static final long serialVersionUID = 222L;
63  
64      /**
65       * list of node types allowed.
66       */
67      private Set<String> allowedTypes = new HashSet<String>();
68  
69      /**
70       * reverse rule.
71       */
72      private boolean reverse = false;
73  
74      public Rule() {
75      }
76  
77      /**
78       * Generate list from string array.
79       * @param allowedTypes
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       * Generate list from the string.
89       * @param allowedTypes
90       * @param separator
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      public void setAllowedTypes(String[] types) {
100         for (String t : types) {
101             this.addAllowType(t);
102         }
103     }
104 
105     /**
106      * Add to allow list.
107      * @param nodeType
108      */
109     public void addAllowType(String nodeType) {
110         if (nodeType != null) {
111             this.allowedTypes.add(nodeType);
112         }
113     }
114 
115     /**
116      * Remove from allow list.
117      * @param nodeType
118      */
119     public void removeAllowType(String nodeType) {
120         if (nodeType != null) {
121             this.allowedTypes.remove(nodeType);
122         }
123     }
124 
125     /**
126      * True if allowedTypes contains nodeType.
127      * Method return false also if nodeType is subtype of one of the allowedTypes.
128      * @deprecated since 4.5.5 - use {@link info.magnolia.cms.util.Rule.isAllowed(Node)} instead.
129      */
130     public boolean isAllowed(String nodeType) {
131         boolean allowed = this.allowedTypes.contains(nodeType);
132         if (this.reverse) {
133             return !allowed;
134         }
135 
136         return allowed;
137 
138     }
139 
140     /**
141      * True if given nodeType is allowed.
142      * @throws RepositoryException
143      */
144     public boolean isAllowed(Node node) throws RepositoryException {
145         boolean allowed = false;
146 
147         for(String allowedType:this.allowedTypes){
148             allowed |= NodeUtil.isNodeType(node, allowedType);
149         }
150 
151         if (this.reverse) {
152             return !allowed;
153         }
154 
155         return allowed;
156     }
157 
158     /**
159      * Get a string representation of this rule.
160      * @return string representation
161      */
162     @Override
163     public String toString() {
164         StringBuffer buffer = new StringBuffer();
165         Iterator<String> typeIterator = allowedTypes.iterator();
166         while (typeIterator.hasNext()) {
167             buffer.append(typeIterator.next());
168             buffer.append(",");
169         }
170         return buffer.toString();
171     }
172 
173     /**
174      * Set reverse.
175      */
176     public void reverse() {
177         this.reverse = true;
178     }
179 
180     private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
181         final ObjectInputStream.GetField field = ois.readFields();
182         final Object o = field.get("allowedTypes", null);
183         if (o instanceof String[]){
184             Set<String> typesCol = new HashSet<String>();
185             typesCol.addAll(Arrays.asList((String[]) o));
186             this.allowedTypes = typesCol;
187         }
188         if(o instanceof HashSet){
189             this.allowedTypes = (Set<String>) o;
190         }
191         this.reverse = field.get("reverse", false);
192     }
193 }