Commits

Anonymous committed 85bfa63

pulled read methods out to DicomUtils package

  • Participants
  • Parent commits c58ee18

Comments (0)

Files changed (7)

   <groupId>org.nrg</groupId>
   <artifactId>DicomEdit</artifactId>
   <packaging>jar</packaging>
-  <version>2.0.0b4</version>
+  <version>2.0.0b5</version>
   <name>DicomEdit</name>
   <description>Implementation of a language for modifying DICOM metadata</description>
   <url>http://nrg.wustl.edu</url>
     	<artifactId>guava</artifactId>
     	<version>r09</version>
     </dependency>
+    <dependency>
+    	<groupId>org.nrg</groupId>
+    	<artifactId>DicomUtils</artifactId>
+    	<version>1.2.0</version>
+    	<type>jar</type>
+    	<scope>compile</scope>
+    </dependency>
   </dependencies>
 </project>

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

  */
 package org.nrg.dcm.edit;
 
-import java.io.BufferedInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.MalformedURLException;
-import java.net.URI;
-import java.net.URL;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.Map;
 import java.util.TimeZone;
 import java.util.regex.Pattern;
-import java.util.zip.GZIPInputStream;
 
 import org.dcm4che2.data.DicomElement;
 import org.dcm4che2.data.DicomObject;
 import org.dcm4che2.data.Tag;
 import org.dcm4che2.data.UID;
 import org.dcm4che2.data.VR;
-import org.dcm4che2.io.DicomInputStream;
-import org.dcm4che2.io.StopTagInputHandler;
-import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Joiner;
 import com.google.common.collect.ImmutableMap;
         return VALID_UID_PATTERN.matcher(uid).matches();
     }
 
-    public static class DicomObjectException extends Exception {
-        private final static long serialVersionUID = 1L;
-
-        private DicomObjectException(final File f, final String message) {
-            super(buildMessage(f, message));
-        }
-
-        private DicomObjectException(final File f, final String message, final Throwable t) {
-            super(buildMessage(f, message), t);
-        }
-
-        private DicomObjectException(final File f, final Throwable t) {
-            this(f, null, t);
-        }
-
-        private DicomObjectException(final String message) {
-            super(message);
-        }
-
-        private DicomObjectException(final Throwable t) {
-            super(t);
-        }
-
-        private static final String buildMessage(final File f, final String message) {
-            final StringBuffer sb = new StringBuffer(f.getPath());
-            if (null != message) {
-                sb.append(": ").append(message);
-            }
-            return sb.toString();
-        }
-    }
-
-
 
     /**
      * Returns the Transfer Syntax UID of the given DICOM object.
     }
 
 
-    /**
-     * Reads a new DicomObject from the given InputStream
-     * @param in InputStream from which the object will be read
-     * @return new DicomObject
-     * @throws DicomObjectException if the content is not a valid DICOM object
-     * @throws IOException
-     */
-    public static DicomObject read(final InputStream in)
-    throws DicomObjectException,IOException {
-        return read(in, -1);
-    }
-
-
-    /**
-     * Reads a new DicomObject from the given InputStream
-     * @param in InputStream from which the object will be read
-     * @param maxTag last DICOM attribute to be included in the object
-     * @return new DicomObject
-     * @throws DicomObjectException if the content is not a valid DICOM object
-     * @throws IOException
-     */
-    public static DicomObject read(final InputStream in, final int maxTag)
-    throws DicomObjectException,IOException {
-        IOException ioexception = null;
-        final BufferedInputStream bin = new BufferedInputStream(in);
-        try {
-            final DicomInputStream din = new DicomInputStream(bin);
-            try {
-                if (maxTag > 0) {
-                    din.setHandler(new StopTagInputHandler(maxTag+1));
-                }
-                final DicomObject o = din.readDicomObject();
-                if (o.contains(Tag.SOPClassUID)) {
-                    return o;
-                } else {
-                    throw new DicomObjectException("no SOP class UID");
-                }
-            } catch (IOException e) {
-                throw ioexception = e;
-            } catch (DicomObjectException e) {
-                throw e;
-            } catch (Throwable e) {
-                throw new DicomObjectException(e);
-            } finally {
-                try {
-                    din.close();
-                } catch (IOException e) {
-                    throw ioexception = (null == ioexception) ? e : ioexception;
-                }
-            }
-        } finally {
-            try {
-                bin.close();
-            } catch (IOException e) {
-                throw ioexception = (null == ioexception) ? e : ioexception;
-            }
-        }
-    }
-
-
-    /**
-     * Reads the named file into a new DicomObject
-     * @param file File to be read
-     * @return new DicomObject
-     * @throws DicomObjectException if the content is not a valid DICOM object
-     * @throws IOException
-     */
-    public static DicomObject read(final File file) throws DicomObjectException, IOException {
-        return read(file, -1);
-    }
-
-
-    /**
-     * Reads the named file into a new DicomObject
-     * @param file File to be read
-     * @param maxTag last tag to be included in the object
-     * @return new DicomObject 
-     * @throws DicomObjectException if the content is not a valid DICOM object
-     * @throws IOException
-     */
-    public static DicomObject read(final File file, final int maxTag)
-    throws DicomObjectException,IOException {
-        IOException ioexception = null;
-        InputStream fin = new FileInputStream(file);
-        try {
-            if (file.getName().endsWith(".gz")) {
-                fin = new GZIPInputStream(fin);
-            }
-            LoggerFactory.getLogger(DicomUtils.class).trace("reading {}", file);
-            return read(fin, maxTag);
-        } catch (IOException e) {
-            throw ioexception = e;
-        } catch (DicomObjectException e) {
-            throw new DicomObjectException(file, e.getMessage(), e.getCause());
-        } finally {
-            try {
-                fin.close();
-            } catch (IOException e) {
-                throw ioexception = (null == ioexception) ? e : ioexception;
-            }
-        }
-    }
-
-    public static DicomObject read(final URI uri, final int maxTag)
-    throws DicomObjectException,IOException,MalformedURLException {
-        if ("file".equals(uri.getScheme())) {
-            return read(new File(uri), maxTag);
-        } else if (uri.isAbsolute()){
-            final URL url = uri.toURL();
-            final InputStream in = url.openStream();
-            IOException ioexception = null;
-            try {
-                return read(in, maxTag);
-            } catch (IOException e) {
-                throw ioexception = e;
-            } finally {
-                try {
-                    in.close();
-                } catch (IOException e) {
-                    throw ioexception = (null == ioexception) ? e : ioexception;
-                }
-            }
-        } else {
-            throw new IllegalArgumentException("URIs must be absolute");
-        }
-    }
-
-    public static DicomObject read(final URI uri)
-    throws DicomObjectException,IOException,MalformedURLException {
-        return read(uri, -1);
-    }
-
-
     private static final TimeZone TIME_ZONE = Calendar.getInstance().getTimeZone();
 
     /**

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

 import org.antlr.runtime.tree.CommonTreeNodeStream;
 import org.dcm4che2.data.DicomObject;
 import org.dcm4che2.io.DicomOutputStream;
-import org.nrg.dcm.edit.DicomUtils.DicomObjectException;
+import org.nrg.dcm.DicomUtils;
 import org.nrg.dcm.edit.fn.Format;
 import org.nrg.dcm.edit.fn.GetURL;
 import org.nrg.dcm.edit.fn.Lowercase;
 
 
     public DicomObject apply(final File f)
-    throws IOException,AttributeException,DicomObjectException,ScriptEvaluationException {
+    throws IOException,AttributeException,ScriptEvaluationException {
         return apply(f, DicomUtils.read(f));
     }
 

src/main/java/org/nrg/dcm/io/BatchExporter.java

 import java.util.Map;
 
 import org.dcm4che2.data.DicomObject;
+import org.nrg.dcm.DicomUtils;
 import org.nrg.dcm.edit.Action;
-import org.nrg.dcm.edit.DicomUtils;
 import org.nrg.dcm.edit.Statement;
 import org.nrg.util.CanceledOperationException;
 import org.nrg.util.EditProgressMonitor;

src/main/java/org/nrg/dcm/io/TransferCapabilityExtractor.java

 /**
- * Copyright (c) 2009 Washington University
+ * Copyright (c) 2009,2011 Washington University
  */
 package org.nrg.dcm.io;
 
 import java.io.File;
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
 import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
 import java.util.List;
-import java.util.Map;
 import java.util.Set;
 
 import org.dcm4che2.data.DicomObject;
 import org.dcm4che2.data.Tag;
 import org.dcm4che2.net.TransferCapability;
-import org.nrg.dcm.edit.DicomUtils;
-import org.nrg.dcm.edit.DicomUtils.DicomObjectException;
+import org.nrg.dcm.DicomUtils;
+
+import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
+import com.google.common.collect.LinkedHashMultimap;
+import com.google.common.collect.Lists;
+import com.google.common.collect.SetMultimap;
+import com.google.common.collect.Sets;
 
 /**
- * @author Kevin A. Archie <karchie@npg.wustl.edu>
+ * @author Kevin A. Archie <karchie@wustl.edu>
  *
  */
 public final class TransferCapabilityExtractor {
-	private TransferCapabilityExtractor() {}
+    private TransferCapabilityExtractor() {}
 
-	private final static String[] STRING_ARRAY = new String[0];
-	private final static TransferCapability[] TRANSFER_CAPABILITY_ARRAY = new TransferCapability[0];
+    public static TransferCapability[] getTransferCapabilities(final Iterator<File> files, final String role) {
+        final SetMultimap<String,String> tcElements = LinkedHashMultimap.create();
+        assert Tag.TransferSyntaxUID < Tag.SOPClassUID;
+        while (files.hasNext()) {
+            final File f = files.next();
+            try {
+                final DicomObject o = DicomUtils.read(f, Tag.SOPClassUID);
+                tcElements.put(o.getString(Tag.SOPClassUID), DicomUtils.getTransferSyntaxUID(o));
+            } catch (IOException skip) {}
+        }
 
-	public static TransferCapability[] getTransferCapabilities(final Iterator files, final String role) {
-		final Map tcElements = new LinkedHashMap();
-		while (files.hasNext()) {
-			final File f = (File)files.next();
-			assert Tag.TransferSyntaxUID < Tag.SOPClassUID;
-			try {
-				final DicomObject o = DicomUtils.read(f, Tag.SOPClassUID);
-				final String tsUID = DicomUtils.getTransferSyntaxUID(o);
-				final String sopClassUID = o.getString(Tag.SOPClassUID);
-				final Collection ts = (Collection)tcElements.get(sopClassUID);
-				if (null == ts) {
-					tcElements.put(sopClassUID, new LinkedHashSet(Collections.singleton(tsUID)));
-				} else {
-					ts.add(tsUID);
-				}
-			} catch (IOException skip) {
-			} catch (DicomObjectException skip) {}
-		}
+        return getTransferCapabilities(tcElements, role);
+    }
 
-		return getTransferCapabilities(tcElements, role);
-	}
+    public static TransferCapability[] getTransferCapabilities(final Iterable<File> files, final String role) {
+        return getTransferCapabilities(files.iterator(), role);
+    }
 
-	public static TransferCapability[] getTransferCapabilities(final Collection files, final String role) {
-		return getTransferCapabilities(files.iterator(), role);
-	}
-	
-	/**
-	 * 
-	 * @param tsToSOP Map : SOP class UID (String) -> Collection of Transfer Syntax (String)
-	 * @param role "SCP" or "SCU"
-	 * @return array of TransferCapabilities
-	 */
-	public static TransferCapability[] getTransferCapabilities(final Map sopToTS, final String role) {
-		final List tcs = new ArrayList(sopToTS.size());
-		for (final Iterator mei = sopToTS.entrySet().iterator(); mei.hasNext(); ) {
-			final Map.Entry me = (Map.Entry)mei.next();
-			final String sopClassUID = (String)me.getKey();
-			if (null != sopClassUID) {
-				final Set tsuids = new LinkedHashSet((Collection)me.getValue());
-				tsuids.remove(null);
-				if (!tsuids.isEmpty()) {
-					tcs.add(new TransferCapability(sopClassUID,
-							(String[])tsuids.toArray(STRING_ARRAY),
-							role));
-				}
-			}
-		}
-		return (TransferCapability[])tcs.toArray(TRANSFER_CAPABILITY_ARRAY);
-	}
+    private static final TransferCapability[] EMPTY_TC_ARRAY = new TransferCapability[0];
+    private static final String[] EMPTY_STRING_ARRAY = new String[0];
+    private static final Predicate<String> notNull = Predicates.notNull();
+
+    /**
+     * Generates an array of TransferCapability from a SetMultimap of SOP -> transfer syntax
+     * @param tsToSOP Map : SOP class UID (String) -> Collection of Transfer Syntax (String)
+     * @param role "SCP" or "SCU"
+     * @return array of TransferCapabilities
+     */
+    public static TransferCapability[] getTransferCapabilities(final SetMultimap<String,String> sopToTS, final String role) {
+        final List<TransferCapability> tcs = Lists.newArrayListWithCapacity(sopToTS.size());
+        for (final String sop : sopToTS.keySet()) {
+            final Set<String> tsuids = Sets.filter(sopToTS.get(sop), notNull);
+            if (!tsuids.isEmpty()) {
+                tcs.add(new TransferCapability(sop, tsuids.toArray(EMPTY_STRING_ARRAY), role));
+            }
+        }
+        return tcs.toArray(EMPTY_TC_ARRAY);
+    }
 }

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

 
 import org.dcm4che2.data.DicomObject;
 import org.dcm4che2.data.Tag;
+import org.nrg.dcm.DicomUtils;
 import org.nrg.dcm.edit.ConstantValue;
-import org.nrg.dcm.edit.DicomUtils;
-import org.nrg.dcm.edit.DicomUtils.DicomObjectException;
 import org.nrg.dcm.edit.IntegerValue;
 import org.nrg.dcm.edit.ScriptEvaluationException;
 import org.nrg.dcm.edit.ScriptFunction;
      * @throws IOException 
      * @throws DicomObjectException 
      */
-    public void testApply() throws ScriptEvaluationException,DicomObjectException,IOException {
+    public void testApply() throws ScriptEvaluationException,IOException {
         final ScriptFunction match = new Match();
         assertEquals("v00", match.apply(Arrays.asList(new Value[] {
                 new ConstantValue("s01_v00_mr1"),

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

 
 import org.dcm4che2.data.DicomObject;
 import org.dcm4che2.data.Tag;
+import org.nrg.dcm.DicomUtils;
 import org.nrg.dcm.edit.ConstantValue;
-import org.nrg.dcm.edit.DicomUtils;
 import org.nrg.dcm.edit.ScriptEvaluationException;
 import org.nrg.dcm.edit.ScriptFunction;
 import org.nrg.dcm.edit.SingleTagValue;