+
+ public static SubId getSubId(String item)
+ {
+ return new SubId(item);
+ }
+
+ /**
+ * A helper class to parse a string of the possible forms "content"
+ * "[index]content", "[keyName=keyValue]content" and return the integer index,
+ * the strings keyName and keyValue, and the content after the square brackets
+ * (if present). Values not set `will be -1 or null.
+ */
+ public static class SubId
+ {
+ protected int index = 0;
+
+ protected String keyName = null;
+
+ protected String keyValue = null;
+
+ protected String content = null;
+
+ public SubId()
+ {
+ }
+
+ public SubId(String item)
+ {
+ this.parseVal(item);
+ }
+
+ public void parseVal(String item)
+ {
+ if (item.indexOf('[') == 0 && item.indexOf(']') > 1)
+ {
+ int openBracket = item.indexOf('[');
+ int closeBracket = item.indexOf(']');
+ String indexString = item.substring(openBracket + 1, closeBracket);
+ this.content = item.substring(closeBracket + 1);
+ int equals = indexString.indexOf('=');
+ if (equals > -1)
+ {
+ this.keyName = indexString.substring(0, equals);
+ this.keyValue = indexString.substring(equals + 1);
+ this.index = -1;
+ }
+ else
+ {
+ try
+ {
+ this.index = Integer.parseInt(indexString);
+ } catch (NumberFormatException e)
+ {
+ Console.warn("Failed to obtain sequenced id or index from '"
+ + item + "'. Setting index=0 and using content='"
+ + content + "'.");
+ }
+ }
+ }
+ else
+ {
+ this.content = item;
+ }
+ }
+ }