|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
InsertText.java | 87.5% | 92.6% | 100% | 92.1% |
|
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 {@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 <pre> 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 | 5 | protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) |
43 | { | |
44 | 5 | if (cycle.isRewinding()) |
45 | 1 | return; |
46 | ||
47 | 4 | String value = getValue(); |
48 | ||
49 | 4 | if (value == null) |
50 | 1 | return; |
51 | ||
52 | 3 | StringReader reader = null; |
53 | 3 | LineNumberReader lineReader = null; |
54 | 3 | InsertTextMode mode = getMode(); |
55 | 3 | boolean raw = getRaw(); |
56 | ||
57 | 3 | try |
58 | { | |
59 | 3 | reader = new StringReader(value); |
60 | ||
61 | 3 | lineReader = new LineNumberReader(reader); |
62 | ||
63 | 3 | int lineNumber = 0; |
64 | ||
65 | 3 | while (true) |
66 | { | |
67 | 11 | String line = lineReader.readLine(); |
68 | ||
69 | // Exit loop at end of file. | |
70 | ||
71 | 11 | if (line == null) |
72 | 3 | break; |
73 | ||
74 | 8 | mode.writeLine(lineNumber, line, writer, raw); |
75 | ||
76 | 8 | lineNumber++; |
77 | } | |
78 | ||
79 | } | |
80 | catch (IOException ex) | |
81 | { | |
82 | 0 | throw new ApplicationRuntimeException(HTMLMessages.textConversionError(ex), this, null, |
83 | ex); | |
84 | } | |
85 | finally | |
86 | { | |
87 | 3 | close(lineReader); |
88 | 3 | close(reader); |
89 | } | |
90 | ||
91 | } | |
92 | ||
93 | 6 | private void close(Reader reader) |
94 | { | |
95 | 6 | if (reader == null) |
96 | 0 | return; |
97 | ||
98 | 6 | try |
99 | { | |
100 | 6 | 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, {@link InsertTextMode#BREAK}. | |
122 | * | |
123 | * @since 3.0 | |
124 | */ | |
125 | 2 | protected void finishLoad() |
126 | { | |
127 | 2 | setMode(InsertTextMode.BREAK); |
128 | } | |
129 | ||
130 | } |
|