2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * $Id: PropertyConverterCommand.java 471910 2006-11-06 22:44:56Z gvanmatre $
20   */
21  package org.apache.shale.clay.component.chain;
22  
23  import javax.faces.component.UIComponent;
24  import javax.faces.component.ValueHolder;
25  import javax.faces.context.FacesContext;
26  import javax.faces.convert.Converter;
27  
28  import org.apache.commons.chain.Command;
29  import org.apache.commons.chain.Context;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.shale.clay.config.beans.AttributeBean;
33  import org.apache.shale.clay.config.beans.ComponentBean;
34  
35  /***
36   * <p>
37   * This <code>Command</code> will create an <code>converter</code> value
38   * binding and assign it to the <code>UIComponent</code> implementing the
39   * <code>ValueHolder</code> interface.
40   * </p>
41   */
42  
43  public class PropertyConverterCommand extends AbstractCommand implements Command {
44  
45      /***
46       * <p>
47       * Common logger utility class.
48       * </p>
49       */
50      private static Log log;
51      static {
52          log = LogFactory.getLog(PropertyActionCommand.class);
53      }
54  
55      /***
56       * <p>
57       * Looks to see if the {@link AttributeBean} on the {@link ClayContext} is a
58       * <code>converter</code> attribute. If it is, create a
59       * <code>ValueBinding</code> or a <code>Converter</code> and assign it to the
60       * component returning a <code>true</code> value. Otherwise, return a <code>false</code>
61       * value. This <code>Command</code> is invoked from the
62       * {@link AssignPropertiesCommand} chain.
63       * </p>
64       *
65       * @param context common chains
66       * @return <code>true</code> if the chain is complete
67       * @exception Exception propagated up to the top of the chain
68       */
69      public boolean execute(Context context) throws Exception {
70  
71          boolean isFinal = false;
72  
73          ClayContext clayContext = (ClayContext) context;
74          if (clayContext == null) {
75              throw new NullPointerException(getMessages().getMessage("clay.null.clayContext"));
76          }
77          AttributeBean attributeBean = clayContext.getAttribute();
78          if (attributeBean == null) {
79              throw new NullPointerException(getMessages().getMessage("clay.null.attributeBean"));
80          }
81          ComponentBean displayElement = clayContext.getDisplayElement();
82          if (displayElement == null) {
83              throw new NullPointerException(getMessages().getMessage("clay.null.componentBean"));
84          }
85          FacesContext facesContext = clayContext.getFacesContext();
86          if (facesContext == null) {
87              throw new NullPointerException(getMessages().getMessage("clay.null.facesContext"));
88          }
89  
90          if (attributeBean.getName().equals("converter") && attributeBean.getValue() != null) {
91              isFinal = true;
92  
93              UIComponent child = (UIComponent) clayContext.getChild();
94              if (child == null) {
95                  throw new NullPointerException(getMessages().getMessage("clay.null.childComponent"));
96              }
97  
98              if (child instanceof ValueHolder) {
99                  String expr = null;
100 
101                 boolean isEL = isValueReference(attributeBean.getValue());
102                 expr = replaceMnemonic(clayContext);
103 
104                 if (isEL) {
105                    getTagUtils().setValueBinding(child, "converter", expr);
106                 } else {
107                     Converter converter = null;
108                     try {
109                         converter = facesContext.getApplication().createConverter(expr);
110                     } catch (Exception e) {
111                         log.error(getMessages().getMessage("create.converter.error",
112                                 new Object[] { displayElement }), e);
113                                 throw e;
114                     }
115                     ((ValueHolder) child).setConverter(converter);
116                 }
117 
118             } else {
119                 log.error(getMessages().getMessage("property.converter.error", new Object[] {attributeBean}));
120             }
121         }
122 
123         return isFinal;
124     }
125 }
126