/* * [RepairSeeID.java] * * Summary: Make unique
links. * * Copyright: (c) 2014-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 2014-07-01 initial version */ package com.mindprod.repair; import com.mindprod.commandline.CommandLine; import com.mindprod.common18.EIO; import com.mindprod.common18.ST; import com.mindprod.fastcat.FastCat; import com.mindprod.filter.AllButSVNDirectoriesFilter; import com.mindprod.filter.ExtensionListFilter; import com.mindprod.hunkio.HunkIO; import java.io.File; import java.io.IOException; import static java.lang.System.*; /** * Make unique
links. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2014-07-01 initial version * @since 2014-07-01 */ public class RepairSeeID { private static final String MARKER = "
"; private static final String REPLACEMENT_MARKER = "
"; public static void main( String[] args ) { CommandLine commandLine = new CommandLine( args, new AllButSVNDirectoriesFilter(), new ExtensionListFilter( "html" ) ); for ( File file : commandLine ) { try { final String big = HunkIO.readEntireFile( file ); if ( big == null ) { err.println( "Problems reading file " + EIO.getCanOrAbsPath( file ) ); err.println( "File left as is." ); continue; } int instances = ST.countInstances( big, MARKER ); if ( instances < 2 ) { continue; } // make all but last instance unique. final FastCat sb = new FastCat( ( instances - 1 ) * 2 + 1 ); int start = 0; for ( int i = 0; i < instances - 1; i++ ) { // find next marker final int place = big.indexOf( MARKER, start ); // append chunk prior to maker sb.append( big.substring( start, place ) ); // append a replacement unique marker sb.append( REPLACEMENT_MARKER ); // contiue after end of marker start = place + MARKER.length(); } // append tail end after penuntimate marker sb.append( big.substring( start ) ); HunkIO.writeEntireFile( file, sb.toString() ); out.println( instances + " instances in " + EIO.getCanOrAbsPath( file ) ); } catch ( IOException e ) { e.printStackTrace( err ); err.println(); System.exit( 1 ); } } } }