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: CommentBuilder.java 480266 2006-11-28 23:04:10Z gvanmatre $
20   */
21  package org.apache.shale.clay.parser.builder;
22  
23  import java.util.Iterator;
24  
25  import org.apache.shale.clay.config.beans.AttributeBean;
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>This {@link Builder} will render a HTML {@link Node} as
32   * an HTML comment.  All nodes under the comment will be concatenated
33   * as their original raw text value within the HTML document.
34   * </p>
35   *
36   */
37  public class CommentBuilder extends VerbatimBuilder {
38  
39      /***
40       * <p>This method is overridden to return a <code>true</code> value indicating
41       * that this {@link Builder} will handle child nodes under the
42       * associated HTML {@link Node}.  This method can easily be confused
43       * with the <code>isChildrenAllowed()</code> method of the super
44       * class {@link Builder}.  The distinction is that the
45       * <code>isChildrenAllowed()</code> method signifies that the
46       * associated JSF component supports children components. This method
47       * signifies that this builder will handle building child nodes
48       * similarly to the jsf component's <code>getRendersChildren()</code> method.
49       * </p>
50       *
51       * @param node markup
52       * @param target child config bean
53       * @return <code>true</code>
54       */
55      protected boolean getBuildNodeBody(Node node, ElementBean target) {
56          return true;
57      }
58  
59      /***
60       * <p>The super implementation is invoked to build a target
61       * {@link org.apache.shale.clay.config.beans.ElementBean}.
62       * The body of the comment is constructed by capturing the text
63       * of all child HTML nodes within the comment body.</p>
64       *
65       * @param node markup
66       * @param target child config bean
67       * @param root parent config bean
68       */
69      protected void encodeBegin(Node node, ElementBean target, ComponentBean root) {
70          super.encodeBegin(node, target, root);
71  
72          AttributeBean valueAttr = target.getAttribute("value");
73          StringBuffer comment = new StringBuffer(valueAttr.getValue());
74          captureComment(node, comment);
75          valueAttr.setValue(comment.toString());
76          valueAttr.setBindingType(AttributeBean.BINDING_TYPE_NONE);
77  
78      }
79  
80      /***
81       * <p>Recursively traverses the children of the HTML
82       * {@link org.apache.shale.clay.parser.Node} concatenating
83       * the raw text of each {@link org.apache.shale.clay.parser.Token}
84       * into the <code>commentBody</code>.</p>
85       *
86       * @param node markup
87       * @param commentBody concatenated child node's raw text
88       */
89      protected void captureComment(Node node, StringBuffer commentBody) {
90          Iterator ni = node.getChildren().iterator();
91          while (ni.hasNext()) {
92             Node child = (Node) ni.next();
93             commentBody.append(child.getToken().getRawText());
94             captureComment(child, commentBody);
95          }
96      }
97  
98  
99      /***
100      * <p>This override cancels the super implementation.  The overridden
101      * method handles the ending comment tag, "--&gt;".</p>
102      *
103      * @param node markup
104      * @param target child config bean
105      * @param root parent config bean
106      */
107     protected void encodeEnd(Node node, ElementBean target, ComponentBean root) {
108 
109     }
110 
111 
112 }