001    // Copyright 2008, 2011 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.tapestry5.corelib.components;
016    
017    import org.apache.tapestry5.MarkupWriter;
018    import org.apache.tapestry5.annotations.Mixin;
019    import org.apache.tapestry5.annotations.Parameter;
020    import org.apache.tapestry5.corelib.mixins.DiscardBody;
021    
022    import java.util.regex.Pattern;
023    
024    /**
025     * Outputs paragraph oriented text, typically collected via a {@link org.apache.tapestry5.corelib.components.TextArea}
026     * component.  The TextArea is split into lines, and each line it output inside its own <p> element.
027     * 
028     * @see TextArea
029     * @tapestrydoc
030     */
031    public class TextOutput
032    {
033        @Parameter(required = true)
034        private String value;
035    
036        @Mixin
037        private DiscardBody discardBody;
038    
039        private static final Pattern SPLIT_PATTERN = Pattern.compile("((\\r\\n)|\\r|\\n)", Pattern.MULTILINE);
040    
041        void beginRender(MarkupWriter writer)
042        {
043            if (value == null) return;
044    
045            String[] lines = SPLIT_PATTERN.split(value);
046    
047            for (String line : lines)
048            {
049                writer.element("p");
050    
051                writer.write(line.trim());
052    
053                writer.end();
054            }
055        }
056    
057        void injectValue(String value)
058        {
059            this.value = value;
060        }
061    }