/* * [SortBookMenu.java] * * Summary: Sort a book menu. * * Copyright: (c) 2009-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 2009-01-01 initial version */ package com.mindprod.stores; 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 java.util.regex.Matcher; import java.util.regex.Pattern; import static java.lang.System.*; /** * Sort a book menu. *

* Obsolete. Needs to be modified to use new ID style. *

* find anchor in * {Behind the War on Terror by Nafeez Ahmed} * {Crossing the Rubicon by Michael C. Ruppert} * * @author Roedy Green, Canadian Mind Products * @version 1.0 2009-01-01 initial version * @since 2009-01-01 */ public class SortBookMenu { private static final Pattern nameFinder = Pattern.compile( "\\{]" ); public static void main( String[] args ) throws IOException { ArrayList a = new ArrayList<>( 200 ); // O P E N BufferedReader br = EIO.getBufferedReader( new File( "C:/temp/menu.html" ), 4 * 1024, EIO.UTF8 ); // R E A D String line; // File being read need not have have a terminal \n. // File being read may safely use any mixture of \r\n, \r or \n line terminators. while ( ( line = br.readLine() ) != null ) { // line looks like // {Hegemony Or Survival by Noam Chomsky} // we want to sort by the last word. a.add( line ); } // C L O S E br.close(); Collections.sort( a, new ByAuthor() ); // O P E N final PrintWriter prw = EIO.getPrintWriter( new File( "C:/temp/sortedmenu.html" ), 4 * 1024, EIO.UTF8 ); // sort by author then title // read in list of book expansions. final String books = HunkIO.readEntireFile( new File( "C:/temp/books.html" ) ); // where we build sorted list of book, without expansions. final StringBuilder sb = new StringBuilder( books.length() ); for ( String item : a ) { // W R I T E prw.println( item );// to file, not console. // item look like this: // {Hegemony Or Survival by Noam Chomsky} final Matcher m = nameFinder.matcher( item ); if ( !m.find() || m.groupCount() != 1 ) { throw new IllegalArgumentException( "missing anchor on " + item ); } final String anchor = m.group( 1 ); final int start = books.indexOf( "", start + anchor.length() + 13 ); if ( end < 0 ) { throw new IllegalArgumentException( "can't find --> for " + item ); } final String bookMacro = books.substring( start, end + 3 ); if ( !bookMacro.contains( "isbn=" ) ) { throw new IllegalArgumentException( "can't find isbn= in first comment after anchor for " + item ); } sb.append( bookMacro ); sb.append( "\n

" ); } // end for item // C L O S E prw.close(); // write to different file so can compare. HunkIO.writeEntireFile( new File( "C:/temp/sortedbooks.html" ), sb.toString() ); out.println( "done" ); } }