View Javadoc

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