View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.view.facelets.tag.jsf;
20  
21  import java.io.IOException;
22  
23  import javax.el.ELException;
24  import javax.el.ValueExpression;
25  import javax.faces.FacesException;
26  import javax.faces.component.EditableValueHolder;
27  import javax.faces.component.UIComponent;
28  import javax.faces.validator.Validator;
29  import javax.faces.view.facelets.FaceletContext;
30  import javax.faces.view.facelets.FaceletException;
31  import javax.faces.view.facelets.MetaRuleset;
32  import javax.faces.view.facelets.TagAttribute;
33  import javax.faces.view.facelets.TagConfig;
34  import javax.faces.view.facelets.TagException;
35  import javax.faces.view.facelets.ValidatorConfig;
36  
37  import org.apache.myfaces.view.facelets.tag.MetaTagHandlerImpl;
38  
39  /**
40   * Handles setting a Validator instance on a EditableValueHolder. Will wire all attributes set to the Validator instance
41   * created/fetched. Uses the "binding" attribute for grabbing instances to apply attributes to. <p/> Will only
42   * set/create Validator is the passed UIComponent's parent is null, signifying that it wasn't restored from an existing
43   * tree.
44   * 
45   * @deprecated use javax.faces.view.facelets.ValidatorHandler instead
46   * @author Jacob Hookom
47   * @version $Id$
48   */
49  @Deprecated
50  public class ValidateHandler extends MetaTagHandlerImpl
51  {
52  
53      private final TagAttribute binding;
54  
55      private String validatorId;
56  
57      /**
58       * 
59       * @param config
60       * @deprecated
61       */
62      public ValidateHandler(TagConfig config)
63      {
64          super(config);
65          this.binding = this.getAttribute("binding");
66      }
67  
68      public ValidateHandler(ValidatorConfig config)
69      {
70          this((TagConfig) config);
71          this.validatorId = config.getValidatorId();
72      }
73  
74      /**
75       * 
76       * @see javax.faces.view.facelets.FaceletHandler#apply(javax.faces.view.facelets.FaceletContext, javax.faces.component.UIComponent)
77       */
78      public final void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException,
79              FaceletException, ELException
80      {
81  
82          if (parent == null || !(parent instanceof EditableValueHolder))
83          {
84              throw new TagException(this.tag, "Parent not an instance of EditableValueHolder: " + parent);
85          }
86  
87          // only process if it's been created
88          if (parent.getParent() == null)
89          {
90              // cast to a ValueHolder
91              EditableValueHolder evh = (EditableValueHolder) parent;
92              ValueExpression ve = null;
93              Validator v = null;
94              if (this.binding != null)
95              {
96                  ve = this.binding.getValueExpression(ctx, Validator.class);
97                  v = (Validator) ve.getValue(ctx);
98              }
99              if (v == null)
100             {
101                 v = this.createValidator(ctx);
102                 if (ve != null)
103                 {
104                     ve.setValue(ctx, v);
105                 }
106             }
107             if (v == null)
108             {
109                 throw new TagException(this.tag, "No Validator was created");
110             }
111             this.setAttributes(ctx, v);
112             evh.addValidator(v);
113         }
114     }
115 
116     /**
117      * Template method for creating a Validator instance
118      * 
119      * @param ctx
120      *            FaceletContext to use
121      * @return a new Validator instance
122      */
123     protected Validator createValidator(FaceletContext ctx)
124     {
125         if (this.validatorId == null)
126         {
127             throw new TagException(
128                                    this.tag,
129                                    "Default behavior invoked of requiring a validator-id passed in the "
130                                    + "constructor, must override ValidateHandler(ValidatorConfig)");
131         }
132         return ctx.getFacesContext().getApplication().createValidator(this.validatorId);
133     }
134 
135     protected MetaRuleset createMetaRuleset(Class type)
136     {
137         return super.createMetaRuleset(type).ignore("binding");
138     }
139 
140 }