package org.apache.maven.continuum.web.tool;

/*
 * Copyright 2004-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.util.StringUtils;

import java.io.File;
import java.util.Iterator;
import java.util.List;

/**
 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
 * @version $Id$
 */
public class WorkingCopyContentGenerator
    extends AbstractLogEnabled
{
    private File basedir;

    public String generate( Object item, String baseUrl, File basedir )
    {
        this.basedir = basedir;

        List directoryEntries = (List) item;

        StringBuffer buf = new StringBuffer();

        buf.append( "+&nbsp;<a href=\"" + baseUrl + "?userDirectory=/\">/</a><br />" );

        print( directoryEntries, "&nbsp;&nbsp;", baseUrl, buf );

        return buf.toString();
    }

    private void print( List dirs, String indent, String baseUrl, StringBuffer buf )
    {
        for ( Iterator i = dirs.iterator(); i.hasNext(); )
        {
            Object obj = i.next();

            print( obj, indent, baseUrl, buf );
        }
    }
    private void print( Object obj, String indent, String baseUrl, StringBuffer buf )
    {
        if ( obj instanceof File )
        {
            File f = (File) obj;;

            if ( !f.isDirectory() )
            {
                String userDirectory = null;

                if ( f.getParentFile().getAbsolutePath().equals( basedir.getAbsolutePath() ) )
                {
                    userDirectory = "/";
                }
                else
                {
                    userDirectory = f.getParentFile().getAbsolutePath().substring( basedir.getAbsolutePath().length() + 1 );
                }

                userDirectory = StringUtils.replace( userDirectory, "\\", "/" );

                buf.append( indent + "&nbsp;&nbsp;&nbsp;<a href=\"" + baseUrl + "?userDirectory=" + userDirectory + "&file=" + f.getName() + "\">" + f.getName() + "</a><br />" );
            }
            else
            {
                String userDirectory = f.getAbsolutePath().substring( basedir.getAbsolutePath().length() + 1 );

                userDirectory = StringUtils.replace( userDirectory, "\\", "/" );

                buf.append( indent + "+&nbsp;<a href=\"" + baseUrl + "?userDirectory=" + userDirectory + "\">" + f.getName() + "</a><br />" );
            }
        }
        else
        {
            print( (List) obj, indent + "&nbsp;&nbsp;", baseUrl, buf );
        }
    }
}
@@ -1901,32 +1901,229 @@
 )%0A            %7B%0A
+                String fileName = f.getName();%0A%0A                if ( !%22.cvsignore%22.equals( fileName ) && !%22vssver.scc%22.equals( fileName ) && !%22.DS_Store%22.equals( fileName ) )%0A                %7B%0A    
                 
@@ -2152,16 +2152,20 @@
  null;%0A%0A
+    
         
@@ -2259,32 +2259,36 @@
                 
+    
 %7B%0A              
@@ -2285,32 +2285,36 @@
                 
+ 
+   
 userDirectory = 
@@ -2326,32 +2326,36 @@
                 
+    
 %7D%0A              
@@ -2348,32 +2348,36 @@
                 
+ 
+   
 else%0A           
@@ -2373,32 +2373,36 @@
                 
+    
 %7B%0A              
@@ -2399,32 +2399,36 @@
                 
+ 
+   
 userDirectory = 
@@ -2528,24 +2528,28 @@
             
+    
 %7D%0A%0A         
@@ -2547,32 +2547,36 @@
                 
+ 
+   
 userDirectory = 
@@ -2617,32 +2617,36 @@
 , %22%5C%5C%22, %22/%22 );%0A%0A
+    
                 
@@ -2753,34 +2753,31 @@
 %22&file=%22 + f
-.get
+ile
 Name
-()
  + %22%5C%22%3E%22 + f
@@ -2772,34 +2772,31 @@
  + %22%5C%22%3E%22 + f
-.get
+ile
 Name
-()
  + %22%3C/a%3E%3Cbr 
@@ -2794,32 +2794,50 @@
 %22%3C/a%3E%3Cbr /%3E%22 );%0A
+                %7D%0A
             %7D%0A  
@@ -2857,32 +2857,231 @@
 e%0A            %7B%0A
+                String directoryName = f.getName();%0A%0A                if ( !%22CVS%22.equals( directoryName ) && !%22.svn%22.equals( directoryName ) && !%22SCCS%22.equals( directoryName ) )%0A                %7B%0A    
                 
@@ -3185,32 +3185,36 @@
                 
+    
 userDirectory = 
@@ -3271,32 +3271,36 @@
                 
+   
+ 
 buf.append( inde
@@ -3380,27 +3380,29 @@
 %22%5C%22%3E%22 + 
-f.get
+directory
 Name
-()
  + %22%3C/a%3E
@@ -3404,32 +3404,50 @@
 %22%3C/a%3E%3Cbr /%3E%22 );%0A
+                %7D%0A
             %7D%0A  

Exclude scm files (CVS, .svn, ...)