This project has retired. For details please refer to its Attic page.
DefaultAttributeResolver xref
View Javadoc

1   /*
2    * $Id: DefaultAttributeResolver.java 788032 2009-06-24 14:08:32Z 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.template;
23  
24  import org.apache.tiles.Attribute;
25  import org.apache.tiles.AttributeContext;
26  import org.apache.tiles.Expression;
27  import org.apache.tiles.TilesContainer;
28  
29  /***
30   * The default implementation of AttributeResolver.
31   *
32   * @version $Rev: 788032 $ $Date: 2009-06-24 16:08:32 +0200 (mer, 24 giu 2009) $
33   * @since 2.2.0
34   */
35  public class DefaultAttributeResolver implements AttributeResolver {
36  
37      /*** {@inheritDoc} */
38      public Attribute computeAttribute(TilesContainer container, Attribute attribute,
39              String name, String role, boolean ignore,
40              Object defaultValue, String defaultValueRole, String defaultValueType, Object... requestItems) {
41          if (attribute == null) {
42              AttributeContext evaluatingContext = container
43                      .getAttributeContext(requestItems);
44              attribute = evaluatingContext.getAttribute(name);
45              if (attribute == null) {
46                  attribute = computeDefaultAttribute(defaultValue,
47                          defaultValueRole, defaultValueType);
48                  if (attribute == null && !ignore) {
49                      throw new NoSuchAttributeException("Attribute '" + name
50                              + "' not found.");
51                  }
52              }
53          }
54          if (attribute != null && role != null && !"".equals(role.trim())) {
55              attribute = new Attribute(attribute);
56              attribute.setRole(role);
57          }
58          return attribute;
59      }
60  
61      /***
62       * Computes the default attribute.
63       *
64       * @param defaultValue The default value of the attribute.
65       * @param defaultValueRole The default role of tha attribute.
66       * @param defaultValueType The default type of the attribute.
67       * @return The default attribute.
68       */
69      private Attribute computeDefaultAttribute(Object defaultValue,
70              String defaultValueRole, String defaultValueType) {
71          Attribute attribute = null;
72          if (defaultValue != null) {
73              if (defaultValue instanceof Attribute) {
74                  attribute = (Attribute) defaultValue;
75              } else if (defaultValue instanceof String) {
76                  attribute = new Attribute(defaultValue, (Expression) null,
77                          defaultValueRole, defaultValueType);
78              }
79          }
80          return attribute;
81      }
82  }