KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > valid > RenderString


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.valid;
16
17 import java.io.Serializable JavaDoc;
18
19 import org.apache.tapestry.IMarkupWriter;
20 import org.apache.tapestry.IRender;
21 import org.apache.tapestry.IRequestCycle;
22
23 /**
24  * A wrapper around {@link String} that allows the String to be renderred. This is primarily
25  * used to present error messages.
26  *
27  * @author Howard Lewis Ship
28  */

29
30 public class RenderString implements IRender, Serializable JavaDoc
31 {
32     private static final long serialVersionUID = 6215074338439140780L;
33
34     private String JavaDoc _string;
35
36     private boolean _raw = false;
37
38     public RenderString(String JavaDoc string)
39     {
40         _string = string;
41     }
42
43     /**
44      * @param string
45      * the string to render
46      * @param raw
47      * if true, the String is rendered as-is, with no filtering. If false (the default),
48      * the String is filtered.
49      */

50
51     public RenderString(String JavaDoc string, boolean raw)
52     {
53         _string = string;
54         _raw = raw;
55     }
56
57     /**
58      * Renders the String to the writer. Does nothing if the string is null. If raw is true, uses
59      * {@link IMarkupWriter#printRaw(String)}, otherwise {@link IMarkupWriter#print(String)}.
60      */

61
62     public void render(IMarkupWriter writer, IRequestCycle cycle)
63     {
64         if (_string == null)
65             return;
66
67         writer.print(_string, _raw);
68     }
69
70     public String JavaDoc getString()
71     {
72         return _string;
73     }
74
75     public boolean isRaw()
76     {
77         return _raw;
78     }
79
80     /**
81      * Returns the string that would be rendered. This is part of the contract for error renderers
82      * used with validation ... must provide a user-presentable toString() that does not include any
83      * markup.
84      */

85
86     public String JavaDoc toString()
87     {
88         return _string;
89     }
90 }
Popular Tags