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: SelectOneMenuBuilder.java 464373 2006-10-16 04:21:54Z rahul $
20   */
21  package org.apache.shale.clay.parser.builder;
22  
23  import java.util.ArrayList;
24  import java.util.Iterator;
25  
26  import org.apache.shale.clay.config.beans.ComponentBean;
27  import org.apache.shale.clay.config.beans.ElementBean;
28  import org.apache.shale.clay.parser.Node;
29  
30  /***
31   * <p>
32   * This {@link Builder} will create a target
33   * {@link org.apache.shale.clay.config.beans.ElementBean} for a single
34   * select html {@link org.apache.shale.clay.parser.Node}. The mapping between the
35   * html and the builder is handled by the
36   * {@link org.apache.shale.clay.parser.builder.chain.SelectBuilderRule}.
37   * </p>
38   */
39  public class SelectOneMenuBuilder extends Builder {
40  
41      /***
42       * <p>
43       * Returns a JSF component type of
44       * <code>javax.faces.HtmlSelectOneMenu</code> that will populate the
45       * target {@link org.apache.shale.clay.config.beans.ElementBean}.
46       * </p>
47       *
48       * @param node markup node
49       * @return component type
50       */
51      protected String getComponentType(Node node) {
52          return "javax.faces.HtmlSelectOneMenu";
53      }
54  
55      /***
56       * <p>
57       * Returns the <code>jsfid</code> that will populate the target
58       * {@link org.apache.shale.clay.config.beans.ElementBean}.
59       * </p>
60       *
61       * @param node markup node
62       * @return jsfid
63       */
64      protected String getJsfid(Node node) {
65          return "selectOneMenu";
66      }
67  
68      /***
69       * <p>
70       * Returns a <code>true</code> value indicating that the target JSF
71       * component can have children.
72       * </p>
73       *
74       * @return <code>true</code>
75       */
76      public boolean isChildrenAllowed() {
77          return true;
78      }
79  
80  
81      /***
82       * <p>Remove any child nodes that are not "option" nodes.</p>
83       *
84       * @param node markup
85       * @param target child node
86       * @param root child's parent
87       */
88      protected void encodeBegin(Node node, ElementBean target, ComponentBean root) {
89  
90          //remove any children that are not option nodes
91          ArrayList delList = new ArrayList();
92          Iterator ci = node.getChildren().iterator();
93          while (ci.hasNext()) {
94             Node child = (Node) ci.next();
95             if (child.getName() == null || !child.getName().equalsIgnoreCase("option")) {
96                delList.add(child);
97             }
98          }
99          for (int i = 0; i < delList.size(); i++) {
100            node.getChildren().remove(delList.get(i));
101         }
102         delList.clear();
103 
104         super.encodeBegin(node, target, root);
105     }
106 
107 }
108