/* * [AirCarriers.java] * * Summary: Maintain a list of airline carriers and present as an HTML table. * * 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-05-07 initial version * 1.1 2012-04-11 store airline name in entified form. */ package com.mindprod.repair; import com.mindprod.amper.Amper; import com.mindprod.common18.BigDate; import com.mindprod.common18.EIO; import com.mindprod.common18.ST; import com.mindprod.csv.CSVReader; import com.mindprod.csv.CSVWriter; import com.mindprod.fastcat.FastCat; import com.mindprod.htmlmacros.macro.Global; import com.mindprod.htmlmacros.support.AssignCSSClasses; import com.mindprod.htmlmacros.support.AssignCSSClassesForMindprod; import com.mindprod.htmlmacros.support.ConfigurationForMindprod; import java.io.EOFException; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import static java.lang.System.*; /* * TOGO: needs a letter index, more urls, compare with official source Swiss site. */ /** * Maintain a list of airline carriers and present as an HTML table. * * @author Roedy Green, Canadian Mind Products * @version 1.1 2012-04-11 store airline name in entified form * @since 2009-05-07 */ public class AirCarriers { /** * it testing data, do not want to disturb published table on website yet */ private static final boolean TESTING = false; private static final String DO_NOT_EDIT = "\n"; /** * code that describes how mindprod.com assigns CSS classes to various files */ private static final AssignCSSClasses assigner = new AssignCSSClassesForMindprod(); private static String sourceDirname; private static String webrootDirname; /** * true if item we are working on contains unique info not already collected. */ private static boolean containsUniqueInfo; /** * where generated HTML will eventually end up. */ private static File fileBeingProcessed; private static void checkLet3( final String cname, final String clet3, final Carrier existing ) { if ( clet3.length() != 0 ) { final String elet3 = existing.getLet3(); if ( elet3.length() == 0 ) { existing.setLet3( clet3 ); } else if ( !clet3.equals( elet3 ) ) { out.println( clet3 + " vs " + elet3 + " for " + cname ); containsUniqueInfo = true; } } } private static void checkName( final String cname, final Carrier existing ) { final String ename = existing.getName(); if ( !cname.equals( ename ) ) { out.println( cname + " vs " + ename ); containsUniqueInfo = true; } // no point in trying to patch. } private static void checkTick3( final String cname, final int ctick3, final Carrier existing ) { if ( ctick3 != 0 ) { final int etick3 = existing.getTick3(); if ( etick3 == 0 ) { existing.setTick3( ctick3 ); } else if ( ctick3 != etick3 ) { out.println( ctick3 + " vs " + etick3 + " for " + cname ); containsUniqueInfo = true; } } } private static void checkUrl( final String cname, final String curl, final Carrier existing ) { if ( curl.length() != 0 ) { final String eurl = existing.getUrl(); if ( eurl.length() == 0 ) { existing.setUrl( curl ); } else if ( !curl.equals( eurl ) ) { out.println( curl + " vs " + eurl + " for " + cname ); containsUniqueInfo = true; } } } /* just needs config on command line, rest from program */ public static void main( String[] args ) throws IOException { Global.installConfiguration( new ConfigurationForMindprod() ); webrootDirname = Global.configuration.getLocalWebrootWithSlashes(); sourceDirname = Global.configuration.getSourceDirWithSlashes(); final HashMap sofar = new HashMap<>( 1500 ); final ArrayList others = new ArrayList<>( 1500 ); final CSVReader r = new CSVReader( new FileReader( "air.csv" ) ); try { while ( true ) { String[] allFields = r.getAllFieldsInLine(); // name of airline final String cname = allFields[ 0 ]; // two-letter code final String clet2 = allFields[ 1 ]; // 3-digit code final int ctick3 = allFields.length > 2 ? Integer.parseInt( allFields[ 2 ] ) : 0; // 3-letter code final String clet3 = allFields.length > 3 ? allFields[ 3 ] : ""; // website url final String curl = allFields.length > 4 ? allFields[ 4 ] : ""; final Carrier c = new Carrier( cname, clet2, ctick3, clet3, curl ); if ( clet2.length() > 0 ) { final Carrier existing = sofar.get( clet2 ); if ( existing == null ) { sofar.put( clet2, c ); } else { containsUniqueInfo = false; // merge new record with existing as best as can checkName( cname, existing ); if ( !containsUniqueInfo ) { checkTick3( cname, ctick3, existing ); checkLet3( cname, clet3, existing ); checkUrl( cname, curl, existing ); } if ( containsUniqueInfo ) { others.add( c ); } } } else { // had no let2 others.add( c ); } } } catch ( EOFException e ) { } // merged lines for ( Carrier c : sofar.values() ) { others.add( c ); } Collections.sort( others ); // tidy and generate HTML in one pass final CSVWriter w = new CSVWriter( EIO.getPrintWriter( new File( "airtidied.csv" ), 8 * 1024, EIO.UTF8 ) ); w.nl( "airlines" ); w.nl( "#name(entified), code2, number, code3, url" ); final String outFilename = TESTING ? ( sourceDirname + "/repair/airlinecodes.html" ) : ( webrootDirname + "/jgloss/include/airlinecodes.htmlfrag" ); final PrintWriter p = EIO.getPrintWriter( new File( outFilename ), 8 * 1024, EIO.UTF8 ); fileBeingProcessed = new File( webrootDirname + "/jgloss/airlineticket.html" ); int everyTenthLine = 0; p.print( DO_NOT_EDIT ); for ( Carrier c : others ) { w.put( c.getName() ); w.put( c.getLet2() ); w.put( c.getTick3() ); w.put( c.getLet3() ); w.put( c.getUrl() ); w.nl(); p.print( c.formatAsHTML() + "\n" ); } out.println( others.size() + " html lines generated" ); w.close(); p.close(); } static class Carrier implements Comparable { private static String prevName = ""; String let2; String let3; String name; String url; int tick3; /** * constructor * * @param name name of airline * @param let2 2-letter code * @param tick3 3-digit ticketing number. * @param let3 3-letter code * @param url airline website */ Carrier( final String name, final String let2, final int tick3, final String let3, final String url ) { this.name = name; this.let2 = let2; this.tick3 = tick3; this.let3 = let3; this.url = url; } /** * Sort air carriers by name with 2-char code tie breaker. * Defines default the sort order for Carrier Objects. * Compare this Carrier with another Carrier. * Compares name then let2. * Informally, returns (this-other) or +ve if this is more positive than other. * * @param other other Carrier to compare with this one * * @return +ve if this>other, 0 if this==other, -ve if this<other */ public final int compareTo( Carrier other ) { int diff = this.name.compareTo( other.name ); if ( diff != 0 ) { return diff; } return this.let2.compareTo( other.let2 ); } /** * return info formatted at a line in an html table * * @return generated HTML to describe this air carrier */ public String formatAsHTML() { final FastCat sb = new FastCat( 15 ); sb.append( "" ); if ( !name.equals( prevName ) ) { if ( url.length() > 0 ) { sb.append( "" ); sb.append( name ); sb.append( "" ); } else { sb.append( name ); } } prevName = name; sb.append( "" ); sb.append( let2 ); sb.append( "" ); if ( tick3 != 0 ) { sb.append( ST.toLZ( tick3, 3 ) ); } sb.append( "" ); sb.append( let3 ); sb.append( "" ); return sb.toString(); } public String getLet2() { return let2; } public void setLet2( final String let2 ) { this.let2 = let2; } public String getLet3() { return let3; } public void setLet3( final String let3 ) { this.let3 = let3; } public String getName() { return name; } public void setName( final String name ) { this.name = name; } public int getTick3() { return tick3; } public void setTick3( final int tick3 ) { this.tick3 = tick3; } public String getUrl() { return url; } public void setUrl( final String url ) { this.url = url; } } }