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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.util; import java.io.*; import org.litesoft.commonfoundation.iterators.*; import org.litesoft.commonfoundation.typeutils.*; import java.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.out.println(); System.out.println( "Note: The \\\\ before each File Pattern keeps both your shell AND Java from expanding the pattern based on the current directory" ); System.exit( 1 ); } private boolean mCleanDestDirs = System.getProperty( "CleanDestDirs" ) != null; private boolean mChanges = false; private int mNotifications = 0; private int mSilentChecks = 0; private void notify( Character pAction, String pWhat ) { if ( pAction == null ) { if ( ++mSilentChecks < 25 ) { return; } mSilentChecks = 0; System.out.print( SILENT_THRESHOLD ); if ( ++mNotifications >= 80 ) { System.out.println(); mNotifications = 0; } return; } if ( mNotifications != 0 ) { System.out.println(); mNotifications = 0; } System.out.println( pAction + " - " + pWhat ); mChanges = true; } @SuppressWarnings({"ResultOfMethodCallIgnored"}) protected void process( String pName, String pVersion, String[] args ) throws IOException { 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]; for ( int i = 3; i < args.length; i++ ) { zFilePatterns[i - 3] = deEscape( i, args[i] ); } process( sourcePath, destinationPath, xtraFilesPath, zFilePatterns ); if ( mChanges ) { System.out.println(); System.out.print( "Press Enter to Exit: " ); System.in.read(); } } private String deEscape( int pIndex, String pArg ) { String rv = pArg.replace( '/', '\\' ); while ( rv.startsWith( "\\" ) ) { rv = rv.substring( 1 ); } if ( rv.length() == 0 ) { throw new IllegalArgumentException( "Nothing left after cleaning Argument[" + pIndex + "]: " + pArg ); } if ( rv.contains( "\\" ) ) { throw new IllegalArgumentException( "Argument[" + pIndex + "] '" + pArg + "' after cleaning still contained a back slash ('\\'): " + rv ); } return rv; } private void process( File pSourcePath, File pDestinationPath, File pXtraFilesPath, String[] pFilePatterns ) { Directories.checkDirectoryable( pSourcePath ); Directories.checkDirectoryable( pDestinationPath ); Directories.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 { if ( mCleanDestDirs ) { File zDestinationPath = new File( pDestinationPath, dest ); File zXtraFilesPath = new File( pXtraFilesPath, dest ); notify( moveDirForced( zDestinationPath, zXtraFilesPath ), zDestinationPath.getPath() ); } 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 ); } if ( mCleanDestDirs ) { for (; dest != null; dest = getNextOrNull( destination ) ) { File zDestinationPath = new File( pDestinationPath, dest ); File zXtraFilesPath = new File( pXtraFilesPath, dest ); notify( moveDirForced( zDestinationPath, zXtraFilesPath ), zDestinationPath.getPath() ); } } } private void process( File pSourcePath, File pDestinationPath, File pXtraFilesPath, String pFilePattern ) { FilenameFilter filter = FileUtils.createFileNameFilter( Strings.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 ); source = getNextOrNull( sources ); dest = getNextOrNull( destination ); } else if ( i < 0 ) // source < dest { notify( copyAddFile( pSourcePath, pDestinationPath, source ), source ); source = getNextOrNull( sources ); } else // source > dest { notify( moveFileForced( pDestinationPath, pXtraFilesPath, dest ), dest ); dest = getNextOrNull( destination ); } } for (; source != null; source = getNextOrNull( sources ) ) { notify( copyAddFile( pSourcePath, pDestinationPath, source ), source ); } for (; dest != null; dest = getNextOrNull( destination ) ) { notify( moveFileForced( pDestinationPath, pXtraFilesPath, dest ), dest ); } } private Iterator<String> getFilesSorted( File pPath, FilenameFilter pFilter ) { String[] files = Strings.EMPTY_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 = Strings.EMPTY_ARRAY; if ( pPath.isDirectory() ) { files = pPath.list( Directories.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
Revision | Author | Commited | Message |
---|---|---|---|
947 Diff | GeorgeS | Fri 06 Jun, 2014 23:36:56 +0000 | Correct Spelling of package! |
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
823 Diff | GeorgeS | Sun 19 Aug, 2012 16:10:13 +0000 | |
802 Diff | GeorgeS | Wed 15 Aug, 2012 04:04:47 +0000 | |
800 Diff | GeorgeS | Wed 15 Aug, 2012 03:33:38 +0000 | |
84 Diff | GeorgeS | Thu 11 Nov, 2010 20:22:50 +0000 | |
80 Diff | GeorgeS | Fri 29 Oct, 2010 17:23:26 +0000 | |
76 Diff | GeorgeS | Wed 27 Oct, 2010 00:24:59 +0000 | |
74 Diff | GeorgeS | Mon 25 Oct, 2010 21:23:41 +0000 | |
73 | GeorgeS | Mon 25 Oct, 2010 20:47:48 +0000 |