Commits

Anonymous committed a593e05

fix echo on map; better handling for null value or pattern to match

  • Participants
  • Parent commits 755ef15
  • Branches v2.1.x
  • Tags v2.1.3

Comments (0)

Files changed (4)

   <groupId>org.nrg</groupId>
   <artifactId>DicomEdit</artifactId>
   <packaging>jar</packaging>
-  <version>2.1.2</version>
+  <version>2.1.3</version>
   <name>DicomEdit</name>
   <description>Language for modifying DICOM metadata</description>
   <url>http://nrg.wustl.edu</url>

src/main/java/org/nrg/dcm/edit/Echo.java

      * (non-Javadoc)
      * @see org.nrg.dcm.edit.Operation#apply(java.util.Map)
      */
-    public String apply(final Map<Integer,String> vals) { return null; }
+    public String apply(final Map<Integer,String> vals) {
+        String m;
+        try {
+            m = v.on(vals);
+        } catch (ScriptEvaluationException e) {
+            m = "[uninterpretable: " + v + "]";
+        }
+        System.out.print(m);
+        return null;
+    }
 
     public SortedSet<Integer> getAffectedTags() { return EMPTY; }
 

src/main/java/org/nrg/dcm/edit/fn/Match.java

         };
     }
 
-    private static String evaluate(final String value, final String pattern, final int group) {
-        final Matcher m = Pattern.compile(pattern).matcher(value);
-        return m.matches() ? m.group(group) : null;
+    private static String evaluate(final String value, final String pattern, final int group)
+            throws ScriptEvaluationException {
+        if (null == pattern) {
+            throw new ScriptEvaluationException("match pattern is null");
+        } else if (null == value) {
+            return null;
+        } else {
+            final Matcher m = Pattern.compile(pattern).matcher(value);
+            return m.matches() ? m.group(group) : null;
+        }
     }
 }

src/test/java/org/nrg/dcm/edit/fn/MatchTest.java

                 new IntegerValue("1")
         })).on(o4));
     }
+    
+    public void testApplyNullValue() throws ScriptEvaluationException {
+        final ScriptFunction match = new Match();
+        assertNull(match.apply(Arrays.asList(new Value[] {
+                new ConstantValue(null),
+                new ConstantValue(".*_(v[0-9]+)_.*"),
+                new IntegerValue("0")
+        })).on(context));
+    }
+
+    public void testApplyNullPattern() {
+        final ScriptFunction match = new Match();
+        try {
+            match.apply(Arrays.asList(new Value[] {
+                    new ConstantValue("foo"),
+                    new ConstantValue(null),
+                    new IntegerValue("0")
+            })).on(context);
+            fail("expected ScriptEvaluationException for null pattern");
+        } catch (ScriptEvaluationException expected) {}        
+    }
 }