View Javadoc

1   package org.apache.maven.continuum.web.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
33   * @version $Id: WorkingCopyContentGenerator.java 1372260 2012-08-13 04:29:09Z brett $
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\">&nbsp;<a href=\"" ).append( baseUrl ).append( urlParamSeparator ).append(
70              "userDirectory=/\">/</a><br /></td><td>&nbsp;</td><td>&nbsp;</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( "&nbsp;&nbsp;<img src=\"" ).append(
116                     imagesBaseUrl ).append( "file.gif\">&nbsp;<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\">&nbsp;<a href =\"" ).append( baseUrl ).append(
140                     urlParamSeparator ).append( "userDirectory=" ).append( userDirectory ).append( "\">" ).append(
141                     directoryName ).append( "</a></td><td width=\"1%\">" + "&nbsp;" + "</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, " ", "&nbsp;" );
157     }
158 
159     private static String getReadableFileSize( long fileSizeInBytes )
160     {
161         if ( fileSizeInBytes >= GIGA )
162         {
163             return decFormatter.format( fileSizeInBytes / GIGA ) + "&nbsp;Gb";
164         }
165         else if ( fileSizeInBytes >= MEGA )
166         {
167             return decFormatter.format( fileSizeInBytes / MEGA ) + "&nbsp;Mb";
168         }
169         else if ( fileSizeInBytes >= KILO )
170         {
171             return decFormatter.format( fileSizeInBytes / KILO ) + "&nbsp;Kb";
172         }
173         else if ( fileSizeInBytes > 0 && fileSizeInBytes < KILO )
174         {
175             return decFormatter.format( fileSizeInBytes ) + "&nbsp;b";
176         }
177 
178         return "0&nbsp;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( "&nbsp;&nbsp;" );
200             userdir = userdir.substring( userdir.indexOf( File.separator ) + 1 );
201         }
202         return indent.toString();
203     }
204 }