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: InputBuilderRule.java 464373 2006-10-16 04:21:54Z rahul $
20   */
21  package org.apache.shale.clay.parser.builder.chain;
22  
23  import org.apache.commons.chain.Command;
24  import org.apache.commons.chain.Context;
25  import org.apache.shale.clay.parser.Node;
26  import org.apache.shale.clay.parser.builder.Builder;
27  import org.apache.shale.clay.parser.builder.CommandButtonBuilder;
28  import org.apache.shale.clay.parser.builder.InputTextBuilder;
29  import org.apache.shale.clay.parser.builder.SelectBooleanCheckboxBuilder;
30  import org.apache.shale.clay.parser.builder.SelectOneRadioBuilder;
31  
32  /***
33   * <p>
34   * This rule <code>Command</code> has an implied mapping to several html
35   * element types. The associated {@link Builder} will be returned regardless of
36   * the <code>jsfid</code>.
37   * <p>
38   */
39  public class InputBuilderRule implements Command {
40  
41      /***
42       * <p>Array of target input {@link Builder}s.</p>
43       */
44      private static final Builder[] BUILDERS = { new InputTextBuilder(),
45              new SelectBooleanCheckboxBuilder(), new SelectOneRadioBuilder(),
46              new CommandButtonBuilder() };
47  
48      /***
49       * <p>
50       * If the html {@link Node} is a input element, return the correct builder.
51       * If a match is found, the chain is ended.<br>
52       * <dl>
53       * <dt>Input type attribute to {@link Builder}
54       * <dd><b>text</b> {@link org.apache.shale.clay.parser.builder.InputTextBuilder}
55       * <dd><b>checkbox</b> {@link org.apache.shale.clay.parser.builder.SelectBooleanCheckboxBuilder}
56       * <dd><b>raido</b> {@link org.apache.shale.clay.parser.builder.SelectOneRadioBuilder}
57       * <dd><b>submit</b>{@link org.apache.shale.clay.parser.builder.CommandButtonBuilder}
58       * </dl>
59       * <br>
60       * The {@link BuilderRuleContext} will hold the current html {@link Node} and
61       * target {@link Builder}.
62       * </p>
63       *
64       * @param context common chains context
65       * @return <code>true</code> if the chain is done
66       * @throws Exception checked exception
67       */
68      public boolean execute(Context context) throws Exception {
69  
70          boolean isFinal = false;
71  
72          BuilderRuleContext builderRuleContext = (BuilderRuleContext) context;
73          Node node = builderRuleContext.getNode();
74          if (!node.isComment() && node.getName() != null
75               && node.getName().equalsIgnoreCase("input")) {
76  
77              String type = (String) node.getAttributes().get("type");
78              if (type != null && type.equalsIgnoreCase("text")) {
79                  builderRuleContext.setBuilder(BUILDERS[0]);
80                  isFinal = true;
81              } else if (type != null && type.equalsIgnoreCase("checkbox")) {
82                  builderRuleContext.setBuilder(BUILDERS[1]);
83                  isFinal = true;
84              } else if (type != null && type.equalsIgnoreCase("radio")) {
85                  builderRuleContext.setBuilder(BUILDERS[2]);
86                  isFinal = true;
87              } else if (type != null && type.equalsIgnoreCase("submit")) {
88                  builderRuleContext.setBuilder(BUILDERS[BUILDERS.length - 1]);
89                  isFinal = true;
90              }
91  
92          }
93  
94          return isFinal;
95      }
96  
97  }
98