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     */
030    public class TextOutput
031    {
032        @Parameter(required = true)
033        private String value;
034    
035        @Mixin
036        private DiscardBody discardBody;
037    
038        private static final Pattern SPLIT_PATTERN = Pattern.compile("((\\r\\n)|\\r|\\n)", Pattern.MULTILINE);
039    
040        void beginRender(MarkupWriter writer)
041        {
042            if (value == null) return;
043    
044            String[] lines = SPLIT_PATTERN.split(value);
045    
046            for (String line : lines)
047            {
048                writer.element("p");
049    
050                writer.write(line.trim());
051    
052                writer.end();
053            }
054        }
055    
056        void injectValue(String value)
057        {
058            this.value = value;
059        }
060    }