KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > html > InsertText


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.html;
16
17 import java.io.IOException JavaDoc;
18 import java.io.LineNumberReader JavaDoc;
19 import java.io.Reader JavaDoc;
20 import java.io.StringReader JavaDoc;
21
22 import org.apache.hivemind.ApplicationRuntimeException;
23 import org.apache.tapestry.AbstractComponent;
24 import org.apache.tapestry.IMarkupWriter;
25 import org.apache.tapestry.IRequestCycle;
26
27 /**
28  * Inserts formatted text (possibly collected using a {@link org.apache.tapestry.form.TextArea}
29  * component. [<a HREF="../../../../../ComponentReference/InsertText.html">Component Reference</a>]
30  * <p>
31  * To maintain the line breaks provided originally, this component will break the input into
32  * individual lines and insert additional HTML to make each line seperate.
33  * <p>
34  * This can be down more simply, using the &lt;pre&gt; HTML element, but that usually renders the
35  * text in a non-proportional font.
36  *
37  * @author Howard Lewis Ship
38  */

39
40 public abstract class InsertText extends AbstractComponent
41 {
42     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
43     {
44         if (cycle.isRewinding())
45             return;
46
47         String JavaDoc value = getValue();
48
49         if (value == null)
50             return;
51
52         StringReader JavaDoc reader = null;
53         LineNumberReader JavaDoc lineReader = null;
54         InsertTextMode mode = getMode();
55         boolean raw = getRaw();
56
57         try
58         {
59             reader = new StringReader JavaDoc(value);
60
61             lineReader = new LineNumberReader JavaDoc(reader);
62
63             int lineNumber = 0;
64
65             while (true)
66             {
67                 String JavaDoc line = lineReader.readLine();
68
69                 // Exit loop at end of file.
70

71                 if (line == null)
72                     break;
73
74                 mode.writeLine(lineNumber, line, writer, raw);
75
76                 lineNumber++;
77             }
78
79         }
80         catch (IOException JavaDoc ex)
81         {
82             throw new ApplicationRuntimeException(HTMLMessages.textConversionError(ex), this, null,
83                     ex);
84         }
85         finally
86         {
87             close(lineReader);
88             close(reader);
89         }
90
91     }
92
93     private void close(Reader JavaDoc reader)
94     {
95         if (reader == null)
96             return;
97
98         try
99         {
100             reader.close();
101         }
102         catch (IOException JavaDoc 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 JavaDoc getValue();
115     
116     /** Parameter */
117     
118     public abstract boolean getRaw();
119
120     /**
121      * Sets the mode parameter property to its default, {@link InsertTextMode#BREAK}.
122      *
123      * @since 3.0
124      */

125     protected void finishLoad()
126     {
127         setMode(InsertTextMode.BREAK);
128     }
129
130 }
Popular Tags