Coverage Report - org.apache.tiles.Attribute
 
Classes in this File Line Coverage Branch Coverage Complexity
Attribute
98%
96/97
84%
42/50
2.083
 
 1  
 /*
 2  
  * $Id: Attribute.java 1035061 2010-11-14 20:32:50Z apetrelli $
 3  
  *
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  * http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 package org.apache.tiles;
 23  
 
 24  
 import static org.apache.tiles.CompareUtil.*;
 25  
 
 26  
 import java.io.Serializable;
 27  
 import java.util.HashSet;
 28  
 import java.util.Iterator;
 29  
 import java.util.Set;
 30  
 
 31  
 import org.apache.tiles.request.Request;
 32  
 
 33  
 /**
 34  
  * Common implementation of attribute definition.
 35  
  *
 36  
  * @version $Rev: 1035061 $ $Date: 2010-11-15 07:32:50 +1100 (Mon, 15 Nov 2010) $
 37  
  */
 38  0
 public class Attribute implements Serializable, Cloneable {
 39  
 
 40  
     /**
 41  
      * The name of the template renderer.
 42  
      */
 43  
     private static final String TEMPLATE_RENDERER = "template";
 44  
 
 45  
     /**
 46  
      * The roles that can render this attribute.
 47  
      * @since 2.0.6
 48  
      */
 49  247
     protected Set<String> roles = null;
 50  
 
 51  
     /**
 52  
      * The value of the attribute.
 53  
      */
 54  247
     protected Object value = null;
 55  
 
 56  
     /**
 57  
      * The expression to evaluate. Ignored if {@link #value} is not
 58  
      * <code>null</code>.
 59  
      *
 60  
      * @since 2.2.0
 61  
      */
 62  247
     protected Expression expressionObject = null;
 63  
 
 64  
     /**
 65  
      * The renderer name of the attribute. Default names are <code>string</code>,
 66  
      * <code>template</code>, <code>definition</code>, <code>object</code>.
 67  
      */
 68  247
     private String renderer = null;
 69  
 
 70  
     /**
 71  
      * Constructor.
 72  
      *
 73  
      */
 74  35
     public Attribute() {
 75  35
     }
 76  
 
 77  
     /**
 78  
      * Constructor.
 79  
      *
 80  
      * @param value Object to store.
 81  
      */
 82  121
     public Attribute(Object value) {
 83  121
         this.value = value;
 84  121
     }
 85  
 
 86  
     /**
 87  
      * Copy constructor.
 88  
      *
 89  
      * @param attribute The attribute to copy from.
 90  
      */
 91  76
     public Attribute(Attribute attribute) {
 92  76
         this.roles = attribute.roles;
 93  76
         this.value = attribute.getValue();
 94  76
         if (attribute.expressionObject != null) {
 95  8
             this.expressionObject = new Expression(attribute.expressionObject);
 96  
         } else {
 97  68
             this.expressionObject = null;
 98  
         }
 99  76
         this.renderer = attribute.renderer;
 100  76
     }
 101  
 
 102  
     /**
 103  
      * Constructor.
 104  
      *
 105  
      * @param value Object to store.
 106  
      * @param role  Asociated role.
 107  
      */
 108  1
     public Attribute(Object value, String role) {
 109  1
         this.value = value;
 110  1
         setRole(role);
 111  1
     }
 112  
 
 113  
     /**
 114  
      * Constructor.
 115  
      *
 116  
      * @param value Object to store. If specified, the <code>expression</code>
 117  
      * parameter will be ignored.
 118  
      * @param expression The expression to be evaluated. Ignored if the
 119  
      * <code>value</code> is not null.
 120  
      * @param role Associated role.
 121  
      * @param rendererName The renderer name.
 122  
      * @since 2.2.0
 123  
      */
 124  14
     public Attribute(Object value, Expression expression, String role, String rendererName) {
 125  14
         this.value = value;
 126  14
         this.expressionObject = expression;
 127  14
         this.renderer = rendererName;
 128  14
         setRole(role);
 129  14
     }
 130  
 
 131  
     /**
 132  
      * Creates a template attribute, starting from the name of the template.
 133  
      *
 134  
      * @param template The template that will be rendered.
 135  
      * @return The template attribute.
 136  
      * @since 2.1.2
 137  
      */
 138  
     public static Attribute createTemplateAttribute(String template) {
 139  12
         Attribute attribute = new Attribute();
 140  12
         attribute.setValue(template);
 141  12
         attribute.setRenderer(TEMPLATE_RENDERER);
 142  12
         return attribute;
 143  
     }
 144  
 
 145  
     /**
 146  
      * Creates a template attribute, starting from the name of the template.
 147  
      *
 148  
      * @param template The template that will be rendered.
 149  
      * @param templateExpression The template expression that will be evaluated
 150  
      * to a template.
 151  
      * @param templateType The type, or renderer, of the template. If null, the
 152  
      * default <code>template</code> will be used.
 153  
      * @param role The comma-separated roles for which the template is
 154  
      * authorized to be rendered.
 155  
      * @return The template attribute.
 156  
      * @since 2.2.2
 157  
      */
 158  
     public static Attribute createTemplateAttribute(String template,
 159  
             String templateExpression, String templateType, String role) {
 160  1
         Attribute templateAttribute = createTemplateAttribute(template);
 161  1
         templateAttribute.setRole(role);
 162  1
         if (templateType != null) {
 163  1
             templateAttribute.setRenderer(templateType);
 164  
         }
 165  1
         templateAttribute
 166  
                 .setExpressionObject(Expression
 167  
                         .createExpressionFromDescribedExpression(templateExpression));
 168  1
         return templateAttribute;
 169  
     }
 170  
 
 171  
     /**
 172  
      * Creates a template attribute, starting from the expression to evaluate to
 173  
      * obtain the template.
 174  
      *
 175  
      * @param templateExpression The expression to evaluate.
 176  
      * @return The template attribute.
 177  
      * @since 2.1.2
 178  
      */
 179  
     public static Attribute createTemplateAttributeWithExpression(
 180  
             String templateExpression) {
 181  1
         Attribute attribute = new Attribute();
 182  1
         attribute.setExpressionObject(new Expression(templateExpression));
 183  1
         attribute.setRenderer(TEMPLATE_RENDERER);
 184  1
         return attribute;
 185  
     }
 186  
 
 187  
     /**
 188  
      * Get role.
 189  
      * @return the name of the required role(s)
 190  
      */
 191  
     public String getRole() {
 192  5
         String retValue = null;
 193  
 
 194  5
         if (roles != null && !roles.isEmpty()) {
 195  3
             StringBuilder builder = new StringBuilder();
 196  3
             Iterator<String> roleIt = roles.iterator();
 197  3
             if (roleIt.hasNext()) {
 198  3
                 builder.append(roleIt.next());
 199  4
                 while (roleIt.hasNext()) {
 200  1
                     builder.append(",");
 201  1
                     builder.append(roleIt.next());
 202  
                 }
 203  3
                 retValue = builder.toString();
 204  
             }
 205  
         }
 206  
 
 207  5
         return retValue;
 208  
     }
 209  
 
 210  
     /**
 211  
      * Returns the roles that can render this attribute.
 212  
      *
 213  
      * @return The enabled roles.
 214  
      * @since 2.0.6
 215  
      */
 216  
     public Set<String> getRoles() {
 217  20
         return roles;
 218  
     }
 219  
 
 220  
     /**
 221  
      * Set role.
 222  
      *
 223  
      * @param role Associated role.
 224  
      */
 225  
     public void setRole(String role) {
 226  18
         if (role != null && role.trim().length() > 0) {
 227  15
             String[] rolesStrings = role.split("\\s*,\\s*");
 228  15
             roles = new HashSet<String>();
 229  40
             for (int i = 0; i < rolesStrings.length; i++) {
 230  25
                 roles.add(rolesStrings[i]);
 231  
             }
 232  15
         } else {
 233  3
             roles = null;
 234  
         }
 235  18
     }
 236  
 
 237  
     /**
 238  
      * Sets the roles that can render this attribute.
 239  
      *
 240  
      * @param roles The enabled roles.
 241  
      * @since 2.0.6
 242  
      */
 243  
     public void setRoles(Set<String> roles) {
 244  5
         this.roles = roles;
 245  5
     }
 246  
 
 247  
     /**
 248  
      * Get value.
 249  
      * @return the value
 250  
      */
 251  
     public Object getValue() {
 252  181
         return value;
 253  
     }
 254  
 
 255  
     /**
 256  
      * Set value.
 257  
      *
 258  
      * @param value New value.
 259  
      */
 260  
     public void setValue(Object value) {
 261  50
         this.value = value;
 262  50
     }
 263  
 
 264  
     /**
 265  
      * Returns The expression to evaluate. Ignored if {@link #value} is not
 266  
      * <code>null</code>.
 267  
      *
 268  
      * @return The expression to be evaluated.
 269  
      * @since 2.2.0
 270  
      */
 271  
     public Expression getExpressionObject() {
 272  23
         return expressionObject;
 273  
     }
 274  
 
 275  
     /**
 276  
      * Sets The expression to evaluate. Ignored if {@link #value} is not
 277  
      * <code>null</code>.
 278  
      *
 279  
      * @param expressionObject The expression to be evaluated.
 280  
      * @since 2.2.0
 281  
      */
 282  
     public void setExpressionObject(Expression expressionObject) {
 283  3
         this.expressionObject = expressionObject;
 284  3
     }
 285  
 
 286  
     /** {@inheritDoc} */
 287  
     @Override
 288  
     public String toString() {
 289  3
         if (value != null) {
 290  2
             return value.toString();
 291  
         }
 292  1
         return null;
 293  
     }
 294  
 
 295  
     /**
 296  
      * Returns the renderer name to use.
 297  
      *
 298  
      * @return The renderer name.
 299  
      * @since 2.1.0
 300  
      */
 301  
     public String getRenderer() {
 302  15
         return renderer;
 303  
     }
 304  
 
 305  
     /**
 306  
      * Sets the renderer name to use.
 307  
      *
 308  
      * @param rendererName The renderer.
 309  
      * @since 2.1.0
 310  
      */
 311  
     public void setRenderer(String rendererName) {
 312  15
         this.renderer = rendererName;
 313  15
     }
 314  
 
 315  
     /**
 316  
      * Inherits an attribute, i.e. overwrites null properties with the ones
 317  
      * provided by the attribute.
 318  
      *
 319  
      * @param attribute The attribute to inherit.
 320  
      * @since 2.1.2
 321  
      */
 322  
     public void inherit(Attribute attribute) {
 323  3
         if (value == null) {
 324  3
             value = attribute.getValue();
 325  
         }
 326  3
         Expression targetExpressionObject = attribute.getExpressionObject();
 327  3
         if (targetExpressionObject != null
 328  
                 && (expressionObject == null || expressionObject
 329  
                         .getExpression() == null)) {
 330  2
             expressionObject = new Expression(targetExpressionObject);
 331  
         }
 332  3
         if (roles == null || roles.isEmpty()) {
 333  2
             roles = attribute.getRoles();
 334  
         }
 335  3
         if (renderer == null) {
 336  3
             renderer = attribute.getRenderer();
 337  
         }
 338  3
     }
 339  
 
 340  
     /** {@inheritDoc} */
 341  
     @Override
 342  
     public boolean equals(Object obj) {
 343  51
         Attribute attribute = (Attribute) obj;
 344  51
         return nullSafeEquals(value, attribute.value)
 345  
                 && nullSafeEquals(renderer, attribute.renderer)
 346  
                 && nullSafeEquals(roles, attribute.roles)
 347  
                 && nullSafeEquals(expressionObject, attribute.expressionObject);
 348  
     }
 349  
 
 350  
     /**
 351  
      * Checks if the current user can use this attribute.
 352  
      *
 353  
      * @param request The request context.
 354  
      * @return <code>true</code> if the current user can see this attribute.
 355  
      * @since 3.0.0
 356  
      */
 357  
     public boolean isPermitted(Request request) {
 358  3
         if (roles == null || roles.isEmpty()) {
 359  1
             return true;
 360  
         }
 361  
 
 362  2
         boolean retValue = false;
 363  
 
 364  2
         for (Iterator<String> roleIt = roles.iterator(); roleIt.hasNext()
 365  4
                 && !retValue;) {
 366  2
             retValue = request.isUserInRole(roleIt.next());
 367  
         }
 368  
 
 369  2
         return retValue;
 370  
     }
 371  
 
 372  
     /** {@inheritDoc} */
 373  
     @Override
 374  
     public int hashCode() {
 375  18
         return nullSafeHashCode(value) + nullSafeHashCode(renderer)
 376  
                 + nullSafeHashCode(roles) + nullSafeHashCode(expressionObject);
 377  
     }
 378  
 
 379  
     /** {@inheritDoc} */
 380  
     @Override
 381  
     public Attribute clone() {
 382  48
         return new Attribute(this);
 383  
     }
 384  
 }