1 package org.apache.maven.continuum.web.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.codehaus.plexus.util.StringUtils;
23
24 import java.io.File;
25 import java.text.DecimalFormat;
26 import java.text.SimpleDateFormat;
27 import java.util.Calendar;
28 import java.util.Date;
29 import java.util.List;
30
31
32
33
34
35 public class WorkingCopyContentGenerator
36 {
37 private File basedir;
38
39 private String urlParamSeparator;
40
41 private static final DecimalFormat decFormatter = new DecimalFormat( "###.##" );
42
43 private static final long KILO = 1024;
44
45 private static final long MEGA = 1024 * KILO;
46
47 private static final long GIGA = 1024 * MEGA;
48
49 private boolean odd = false;
50
51 public String generate( List<File> files, String baseUrl, String imagesBaseUrl, File basedir )
52 {
53 this.basedir = basedir;
54 if ( baseUrl.indexOf( "?" ) > 0 )
55 {
56 urlParamSeparator = "&";
57 }
58 else
59 {
60 urlParamSeparator = "?";
61 }
62
63 StringBuffer buf = new StringBuffer();
64
65 buf.append( "<div class=\"eXtremeTable\" >" );
66 buf.append( "<table class=\"tableRegion\" width=\"100%\">\n" );
67
68 buf.append( "<tr class=\"odd\"><td><img src=\"" ).append( imagesBaseUrl ).append(
69 "icon_arrowfolder1_sml.gif\"> <a href=\"" ).append( baseUrl ).append( urlParamSeparator ).append(
70 "userDirectory=/\">/</a><br /></td><td> </td><td> </td>" );
71
72 print( basedir, files, baseUrl, imagesBaseUrl, buf );
73
74 buf.append( "</table>\n" );
75 buf.append( "</div>\n" );
76
77 return buf.toString();
78 }
79
80 private void print( File basedir, List<File> files, String baseUrl, String imagesBaseUrl, StringBuffer buf )
81 {
82 for ( File f : files )
83 {
84 print( f, getIndent( basedir, f ), baseUrl, imagesBaseUrl, buf );
85 }
86 }
87
88 private void print( File f, String indent, String baseUrl, String imagesBaseUrl, StringBuffer buf )
89 {
90 String cssClass = odd ? "odd" : "even";
91
92 if ( !f.isDirectory() )
93 {
94 String fileName = f.getName();
95
96 if ( !".cvsignore".equals( fileName ) && !"vssver.scc".equals( fileName ) &&
97 !".DS_Store".equals( fileName ) && !"release.properties".equals( fileName ) )
98 {
99 String userDirectory;
100
101 if ( f.getParentFile().getAbsolutePath().equals( basedir.getAbsolutePath() ) )
102 {
103 userDirectory = "/";
104 }
105 else
106 {
107 userDirectory = f.getParentFile().getAbsolutePath().substring(
108 basedir.getAbsolutePath().length() + 1 );
109 }
110
111 userDirectory = StringUtils.replace( userDirectory, "\\", "/" );
112
113 buf.append( "<tr class=\"" ).append( cssClass ).append( "\">" );
114
115 buf.append( "<td width=\"98%\">" ).append( indent ).append( " <img src=\"" ).append(
116 imagesBaseUrl ).append( "file.gif\"> <a href=\"" ).append( baseUrl ).append(
117 urlParamSeparator ).append( "userDirectory=" ).append( userDirectory ).append( "&file=" ).append(
118 fileName ).append( "\">" ).append( fileName ).append( "</a></td><td width=\"1%\">" ).append(
119 getReadableFileSize( f.length() ) ).append( "</td><td width=\"1%\">" ).append( getFormattedDate(
120 f.lastModified() ) ).append( "</td>\n" );
121 buf.append( "</tr>\n" );
122
123 odd = !odd;
124 }
125 }
126 else
127 {
128 String directoryName = f.getName();
129
130 if ( !"CVS".equals( directoryName ) && !".svn".equals( directoryName ) && !"SCCS".equals( directoryName ) )
131 {
132 String userDirectory = f.getAbsolutePath().substring( basedir.getAbsolutePath().length() + 1 );
133
134 userDirectory = StringUtils.replace( userDirectory, "\\", "/" );
135
136 buf.append( "<tr class=\"" ).append( cssClass ).append( "\">" );
137
138 buf.append( "<td width=\"98%\">" ).append( indent ).append( "<img src=\"" ).append(
139 imagesBaseUrl ).append( "icon_arrowfolder1_sml.gif\"> <a href =\"" ).append( baseUrl ).append(
140 urlParamSeparator ).append( "userDirectory=" ).append( userDirectory ).append( "\">" ).append(
141 directoryName ).append( "</a></td><td width=\"1%\">" + " " + "</td><td width=\"1%\">" ).append(
142 getFormattedDate( f.lastModified() ) ).append( "</td>" );
143 buf.append( "</tr>\n" );
144
145 odd = !odd;
146 }
147 }
148 }
149
150 private String getFormattedDate( long timestamp )
151 {
152 Calendar cal = Calendar.getInstance();
153 cal.setTimeInMillis( timestamp );
154 Date date = cal.getTime();
155 String res = new SimpleDateFormat( "MMM dd, yyyy hh:mm:ss aaa z" ).format( date );
156 return StringUtils.replace( res, " ", " " );
157 }
158
159 private static String getReadableFileSize( long fileSizeInBytes )
160 {
161 if ( fileSizeInBytes >= GIGA )
162 {
163 return decFormatter.format( fileSizeInBytes / GIGA ) + " Gb";
164 }
165 else if ( fileSizeInBytes >= MEGA )
166 {
167 return decFormatter.format( fileSizeInBytes / MEGA ) + " Mb";
168 }
169 else if ( fileSizeInBytes >= KILO )
170 {
171 return decFormatter.format( fileSizeInBytes / KILO ) + " Kb";
172 }
173 else if ( fileSizeInBytes > 0 && fileSizeInBytes < KILO )
174 {
175 return decFormatter.format( fileSizeInBytes ) + " b";
176 }
177
178 return "0 b";
179 }
180
181 private String getIndent( File basedir, File userFile )
182 {
183 String root = basedir.getAbsolutePath();
184 String userdir;
185 if ( userFile.isDirectory() )
186 {
187 userdir = userFile.getAbsolutePath();
188 }
189 else
190 {
191 userdir = userFile.getParentFile().getAbsolutePath();
192 }
193
194 userdir = userdir.substring( root.length() );
195
196 StringBuffer indent = new StringBuffer();
197 while ( userdir.indexOf( File.separator ) >= 0 )
198 {
199 indent.append( " " );
200 userdir = userdir.substring( userdir.indexOf( File.separator ) + 1 );
201 }
202 return indent.toString();
203 }
204 }