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: SymbolTag.java 464373 2006-10-16 04:21:54Z rahul $
20   */
21  package org.apache.shale.clay.taglib;
22  
23  import javax.faces.component.UIComponent;
24  import javax.faces.webapp.UIComponentTag;
25  import javax.servlet.jsp.JspException;
26  import javax.servlet.jsp.tagext.TagSupport;
27  
28  import org.apache.shale.clay.component.Clay;
29  import org.apache.shale.clay.config.beans.SymbolBean;
30  import org.apache.shale.util.Messages;
31  
32  /***
33   * <p>This Tag is used to add replacement symbols to the {@link Clay} component.
34   * Replacement symbols are substituted within the meta-data used to build the subtree
35   * under the Clay component.  This tag is similar to the standard JSF attribute tag.
36   * The <code>name</code> attribute will be prepended with an '@' character.
37   * The "at" character is the symbol identifier.</p>
38   */
39  public class SymbolTag extends TagSupport {
40  
41      /***
42       * <p>Unique id used by the <code>Serializable</code> interface.</p>
43       */
44      private static final long serialVersionUID = 3977021747121698357L;
45  
46      /***
47       * <p>Message resources for this class.</p>
48       */
49      private static Messages messages = new Messages(
50              "org.apache.shale.clay.Bundle", SymbolTag.class.getClassLoader());
51  
52      /***
53       * <p>The <code>name</code> of the symbol.</p>
54       */
55      private String name = null;
56  
57      /***
58       * <p>The <code>value</code> of the symbol.</p>
59       */
60      private String value = null;
61  
62      /***
63       * <p>Returns the name of the symbol.</p>
64       *
65       * @return name of the symbol
66       */
67      public String getName() {
68          return name;
69      }
70  
71      /***
72       * <p>Sets the <code>name</code> of the symbol.</p>
73       *
74       * @param name of the symbol
75       */
76      public void setName(String name) {
77          this.name = name;
78      }
79  
80      /***
81       * <p>Returns the value for the symbol.</p>
82       *
83       * @return value of the symbol
84       */
85      public String getValue() {
86          return value;
87      }
88  
89      /***
90       * <p>Sets the <code>value</code> for the symbol.</p>
91       *
92       * @param value of the symbol
93       */
94      public void setValue(String value) {
95          this.value = value;
96      }
97  
98      /***
99       * <p>Finds the parent component and adds the symbol to the
100      * Clay component's symbol table.  The parent has to be a {@link Clay} component.
101      * </p>
102      *
103      * @return next transition
104      * @exception JspException general JSP tag
105      */
106     public int doStartTag() throws JspException {
107           UIComponentTag parentTag = UIComponentTag.getParentUIComponentTag(pageContext);
108           if (parentTag == null) {
109              throw new JspException(messages.getMessage("clayparent.notfound"));
110           }
111 
112           UIComponent parentComponent = parentTag.getComponentInstance();
113           if (parentComponent == null) {
114               throw new JspException(messages.getMessage("clayparent.notfound"));
115           }
116 
117           if (!(parentComponent instanceof Clay)) {
118               throw new JspException(messages.getMessage("clayparent.notfound"));
119           }
120 
121           Clay clayParent = (Clay) parentComponent;
122 
123           StringBuffer tmp = new StringBuffer(name);
124           if (tmp.charAt(0) != '@') {
125             tmp.insert(0, '@');
126           }
127           SymbolBean symbol = new SymbolBean();
128           symbol.setName(tmp.toString());
129           symbol.setValue(value);
130           clayParent.getSymbols().put(symbol.getName(), symbol);
131 
132           return super.doStartTag();
133     }
134 
135 }