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  package org.apache.shale.validator.faces;
19  
20  import java.io.OutputStream;
21  import java.io.Writer;
22  import javax.faces.context.ResponseStream;
23  import javax.faces.context.ResponseWriter;
24  import javax.faces.render.RenderKit;
25  import javax.faces.render.Renderer;
26  import javax.faces.render.ResponseStateManager;
27  
28  /***
29   * <p>Decorates the original <code>RenderKit</code> passed by the
30   * overloaded constructor in the {@link org.apache.shale.view.faces.ViewViewHandler}.
31   * The majority of the implementation is passed on the the original
32   * <code>RenderKit</code> but requests for renderers registered with the
33   * "javax.faces.Command" and "javax.faces.Input" families are decorated.
34   * Only renderers in the "javax.faces.Command" family of types
35   * "javax.faces.Link" and "javax.faces.Button" are considered.  These are
36   * renderers from the vanilla JSF runtime that have predictable behavior.
37   * The wrapper adds special behavior for the
38   * {@link org.apache.shale.validator.CommonsValidator} validator and
39   * {@link org.apache.shale.component.ValidatorScript} component.</p>
40   */
41  public class ValidatorRenderKit extends RenderKit {
42  
43      private static final String COMMAND_FAMILY = "javax.faces.Command";
44      private static final String INPUT_FAMILY = "javax.faces.Input";
45      private static final String COMMAND_LINK_TYPE = "javax.faces.Link";
46      private static final String COMMAND_BUTTON_TYPE = "javax.faces.Button";
47  
48  
49      /***
50       * <p>The original RenderKit.</p>
51       */
52      private RenderKit defaultRenderKit = null;
53  
54  
55      /***
56       * <p>This constructor is overloaded to pass the original
57       * <code>RenderKit</code></p>.
58       *
59       * @param defaultRenderKit The default RenderKit that we will be wrapping
60       */
61      public ValidatorRenderKit(RenderKit defaultRenderKit) {
62         this.defaultRenderKit = defaultRenderKit;
63      }
64  
65  
66      /*** {@inheritDoc} */
67      public void addRenderer(String componentFamily, String rendererType, Renderer renderer) {
68         this.defaultRenderKit.addRenderer(componentFamily, rendererType, renderer);
69      }
70  
71  
72      /***
73       * <p>If the component family is not "javax.faces.Command" or
74       * "javax.faces.Input", the <code>defaultRenderKit</code> handles the
75       * request.  If the family is "javax.faces.Command", and the renderer type
76       * is "javax.faces.Link" or "javax.faces.Button" the default
77       * renderer is decorated with {@link org.apache.shale.renderer.ValidatorCommandRenderer}.
78       * If the component family is "javax.faces.Input", the default
79       * renderer is decorated with {@link org.apache.shale.renderer.ValidatorInputRenderer}.
80       *
81       * @param componentFamily Component family for which to retrieve a Renderer
82       * @param rendererType Renderer type for which to retrieve a Renderer
83       */
84      public Renderer getRenderer(String componentFamily, String rendererType) {
85          Renderer target = defaultRenderKit.getRenderer(componentFamily, rendererType);
86          if (componentFamily.equals(COMMAND_FAMILY) &&
87              (rendererType.equals(COMMAND_LINK_TYPE) ||
88               rendererType.equals(COMMAND_BUTTON_TYPE))) {
89             if (!(target instanceof ValidatorCommandRenderer)) {
90                 target = new ValidatorCommandRenderer(target);
91                 addRenderer(componentFamily, rendererType, target);
92             }
93          } else if (componentFamily.equals(INPUT_FAMILY)) {
94              if (!(target instanceof ValidatorInputRenderer)) {
95                  target = new ValidatorInputRenderer(target);
96                  addRenderer(componentFamily, rendererType, target);
97              }
98          }
99          return target;
100     }
101 
102 
103     /*** {@inheritDoc} */
104     public ResponseStateManager getResponseStateManager() {
105         return defaultRenderKit.getResponseStateManager();
106     }
107 
108 
109     /*** {@inheritDoc} */
110     public ResponseWriter createResponseWriter(Writer writer,
111             String contentTypeList,
112             String characterEncoding) {
113         return defaultRenderKit.createResponseWriter(writer, contentTypeList, characterEncoding);
114     }
115 
116 
117     /*** {@inheritDoc} */
118     public ResponseStream createResponseStream(OutputStream outputStream) {
119         return defaultRenderKit.createResponseStream(outputStream);
120     }
121 
122 
123 }