/* * [PatchSees.java] * * Summary: One shot to repair damage the see section of the files. * * 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: * 2.0 2014-04-18 initial version */ package com.mindprod.seesort; import com.mindprod.commandline.CommandLine; import com.mindprod.common18.EIO; import com.mindprod.filter.AllButSVNDirectoriesFilter; import com.mindprod.filter.ExtensionListFilter; import com.mindprod.hunkio.HunkIO; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; import static java.lang.System.*; /** * One shot to repair damage the see section of the files. *

* Removes Acronyms from the SEE section * Run from within INtelliJ. Hopefully it will not have to be run again. * * @author Roedy Green, Canadian Mind Products * @version 2.0 2014-04-18 initial version * @since 2014-04-18 */ public final class PatchSees { /** * do we actually want to change the files. True= change files false= proofread the changes. */ private static final boolean CHANGE_FILES = true; private static final String ACRO_PATTERN = "[A-Za-z0-9_\\./\\-+]+"; // we don't deal with "xxx xxx" private static final Pattern JUNK = Pattern.compile( "" + ACRO_PATTERN + "|" + ".+|" + "\\(\\)|" + "" ); // expansions without classes. private static final Pattern NUGGET = Pattern.compile( "" + "" ); private static final Pattern JUNK2 = Pattern.compile( "class=\"(?:acronymdef|acronym) " ); /** * undisplayed copyright notice */ @SuppressWarnings( { "UnusedDeclaration" } ) private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 2014-2017 Roedy Green, Canadian Mind Products, http://mindprod.com"; private static final String endMarker = ""; @SuppressWarnings( { "UnusedDeclaration" } ) private static final String RELEASE_DATE = "2014-04-18"; /** * embedded version string. */ @SuppressWarnings( { "UnusedDeclaration" } ) private static final String VERSION_STRING = "1.0"; /** * Constructor */ private PatchSees() { } /** * patch see section of a file * * @param part piece to correct * @param fileBeingProcessed file we are processing.’ * * @return corrected see section */ private static String patchString( String part, File fileBeingProcessed ) { // want to remove markup of form: // XXX = null // ... = null // (...) = null // = null // = SDK // legacy. Must be StringBuffer final StringBuffer sb = new StringBuffer( part.length() ); // pass1 final Matcher m = JUNK.matcher( part ); while ( m.find() ) { // remove JUNK m.appendReplacement( sb, "" ); } m.appendTail( sb ); part = sb.toString(); sb.setLength( 0 ); // pass2 final Matcher n = NUGGET.matcher( part ); while ( n.find() ) { // preplace it with just the acronym n.appendReplacement( sb, n.group( 1 ) ); } n.appendTail( sb ); part = sb.toString(); // pass3 sb.setLength( 0 ); final Matcher p = JUNK2.matcher( part ); while ( p.find() ) { // remove JUNK2 p.appendReplacement( sb, "class=\"" ); } p.appendTail( sb ); return sb.toString(); } /** * Patch damage to file. * * @param fileBeingProcessed the file currently being processed. * * @throws java.io.IOException if cannot read file */ private static void processOneFile( File fileBeingProcessed ) throws IOException { String startMarker = "

"; String big = HunkIO.readEntireFile( fileBeingProcessed ); int start = big.indexOf( startMarker ); if ( start < 0 ) { // legitimates might not have
in " + fileBeingProcessed ); return; } String part = big.substring( start, end ); String result = patchString( part, fileBeingProcessed ); if ( result.equals( part ) ) { // nothing changed. No need to write results. return; } if ( !CHANGE_FILES ) { out.println( result ); } else { // actually change the files. // generate output into a temporary file until we are sure all is ok. // create a temp file in the same directory as filename // create a tempFile in the same directory as // the input file we have just processed. final File tempFile = HunkIO.createTempFile( "temp_", ".tmp", fileBeingProcessed ); FileWriter emit = new FileWriter( tempFile ); emit.write( big.substring( 0, start ) + result + big.substring( end ) ); emit.close(); // successfully created output in same directory as input, // Now make it replace the input file. HunkIO.deleteAndRename( tempFile, fileBeingProcessed ); } } // end processOneFile /** * correts acromyn damage to see files. * * @param args name of *.ser file to use, names of files to process, dirs, files, -s, *.*, no wildcards. If there is * no index for the dir, then use empty.ser * sorting will work, but repairing links will be inhibited */ public static void main( String[] args ) { CommandLine commandLine = new CommandLine( args, new AllButSVNDirectoriesFilter(), new ExtensionListFilter( "html" ) ); for ( File file : commandLine ) { try { out.println( " " + EIO.getCanOrAbsPath( file ) ); processOneFile( file ); } catch ( FileNotFoundException e ) { err.println( "\nError: " + EIO.getCanOrAbsPath( file ) + " not found.\n" ); } catch ( Exception e ) { err.println( "\nError: " + e.getMessage() + " in file " + EIO.getCanOrAbsPath( file ) + "\n" ); } catch ( Error e ) { e.printStackTrace( err ); err.println( "\nCatastrophic Error: " + e.getMessage() + " in file " + EIO.getCanOrAbsPath( file ) + "\n" ); err.println(); } } // end for } // end main } // end patch