/hg/icedtea-web: Change PolicyEditor to use OptionParser

ldracz at icedtea.classpath.org ldracz at icedtea.classpath.org
Wed Jan 21 16:43:01 UTC 2015


changeset b6e1270385f4 in /hg/icedtea-web
details: http://icedtea.classpath.org/hg/icedtea-web?cmd=changeset;node=b6e1270385f4
author: Lukasz Dracz <ldracz at redhat.com>
date: Wed Jan 21 11:42:48 2015 -0500

	Change PolicyEditor to use OptionParser

	2014-01-21  Lukasz Dracz  <ldracz at redhat.com>

		Change PolicyEditor to use OptionParser
		* netx/net/sourceforge/jnlp/OptionsDefinitions.java
		* netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java:
		(argsToMap): removed
		* tests/netx/unit/net/sourceforge/jnlp/security/policyeditor/PolicyEditorTest.java:
		(testArgsToMap): removed


diffstat:

 ChangeLog                                                                        |   9 +
 netx/net/sourceforge/jnlp/OptionsDefinitions.java                                |   4 +-
 netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java                |  55 ++--------
 tests/netx/unit/net/sourceforge/jnlp/security/policyeditor/PolicyEditorTest.java |  19 ---
 4 files changed, 22 insertions(+), 65 deletions(-)

diffs (146 lines):

diff -r 02dd1b796d4b -r b6e1270385f4 ChangeLog
--- a/ChangeLog	Mon Dec 22 10:14:18 2014 +0100
+++ b/ChangeLog	Wed Jan 21 11:42:48 2015 -0500
@@ -1,3 +1,12 @@
+2014-01-21  Lukasz Dracz  <ldracz at redhat.com>
+
+	Change PolicyEditor to use OptionParser
+	* netx/net/sourceforge/jnlp/OptionsDefinitions.java
+	* netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java:
+	(argsToMap): removed
+	* tests/netx/unit/net/sourceforge/jnlp/security/policyeditor/PolicyEditorTest.java:
+	(testArgsToMap): removed
+
 2014-12-20  Jiri Vanek  <jvanek at redhat.com>
 
 	Added -html switch for javaws to run applets out of browser in applets sandbox
diff -r 02dd1b796d4b -r b6e1270385f4 netx/net/sourceforge/jnlp/OptionsDefinitions.java
--- a/netx/net/sourceforge/jnlp/OptionsDefinitions.java	Mon Dec 22 10:14:18 2014 +0100
+++ b/netx/net/sourceforge/jnlp/OptionsDefinitions.java	Wed Jan 21 11:42:48 2015 -0500
@@ -85,8 +85,8 @@
         HELP2("-help", "BOHelp2"),
         //policyeditor
         //-help
-        FILE("-file", "policy_file", "PBOFile"),
-        CODEBASE("-codebase", "url", "PBOCodebase");
+        FILE("-file", "policy_file", "PBOFile", NumberOfArguments.ONE),
+        CODEBASE("-codebase", "url", "PBOCodebase", NumberOfArguments.ONE_OR_MORE);
 
         public final String option;
 
diff -r 02dd1b796d4b -r b6e1270385f4 netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java
--- a/netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java	Mon Dec 22 10:14:18 2014 +0100
+++ b/netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java	Wed Jan 21 11:42:48 2015 -0500
@@ -111,6 +111,7 @@
 import net.sourceforge.jnlp.util.docprovider.TextsProvider;
 import net.sourceforge.jnlp.util.docprovider.formatters.formatters.PlainTextFormatter;
 import net.sourceforge.jnlp.util.logging.OutputController;
+import net.sourceforge.jnlp.util.optionparser.OptionParser;
 
 /**
  * This class provides a policy editing tool as a simpler alternate to
@@ -1582,9 +1583,9 @@
      * -help will print a help message and immediately return (no editor instance opens)
      */
     public static void main(final String[] args) {
-        final Map<String, String> argsMap = argsToMap(args);
+        final OptionParser optionParser = new OptionParser(args, OptionsDefinitions.getPolicyEditorOptions());
 
-        if (argsMap.containsKey(HELP_FLAG)) {
+        if (optionParser.hasOption(OptionsDefinitions.OPTIONS.HELP1)) {
             final TextsProvider helpMessagesProvider = new PolicyEditorTextsProvider("utf-8", new PlainTextFormatter(), true, true);
             String HELP_MESSAGE = "\n";
             if (JNLPRuntime.isDebug()) {
@@ -1609,19 +1610,18 @@
         SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {
-                String filepath = argsMap.get(FILE_FLAG);
-                if (filepath == null && args.length == 1) {
+                String filepath = optionParser.getParam(OptionsDefinitions.OPTIONS.FILE);
+                if (optionParser.getMainArgs().size() == 0) {
+                    filepath = null;
+                } else if (filepath == "") {
                     // maybe the user just forgot the -file flag, so try to open anyway
-                    filepath = args[0];
+                    filepath = optionParser.getMainArg();
                 }
                 final PolicyEditorWindow frame = getPolicyEditorFrame(filepath);
                 frame.asWindow().setVisible(true);
-                final String codebaseStr = argsMap.get(CODEBASE_FLAG);
-                if (codebaseStr != null) {
-                    final String[] urls = codebaseStr.split(" ");
-                    for (final String url : urls) {
-                        frame.getPolicyEditor().addNewCodebase(url);
-                    }
+                final List<String> codebases = optionParser.getParams(OptionsDefinitions.OPTIONS.CODEBASE);
+                for (final String url : codebases) {
+                    frame.getPolicyEditor().addNewCodebase(url);
                 }
             }
         });
@@ -1637,37 +1637,4 @@
         return new PolicyEditor(filepath);
     }
 
-    /**
-     * Create a Map out of argv
-     * @param args command line flags and parameters given to the program
-     * @return a Map representation of the command line arguments
-     */
-    static Map<String, String> argsToMap(final String[] args) {
-        final List<String> argsList = Arrays.<String> asList(args);
-        final Map<String, String> map = new HashMap<>();
-
-        if (argsList.contains(HELP_FLAG)) {
-            map.put(HELP_FLAG, null);
-        }
-
-        if (argsList.contains(FILE_FLAG)) {
-            map.put(FILE_FLAG, argsList.get(argsList.indexOf(FILE_FLAG) + 1));
-        }
-
-        if (argsList.contains(CODEBASE_FLAG)) {
-            final int flagIndex = argsList.indexOf(CODEBASE_FLAG);
-            final StringBuilder sb = new StringBuilder();
-            for (int i = flagIndex + 1; i < argsList.size(); ++i) {
-                final String str = argsList.get(i);
-                if (str.equals(HELP_FLAG) || str.equals(CODEBASE_FLAG) || str.equals(FILE_FLAG)) {
-                    break;
-                }
-                sb.append(str);
-                sb.append(" ");
-            }
-            map.put(CODEBASE_FLAG, sb.toString().trim());
-        }
-        return map;
-    }
-
 }
diff -r 02dd1b796d4b -r b6e1270385f4 tests/netx/unit/net/sourceforge/jnlp/security/policyeditor/PolicyEditorTest.java
--- a/tests/netx/unit/net/sourceforge/jnlp/security/policyeditor/PolicyEditorTest.java	Mon Dec 22 10:14:18 2014 +0100
+++ b/tests/netx/unit/net/sourceforge/jnlp/security/policyeditor/PolicyEditorTest.java	Wed Jan 21 11:42:48 2015 -0500
@@ -265,23 +265,4 @@
         }
     }
 
-    @Test
-    public void testArgsToMap() throws Exception {
-        final String[] args = new String[] {
-                "-codebase", "http://example.com http://icedtea.classpath.org",
-                "-file", "/tmp/some-policy-file.tmp",
-                "-help"
-        };
-        Map<String, String> map = PolicyEditor.argsToMap(args);
-        assertTrue("Args map should contain help flag", map.containsKey("-help"));
-        assertTrue("Value for -help should be null but was " + map.get("-help"),
-                map.get("-help") == null);
-        assertTrue("Args map should contain file flag", map.containsKey("-file"));
-        assertTrue("Value for -file should be /tmp/some-policy-file.tmp but was " + map.get("-file"),
-                map.get("-file").equals("/tmp/some-policy-file.tmp"));
-        assertTrue("Args map should contain codebase flag", map.containsKey("-codebase"));
-        assertTrue("Value for codebase flag should be \"http://example.com http://icedtea.classpath.org\" but was " + map.get("-codebase"),
-                map.get("-codebase").equals("http://example.com http://icedtea.classpath.org"));
-    }
-
 }


More information about the distro-pkg-dev mailing list