Commits

Anonymous committed a395011

added uppercase function

  • Participants
  • Parent commits 465013a
  • Tags v1.5.3

Comments (0)

Files changed (5)

   <groupId>org.nrg</groupId>
   <artifactId>DicomEdit</artifactId>
   <packaging>jar</packaging>
-  <version>1.5.2</version>
+  <version>1.5.3</version>
   <name>DicomEdit</name>
   <description>Implementation of a language for modifying DICOM metadata</description>
   <url>http://nrg.wustl.edu</url>

File src/main/java/org/nrg/dcm/edit/ScriptApplicator.java

 import org.nrg.dcm.edit.fn.Match;
 import org.nrg.dcm.edit.fn.Replace;
 import org.nrg.dcm.edit.fn.Substring;
+import org.nrg.dcm.edit.fn.Uppercase;
 import org.nrg.dcm.edit.gen.UIDGenerator;
 import org.nrg.io.FileMapper;
 import org.nrg.io.PresuffixFileMapper;
                     astParser.setFunction(Substring.name, new Substring());
                     astParser.setFunction(Format.name, new Format());
                     astParser.setFunction(Lowercase.name, new Lowercase());
+                    astParser.setFunction(Uppercase.name, new Uppercase());
                     astParser.setFunction(GetURL.name, new GetURL());
                     astParser.setFunction(Replace.name, new Replace());
                     astParser.setFunction(Match.name, new Match());
                         astParser.setGenerator((String)me.getKey(), (ValueGenerator)me.getValue());
                     }
                     if (!generators.containsKey(UIDGenerator.name)) {
-                        logger.trace("adding generator " + UIDGenerator.name);
+                        logger.trace("adding generator {}", UIDGenerator.name);
                         astParser.setGenerator(UIDGenerator.name, new UIDGenerator());
                     }
                     logger.trace("ready to parse");
     public ScriptApplicator(final InputStream script, final Map functions)
     throws IOException,ScriptEvaluationException {
         this(script, functions, Collections.EMPTY_MAP);
-
-
     }
 
     public ScriptApplicator(final InputStream script)
         }
         return tags;
     }
-    
+
     public Variable getVariable(final String label) {
         return astParser.getVariable(label);
     }

File src/main/java/org/nrg/dcm/edit/fn/Uppercase.java

+/**
+ * Copyright (c) 2010,2011 Washington University
+ */
+package org.nrg.dcm.edit.fn;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.dcm4che2.data.DicomObject;
+import org.nrg.dcm.edit.ScriptEvaluationException;
+import org.nrg.dcm.edit.ScriptFunction;
+import org.nrg.dcm.edit.Value;
+
+/**
+ * @author Kevin A. Archie <karchie@wustl.edu>
+ *
+ */
+public final class Uppercase implements ScriptFunction {
+    public static final String name = "uppercase";
+
+    /* (non-Javadoc)
+     * @see org.nrg.dcm.edit.ScriptFunction#apply(java.util.List)
+     */
+    public Value apply(final List args) throws ScriptEvaluationException {
+        final Value v;
+        try {
+            v = (Value)args.get(0);
+        } catch (IndexOutOfBoundsException e) {
+            throw new ScriptEvaluationException(name + " requires one argument");
+        }
+        return new Value() {
+            public String on(Map m) throws ScriptEvaluationException {
+                return toUpper(v.on(m));
+            }
+
+            public String on(DicomObject o) throws ScriptEvaluationException {
+                return toUpper(v.on(o));
+            }
+
+            public Set getVariables() { return v.getVariables(); }
+
+            public Set getTags() { return v.getTags(); }
+        };
+    }
+
+    private static String toUpper(final String s) {
+        return null == s ? null : s.toUpperCase();
+    }
+}

File src/test/java/org/nrg/dcm/edit/fn/LowercaseTest.java

+/**
+ * Copyright (c) 2011 Washington University
+ */
+package org.nrg.dcm.edit.fn;
+
+import java.util.Arrays;
+import java.util.Collections;
+
+import org.dcm4che2.data.DicomObject;
+import org.nrg.dcm.edit.ConstantValue;
+import org.nrg.dcm.edit.ScriptEvaluationException;
+import org.nrg.dcm.edit.Value;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Kevin A. Archie <karchie@wustl.edu>
+ *
+ */
+public class LowercaseTest extends TestCase {
+    private static final DicomObject nobj = null;
+    private static final Value vFOO = new ConstantValue("FOO");
+    private static final Value vBar = new ConstantValue("Bar");
+    private static final Value v1bAzbang = new ConstantValue("1bAz!");
+    
+    /**
+     * Test method for {@link org.nrg.dcm.edit.fn.Lowercase#apply(java.util.List)}.
+     */
+    public void testApplyNoArgs() {
+        final Lowercase l = new Lowercase();
+        try {
+            l.apply(Collections.emptyList());
+            fail("expected ScriptEvaluationException for empty arguments list");
+        } catch (ScriptEvaluationException ok) {}
+    }
+    
+    /**
+     * Test method for {@link org.nrg.dcm.edit.fn.Lowercase#apply(java.util.List)}.
+     */
+    public void testApply() throws ScriptEvaluationException {
+        final Lowercase l = new Lowercase();
+        assertEquals("foo", l.apply(Collections.singletonList(vFOO)).on(nobj));
+        assertEquals("bar", l.apply(Collections.singletonList(vBar)).on(nobj));
+        assertEquals("1baz!", l.apply(Collections.singletonList(v1bAzbang)).on(nobj));
+    }
+}

File src/test/java/org/nrg/dcm/edit/fn/UppercaseTest.java

+/**
+ * Copyright (c) 2011 Washington University
+ */
+package org.nrg.dcm.edit.fn;
+
+import java.util.Arrays;
+import java.util.Collections;
+
+import org.dcm4che2.data.DicomObject;
+import org.nrg.dcm.edit.ConstantValue;
+import org.nrg.dcm.edit.ScriptEvaluationException;
+import org.nrg.dcm.edit.Value;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Kevin A. Archie <karchie@wustl.edu>
+ *
+ */
+public class UppercaseTest extends TestCase {
+    private static final DicomObject nobj = null;
+    private static final Value vfoo = new ConstantValue("foo");
+    private static final Value vBar = new ConstantValue("Bar");
+    private static final Value v1bAzbang = new ConstantValue("1bAz!");
+    
+    /**
+     * Test method for {@link org.nrg.dcm.edit.fn.Uppercase#apply(java.util.List)}.
+     */
+    public void testApplyNoArgs() {
+        final Uppercase u = new Uppercase();
+        try {
+            u.apply(Collections.emptyList());
+            fail("expected ScriptEvaluationException for empty arguments list");
+        } catch (ScriptEvaluationException ok) {}
+    }
+
+    /**
+     * Test method for {@link org.nrg.dcm.edit.fn.Uppercase#apply(java.util.List)}.
+     */
+    public void testApply() throws ScriptEvaluationException {
+        final Uppercase u = new Uppercase();
+        assertEquals("FOO", u.apply(Collections.singletonList(vfoo)).on(nobj));
+        assertEquals("BAR", u.apply(Collections.singletonList(vBar)).on(nobj));
+        assertEquals("1BAZ!", u.apply(Collections.singletonList(v1bAzbang)).on(nobj));
+    }
+}