/*
* [TidyForJars.java]
*
* Summary: Tidy/sort files of the form for*.xml.
*
* Copyright: (c) 2016-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
*
* Licence: This software may be copied and used freely for any purpose but military.
* http://mindprod.com/contact/nonmil.html
*
* Requires: JDK 1.8+
*
* Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
*
* Version History:
* 1.0 2016-06-03 initial version
*/
package com.mindprod.repair;
import com.mindprod.common18.EIO;
import com.mindprod.common18.ST;
import com.mindprod.filter.ClamFilter;
import com.mindprod.filter.OnlyDirectoriesFilter;
import com.mindprod.hunkio.HunkIO;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import static java.lang.System.*;
/**
* Tidy/sort files of the form for*.xml.
*
* These files control ANT GenJar, java.exe, copy, zip building ...
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2016-06-03 initial versions
* @since 2016-06-03
*/
public class TidyForJars
{
/**
* dedup and array list
*
* @param a ArrayList of Strings
*/
private static void deDup( ArrayList a )
{
int n = a.size();
if ( n <= 1 )
{
return;
}
String prev = a.get( n - 1 ); // last elt
// work down from second last elt
for ( int i = n - 2; i >= 0; i-- )
{
String here = a.get( i );
if ( here.equals( prev ) )
{
err.println( "dup removed " + here );
a.remove( i );
}
else
{
prev = here;
}
}
}
/**
* should this line be sorted?
*
* @param line from the .xml file
*
* @return true if should be sorted
*/
private static boolean isSortable( String line )
{
if ( line.startsWith( " toSort = new ArrayList<>( 200 );
ArrayList sorted = new ArrayList<>( 200 );
String line;
// R E A D.
while ( ( line = br.readLine() ) != null )
{
line = line.trim();
if ( ST.isEmpty( line ) )
{
continue;
}
if ( isSortable( line ) )
{
toSort.add( " " + line ); // indented
}
else
{
if ( toSort.size() > 0 )
{
Collections.sort( toSort );
sorted.addAll( toSort );
toSort.clear();
}
sorted.add( line );
}
}
br.close();
// process the last batch of lines to sort if any.
if ( toSort.size() > 0 )
{
Collections.sort( toSort );
sorted.addAll( toSort );
toSort.clear();
deDup( sorted );
}
// display sorted file
for ( String elt : sorted )
{
out.println( elt );
}
if ( !dry )
{
writeBackSortedFile( toTidy, sorted );
}
}
catch ( IOException e )
{
out.println( "IO Failure on " + EIO.getCanOrAbsPath( toTidy ) + " " + e.getMessage() );
}
// sort alphabeticall except sorted ) throws IOException
{
// write sorted stuff back out, to temp file, then rename.
// O P E N
final File tempFile = HunkIO.createTempFile( "temp_", ".tmp", toTidy );
final FileWriter fw = new FileWriter( tempFile, false /* append */ );
final BufferedWriter bw = new BufferedWriter( fw, 16_384 /* 32K bytes/16K chars, 50% of 64K byte allocation is optimal */ );
final PrintWriter prw = new PrintWriter( bw, false /* auto flush on println */ );
// W R I T E
for ( String line : sorted )
{
prw.println( line );
}
prw.close();
HunkIO.deleteAndRename( tempFile, toTidy );
}
/**
* only parm is -dry
*/
public static void main( String[] args )
{
final boolean dry = ( args.length > 0 && args[ 0 ].equals( "-dry " ) );
final ClamFilter clamFilter = new ClamFilter( "for", ".xml" );
final File base = new File( "E:/com/mindprod" );
final String[] dirs = base.list( new OnlyDirectoriesFilter() );
for ( String dir : dirs )
{
final String[] files = new File( base, dir ).list( clamFilter );
for ( String file : files )
{
final File toTidy = new File( new File( base, dir ), file );
out.println( EIO.getCanOrAbsPath( toTidy ) );
readAndSortFile( toTidy, dry );
}
}
}
}