Commits

Anonymous committed c58ee18

added function urlEncode

  • Participants
  • Parent commits 813ea15

Comments (0)

Files changed (5)

   <groupId>org.nrg</groupId>
   <artifactId>DicomEdit</artifactId>
   <packaging>jar</packaging>
-  <version>2.0.0b3</version>
+  <version>2.0.0b4</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/MessageFormatValue.java

     public String on(final Map<Integer,String> m) throws ScriptEvaluationException {
         final Object[] vals = new Object[values.size()];
         for (int i = 0; i < values.size(); i++) {
-            vals[i] = ((Value)values.get(i)).on(m);
+            final Value v = values.get(i);
+            vals[i] = v.on(m);
         }
         return MessageFormat.format(format.on(m), vals);
     }

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

 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.fn.UrlEncode;
 import org.nrg.dcm.edit.gen.UIDGenerator;
 import org.nrg.io.PresuffixFileMapper;
 import org.nrg.util.GraphUtils;
                     astParser.setFunction(GetURL.name, new GetURL());
                     astParser.setFunction(Replace.name, new Replace());
                     astParser.setFunction(Match.name, new Match());
+                    astParser.setFunction(UrlEncode.name, new UrlEncode());
                     for (final Map.Entry<String,ScriptFunction> me : functions.entrySet()) {
                         logger.trace("adding function {}", me);
                         astParser.setFunction(me.getKey(), me.getValue());
                     }
                     logger.trace("ready to parse");
                     statements = astParser.script();
-                    logger.trace(statements.toString());
+                    logger.trace("{}", statements);
                 }
             } catch (RecognitionException e) {
                 throw new ScriptEvaluationException("error parsing script", e);

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

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

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

+package org.nrg.dcm.edit.fn;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+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;
+
+public class UrlEncodeTest extends TestCase {
+    private static final DicomObject nobj = null;
+    private static final Value v0 = new ConstantValue("no-change-needed");
+    private static final List<Value> v0l = Collections.singletonList(v0);
+    private static final Value v1 = new ConstantValue("has space");
+    private static final List<Value> v1l = Collections.singletonList(v1);
+    private static final Value v2 = new ConstantValue("lots of $%^@! characters");
+    private static final List<Value> v2l = Collections.singletonList(v2);
+    
+    public void testApplyNoArgs() {
+        final UrlEncode u = new UrlEncode();
+        try {
+            u.apply(new ArrayList<Value>());
+            fail("expected ScriptEvaluationException for empty arguments list");
+        } catch (ScriptEvaluationException ok) {}
+    }
+    
+    public void testApply() throws ScriptEvaluationException,UnsupportedEncodingException {
+        final UrlEncode u = new UrlEncode();
+        assertEquals("no-change-needed", u.apply(v0l).on(nobj));
+        assertEquals("has+space", u.apply(v1l).on(nobj));
+        assertEquals("lots+of+%24%25%5E%40%21+characters", u.apply(v2l).on(nobj));
+        assertEquals(v2.on(nobj), URLDecoder.decode(u.apply(v2l).on(nobj), "UTF-8"));
+    }
+}