001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry.valid;
016
017 import java.io.Serializable;
018
019 import org.apache.tapestry.IMarkupWriter;
020 import org.apache.tapestry.IRender;
021 import org.apache.tapestry.IRequestCycle;
022
023 /**
024 * A wrapper around {@link String} that allows the String to be renderred. This is primarily
025 * used to present error messages.
026 *
027 * @author Howard Lewis Ship
028 */
029
030 public class RenderString implements IRender, Serializable
031 {
032 private static final long serialVersionUID = 6215074338439140780L;
033
034 private String _string;
035
036 private boolean _raw = false;
037
038 public RenderString(String string)
039 {
040 _string = string;
041 }
042
043 /**
044 * @param string
045 * the string to render
046 * @param raw
047 * if true, the String is rendered as-is, with no filtering. If false (the default),
048 * the String is filtered.
049 */
050
051 public RenderString(String string, boolean raw)
052 {
053 _string = string;
054 _raw = raw;
055 }
056
057 /**
058 * Renders the String to the writer. Does nothing if the string is null. If raw is true, uses
059 * {@link IMarkupWriter#printRaw(String)}, otherwise {@link IMarkupWriter#print(String)}.
060 */
061
062 public void render(IMarkupWriter writer, IRequestCycle cycle)
063 {
064 if (_string == null)
065 return;
066
067 writer.print(_string, _raw);
068 }
069
070 public String getString()
071 {
072 return _string;
073 }
074
075 public boolean isRaw()
076 {
077 return _raw;
078 }
079
080 /**
081 * Returns the string that would be rendered. This is part of the contract for error renderers
082 * used with validation ... must provide a user-presentable toString() that does not include any
083 * markup.
084 */
085
086 public String toString()
087 {
088 return _string;
089 }
090 }