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 |
11 |
public abstract class Insert extends AbstractComponent |
37 |
|
{ |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) |
44 |
|
{ |
45 |
11 |
if (cycle.isRewinding()) |
46 |
1 |
return; |
47 |
|
|
48 |
10 |
Object value = getValue(); |
49 |
|
|
50 |
10 |
if (value == null) |
51 |
1 |
return; |
52 |
|
|
53 |
9 |
String insert = null; |
54 |
|
|
55 |
9 |
Format format = getFormat(); |
56 |
|
|
57 |
9 |
if (format == null) |
58 |
|
{ |
59 |
7 |
insert = value.toString(); |
60 |
|
} |
61 |
|
else |
62 |
|
{ |
63 |
|
try |
64 |
|
{ |
65 |
2 |
insert = format.format(value); |
66 |
|
} |
67 |
1 |
catch (Exception ex) |
68 |
|
{ |
69 |
1 |
throw new ApplicationRuntimeException(ComponentMessages |
70 |
|
.unableToFormat(this, value, ex), this, getBinding( |
71 |
|
"format").getLocation(), ex); |
72 |
1 |
} |
73 |
|
} |
74 |
|
|
75 |
8 |
boolean render = getRenderTag() || isParameterBound("class"); |
76 |
|
|
77 |
8 |
if (render) |
78 |
|
{ |
79 |
1 |
writer.begin(getTemplateTagName()); |
80 |
|
|
81 |
1 |
renderInformalParameters(writer, cycle); |
82 |
|
|
83 |
1 |
if (getId() != null && !isParameterBound("id")) |
84 |
0 |
renderIdAttribute(writer, cycle); |
85 |
|
} |
86 |
|
|
87 |
8 |
printText(writer, cycle, insert); |
88 |
|
|
89 |
8 |
if (render) |
90 |
1 |
writer.end(); |
91 |
8 |
} |
92 |
|
|
93 |
|
protected void printText(IMarkupWriter writer, IRequestCycle cycle, String value) |
94 |
|
{ |
95 |
8 |
if (getMode() == null) |
96 |
|
{ |
97 |
4 |
writer.print(value, getRaw()); |
98 |
4 |
return; |
99 |
|
} |
100 |
|
|
101 |
4 |
StringReader reader = null; |
102 |
4 |
LineNumberReader lineReader = null; |
103 |
4 |
InsertMode mode = getMode(); |
104 |
4 |
boolean raw = getRaw(); |
105 |
|
|
106 |
|
try { |
107 |
4 |
reader = new StringReader(value); |
108 |
4 |
lineReader = new LineNumberReader(reader); |
109 |
|
|
110 |
4 |
int lineNumber = 0; |
111 |
|
|
112 |
|
while(true) |
113 |
|
{ |
114 |
14 |
String line = lineReader.readLine(); |
115 |
|
|
116 |
|
|
117 |
|
|
118 |
14 |
if (line == null) |
119 |
4 |
break; |
120 |
|
|
121 |
10 |
mode.writeLine(lineNumber, line, writer, raw); |
122 |
|
|
123 |
10 |
lineNumber++; |
124 |
10 |
} |
125 |
|
|
126 |
0 |
} catch (IOException ex) |
127 |
|
{ |
128 |
0 |
throw new ApplicationRuntimeException(ComponentMessages.textConversionError(ex), this, null, ex); |
129 |
|
} finally |
130 |
|
{ |
131 |
4 |
IOUtils.closeQuietly(lineReader); |
132 |
4 |
IOUtils.closeQuietly(reader); |
133 |
4 |
} |
134 |
4 |
} |
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 |
|
} |