diff --git a/src/fr/inria/moex/classapp/PlayClass.java b/src/fr/inria/moex/classapp/PlayClass.java index 24bda6fc7e14434321a2ebba36511fb5d3a9a690..73832b9fe6807a4abc014d4a9d853cc31ad0b46f 100644 --- a/src/fr/inria/moex/classapp/PlayClass.java +++ b/src/fr/inria/moex/classapp/PlayClass.java @@ -1,6 +1,6 @@ /* * - * Copyright (C) INRIA, 2021-2022 + * Copyright (C) INRIA, 2021-2022, 2025 * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by @@ -56,57 +56,57 @@ public class PlayClass { final static Logger logger = LoggerFactory.getLogger( PlayClass.class ); /** Dealing with multilinguism. - This will not work util Java 8 if the encoding is UTF-8 - The best soluton (besides using at least Java 11) is given by - https://stackoverflow.com/a/37039868/7703978 + This will not work util Java 8 if the encoding is UTF-8 + The best soluton (besides using at least Java 11) is given by + https://stackoverflow.com/a/37039868/7703978 **/ private static ResourceBundle bundle; private static Locale loc; public static void updateLocale() { - if ( loc == null ) loc = Locale.getDefault(); - bundle = ResourceBundle.getBundle( "fr.inria.moex.classapp.resources.strings", loc ); + if ( loc == null ) loc = Locale.getDefault(); + bundle = ResourceBundle.getBundle( "fr.inria.moex.classapp.resources.strings", loc ); } public static String getString( final String key ) { if ( bundle == null ) updateLocale(); - String str = bundle.getString(key); - if ( str != null ) return str; - else return "___"; + String str = bundle.getString(key); + if ( str != null ) return str; + else return "___"; } public static String getHTMLFile( final Class cl, final String dir, final String filepref ) { - if ( loc == null ) loc = Locale.getDefault(); - try { - return getFile( cl, "/fr/inria/moex/classgui/resources/", filepref+"_"+loc.getLanguage()+".html" ); - } catch ( Exception ex1 ) { - try { - return getFile( cl, "/fr/inria/moex/classgui/resources/", filepref+".html" ); - } catch ( Exception ex2 ) { - return ex2.toString(); - } - } + if ( loc == null ) loc = Locale.getDefault(); + try { + return getFile( cl, "/fr/inria/moex/classgui/resources/", filepref+"_"+loc.getLanguage()+".html" ); + } catch ( Exception ex1 ) { + try { + return getFile( cl, "/fr/inria/moex/classgui/resources/", filepref+".html" ); + } catch ( Exception ex2 ) { + return ex2.toString(); + } + } } - + public static String getFile( final Class cl, final String dir, final String filename ) throws IOException { - String content = ""; - InputStream instr = cl.getResourceAsStream( dir+filename ); - InputStreamReader strrd = new InputStreamReader( instr, StandardCharsets.UTF_8 ); - BufferedReader rr = new BufferedReader(strrd); - String line; - while ((line = rr.readLine()) != null) - content += line; - return content; + String content = ""; + InputStream instr = cl.getResourceAsStream( dir+filename ); + InputStreamReader strrd = new InputStreamReader( instr, StandardCharsets.UTF_8 ); + BufferedReader rr = new BufferedReader(strrd); + String line; + while ((line = rr.readLine()) != null) + content += line; + return content; } - + public static Locale getLocale() { return loc; } public static void setLocale( final Locale locale ) { loc = locale; - updateLocale(); + updateLocale(); } /** From commonCli **/ @@ -123,38 +123,38 @@ public class PlayClass { // Reuses CommonCLI, create our own if needed public PlayClass() { - parameters = new Properties(); - setUpOptions(); + parameters = new Properties(); + setUpOptions(); } protected void setUpOptions() { - options = new Options(); - Option opt = null; - // h = no arg - //options.addOption( createOption( "h", "help", "Print this page" ) ); - opt = new Option( "h", "Print this page" ); - opt.setLongOpt( "help" ); - options.addOption( opt ); - // o = req - //options.addOption( createRequiredOption( "o", "output", "Send output to FILE", "FILE" ) ); - opt = new Option( "o", "Send output to FILE" ); - opt.setLongOpt( "output" ); - opt.setArgs( 1 ); - opt.setArgName( "FILE" ); - options.addOption( opt ); - // P = req - //options.addOption( createRequiredOption( "P", "params", "Read parameters from FILE", "FILE" ) ); - opt = new Option( "P", "Read parameters from FILE" ); - opt.setLongOpt( "params" ); - opt.setArgs( 1 ); - opt.setArgName( "FILE" ); - options.addOption( opt ); - // D special parameter - opt = new Option( "D", "Use value for given property" ); - opt.setArgs(2); - opt.setValueSeparator('='); - opt.setArgName( "NAME=VALUE" ); - options.addOption( opt ); + options = new Options(); + Option opt = null; + // h = no arg + //options.addOption( createOption( "h", "help", "Print this page" ) ); + opt = new Option( "h", "Print this page" ); + opt.setLongOpt( "help" ); + options.addOption( opt ); + // o = req + //options.addOption( createRequiredOption( "o", "output", "Send output to FILE", "FILE" ) ); + opt = new Option( "o", "Send output to FILE" ); + opt.setLongOpt( "output" ); + opt.setArgs( 1 ); + opt.setArgName( "FILE" ); + options.addOption( opt ); + // P = req + //options.addOption( createRequiredOption( "P", "params", "Read parameters from FILE", "FILE" ) ); + opt = new Option( "P", "Read parameters from FILE" ); + opt.setLongOpt( "params" ); + opt.setArgs( 1 ); + opt.setArgName( "FILE" ); + options.addOption( opt ); + // D special parameter + opt = new Option( "D", "Use value for given property" ); + opt.setArgs(2); + opt.setValueSeparator('='); + opt.setArgName( "NAME=VALUE" ); + options.addOption( opt ); } File saveDir = null; @@ -162,133 +162,133 @@ public class PlayClass { // In spirit, this is final public CommandLine parseCommandLine( String[] args ) throws ParseException { - CommandLineParser parser = new DefaultParser(); - CommandLine line = parser.parse( options, args ); - parameters = line.getOptionProperties( "D" ); - if ( line.hasOption( 'd' ) ) { - logger.warn( "debug command-line switch DEPRECATED, use logging" ); - } - if ( line.hasOption( 'o' ) ) { - outputfilename = line.getOptionValue( 'o' ); - } - if ( line.hasOption( 'P' ) ) { - try ( FileInputStream fis = new FileInputStream( line.getOptionValue( 'P' ) ) ) { - parameters.loadFromXML( fis ); - } catch (FileNotFoundException fnfex) { - logger.warn( "File {} not found", line.getOptionValue( 'P' ), fnfex ); - //throw new ParseException("There isn't such file: " + line.getOptionValue( 'P' ), fnfex); - } catch (IOException ioex) { - logger.warn( "Cannot parse parameter file {}", line.getOptionValue( 'P' ), ioex ); - //throw new ParseException("Error accessing file " + line.getOptionValue( 'P' ), ioex); - } - } - if ( line.hasOption( 'h' ) ) { - usage(0); - } - return line; + CommandLineParser parser = new DefaultParser(); + CommandLine line = parser.parse( options, args ); + parameters = line.getOptionProperties( "D" ); + if ( line.hasOption( 'd' ) ) { + logger.warn( "debug command-line switch DEPRECATED, use logging" ); + } + if ( line.hasOption( 'o' ) ) { + outputfilename = line.getOptionValue( 'o' ); + } + if ( line.hasOption( 'P' ) ) { + try ( FileInputStream fis = new FileInputStream( line.getOptionValue( 'P' ) ) ) { + parameters.loadFromXML( fis ); + } catch (FileNotFoundException fnfex) { + logger.warn( "File {} not found", line.getOptionValue( 'P' ), fnfex ); + //throw new ParseException("There isn't such file: " + line.getOptionValue( 'P' ), fnfex); + } catch (IOException ioex) { + logger.warn( "Cannot parse parameter file {}", line.getOptionValue( 'P' ), ioex ); + //throw new ParseException("Error accessing file " + line.getOptionValue( 'P' ), ioex); + } + } + if ( line.hasOption( 'h' ) ) { + usage(0); + } + return line; } - + // This one has a main() public static void main( String[] args ) { - try { new PlayClass().run( args ); } - catch ( Exception ex ) { - ex.printStackTrace(); - System.exit(-1); - }; + try { new PlayClass().run( args ); } + catch ( Exception ex ) { + ex.printStackTrace(); + System.exit(-1); + }; } // 2020: split into parse+init+process+report // It is unclear if all the treatment public void run( String[] args ) throws Exception { - CommandLine line = null; - logger.debug( getString( "i1" ) ); - try { - line = parseCommandLine( args ); - if ( line == null ) System.exit(1); // or return - // Here deal with command specific arguments - for ( Object o : line.getArgList() ) { - logger.info( getString( "i2a" ), o ); - } - for ( Entry<Object,Object> m : parameters.entrySet() ) { - logger.info( getString( "i2p" ), m.getKey(), m.getValue() ); - } - } catch( ParseException exp ) { - logger.error( exp.getMessage() ); - usage(); - System.exit(-1); - } - logger.debug( getString( "i3" ) ); - // Initialize (already done here with the parsed parameters) - init( parameters ); - // Run session - process(); + CommandLine line = null; + logger.debug( getString( "i1" ) ); + try { + line = parseCommandLine( args ); + if ( line == null ) System.exit(1); // or return + // Here deal with command specific arguments + for ( Object o : line.getArgList() ) { + logger.info( getString( "i2a" ), o ); + } + for ( Entry<Object,Object> m : parameters.entrySet() ) { + logger.info( getString( "i2p" ), m.getKey(), m.getValue() ); + } + } catch( ParseException exp ) { + logger.error( exp.getMessage() ); + usage(); + System.exit(-1); + } + logger.debug( getString( "i3" ) ); + // Initialize (already done here with the parsed parameters) + init( parameters ); + // Run session + process(); } - + public void init( Properties params ) throws IOException, FileNotFoundException { - if ( parameters != params ) parameters = params; // called from outside - // Get load directory - if ( parameters.getProperty( "loadDir" ) != null ) { - loadDir = new File( parameters.getProperty( "loadDir" ) ); - if ( parameters.getProperty( "loadParams" ) != null ) { - // load them from file - Properties storedParams = new Properties(); - storedParams.loadFromXML( new FileInputStream( new File( loadDir, "params.xml" ) ) ); - // == add new params... but loadParams??? to loaded ones? - storedParams.list( System.err ); - // merge both parameter sets... (maybe not savedir) - storedParams.forEach( (k, v) -> - { if ( !((String)k).equals("saveDir") ) - parameters.setProperty( (String)k, (String)v ); }); - //parameters.list( System.err ); - } - } - // Get save directory - if ( parameters.getProperty( "saveDir" ) != null ) { - saveDir = new File( parameters.getProperty( "saveDir" ) ); - saveDir.mkdir(); - if ( parameters.getProperty( "saveParams" ) != null ) { - //store the properties detail into a pre-defined XML file - parameters.storeToXML( new FileOutputStream( new File( saveDir, "params.xml" ) ), - "Lazy lavender session parameters", "UTF-8"); - } - } - // Get nbRuns - if ( parameters.getProperty( "nbRuns" ) != null ) { - nbRuns = Integer.parseInt( parameters.getProperty( "nbRuns" ) ); - } + if ( parameters != params ) parameters = params; // called from outside + // Get load directory + if ( parameters.getProperty( "loadDir" ) != null ) { + loadDir = new File( parameters.getProperty( "loadDir" ) ); + if ( parameters.getProperty( "loadParams" ) != null ) { + // load them from file + Properties storedParams = new Properties(); + storedParams.loadFromXML( new FileInputStream( new File( loadDir, "params.xml" ) ) ); + // == add new params... but loadParams??? to loaded ones? + storedParams.list( System.err ); + // merge both parameter sets... (maybe not savedir) + storedParams.forEach( (k, v) -> + { if ( !((String)k).equals("saveDir") ) + parameters.setProperty( (String)k, (String)v ); }); + //parameters.list( System.err ); + } + } + // Get save directory + if ( parameters.getProperty( "saveDir" ) != null ) { + saveDir = new File( parameters.getProperty( "saveDir" ) ); + saveDir.mkdir(); + if ( parameters.getProperty( "saveParams" ) != null ) { + //store the properties detail into a pre-defined XML file + parameters.storeToXML( new FileOutputStream( new File( saveDir, "params.xml" ) ), + "Lazy lavender session parameters", "UTF-8"); + } + } + // Get nbRuns + if ( parameters.getProperty( "nbRuns" ) != null ) { + nbRuns = Integer.parseInt( parameters.getProperty( "nbRuns" ) ); + } } public void process() throws Exception { - // The real work - for ( int i = nbRuns-1; i >= 0; i-- ) { - logger.debug( getString( "i4" ), i ); - if ( loadDir != null ) { - parameters.setProperty( "loadRunDir", loadDir.toString()+"/"+i ); - } - ClassSession session = new ClassSession(); - - // create save directory - if ( saveDir != null ) { - parameters.setProperty( "runDir", saveDir.toString()+"/"+i ); // could be better... - } - session.init( parameters ); - session.initSession(); - logger.debug( getString( "i9" ), i ); - session.process(); - } - return; + // The real work + for ( int i = nbRuns-1; i >= 0; i-- ) { + logger.debug( getString( "i4" ), i ); + if ( loadDir != null ) { + parameters.setProperty( "loadRunDir", loadDir.toString()+"/"+i ); + } + ClassSession session = new ClassSession(); + + // create save directory + if ( saveDir != null ) { + parameters.setProperty( "runDir", saveDir.toString()+"/"+i ); // could be better... + } + session.init( parameters ); + session.initSession(); + logger.debug( getString( "i9" ), i ); + session.process(); + } + return; } public void exit( int returnCode ) { - System.exit( returnCode ); + System.exit( returnCode ); } public void usage( int returnValue ) { - usage(); - System.exit( returnValue ); + usage(); + System.exit( returnValue ); } public void usage() { - new HelpFormatter().printHelp( 80, "java "+getClass().getName()+" [options]", "\nOptions:", options, "\nSee https://gforge.inria.fr/plugins/mediawiki/wiki/lazylav/index.php/Parameters for an extensive list of properties\n" ); + new HelpFormatter().printHelp( 80, "java "+getClass().getName()+" [options]", "\nOptions:", options, "\nSee https://gforge.inria.fr/plugins/mediawiki/wiki/lazylav/index.php/Parameters for an extensive list of properties\n" ); } }