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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.util;

import java.io.*;

import org.litesoft.commonfoundation.exceptions.*;
import org.litesoft.commonfoundation.stringmatching.*;
import org.litesoft.commonfoundation.typeutils.*;

import java.util.*;

import org.litesoft.commonfoundation.typeutils.Objects;
import org.litesoft.core.util.*;

public class FileUtils extends FileUtil
{
    public static Reader[] filesToReaders( File pDir, String... pFileNames )
            throws FileNotFoundException
    {
        if ( Objects.isNullOrEmpty( pFileNames ) )
        {
            throw new IllegalArgumentException( "No Files provided" );
        }
        Reader[] readers = new Reader[pFileNames.length];
        for ( int i = 0; i < pFileNames.length; i++ )
        {
            File file = new File( pDir, pFileNames[i] );
            readers[i] = new InformativeFileReader( file );
        }
        return readers;
    }

    private static class InformativeFileReader extends FileReader implements ToStringInformative
    {
        private File mFile;

        private InformativeFileReader( File pFile )
                throws FileNotFoundException
        {
            super( pFile );
            mFile = pFile;
        }

        @Override
        public String toString()
        {
            return mFile.getAbsolutePath();
        }
    }

    public static void deleteIfExists( File pFile )
            throws FileSystemException
    {
        Objects.assertNotNull( "File", pFile );
        if ( pFile.isFile() )
        {
            if ( !pFile.delete() || pFile.exists() )
            {
                throw new FileSystemException( "Unable to delete: " + pFile.getAbsolutePath() );
            }
        }
    }

    public static FilenameFilter createFileNameFilter( String... pParts )
    {
        StringMatcher zSM = StringMatcherFactory.createStartsWith( pParts );
        if ( zSM == null )
        {
            throw new IllegalArgumentException( "Must Provide something to Filter on!" );
        }
        return new StringMatcherFilenameFilter( zSM );
    }

    @SuppressWarnings({"UnusedDeclaration"})
    public static String locateFile( String pFile )
            throws FileSystemException
    {
        try
        {
            File file = new File( pFile );
            while ( !file.isFile() )
            {
                File parent = file.getParentFile();
                if ( parent != null )
                {
                    File grandparent = parent.getParentFile();
                    if ( grandparent != null )
                    {
                        file = new File( grandparent, pFile );
                        continue;
                    }
                }
                throw new FileNotFoundException( pFile );
            }
            return file.getAbsolutePath();
        }
        catch ( IOException e )
        {
            throw new FileSystemException( e );
        }
    }

    public static byte[] load( File pFile, int pMaxAllowedSize )
            throws FileSystemException
    {
        Objects.assertNotNull( "File", pFile );
        try
        {
            if ( !pFile.exists() )
            {
                throw new FileNotFoundException( pFile.getAbsolutePath() );
            }
            long fSize = pFile.length();
            if ( fSize > pMaxAllowedSize )
            {
                throw new FileSystemException( "File (" + pFile + ") Too large (" + fSize + "), but max set to: " + pMaxAllowedSize );
            }
            int fileSize = (int) fSize;
            byte[] b = new byte[fileSize];
            InputStream is = new FileInputStream( pFile );
            boolean closed = false;
            try
            {
                int offset = 0;
                while ( offset < fileSize )
                {
                    int i = is.read( b, offset, fileSize - offset );
                    if ( i < 1 )
                    {
                        throw new FileSystemException( "Unable to read the entire file (" + pFile + ").  Expected " + fileSize + ", but only got: " + offset );
                    }
                    offset += i;
                }
                if ( -1 != is.read() )
                {
                    throw new FileSystemException( "Read beyond file (" + pFile + ") size (" + fileSize + ")!" );
                }
                closed = true;
                is.close();
            }
            finally
            {
                if ( !closed )
                {
                    Utils.dispose( is );
                }
            }
            return b;
        }
        catch ( IOException e )
        {
            throw new FileSystemException( e );
        }
    }

    public static void appendTextFile( File pFile, String... pLines )
            throws FileSystemException
    {
        Objects.assertNotNull( "File", pFile );
        File file = new File( pFile.getAbsolutePath() );
        addLines( file, true, pLines );
    }

    public static void store( File pFile, byte[] pBytes )
            throws FileSystemException
    {
        Objects.assertNotNull( "File", pFile );
        File file = Directories.insureParent( new File( pFile.getAbsolutePath() + ".new" ) );
        try
        {
            OutputStream os = new FileOutputStream( file );
            boolean closed = false;
            try
            {
                if ( (pBytes != null) && (pBytes.length != 0) )
                {
                    os.write( pBytes );
                }
                closed = true;
                os.close();
            }
            finally
            {
                if ( !closed )
                {
                    Utils.dispose( os );
                }
            }
        }
        catch ( IOException e )
        {
            throw new FileSystemException( e );
        }
        rollIn( file, pFile, new File( pFile.getAbsolutePath() + ".bak" ) );
    }

    public static void storeTextFile( File pFile, String... pLines )
            throws FileSystemException
    {
        Objects.assertNotNull( "File", pFile );
        File file = new File( pFile.getAbsolutePath() + ".new" );
        addLines( file, false, pLines );
        rollIn( file, pFile, new File( pFile.getAbsolutePath() + ".bak" ) );
    }

    private static void addLines( File pFile, boolean pAppend, String... pLines )
            throws FileSystemException
    {
        try
        {
            addLines( createWriter( pFile, pAppend ), pLines );
        }
        catch ( IOException e )
        {
            throw new FileSystemException( e );
        }
    }

    private static void addLines( BufferedWriter pWriter, String... pLines )
            throws IOException
    {
        boolean closed = false;
        try
        {
            for ( String line : Strings.deNull( pLines ) )
            {
                if ( line != null )
                {
                    pWriter.write( line );
                    pWriter.write( '\n' );
                }
            }
            closed = true;
            pWriter.close();
        }
        finally
        {
            if ( !closed )
            {
                Utils.dispose( pWriter );
            }
        }
    }

    public static String[] loadTextFile( File pFile )
            throws FileSystemException
    {
        Objects.assertNotNull( "File", pFile );
        try
        {
            if ( !pFile.exists() )
            {
                throw new FileNotFoundException( pFile.getAbsolutePath() );
            }
            List<String> lines = new LinkedList<String>();
            BufferedReader reader = createReader( pFile );
            boolean closed = false;
            try
            {
                for ( String line; null != (line = reader.readLine()); )
                {
                    lines.add( line );
                }
                closed = true;
                reader.close();
            }
            finally
            {
                if ( !closed )
                {
                    Utils.dispose( reader );
                }
            }
            return lines.toArray( new String[lines.size()] );
        }
        catch ( IOException e )
        {
            throw new FileSystemException( e );
        }
    }

    @SuppressWarnings({"UnusedDeclaration"})
    public static String loadTextFileAsString( File pFile )
            throws FileSystemException
    {
        return loadTextFileAsString( pFile, null );
    }

    public static String loadTextFileAsString( File pFile, String pLineSeparator )
            throws FileSystemException
    {
        Objects.assertNotNull( "File", pFile );
        pLineSeparator = Strings.deNull( pLineSeparator, "\n" );
        try
        {
            if ( !pFile.exists() )
            {
                throw new FileNotFoundException( pFile.getAbsolutePath() );
            }

            StringBuilder buffer = new StringBuilder();
            BufferedReader reader = createReader( pFile );
            boolean closed = false;
            try
            {
                int count = 0;
                for ( String line; null != (line = reader.readLine()); count++ )
                {
                    if ( count > 0 )
                    {
                        buffer.append( pLineSeparator );
                    }
                    buffer.append( line );
                }
                closed = true;
                reader.close();
            }
            finally
            {
                if ( !closed )
                {
                    Utils.dispose( reader );
                }
            }

            return buffer.toString();
        }
        catch ( IOException e )
        {
            throw new FileSystemException( e );
        }
    }

    public static void rollIn( File pNewFile, File pTargetFile, File pBackupFile )
            throws FileSystemException
    {
        Objects.assertNotNull( "NewFile", pNewFile );
        Objects.assertNotNull( "TargetFile", pTargetFile );
        Objects.assertNotNull( "BackupFile", pBackupFile );
        if ( !pNewFile.exists() )
        {
            throw new FileSystemException( "Does not Exist: " + pNewFile.getPath() );
        }
        boolean targetExisted = false;
        if ( pTargetFile.exists() )
        {
            targetExisted = true;
            deleteIfExists( pBackupFile );
            renameFromTo( pTargetFile, pBackupFile );
        }
        try
        {
            renameFromTo( pNewFile, pTargetFile );
        }
        catch ( FileSystemException e )
        {
            if ( targetExisted )
            {
                attemptToRollBack( pNewFile, pTargetFile, pBackupFile );
            }
            throw e;
        }
    }

    @SuppressWarnings({"ResultOfMethodCallIgnored"})
    private static void attemptToRollBack( File pNewFile, File pTargetFile, File pBackupFile )
    {
        switch ( (pNewFile.exists() ? 0 : 4) + (pTargetFile.exists() ? 0 : 2) + (pBackupFile.exists() ? 0 : 1) )
        {
            // What Happen'd
            case 0: // There: ------------- Nobody --------------
            case 2: // There:            pTargetFile
            case 4: // There: pNewFile
            case 6: // There: pNewFile & pTargetFile
            case 7: // There: pNewFile & pTargetFile & pBackupFile
                return;
            case 3: // There:            pTargetFile & pBackupFile
                pTargetFile.renameTo( pNewFile );
                // Fall Thru
            case 1: // There:                          pBackupFile
            case 5: // There: pNewFile               & pBackupFile
                pBackupFile.renameTo( pTargetFile );
                break;
        }
    }

    /**
     * This method will rename the pSourceFile to the pDestinationFile name.
     * <p/>
     * It is Inherently fragile when dealing with multiple processes playing
     * in the same file system name spaces.  It should therefore NOT be used
     * in a multi-process creation-consumption shared file system name space.
     * <p/>
     * Specifically there are two windows of opportunities for multiple processes
     * to mess with the "as linear" assumptions
     */
    private static void renameFromTo( File pSourceFile, File pDestinationFile )
            throws FileSystemException
    {
        Objects.assertNotNull( "SourceFile", pSourceFile );
        Objects.assertNotNull( "DestinationFile", pDestinationFile );
        // Win 1 Start
        if ( !pSourceFile.exists() )
        {
            throw new FileSystemException( "SourceFile does not exist: " + pSourceFile.getAbsolutePath() );
        }
        if ( pDestinationFile.exists() )
        {
            throw new FileSystemException( "DestinationFile already exists: " + pDestinationFile.getAbsolutePath() );
        }
        // Win 2 Start
        if ( !pSourceFile.renameTo( pDestinationFile ) )    // Win 1 End
        {
            throw renameFailed( pSourceFile, pDestinationFile, "Failed" );
        }
        boolean sThere = pSourceFile.exists();
        boolean dThere = pDestinationFile.exists();
        // Win 2 End
        if ( sThere )
        {
            throw renameFailed( pSourceFile, pDestinationFile, "claims Succeess, but Source still there" + (dThere ? " and so is the Destination!" : "!") );
        }
        if ( !dThere )
        {
            throw renameFailed( pSourceFile, pDestinationFile, "claims Succeess, but the Destination is NOT there!" );
        }
    }

    private static FileSystemException renameFailed( File pSourceFile, File pDestinationFile, String pAdditionalExplanation )
    {
        throw new FileSystemException( "Rename (" + pSourceFile.getAbsolutePath() + ") to (" + pDestinationFile.getAbsolutePath() + ") " + pAdditionalExplanation );
    }

    private static class StringMatcherFilenameFilter implements FilenameFilter
    {
        private StringMatcher mSM;

        public StringMatcherFilenameFilter( StringMatcher pSM )
        {
            mSM = pSM;
        }

        @Override
        public boolean accept( File dir, String name )
        {
            return mSM.matches( name );
        }
    }

    public static void main( String[] args )
            throws Exception
    {
        File zFile = new File( "PersistanceTier/design/NextGEN.xml" );
        String[] lines = loadTextFile( zFile );
        process( lines );
        storeTextFile( zFile, lines );
    }

    private static void process( String[] pLines )
    {
        for ( int i = 0; i < pLines.length; i++ )
        {
            String zLine = pLines[i];
            if ( zLine != null )
            {
                if ( zLine.trim().startsWith( "<column " ) )
                {
                    processColumn( pLines, i, findEONode( pLines, i ) );
                }
                else if ( zLine.trim().startsWith( "<foreignKey " ) )
                {
                    processForeignKey( pLines, i, findEONode( pLines, i ) );
                }
            }
        }
    }

    private static int findEONode( String[] pLines, int pFromIndex )
    {
        int zThruIndex = pFromIndex;
        while ( zThruIndex < pLines.length )
        {
            String zLine = pLines[zThruIndex];
            if ( (zLine != null) && zLine.trim().endsWith( ">" ) )
            {
                break;
            }
            zThruIndex++;
        }
        return zThruIndex;
    }

    private static boolean isAttributeOneOf( String pLine, String... pOneOf )
    {
        for ( String s : pOneOf )
        {
            if ( pLine.startsWith( s + "=" ) )
            {
                return true;
            }
        }
        return false;
    }

    private static void processColumn( String[] pLines, int pBegLine, int pEndLine )
    {
        for ( int i = pBegLine + 1; i < pEndLine; i++ )
        {
            String zLine = pLines[i];
            if ( zLine != null )
            {
                zLine = zLine.trim();
                if ( zLine.equals( "type=\"foreignKey\"" ) ) // No More Columns as ForeignKeys
                {
                    for ( int j = pBegLine; j <= pEndLine; j++ )
                    {
                        pLines[j] = null;
                    }
                    return;
                }
                if ( isAttributeOneOf( zLine, "fkonly", "isContainer", "IsContainer", "ReferenceSortColumn", "ThemDependsOnUs", "UsDependsOnThem", "table", "updateReferencedPO" ) )
                {
                    pLines[i] = null;
                }
            }
        }
    }

    private static void processForeignKey( String[] pLines, int pBegLine, int pEndLine )
    {
        for ( int i = pBegLine + 1; i < pEndLine; i++ )
        {
            String zLine = pLines[i];
            if ( zLine != null )
            {
                zLine = zLine.trim();
                if ( isAttributeOneOf( zLine, "fkonly", "isContainer", "IsContainer", "defaultValue", "enumeration", "len" ) )
                {
                    pLines[i] = null;
                }
            }
        }
    }
}

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

Diff revisions: vs.
Revision Author Commited Message
939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

917 Diff Diff GeorgeS picture GeorgeS Sun 08 Dec, 2013 20:49:56 +0000

1.7 prep & VersionedStaticContentFilter upgrade to new “/ver” model!

823 Diff Diff GeorgeS picture GeorgeS Sun 19 Aug, 2012 16:10:13 +0000
809 Diff Diff GeorgeS picture GeorgeS Thu 16 Aug, 2012 04:10:46 +0000
804 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 12:48:51 +0000
802 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 04:04:47 +0000
76 Diff Diff GeorgeS picture GeorgeS Wed 27 Oct, 2010 00:24:59 +0000
73 Diff Diff GeorgeS picture GeorgeS Mon 25 Oct, 2010 20:47:48 +0000
72 Diff Diff GeorgeS picture GeorgeS Mon 25 Oct, 2010 19:13:46 +0000
63 GeorgeS picture GeorgeS Tue 28 Sep, 2010 13:55:00 +0000