public static void main ( String [ ] args ) throws IOException { Options options = getOptions ( ) ; CommandLine cmd = null ; CommandLineParser parser = new GnuParser ( ) ; try { cmd = parser . parse ( options , args ) ; } catch ( ParseException pe ) { LOG . error ( pe . getMessage ( ) ) ; printHelp ( options ) ; System . exit ( - 1 ) ; } if ( cmd . hasOption ( HELP_OPTION ) ) { printHelp ( options ) ; System . exit ( 0 ) ; } String indexPath = cmd . getOptionValue ( INDEX_PATH_OPTION , DEFAULT_INDEX_DIRECTORY ) ; String [ ] gazetteerPaths = cmd . getOptionValues ( GAZETTEER_FILES_OPTION ) ; if ( gazetteerPaths == null || gazetteerPaths . length == 0 ) { gazetteerPaths = DEFAULT_GAZETTEER_FILES ; } boolean replaceIndex = cmd . hasOption ( REPLACE_INDEX_OPTION ) ; boolean fullAncestry = cmd . hasOption ( FULL_ANCESTRY_OPTION ) ; File idir = new File ( indexPath ) ; // if the index directory exists, delete it if we are replacing, otherwise // exit gracefully if ( idir . exists ( ) ) { if ( replaceIndex ) { LOG . info ( "Replacing index: {}" , idir . getAbsolutePath ( ) ) ; FileUtils . deleteDirectory ( idir ) ; } else { LOG . info ( "{} exists. Remove the directory and try again." , idir . getAbsolutePath ( ) ) ; System . exit ( - 1 ) ; } } List < File > gazetteerFiles = new ArrayList < File > ( ) ; for ( String gp : gazetteerPaths ) { File gf = new File ( gp ) ; if ( gf . isFile ( ) && gf . canRead ( ) ) { gazetteerFiles . add ( gf ) ; } else { LOG . info ( "Unable to read Gazetteer file: {}" , gf . getAbsolutePath ( ) ) ; } } if ( gazetteerFiles . isEmpty ( ) ) { LOG . error ( "No Gazetteer files found." ) ; System . exit ( - 1 ) ; } String altNamesPath = cmd . getOptionValue ( ALTERNATE_NAMES_OPTION ) ; File altNamesFile = altNamesPath != null ? new File ( altNamesPath ) : null ; if ( altNamesFile != null && ! ( altNamesFile . isFile ( ) && altNamesFile . canRead ( ) ) ) { LOG . error ( "Unable to read alternate names file: {}" , altNamesPath ) ; System . exit ( - 1 ) ; } new IndexDirectoryBuilder ( fullAncestry ) . buildIndex ( idir , gazetteerFiles , altNamesFile ) ; }
Turns a GeoNames gazetteer file into a Lucene index and adds some supplementary gazetteer records at the end .