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: SelectItemBuilder.java 464373 2006-10-16 04:21:54Z rahul $
20   */
21  package org.apache.shale.clay.parser.builder;
22  
23  import org.apache.shale.clay.config.beans.AttributeBean;
24  import org.apache.shale.clay.config.beans.ComponentBean;
25  import org.apache.shale.clay.config.beans.ElementBean;
26  import org.apache.shale.clay.parser.Node;
27  
28  /***
29   * <p>
30   * This {@link Builder} will create a {@link ElementBean} that will be used to
31   * create a single html option element. The {@link OptionBuilderRule} will
32   * handle mapping the html element to this builder.
33   * </p>
34   */
35  public class SelectItemBuilder extends Builder {
36  
37      /***
38       * <p>
39       * Returns a <code>jsfid</code> that will be set on the
40       * target {@link ElementBean}. </p>
41       *
42       * @param node markup
43       * @return jsfid
44       */
45      protected String getJsfid(Node node) {
46          return "selectItem";
47      }
48  
49      /***
50       * <p>
51       * Returns the JSF component type of <code>javax.faces.SelectItem</code>
52       * that will populate a {@link ElementBean} and create an html option.
53       * </p>
54       *
55       * @param node markup
56       * @return component type
57       */
58      protected String getComponentType(Node node) {
59          return "javax.faces.SelectItem";
60      }
61  
62      /***
63       * <p>
64       * The default for a single option is to look at the next html node to find
65       * the label.
66       * </p>
67       *
68       * @param node markup
69       * @param target child config bean
70       * @param root parent config bean
71       */
72      protected void encodeBegin(Node node, ElementBean target,
73              ComponentBean root) {
74          super.encodeBegin(node, target, root);
75  
76          if (!getBuildNodeBody(node, target)) {
77              return;
78          }
79  
80          if (target.getAttributes().containsKey("itemValue")
81              && node.getAttributes().containsKey("value")) {
82              String value = (String) node.getAttributes().get("value");
83              AttributeBean attr = target.getAttribute("itemValue");
84              createAttribute(attr, value, target);
85          }
86          if (target.getAttributes().containsKey("itemLabel")
87              && node.getChildren().size() == 1) {
88  
89              Node child = (Node) node.getChildren().get(0);
90              String value = child.getToken().getRawText();
91              AttributeBean attr = target.getAttribute("itemLabel");
92              createAttribute(attr, value, target);
93          }
94  
95      }
96  
97      /***
98       * <p>Returns <code>true</code> by default meaning that the
99       * parent will render children.</p>
100      *
101      * @param node markup
102      * @param target child config bean
103      * @return <code>false</code> if the node's children should be ignored
104      */
105     protected boolean getBuildNodeBody(Node node, ElementBean target) {
106         if (target.getAllowBody() != null) {
107            return super.getBuildNodeBody(node, target);
108         }
109 
110         return true;
111     }
112 
113 }