1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.apache.tapestry.components; |
16 | |
|
17 | |
import org.apache.commons.io.IOUtils; |
18 | |
import org.apache.hivemind.ApplicationRuntimeException; |
19 | |
import org.apache.tapestry.AbstractComponent; |
20 | |
import org.apache.tapestry.IMarkupWriter; |
21 | |
import org.apache.tapestry.IRequestCycle; |
22 | |
|
23 | |
import java.io.IOException; |
24 | |
import java.io.LineNumberReader; |
25 | |
import java.io.StringReader; |
26 | |
import java.text.Format; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | 0 | public abstract class Insert extends AbstractComponent |
37 | |
{ |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) |
44 | |
{ |
45 | 0 | if (cycle.isRewinding()) |
46 | 0 | return; |
47 | |
|
48 | 0 | Object value = getValue(); |
49 | |
|
50 | 0 | if (value == null) |
51 | 0 | return; |
52 | |
|
53 | 0 | String insert = null; |
54 | |
|
55 | 0 | Format format = getFormat(); |
56 | |
|
57 | 0 | if (format == null) |
58 | |
{ |
59 | 0 | insert = value.toString(); |
60 | |
} |
61 | |
else |
62 | |
{ |
63 | |
try |
64 | |
{ |
65 | 0 | insert = format.format(value); |
66 | |
} |
67 | 0 | catch (Exception ex) |
68 | |
{ |
69 | 0 | throw new ApplicationRuntimeException(ComponentMessages |
70 | |
.unableToFormat(this, value, ex), this, getBinding( |
71 | |
"format").getLocation(), ex); |
72 | 0 | } |
73 | |
} |
74 | |
|
75 | 0 | boolean render = getRenderTag() || isParameterBound("class"); |
76 | |
|
77 | 0 | if (render) |
78 | |
{ |
79 | 0 | writer.begin(getTemplateTagName()); |
80 | |
|
81 | 0 | renderInformalParameters(writer, cycle); |
82 | |
|
83 | 0 | if (getId() != null && !isParameterBound("id")) |
84 | 0 | renderIdAttribute(writer, cycle); |
85 | |
} |
86 | |
|
87 | 0 | printText(writer, cycle, insert); |
88 | |
|
89 | 0 | if (render) |
90 | 0 | writer.end(); |
91 | 0 | } |
92 | |
|
93 | |
protected void printText(IMarkupWriter writer, IRequestCycle cycle, String value) |
94 | |
{ |
95 | 0 | if (getMode() == null) |
96 | |
{ |
97 | 0 | writer.print(value, getRaw()); |
98 | 0 | return; |
99 | |
} |
100 | |
|
101 | 0 | StringReader reader = null; |
102 | 0 | LineNumberReader lineReader = null; |
103 | 0 | InsertMode mode = getMode(); |
104 | 0 | boolean raw = getRaw(); |
105 | |
|
106 | |
try { |
107 | 0 | reader = new StringReader(value); |
108 | 0 | lineReader = new LineNumberReader(reader); |
109 | |
|
110 | 0 | int lineNumber = 0; |
111 | |
|
112 | |
while(true) |
113 | |
{ |
114 | 0 | String line = lineReader.readLine(); |
115 | |
|
116 | |
|
117 | |
|
118 | 0 | if (line == null) |
119 | 0 | break; |
120 | |
|
121 | 0 | mode.writeLine(lineNumber, line, writer, raw); |
122 | |
|
123 | 0 | lineNumber++; |
124 | 0 | } |
125 | |
|
126 | 0 | } catch (IOException ex) |
127 | |
{ |
128 | 0 | throw new ApplicationRuntimeException(ComponentMessages.textConversionError(ex), this, null, ex); |
129 | |
} finally |
130 | |
{ |
131 | 0 | IOUtils.closeQuietly(lineReader); |
132 | 0 | IOUtils.closeQuietly(reader); |
133 | 0 | } |
134 | 0 | } |
135 | |
|
136 | |
public abstract Object getValue(); |
137 | |
|
138 | |
public abstract Format getFormat(); |
139 | |
|
140 | |
public abstract boolean getRaw(); |
141 | |
|
142 | |
public abstract boolean getRenderTag(); |
143 | |
|
144 | |
public abstract InsertMode getMode(); |
145 | |
} |