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: PropertyValidatorCommand.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  
27  import org.apache.commons.chain.Command;
28  import org.apache.commons.chain.Context;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.shale.clay.config.beans.AttributeBean;
32  import org.apache.shale.clay.config.beans.ComponentBean;
33  
34  /***
35   * <p>
36   * This <code>Command</code> will create an <code>validator</code> method
37   * binding and assign it to the <code>UIComponent</code> implementing the
38   * <code>EditableValueHolder</code> interface. This <code>Command</code> is
39   * invoked from the {@link AssignPropertiesCommand} chain.
40   * </p>
41   */
42  public class PropertyValidatorCommand extends AbstractCommand implements
43          Command {
44  
45      /***
46       * <p>
47       * Common logger utility class.
48       * </p>
49       */
50      private static Log log;
51      static {
52          log = LogFactory.getLog(PropertyValidatorCommand.class);
53      }
54  
55      /***
56       * <p>
57       * Looks to see if the {@link AttributeBean} on the {@link ClayContext} is a
58       * <code>validator</code> attribute. If it is, create a
59       * <code>MethodBinding</code> and assign it to the component returning a
60       * <code>true</code> value. Otherwise, return a <code>false</code>
61       * value.
62       * </p>
63       *
64       * @param context common chains
65       * @return <code>true</code> if the chain is complete
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().getMessage("clay.null.clayContext"));
75          }
76          AttributeBean attributeBean = clayContext.getAttribute();
77          if (attributeBean == null) {
78              throw new NullPointerException(getMessages().getMessage("clay.null.attributeBean"));
79          }
80          ComponentBean displayElement = clayContext.getDisplayElement();
81          if (displayElement == null) {
82              throw new NullPointerException(getMessages().getMessage("clay.null.componentBean"));
83          }
84          FacesContext facesContext = clayContext.getFacesContext();
85          if (facesContext == null) {
86              throw new NullPointerException(getMessages().getMessage("clay.null.facesContext"));
87          }
88          if (attributeBean.getName().equals("validator") && attributeBean.getValue() != null) {
89              isFinal = true;
90  
91              UIComponent child = (UIComponent) clayContext.getChild();
92              if (child == null) {
93                  throw new NullPointerException("child");
94              }
95  
96              if (child instanceof EditableValueHolder) {
97  
98                  String expr = replaceMnemonic(clayContext);
99                  getTagUtils().setValidator(child, expr);
100 
101             } else {
102                 log.error(getMessages().getMessage("property.validator.error", new Object[] {attributeBean}));
103             }
104         }
105 
106         return isFinal;
107     }
108 }