JAL=3121 more data format validation/tests, help text tweak
[jalview.git] / src / jalview / io / gff / GffHelperBase.java
index ee93c55..3db1755 100644 (file)
@@ -43,6 +43,8 @@ import java.util.Map.Entry;
  */
 public abstract class GffHelperBase implements GffHelperI
 {
+  private static final String INVALID_GFF_ATTRIBUTE_FORMAT = "Invalid GFF attribute format: ";
+
   protected static final String COMMA = ",";
 
   protected static final String EQUALS = "=";
@@ -466,6 +468,45 @@ public abstract class GffHelperBase implements GffHelperI
   {
     Map<String, String> map = new HashMap<>();
     String[] fields = s.split(EQUALS);
+
+    /*
+     * format validation
+     */
+    boolean valid = true;
+    if (fields.length < 2)
+    {
+      /*
+       * need at least A=B here
+       */
+      valid = false;
+    }
+    else if (fields[0].isEmpty() || fields[0].contains(COMMA))
+    {
+      /*
+       * A,B=C is not a valid start, nor is =C
+       */
+      valid = false;
+    }
+    else
+    {
+      for (int i = 1; i < fields.length - 1; i++)
+      {
+        if (fields[i].isEmpty() || !fields[i].contains(COMMA))
+        {
+          /*
+           * intermediate tokens must include value,name
+           */
+          valid = false;
+        }
+      }
+    }
+
+    if (!valid)
+    {
+      System.err.println(INVALID_GFF_ATTRIBUTE_FORMAT + s);
+      return map;
+    }
+
     int i = 0;
     while (i < fields.length - 1)
     {
@@ -481,6 +522,14 @@ public abstract class GffHelperBase implements GffHelperI
               ? before.substring(before.lastIndexOf(COMMA) + 1)
               : before;
 
+      theKey = theKey.trim();
+      if (theKey.isEmpty())
+      {
+        System.err.println(INVALID_GFF_ATTRIBUTE_FORMAT + s);
+        map.clear();
+        return map;
+      }
+
       /*
        * if 'value' looks like a,b,c then all but the last token is the value,
        * unless this is the last field (no more = to follow), in which case