Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
InsertText |
|
| 2.5714285714285716;2.571 |
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; |
|
18 | import java.io.LineNumberReader; |
|
19 | import java.io.Reader; |
|
20 | import java.io.StringReader; |
|
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 |
|
29 | * {@link org.apache.tapestry.form.TextArea} component. [<a |
|
30 | * href="../../../../../ComponentReference/InsertText.html">Component Reference</a>] |
|
31 | * <p> |
|
32 | * To maintain the line breaks provided originally, this component will break |
|
33 | * the input into individual lines and insert additional HTML to make each line |
|
34 | * seperate. |
|
35 | * <p> |
|
36 | * This can be down more simply, using the <pre> HTML element, but that |
|
37 | * usually renders the text in a non-proportional font. |
|
38 | * |
|
39 | * @author Howard Lewis Ship |
|
40 | */ |
|
41 | ||
42 | 6 | public abstract class InsertText extends AbstractComponent |
43 | { |
|
44 | ||
45 | protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) |
|
46 | { |
|
47 | 6 | if (cycle.isRewinding()) return; |
48 | ||
49 | 5 | String value = getValue(); |
50 | ||
51 | 5 | if (value == null) return; |
52 | ||
53 | 4 | StringReader reader = null; |
54 | 4 | LineNumberReader lineReader = null; |
55 | 4 | InsertTextMode mode = getMode(); |
56 | 4 | boolean raw = getRaw(); |
57 | ||
58 | try |
|
59 | { |
|
60 | 4 | reader = new StringReader(value); |
61 | ||
62 | 4 | lineReader = new LineNumberReader(reader); |
63 | ||
64 | 4 | int lineNumber = 0; |
65 | ||
66 | while(true) |
|
67 | { |
|
68 | 14 | String line = lineReader.readLine(); |
69 | ||
70 | // Exit loop at end of file. |
|
71 | ||
72 | 14 | if (line == null) |
73 | 4 | break; |
74 | ||
75 | 10 | mode.writeLine(lineNumber, line, writer, raw); |
76 | ||
77 | 10 | lineNumber++; |
78 | 10 | } |
79 | ||
80 | } |
|
81 | 0 | catch (IOException ex) |
82 | { |
|
83 | 0 | throw new ApplicationRuntimeException(HTMLMessages |
84 | .textConversionError(ex), this, null, ex); |
|
85 | } |
|
86 | finally |
|
87 | { |
|
88 | 4 | close(lineReader); |
89 | 4 | close(reader); |
90 | 4 | } |
91 | ||
92 | 4 | } |
93 | ||
94 | private void close(Reader reader) |
|
95 | { |
|
96 | 8 | if (reader == null) return; |
97 | ||
98 | try |
|
99 | { |
|
100 | 8 | reader.close(); |
101 | } |
|
102 | 0 | catch (IOException e) |
103 | { |
|
104 | 8 | } |
105 | 8 | } |
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 | 3 | setMode(InsertTextMode.BREAK); |
129 | 3 | } |
130 | ||
131 | } |