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: CreateConverterCommand.java 472598 2006-11-08 19:13:12Z 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  import javax.faces.el.ValueBinding;
28  
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 a <code>Converter</code> from the
38   * <code>displayElement</code> attribute of the
39   * {@link org.apache.shale.clay.component.chain.ClayContext} assigning
40   * it to the <code>parent</code>.
41   * </p>
42   *
43   */
44  
45  public class CreateConverterCommand extends AbstractCommand {
46  
47      /***
48       * <p>
49       * Common Logger utility.
50       * </p>
51       */
52      private static Log log;
53      static {
54          log = LogFactory.getLog(CreateConverterCommand.class);
55      }
56  
57      /***
58       * <p>
59       * Creates a object instance realizing the {@link Converter} interface
60       * assigning to the <code>parent</code>. The <code>parent</code> must
61       * implement the {@link ValueHolder} interface.
62       * </p>
63       *
64       * @param context commons chains
65       * @return <code>true</code> if chain is done
66       * @exception Exception propagated up to the top of the chain
67       */
68      public boolean execute(Context context) throws Exception {
69  
70          boolean isFinal = false;
71  
72          ClayContext clayContext = (ClayContext) context;
73          if (clayContext == null) {
74              throw new NullPointerException(getMessages()
75                      .getMessage("clay.null.clayContext"));
76          }
77  
78          UIComponent child = (UIComponent) clayContext.getChild();
79          if (child == null) {
80              throw new NullPointerException(getMessages()
81                      .getMessage("clay.null.childComponent"));
82          }
83  
84          ComponentBean displayElement = clayContext.getDisplayElement();
85          if (displayElement == null) {
86              throw new NullPointerException(getMessages()
87                      .getMessage("clay.null.componentBean"));
88          }
89  
90          ValueHolder parent = (ValueHolder) clayContext.getParent();
91          if (parent == null) {
92              throw new NullPointerException(getMessages()
93                      .getMessage("clay.null.parentComponent"));
94          }
95  
96          FacesContext facesContext = clayContext.getFacesContext();
97          if (facesContext == null) {
98              throw new NullPointerException(getMessages()
99                      .getMessage("clay.null.facesContext"));
100         }
101         Converter converter = null;
102         try {
103 
104             AttributeBean attr = displayElement.getAttribute("binding");
105             if (attr != null && isValueReference(attr.getValue())) {
106                 clayContext.setAttribute(attr);
107                 String expr = replaceMnemonic(clayContext);
108                 ValueBinding vb = facesContext.getApplication().createValueBinding(expr);
109                 converter = (Converter) vb.getValue(facesContext);
110             } else {
111                 // the default converter id comes for the component type
112                 String converterId = displayElement.getComponentType();
113                 // check for a converterId attribute override
114                 attr = displayElement.getAttribute("converterId");
115                 if (attr != null && attr.getValue() != null
116                         && attr.getValue().length() > 0) {
117                     clayContext.setAttribute(attr);
118                     String tmp = getTagUtils().evalString(replaceMnemonic(clayContext));
119                     if (tmp != null && tmp.length() > 0) {
120                         converterId = tmp;
121                     }
122                 }
123                 converter = facesContext.getApplication().createConverter(converterId);
124             }
125         } catch (Exception e) {
126             log.error(getMessages().getMessage("create.converter.error",
127                     new Object[] { displayElement }), e);
128             throw e;
129         }
130 
131         if (converter != null) {
132             parent.setConverter(converter);
133             // reassign the child to the converter for the
134             // AssignPropertiesCommand
135             clayContext.setChild(converter);
136         } else {
137             isFinal = true;
138         }
139 
140         return isFinal;
141     }
142 
143 }