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.
025     * This is primarily used to present error messages.
026     * 
027     * @author Howard Lewis Ship
028     */
029    
030    public class RenderString implements IRender, Serializable
031    {
032    
033        private static final long serialVersionUID = 6215074338439140780L;
034    
035        private String _string;
036    
037        private boolean _raw = false;
038    
039        public RenderString(String string)
040        {
041            _string = string;
042        }
043    
044        /**
045         * @param string
046         *            the string to render
047         * @param raw
048         *            if true, the String is rendered as-is, with no filtering. If
049         *            false (the default), the String is filtered.
050         */
051    
052        public RenderString(String string, boolean raw)
053        {
054            _string = string;
055            _raw = raw;
056        }
057    
058        /**
059         * Renders the String to the writer. Does nothing if the string is null. If
060         * raw is true, uses {@link IMarkupWriter#printRaw(String)}, otherwise
061         * {@link IMarkupWriter#print(String)}.
062         */
063    
064        public void render(IMarkupWriter writer, IRequestCycle cycle)
065        {
066            if (_string == null) return;
067    
068            writer.print(_string, _raw);
069        }
070    
071        public String getString()
072        {
073            return _string;
074        }
075    
076        public boolean isRaw()
077        {
078            return _raw;
079        }
080    
081        /**
082         * Returns the string that would be rendered. This is part of the contract
083         * for error renderers used with validation ... must provide a
084         * user-presentable toString() that does not include any markup.
085         */
086    
087        public String toString()
088        {
089            return _string;
090        }
091    }