KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > ui > common > component > UIOutputText


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the GNU Lesser General Public License as
5  * published by the Free Software Foundation; either version
6  * 2.1 of the License, or (at your option) any later version.
7  * You may obtain a copy of the License at
8  *
9  * http://www.gnu.org/licenses/lgpl.txt
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific
15  * language governing permissions and limitations under the
16  * License.
17  */

18 package org.alfresco.web.ui.common.component;
19
20 import java.io.IOException JavaDoc;
21 import java.net.URLEncoder JavaDoc;
22
23 import javax.faces.component.UIOutput;
24 import javax.faces.context.FacesContext;
25 import javax.faces.context.ResponseWriter;
26 import javax.faces.el.ValueBinding;
27
28 /**
29  * Component that simply renders text
30  *
31  * @author gavinc
32  */

33 public class UIOutputText extends UIOutput
34 {
35    private Boolean JavaDoc encodeForJavaScript = null;
36    
37    /**
38     * Default constructor
39     */

40    public UIOutputText()
41    {
42       setRendererType(null);
43    }
44    
45    /**
46     * @see javax.faces.component.UIComponent#getFamily()
47     */

48    public String JavaDoc getFamily()
49    {
50       return "org.alfresco.faces.OutputText";
51    }
52    
53    /**
54     * @see javax.faces.component.StateHolder#restoreState(javax.faces.context.FacesContext, java.lang.Object)
55     */

56    public void restoreState(FacesContext context, Object JavaDoc state)
57    {
58       Object JavaDoc values[] = (Object JavaDoc[])state;
59       // standard component attributes are restored by the super class
60
super.restoreState(context, values[0]);
61       this.encodeForJavaScript = (Boolean JavaDoc)values[1];
62    }
63    
64    /**
65     * @see javax.faces.component.StateHolder#saveState(javax.faces.context.FacesContext)
66     */

67    public Object JavaDoc saveState(FacesContext context)
68    {
69       Object JavaDoc values[] = new Object JavaDoc[3];
70       // standard component attributes are saved by the super class
71
values[0] = super.saveState(context);
72       values[1] = this.encodeForJavaScript;
73       return values;
74    }
75    
76    /**
77     * Sets whether the text should be encoded for JavaScript consumption
78     *
79     * @param encodeForJavaScript true to escape text
80     */

81    public void setEncodeForJavaScript(boolean encodeForJavaScript)
82    {
83       this.encodeForJavaScript = Boolean.valueOf(encodeForJavaScript);
84    }
85
86    /**
87     * Returns whether the text is going to be encoded or not
88     *
89     * @return true if the text is going to be encoded
90     */

91    public boolean isEncodeForJavaScript()
92    {
93       if (this.encodeForJavaScript == null)
94       {
95          ValueBinding vb = getValueBinding("encodeForJavaScript");
96          if (vb != null)
97          {
98             this.encodeForJavaScript = (Boolean JavaDoc)vb.getValue(getFacesContext());
99          }
100          
101          if (this.encodeForJavaScript == null)
102          {
103             this.encodeForJavaScript = Boolean.FALSE;
104          }
105       }
106       
107       return this.encodeForJavaScript.booleanValue();
108    }
109
110    /**
111     * @see javax.faces.component.UIComponentBase#encodeBegin(javax.faces.context.FacesContext)
112     */

113    public void encodeBegin(FacesContext context) throws IOException JavaDoc
114    {
115       if (isRendered() == false)
116       {
117          return;
118       }
119       
120       ResponseWriter out = context.getResponseWriter();
121       
122       String JavaDoc output = null;
123       
124       if (isEncodeForJavaScript())
125       {
126          output = URLEncoder.encode((String JavaDoc)getValue(), "UTF-8").replace('+', ' ');
127       }
128       else
129       {
130          output = (String JavaDoc)getValue();
131       }
132
133       out.write(output);
134    }
135 }
136
Popular Tags