Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DefaultAttributeResolver |
|
| 6.5;6.5 |
1 | /* | |
2 | * $Id: DefaultAttributeResolver.java 836174 2009-11-14 13:40:31Z 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 | import org.apache.tiles.request.Request; | |
29 | ||
30 | /** | |
31 | * The default implementation of AttributeResolver. | |
32 | * | |
33 | * @version $Rev: 836174 $ $Date: 2009-11-15 00:40:31 +1100 (Sun, 15 Nov 2009) $ | |
34 | * @since 2.2.0 | |
35 | */ | |
36 | 5 | public class DefaultAttributeResolver implements AttributeResolver { |
37 | ||
38 | /** {@inheritDoc} */ | |
39 | public Attribute computeAttribute(TilesContainer container, Attribute attribute, | |
40 | String name, String role, boolean ignore, | |
41 | Object defaultValue, String defaultValueRole, String defaultValueType, Request request) { | |
42 | 5 | if (attribute == null) { |
43 | 4 | AttributeContext evaluatingContext = container |
44 | .getAttributeContext(request); | |
45 | 4 | attribute = evaluatingContext.getAttribute(name); |
46 | 4 | if (attribute == null) { |
47 | 3 | attribute = computeDefaultAttribute(defaultValue, |
48 | defaultValueRole, defaultValueType); | |
49 | 3 | if (attribute == null && !ignore) { |
50 | 1 | throw new NoSuchAttributeException("Attribute '" + name |
51 | + "' not found."); | |
52 | } | |
53 | } | |
54 | } | |
55 | 4 | if (attribute != null && role != null && !"".equals(role.trim())) { |
56 | 0 | attribute = new Attribute(attribute); |
57 | 0 | attribute.setRole(role); |
58 | } | |
59 | 4 | return attribute; |
60 | } | |
61 | ||
62 | /** | |
63 | * Computes the default attribute. | |
64 | * | |
65 | * @param defaultValue The default value of the attribute. | |
66 | * @param defaultValueRole The default role of tha attribute. | |
67 | * @param defaultValueType The default type of the attribute. | |
68 | * @return The default attribute. | |
69 | */ | |
70 | private Attribute computeDefaultAttribute(Object defaultValue, | |
71 | String defaultValueRole, String defaultValueType) { | |
72 | 3 | Attribute attribute = null; |
73 | 3 | if (defaultValue != null) { |
74 | 1 | if (defaultValue instanceof Attribute) { |
75 | 0 | attribute = (Attribute) defaultValue; |
76 | 1 | } else if (defaultValue instanceof String) { |
77 | 1 | attribute = new Attribute(defaultValue, (Expression) null, |
78 | defaultValueRole, defaultValueType); | |
79 | } | |
80 | } | |
81 | 3 | return attribute; |
82 | } | |
83 | } |