01.
package
com.hp.web.demoapp.extensions;
02.
03.
import
java.io.BufferedReader;
04.
import
java.io.File;
05.
import
java.io.FileReader;
06.
import
java.io.IOException;
07.
08.
import
org.apache.log4j.Logger;
09.
import
org.apache.wicket.markup.html.basic.Label;
10.
import
org.apache.wicket.model.Model;
11.
12.
import
com.hp.web.demoapp.page.Application;
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
public
class
FileContentLabel
extends
Label
26.
{
27.
private
static
Logger log = Logger.getLogger( FileContentLabel.
class
);
28.
private
static
final
long
serialVersionUID = 1L;
29.
private
static
final
String newline =
"\n"
;
30.
31.
32.
33.
34.
35.
36.
37.
public
FileContentLabel( String wicketid, String filepath )
38.
{
39.
super
( wicketid );
40.
41.
String contents =
null
;
42.
43.
try
44.
{
45.
contents = readFileContents( Application.getRealpath( filepath ) );
46.
47.
if
( contents ==
null
|| contents.length() <
1
)
48.
contents =
"(missing file)"
;
49.
}
50.
catch
( IOException e )
51.
{
52.
log.warn(
"IOException reading included content "
+ filepath );
53.
}
54.
55.
setDefaultModel(
new
Model< String > contents ) );
56.
}
57.
58.
private
static
String readFileContents( String filepath )
throws
IOException
59.
{
60.
File f =
new
File( filepath );
61.
BufferedReader reader;
62.
String line;
63.
StringBuffer buffer =
new
StringBuffer();
64.
65.
if
( !f.exists() )
66.
return
null
;
67.
68.
reader =
new
BufferedReader(
new
FileReader( filepath ) );
69.
70.
do
71.
{
72.
line = reader.readLine();
73.
74.
if
( line !=
null
)
75.
{
76.
buffer.append( line );
77.
buffer.append( newline );
78.
}
79.
}
80.
while
( line !=
null
);
81.
82.
reader.close();
83.
84.
return
buffer.toString();
85.
}
86.
}