Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.util;

import java.io.*;
import java.util.*;

import org.litesoft.core.util.*;

public abstract class AbstractFileAR
{
    private static final Character DIR_MOVED = 'O';
    private static final Character FILE_MOVED = 'o';
    private static final Character FILE_UPDATED = 'u';
    private static final Character FILE_COPY_ADDED = 'n';
    private static final Character SILENT_THRESHOLD = '.';

    private void showHelp()
    {
        System.out.println( "Requires a minimum of 4 parameters:" );
        System.out.println();
        System.out.println( "   1) Source Directory Path" );
        System.out.println( "   2) Destination Directory Path" );
        System.out.println( "   3) Path for Extra files that match the Pattern found in the Destination Path" );
        System.out.println( " 4-n) File Pattern (e.g. *PO.java - only '*' supported)" );
        System.exit( 1 );
    }

    private int mNotifications = 0;
    private int mSilentChecks = 0;

    private void notify( Character pWhat )
    {
        if ( pWhat == null )
        {
            if ( ++mSilentChecks < 50 )
            {
                return;
            }
            mSilentChecks = 0;
            pWhat = SILENT_THRESHOLD;
        }
        System.out.print( pWhat );
        if ( ++mNotifications >= 80 )
        {
            System.out.println();
            mNotifications = 0;
        }
    }


    protected void process( String pName, String pVersion, String[] args )
    {
        System.out.println( pName + " vs " + pVersion );
        if ( args.length < 4 )
        {
            showHelp();
        }
        File sourcePath = new File( args[0] );
        File destinationPath = new File( args[1] );
        File xtraFilesPath = new File( args[2] );
        if ( !sourcePath.isDirectory() )
        {
            System.out.println( "Not a Directory: " + sourcePath.getAbsolutePath() );
            System.exit( 2 );
        }
        String[] zFilePatterns = new String[args.length - 3];
        System.arraycopy( args, 3, zFilePatterns, 0, zFilePatterns.length );
        process( sourcePath, destinationPath, xtraFilesPath, zFilePatterns );
    }

    private void process( File pSourcePath, File pDestinationPath, File pXtraFilesPath, String[] pFilePatterns )
    {
        DirectoryUtils.checkDirectoryable( pSourcePath );
        DirectoryUtils.checkDirectoryable( pDestinationPath );
        DirectoryUtils.checkDirectoryable( pXtraFilesPath );

        for ( String zFilePattern : pFilePatterns )
        {
            process( pSourcePath, pDestinationPath, pXtraFilesPath, zFilePattern );
        }
        Iterator<String> sources = getDirsSorted( pSourcePath );
        Iterator<String> destination = getDirsSorted( pDestinationPath );

        String source = getNextOrNull( sources );
        String dest = getNextOrNull( destination );
        while ( (source != null) && (dest != null) )
        {
            File zSourcePath = new File( pSourcePath, source );
            int i = source.compareTo( dest );
            if ( i == 0 ) // Equal
            {
                File zDestinationPath = new File( pDestinationPath, source );
                File zXtraFilesPath = new File( pXtraFilesPath, source );
                process( zSourcePath, zDestinationPath, zXtraFilesPath, pFilePatterns );
                source = getNextOrNull( sources );
                dest = getNextOrNull( destination );
            }
            else if ( i < 0 ) // source < dest
            {
                File zDestinationPath = new File( pDestinationPath, source );
                File zXtraFilesPath = new File( pXtraFilesPath, source );
                process( zSourcePath, zDestinationPath, zXtraFilesPath, pFilePatterns );
                source = getNextOrNull( sources );
            }
            else // source > dest
            {
                File zDestinationPath = new File( pDestinationPath, dest );
                File zXtraFilesPath = new File( pXtraFilesPath, dest );
                moveDirForced( zDestinationPath, zXtraFilesPath );
                dest = getNextOrNull( destination );
            }
        }
        for (; source != null; source = getNextOrNull( sources ) )
        {
            File zSourcePath = new File( pSourcePath, source );
            File zDestinationPath = new File( pDestinationPath, source );
            File zXtraFilesPath = new File( pXtraFilesPath, source );
            process( zSourcePath, zDestinationPath, zXtraFilesPath, pFilePatterns );
        }
        for (; dest != null; dest = getNextOrNull( destination ) )
        {
            notify( moveFileForced( pDestinationPath, pXtraFilesPath, dest ) );
        }
    }

    private void process( File pSourcePath, File pDestinationPath, File pXtraFilesPath, String pFilePattern )
    {
        FilenameFilter filter = FileUtils.createFileNameFilter( Utils.parseChar( pFilePattern, '*' ) );

        Iterator<String> sources = getFilesSorted( pSourcePath, filter );
        Iterator<String> destination = getFilesSorted( pDestinationPath, filter );

        String source = getNextOrNull( sources );
        String dest = getNextOrNull( destination );
        while ( (source != null) && (dest != null) )
        {
            int i = source.compareTo( dest );
            if ( i == 0 ) // Equal
            {
                notify( updateFile( pSourcePath, pDestinationPath, source ) );
                source = getNextOrNull( sources );
                dest = getNextOrNull( destination );
            }
            else if ( i < 0 ) // source < dest
            {
                notify( copyAddFile( pSourcePath, pDestinationPath, source ) );
                source = getNextOrNull( sources );
            }
            else // source > dest
            {
                notify( moveFileForced( pDestinationPath, pXtraFilesPath, dest ) );
                dest = getNextOrNull( destination );
            }
        }
        for (; source != null; source = getNextOrNull( sources ) )
        {
            notify( copyAddFile( pSourcePath, pDestinationPath, source ) );
        }
        for (; dest != null; dest = getNextOrNull( destination ) )
        {
            notify( moveFileForced( pDestinationPath, pXtraFilesPath, dest ) );
        }
    }

    private Iterator<String> getFilesSorted( File pPath, FilenameFilter pFilter )
    {
        String[] files = Utils.EMPTY_STRING_ARRAY;
        if ( pPath.isDirectory() )
        {
            files = pPath.list( pFilter );
        }
        else if ( pPath.exists() )
        {
            System.out.println( "Not a Directory: " + pPath.getAbsolutePath() );
            System.exit( 2 );
        }
        Arrays.sort( files );
        return new ArrayIterator<String>( files );
    }

    private Iterator<String> getDirsSorted( File pPath )
    {
        String[] files = Utils.EMPTY_STRING_ARRAY;
        if ( pPath.isDirectory() )
        {
            files = pPath.list( DirectoryUtils.DirsOnlyFilter.INSTANCE );
        }
        else if ( pPath.exists() )
        {
            System.out.println( "Not a Directory: " + pPath.getAbsolutePath() );
            System.exit( 2 );
        }
        Arrays.sort( files );
        return new ArrayIterator<String>( files );
    }

    private String getNextOrNull( Iterator<String> pIterator )
    {
        return pIterator.hasNext() ? pIterator.next() : null;
    }

    protected Character moveDirForced( File pSourcePath, File pDestinationPath )
    {
        return moveDir( pSourcePath, pDestinationPath ) ? DIR_MOVED : null;
    }

    protected Character moveFileForced( File pSourcePath, File pDestinationPath, String pFileName )
    {
        File source = new File( pSourcePath, pFileName );
        File dest = new File( pDestinationPath, pFileName );
        return moveFile( source, dest ) ? FILE_MOVED : null;
    }

    protected Character updateFile( File pSourcePath, File pDestinationPath, String pFileName )
    {
        File source = new File( pSourcePath, pFileName );
        File dest = new File( pDestinationPath, pFileName );
        return updateFile( source, dest ) ? FILE_UPDATED : null;
    }

    protected Character copyAddFile( File pSourcePath, File pDestinationPath, String pFileName )
    {
        File source = new File( pSourcePath, pFileName );
        File dest = new File( pDestinationPath, pFileName );
        return copyAddFile( source, dest ) ? FILE_COPY_ADDED : null;
    }

    /**
     * Move Dir - Assume same volume!
     */
    abstract protected boolean moveDir( File pSource, File pDestination );

    /**
     * Move File - Assume same volume!
     */
    abstract protected boolean moveFile( File pSource, File pDestination );

    /**
     * Update the existing pDestination from the pSource - Assume NOT the same volume!
     */
    abstract protected boolean updateFile( File pSource, File pDestination );

    /**
     * Copy the pSource to the pDestination - Assume NOT the same volume!
     */
    abstract protected boolean copyAddFile( File pSource, File pDestination );
}

Commits for litesoft/trunk/Java/core/Server/src/org/litesoft/util/AbstractFileAR.java

Diff revisions: vs.
Revision Author Commited Message
72 Diff Diff GeorgeS picture GeorgeS Mon 25 Oct, 2010 19:13:46 +0000
71 Diff Diff GeorgeS picture GeorgeS Mon 25 Oct, 2010 18:30:09 +0000
50 Diff Diff GeorgeS picture GeorgeS Tue 13 Apr, 2010 11:51:38 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000