/* * [TidyXenuIniFrag.java] * * Summary: Sorts and dedups a fragment of a Xenu Ini file in xenu.inifrag. * * Copyright: (c) 2012-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 2012-11-30 initial version * 1.1 2016-06-08 now quietly ignore blank lines and Enable lines */ package com.mindprod.brokenlinks; import com.mindprod.common18.EIO; import com.mindprod.hunkio.HunkIO; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import static java.lang.System.*; /** * Sorts and dedups a fragment of a Xenu Ini file in xenu.inifrag. *

* xen.inifrag must contain just a section of String statements manually extracted from a xenu.ini file. * Nothing but the String statements with optional EnableXX=1 and blank lines. No lead [file:///. * You must manually re-insert the tidied result. * String95=file:///E:/mindprod/webstart/foot/ * * @author Roedy Green, Canadian Mind Products * @version 1.1 2016-06-08 now quietly ignore blank lines and Enable lines * @since 2012-11-30 */ public class TidyXenuIniFrag { private static final ArrayList strings = new ArrayList<>( 1000 ); private static String header; private static void deDup() { int n = strings.size(); for ( int i = n - 1; i > 0; i-- ) { String low = strings.get( i - 1 ); String high = strings.get( i ); if ( low.equals( high ) ) { // quietly eliminate the dup strings.remove( i ); } else if ( low.equalsIgnoreCase( high ) ) { // complain and eliminate the dup out.println( "dups differ in case only:" ); out.println( " keeping: " + low ); out.println( " dropping: " + high ); strings.remove( i ); } } out.println( n - strings.size() + " dups removed" ); } /** * read strings from xenu.inifrag file * * @param file file to read string from */ private static void readStrings( File file ) { try { // O P E N final BufferedReader br = EIO.getBufferedReader( file, 24 * 1024, EIO.UTF8 ); // R E A D String line; // line == null means EOF while ( ( line = br.readLine() ) != null ) { line = line.trim(); if ( line.isEmpty() || line.startsWith( "Enable" ) ) { /* ignore it */ continue; } // [file:///E:/mindprodcom/index.html-Exclude] if ( line.startsWith( "[file:///" ) ) { header = line; continue; } int place = line.indexOf( '=' ); if ( place < 0 || !line.startsWith( "String" ) ) { err.println( "ignoring: " + line ); } else { final String url = line.substring( place + 1 ); if ( url.startsWith( "file:///" ) ) { if ( !new File( url.substring( "file:///".length() ) ).exists() ) { err.println( ">>> " + line + " has no corresponding file/dir" ); } } strings.add( url ); } } br.close(); } catch ( IOException e ) { err.println( e.getMessage() + "Could not read xenu.inifrag file" ); System.exit( 2 ); } } /** * write tidied strings for xenu.inifrag file to temp file * * @param file file to write tidied strings to. */ private static void writeStrings( File file ) { try { final PrintWriter prw = EIO.getPrintWriter( file, 24 * 1024, EIO.UTF8 ); if ( header != null ) { prw.println( header ); } int n = strings.size(); for ( int i = 0; i < n; i++ ) { prw.println( "Enable" + ( i + 1 ) + "=1" ); } for ( int i = 0; i < n; i++ ) { prw.println( "String" + ( i + 1 ) + "=" + strings.get( i ) ); } prw.close(); } catch ( IOException e ) { err.println( e.getMessage() + "Could not write tidied file" ); System.exit( 2 ); } } /** * no parms. reads from xenu.inifrag. current dir, containing just strings. * optionally lead [file:///E:/mindprodcom/index.html-Exclude] * optionally Enable10=1 */ public static void main( String[] args ) throws IOException { File originalFile = new File( "xenu.inifrag" ); if ( !originalFile.exists() ) { err.println( EIO.getCanOrAbsPath( originalFile ) + " not found" ); exit( 2 ); } if ( !originalFile.canWrite() ) { // canWrite true implies file exists. err.println( "Cannot write to " + EIO.getCanOrAbsPath( originalFile ) ); exit( 2 ); } final File tempFile = HunkIO.createTempFile( "temp_", ".tmp", originalFile ); readStrings( originalFile ); Collections.sort( strings, String.CASE_INSENSITIVE_ORDER ); deDup(); writeStrings( tempFile ); HunkIO.deleteAndRename( tempFile, originalFile ); } }