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