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.html; 016 017 import org.apache.hivemind.ApplicationRuntimeException; 018 import org.apache.tapestry.AbstractComponent; 019 import org.apache.tapestry.IMarkupWriter; 020 import org.apache.tapestry.IRequestCycle; 021 022 import java.io.IOException; 023 import java.io.LineNumberReader; 024 import java.io.Reader; 025 import java.io.StringReader; 026 027 /** 028 * Inserts formatted text (possibly collected using a 029 * {@link org.apache.tapestry.form.TextArea} component. [<a 030 * href="../../../../../components/general/inserttext.html">Component Reference</a>] 031 * <p> 032 * To maintain the line breaks provided originally, this component will break 033 * the input into individual lines and insert additional HTML to make each line 034 * seperate. 035 * <p> 036 * This can be down more simply, using the <pre> HTML element, but that 037 * usually renders the text in a non-proportional font. 038 * 039 * @author Howard Lewis Ship 040 */ 041 042 public abstract class InsertText extends AbstractComponent 043 { 044 045 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) 046 { 047 if (cycle.isRewinding()) 048 return; 049 050 String value = getValue(); 051 052 if (value == null) 053 return; 054 055 StringReader reader = null; 056 LineNumberReader lineReader = null; 057 InsertTextMode mode = getMode(); 058 boolean raw = getRaw(); 059 060 try 061 { 062 reader = new StringReader(value); 063 lineReader = new LineNumberReader(reader); 064 065 int lineNumber = 0; 066 067 while(true) 068 { 069 String line = lineReader.readLine(); 070 071 // Exit loop at end of file. 072 073 if (line == null) 074 break; 075 076 mode.writeLine(lineNumber, line, writer, raw); 077 078 lineNumber++; 079 } 080 081 } 082 catch (IOException ex) 083 { 084 throw new ApplicationRuntimeException(HTMLMessages.textConversionError(ex), this, null, ex); 085 } 086 finally 087 { 088 close(lineReader); 089 close(reader); 090 } 091 092 } 093 094 private void close(Reader reader) 095 { 096 if (reader == null) return; 097 098 try 099 { 100 reader.close(); 101 } 102 catch (IOException e) 103 { 104 } 105 } 106 107 /** Parameter. */ 108 public abstract InsertTextMode getMode(); 109 110 public abstract void setMode(InsertTextMode mode); 111 112 /** Parameter. */ 113 114 public abstract String getValue(); 115 116 /** Parameter. */ 117 118 public abstract boolean getRaw(); 119 120 /** 121 * Sets the mode parameter property to its default, 122 * {@link InsertTextMode#BREAK}. 123 * 124 * @since 3.0 125 */ 126 protected void finishLoad() 127 { 128 setMode(InsertTextMode.BREAK); 129 } 130 131 }