JAL-629 Opening files in new windows. not working yet
[jalview.git] / src / jalview / bin / argparser / SubVals.java
1 package jalview.bin.argparser;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import jalview.bin.Console;
7
8 /**
9  * A helper class to parse a string of the possible forms "content"
10  * "[index]content", "[keyName=keyValue]content" and return the integer index,
11  * the strings keyName and keyValue, and the content after the square brackets
12  * (if present). Values not set `will be -1 or null.
13  */
14 public class SubVals
15 {
16   private static int NOTSET = -1;
17
18   private int index = NOTSET;
19
20   private Map<String, String> subVals = null;
21
22   private static char SEPARATOR = ';';
23
24   private String content = null;
25
26   public SubVals(String item)
27   {
28     this.parseVals(item);
29   }
30
31   public void parseVals(String item)
32   {
33     if (item == null)
34       return;
35     if (item.indexOf('[') == 0 && item.indexOf(']') > 1)
36     {
37       int openBracket = 0;
38       int closeBracket = item.indexOf(']');
39       String subvalsString = item.substring(openBracket + 1, closeBracket);
40       this.content = item.substring(closeBracket + 1);
41       boolean setIndex = false;
42       for (String subvalString : subvalsString
43               .split(Character.toString(SEPARATOR)))
44       {
45         int equals = subvalString.indexOf('=');
46         if (equals > -1)
47         {
48           if (subVals == null)
49             subVals = new HashMap<>();
50           this.put(subvalString.substring(0, equals),
51                   subvalString.substring(equals + 1));
52         }
53         else
54         {
55           try
56           {
57             this.index = Integer.parseInt(subvalString);
58             setIndex = true;
59           } catch (NumberFormatException e)
60           {
61             // store this non-numeric key as a "true" value
62             subVals.put(subvalsString, "true");
63           }
64         }
65       }
66       if (!setIndex)
67         this.index = NOTSET;
68       else
69         Console.debug("SubVals from '" + subvalsString + "' has index "
70                 + this.index + " set");
71     }
72     else
73     {
74       this.content = item;
75     }
76   }
77
78   protected void put(String key, String val)
79   {
80     if (subVals == null)
81       subVals = new HashMap<>();
82     subVals.put(key, val);
83   }
84
85   public boolean notSet()
86   {
87     // notSet is true if content present but nonsensical
88     return index == NOTSET && subVals == null;
89   }
90
91   public String get(String key)
92   {
93     return subVals == null ? null : subVals.get(key);
94   }
95
96   public boolean has(String key)
97   {
98     return subVals == null ? false : subVals.containsKey(key);
99   }
100
101   public int getIndex()
102   {
103     return index;
104   }
105
106   public String getContent()
107   {
108     return content;
109   }
110
111   public String toString()
112   {
113     StringBuilder sb = new StringBuilder();
114     if (subVals == null)
115       return "";
116     for (Map.Entry<String, String> m : subVals.entrySet())
117     {
118       sb.append(m.getKey()).append('=').append(m.getValue()).append("\n");
119     }
120     return sb.toString();
121   }
122 }