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: CreateValidatorCommand.java 471910 2006-11-06 22:44:56Z gvanmatre $
20   */
21  package org.apache.shale.clay.component.chain;
22  
23  import javax.faces.component.EditableValueHolder;
24  import javax.faces.component.UIComponent;
25  import javax.faces.context.FacesContext;
26  import javax.faces.el.ValueBinding;
27  import javax.faces.validator.Validator;
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   * Creates an object implementing the {@link Validator} interface and assigns it
38   * to the <code>parent</code> from the {@link ClayContext}.
39   * </p>
40   */
41  public class CreateValidatorCommand extends AbstractCommand {
42  
43      /***
44       * <p>
45       * Common Logger utility.
46       * </p>
47       */
48      private static Log log;
49      static {
50          log = LogFactory.getLog(CreateValidatorCommand.class);
51      }
52  
53      /***
54       * <p>
55       * Creates a faces validator object that is registered in the
56       * <strong>faces-config.xml</strong> file and assigns it to the parent.
57       * </p>
58       *
59       * @param context commons chains
60       * @return <code>true</code> if the chain is final
61       * @exception Exception propagated to the top chain
62       */
63      public boolean execute(Context context) throws Exception {
64  
65          boolean isFinal = false;
66  
67          ClayContext clayContext = (ClayContext) context;
68          if (clayContext == null) {
69              throw new NullPointerException(getMessages()
70                      .getMessage("clay.null.clayContext"));
71          }
72  
73          UIComponent child = (UIComponent) clayContext.getChild();
74          if (child == null) {
75              throw new NullPointerException(getMessages()
76                      .getMessage("clay.null.childComponent"));
77          }
78  
79          ComponentBean displayElement = clayContext.getDisplayElement();
80          if (displayElement == null) {
81              throw new NullPointerException(getMessages()
82                      .getMessage("clay.null.componentBean"));
83          }
84  
85          EditableValueHolder parent = (EditableValueHolder) clayContext
86                  .getParent();
87          if (parent == null) {
88              throw new NullPointerException(getMessages()
89                      .getMessage("clay.null.parentComponent"));
90          }
91  
92          FacesContext facesContext = clayContext.getFacesContext();
93          if (facesContext == null) {
94              throw new NullPointerException(getMessages()
95                      .getMessage("clay.null.facesContext"));
96          }
97  
98          Validator validator = null;
99          try {
100             AttributeBean attr = displayElement.getAttribute("binding");
101             if (attr != null && isValueReference(attr.getValue())) {
102                 clayContext.setAttribute(attr);
103                 String expr = replaceMnemonic(clayContext);
104                 ValueBinding vb = facesContext.getApplication()
105                         .createValueBinding(expr);
106                 validator = (Validator) vb.getValue(facesContext);
107 
108             } else {
109                 validator = facesContext.getApplication().createValidator(
110                         displayElement.getComponentType());
111             }
112         } catch (Exception e) {
113             log.error(getMessages().getMessage("create.validator.error",
114                     new Object[] { displayElement }), e);
115             throw e;
116         }
117         parent.addValidator(validator);
118         // reassign the child to the validator for the
119         // AssignPropertiesCommand
120         clayContext.setChild(validator);
121 
122         return isFinal;
123     }
124 
125 }