Commits

P_W999 committed 60b9ba8

- starting v0.0.2
- changed exception handling a bit

  • Participants
  • Parent commits a2c4c7c

Comments (0)

Files changed (21)

src/be/pw/jexif/JExifInfo.java

  ******************************************************************************/
 package be.pw.jexif;
 
+import java.io.File;
+
 import be.pw.jexif.enums.tag.Tag;
 import be.pw.jexif.exception.ExifError;
 import be.pw.jexif.exception.JExifException;
-import com.google.common.annotations.Beta;
 
-import java.io.File;
+import com.google.common.annotations.Beta;
 
 /**
  * The JExifInfo class can be used to read or write a Tag from and to a given file.<br />

src/be/pw/jexif/JExifTool.java

  ******************************************************************************/
 package be.pw.jexif;
 
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.text.MessageFormat;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 import be.pw.jexif.enums.Errors;
 import be.pw.jexif.enums.tag.Tag;
 import be.pw.jexif.exception.ExifError;
 import be.pw.jexif.internal.thread.event.DebugHandler;
 import be.pw.jexif.internal.thread.event.EventHandler;
 import be.pw.jexif.internal.util.Cal10nUtil;
+
 import com.google.common.annotations.Beta;
 import com.google.common.base.Preconditions;
 import com.google.common.eventbus.EventBus;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.*;
-import java.text.MessageFormat;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
 
 /**
  * JExifTool is the main class to use. <br />

src/be/pw/jexif/enums/Errors.java

 @BaseName("errorMessages")
 @LocaleData({@Locale("en")})
 public enum Errors {
-    GENERAL, EXIFTHREAD, IO_ARGSFILE, IO_CLOSING, INTERRUPTED_SLEEP, DEADLOCK, VALIDATION_TAGLIST, IO_BUILD_ARGUMENTS, VALIDATION_VALUESLIST, EXIFTOOL_EXECUTION_ERROR, EXIFTOOL_EXECUTION_WARNING, RETREIVE_PARSER_ERROR, PARSER_UNKNOWNTYPE, PARSER_MISMATCH, VALIDATION_WRITE_PROTECTED_TAG, VALIDATION_WRITE_AVOIDED_TAG, VALIDATION_WRITE_UNSAFE_TAG, UNKNOWN_DATATYPE, VALIDATION_NEGATIVE_UNSIGNED, VALIDATION_EXCEEDS_LIMITS, VALIDATION_RAT_FOR_INT, VALIDATION_INVALID_TYPE
+    GENERAL, EXIFTHREAD, IO_ARGSFILE, IO_CLOSING, INTERRUPTED_SLEEP, DEADLOCK, VALIDATION_TAGLIST, IO_BUILD_ARGUMENTS, VALIDATION_VALUESLIST, EXIFTOOL_EXECUTION_ERROR, EXIFTOOL_EXECUTION_WARNING, RETREIVE_PARSER_ERROR, PARSER_UNKNOWNTYPE, PARSER_MISMATCH, VALIDATION_WRITE_PROTECTED_TAG, VALIDATION_WRITE_AVOIDED_TAG, VALIDATION_WRITE_UNSAFE_TAG, UNKNOWN_DATATYPE, VALIDATION_NEGATIVE_UNSIGNED, VALIDATION_EXCEEDS_LIMITS, VALIDATION_RAT_FOR_INT, VALIDATION_INVALID_TYPE, IO_FILE_NOT_VALID
 }

src/be/pw/jexif/enums/tag/ExifIFD.java

  ******************************************************************************/
 package be.pw.jexif.enums.tag;
 
+import static be.pw.jexif.enums.DataType.INT16S;
+import static be.pw.jexif.enums.DataType.INT16U;
+import static be.pw.jexif.enums.DataType.INT32S;
+import static be.pw.jexif.enums.DataType.INT32U;
+import static be.pw.jexif.enums.DataType.INT8U;
+import static be.pw.jexif.enums.DataType.RAT64S;
+import static be.pw.jexif.enums.DataType.RAT64U;
+import static be.pw.jexif.enums.DataType.STRING;
+import static be.pw.jexif.enums.DataType.UNDEF;
 import be.pw.jexif.enums.DataType;
 
-import static be.pw.jexif.enums.DataType.*;
-
 /**
  * Implementation of the ExifIFD group.
  */

src/be/pw/jexif/enums/tag/IFD0.java

  ******************************************************************************/
 package be.pw.jexif.enums.tag;
 
-import be.pw.jexif.enums.DataType;
+import static be.pw.jexif.enums.DataType.FLOAT;
+import static be.pw.jexif.enums.DataType.INT16U;
+import static be.pw.jexif.enums.DataType.INT32U;
+import static be.pw.jexif.enums.DataType.INT8U;
+import static be.pw.jexif.enums.DataType.RAT64S;
+import static be.pw.jexif.enums.DataType.RAT64U;
+import static be.pw.jexif.enums.DataType.STRING;
+import static be.pw.jexif.enums.DataType.UNDEF;
 
 import java.util.HashMap;
 import java.util.Map;
 
-import static be.pw.jexif.enums.DataType.*;
+import be.pw.jexif.enums.DataType;
 
 /**
  * Implementation of the IFD0 tag group.

src/be/pw/jexif/exception/JExifException.java

 	/**
 	 * Hidden constructor to prevent creation of JExifExceptions without information
 	 */
-	@SuppressWarnings("unused")
-	private JExifException() {
+	protected JExifException() {
 		super();
 	}
 

src/be/pw/jexif/internal/action/impl/ActionFactory.java

 package be.pw.jexif.internal.action.impl;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.Map;
 
 import be.pw.jexif.enums.tag.Tag;
-import be.pw.jexif.exception.JExifException;
+import be.pw.jexif.exception.JExifValidationException;
 
 /**
  * Factory for the different action.
 	 * @param file the file from which the tags should be retrieved. May not be null.
 	 * @param tags the tags that should be read. May not be null.
 	 * @return an instance of a TagReadAction or null if validation failed.
+	 * @throws IOException if the given file does not exist
 	 */
-	public static TagReadAction createReadAction(final File file, final Tag... tags) {
+	public static TagReadAction createReadAction(final File file, final Tag... tags) throws IOException {
 		TagReadAction read = new TagReadAction();
 		read.setParams(file, tags);
 		return read;
 	 * @param file the file from which the tags should be retrieved. May not be null.
 	 * @param tags the tags that should be read. May not be null.
 	 * @return an instance of a TagReadAction or null if validation failed.
+	 * @throws IOException if the given file does not exist
 	 */
-	public static TagReadAction createExactReadAction(final File file, final Tag... tags) {
-
+	public static TagReadAction createExactReadAction(final File file, final Tag... tags) throws IOException {
 		TagReadExactAction read = new TagReadExactAction();
 		read.setParams(file, tags);
 		return read;
 
 	}
 
-	public static TagWriteAction createTagWriteAction(final File file, final Map<Tag, String> valuesToWrite) throws JExifException {
-
+	public static TagWriteAction createTagWriteAction(final File file, final Map<Tag, String> valuesToWrite) throws JExifValidationException, IOException {
 		TagWriteAction read = new TagWriteAction();
 		read.setParams(file, valuesToWrite);
 		return read;

src/be/pw/jexif/internal/action/impl/TagReadAction.java

 	}
 
 	/**
-	 * Alows settings the required parameters. AnnotationValidation is possible.
+	 * Allows settings the required parameters. AnnotationValidation is possible.
 	 * @param file the file from which the provided Tags should be read.
 	 * @param tags the Tags that must be read from the provided file.
+	 * @throws IOException if the given file does not exist.
 	 */
-	public void setParams(final File file, final Tag... tags) {
+	void setParams(final File file, final Tag... tags) throws IOException {
 		Preconditions.checkNotNull(file);
+		if (!file.exists())  {
+			throw new IOException(Cal10nUtil.get(Errors.IO_FILE_NOT_VALID, file.getAbsolutePath()));
+		}
 		Preconditions.checkNotNull(tags);
 		Preconditions.checkArgument(tags.length != 0, Cal10nUtil.get(Errors.VALIDATION_TAGLIST));
 		this.file = file;
 
 	/**
 	 * {@inheritDoc}
-	 * @throws JExifException in case of an IO-Exception or an IndexOutOfBoundException.
+	 * @throws JExifException in case of an IO-Exception.
 	 */
 	@Override
 	public String[] buildArguments() throws JExifException {
 		} catch (IOException e) {
 			LOG.error(Cal10nUtil.get(Errors.IO_BUILD_ARGUMENTS), e);
 			throw new JExifException(e);
-		} catch (IndexOutOfBoundsException e) {
-			LOG.error("Oops", e);
-			throw new JExifException(e);
-		}
+		} 
 	}
 }

src/be/pw/jexif/internal/action/impl/TagWriteAction.java

 import be.pw.jexif.enums.Errors;
 import be.pw.jexif.enums.tag.Tag;
 import be.pw.jexif.exception.JExifException;
+import be.pw.jexif.exception.JExifValidationException;
 import be.pw.jexif.internal.action.AbstractAction;
 import be.pw.jexif.internal.action.IAction;
 import be.pw.jexif.internal.constants.ExecutionConstant;
 	 * @param valuesToWrite a Map combining the Tag-Value combination. The value element shall be written to the Tag.<br />
 	 *            If you want to clear a Tag, just specify an empty String.
 	 * @throws JExifException if the give value is not of a valid type.
+	 * @throws IOException if the given file does not exist.
 	 * @throws IllegalArgumentException if provided fields are null, empty or if trying to write to a protected/avoided/unsafe tag.
 	 */
-	public void setParams(final File file, final Map<Tag, String> valuesToWrite) throws JExifException {
+	void setParams(final File file, final Map<Tag, String> valuesToWrite) throws JExifValidationException, IOException {
 		Preconditions.checkNotNull(file);
+		if (!file.exists())  {
+			throw new IOException(Cal10nUtil.get(Errors.IO_FILE_NOT_VALID, file.getAbsolutePath()));
+		}
 		Preconditions.checkNotNull(valuesToWrite);
 		Preconditions.checkArgument(valuesToWrite.size() != 0, Cal10nUtil.get(Errors.VALIDATION_VALUESLIST));
 		for (Entry<Tag, String> entry : valuesToWrite.entrySet()) {
 		} catch (IOException e) {
 			LOG.error(Cal10nUtil.get(Errors.IO_BUILD_ARGUMENTS), e);
 			throw new JExifException(e);
-		} catch (IndexOutOfBoundsException e) {
-			LOG.error("Oops", e);
-			throw new JExifException(e);
-		}
+		} 
 	}
 }

src/be/pw/jexif/internal/result/IResultParser.java

 
 import java.util.List;
 
-import be.pw.jexif.exception.JExifException;
 import be.pw.jexif.internal.action.IAction;
 
 /**
 	 * @param action the action where the results will be stored.
 	 * @param actionUID the UID of the action.
 	 * @param result the list with the output of ExifTool.
-	 * @throws JExifException if the actionUID doesn't match the action's id or if the wrong parser is used.
 	 */
-	void parse(final IAction action, final String actionUID, final List<String> result) throws JExifException;
+	void parse(final IAction action, final String actionUID, final List<String> result) ;
 
 	/**
 	 * Returns the {@link IAction} for which this parser can be used.

src/be/pw/jexif/internal/result/impl/TagReadParser.java

 
 import be.pw.jexif.enums.Errors;
 import be.pw.jexif.enums.tag.Tag;
-import be.pw.jexif.exception.JExifException;
 import be.pw.jexif.internal.action.IAction;
 import be.pw.jexif.internal.action.impl.TagReadAction;
 import be.pw.jexif.internal.constants.ExecutionConstant;
 import be.pw.jexif.internal.util.Cal10nUtil;
 import be.pw.jexif.internal.util.TagUtil;
 
+import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
 
 /**
 	 * {@inheritDoc}
 	 */
 	@Override
-	public final void parse(final IAction action, final String actionUID, final List<String> result) throws JExifException {
-		if (!action.getClass().equals(validFor())) {
-			throw new JExifException(Cal10nUtil.get(Errors.PARSER_MISMATCH, getClass().getSimpleName(), action.getClass().getSimpleName()));
-		}
-		if (!action.getId().equals(actionUID)) {
-			throw new JExifException("Invalid action UID");
-		}
+	public final void parse(final IAction action, final String actionUID, final List<String> result)  {
+		Preconditions.checkArgument(!action.getClass().equals(validFor()), Cal10nUtil.get(Errors.PARSER_MISMATCH, getClass().getSimpleName(), action.getClass().getSimpleName()));
+		Preconditions.checkArgument(!action.getId().equals(actionUID), "Invalid action UID");
+		
 		Tag t;
 		String r;
 		for (String line : result) {

src/be/pw/jexif/internal/result/impl/TagWriteParser.java

 import org.slf4j.LoggerFactory;
 
 import be.pw.jexif.enums.Errors;
-import be.pw.jexif.exception.JExifException;
 import be.pw.jexif.internal.action.IAction;
 import be.pw.jexif.internal.action.impl.TagWriteAction;
 import be.pw.jexif.internal.constants.ExecutionConstant;
 import be.pw.jexif.internal.result.IResultParser;
 import be.pw.jexif.internal.util.Cal10nUtil;
 
+import com.google.common.base.Preconditions;
+
 public class TagWriteParser implements IResultParser {
 
 	private static final Logger LOG = LoggerFactory.getLogger(TagWriteParser.class);
 	}
 
 	@Override
-	public void parse(final IAction action, final String actionUID, final List<String> result) throws JExifException {
-		if (!action.getClass().equals(validFor())) {
-			throw new JExifException(Cal10nUtil.get(Errors.PARSER_MISMATCH, getClass().getSimpleName(), action.getClass().getSimpleName()));
-		}
-		if (!action.getId().equals(actionUID)) {
-			throw new JExifException("Invalid action UID");
-		}
+	public void parse(final IAction action, final String actionUID, final List<String> result) {
+		Preconditions.checkArgument(!action.getClass().equals(validFor()), Cal10nUtil.get(Errors.PARSER_MISMATCH, getClass().getSimpleName(), action.getClass().getSimpleName()));
+		Preconditions.checkArgument(!action.getId().equals(actionUID), "Invalid action UID");
+
 		for (String line : result) {
 			if (!line.contains(ExecutionConstant.EXIFTOOLREADY) && !line.isEmpty()) {
 				LOG.trace(line);

src/be/pw/jexif/internal/thread/RunnableReader.java

 	private final EventBus bus;
 
 	/**
-	 * Inidicates whether this reader is used to read the error-stream.
+	 * Indicates whether this reader is used to read the error-stream.
 	 */
 	private final boolean error;
 
 			LOG.error("Error in reader thread", e);
 		} finally {
 			try {
-				reader.close();
-				LOG.trace("Reader closed");
+				if (reader != null) {
+					reader.close();
+					LOG.trace("Reader closed");
+				}
 			} catch (IOException e) {
 				LOG.error("Failed to close reader", e);
 			}

src/be/pw/jexif/internal/thread/event/EventHandler.java

  ******************************************************************************/
 package be.pw.jexif.internal.thread.event;
 
-import be.pw.jexif.internal.action.IAction;
-import be.pw.jexif.internal.constants.ExecutionConstant;
-import com.google.common.eventbus.Subscribe;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
 import java.text.MessageFormat;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import be.pw.jexif.internal.action.IAction;
+import be.pw.jexif.internal.constants.ExecutionConstant;
+
+import com.google.common.eventbus.Subscribe;
+
 /**
  * Handles the different events.
  *

src/be/pw/jexif/internal/util/Cal10nUtil.java

  ******************************************************************************/
 package be.pw.jexif.internal.util;
 
+import java.text.MessageFormat;
+import java.util.Locale;
+
 import be.pw.jexif.enums.Errors;
 import ch.qos.cal10n.IMessageConveyor;
 import ch.qos.cal10n.MessageConveyor;
 import ch.qos.cal10n.MessageConveyorException;
 
-import java.text.MessageFormat;
-import java.util.Locale;
-
 /**
  * A utility for i18n'ing the API's output. <br />
  * Uses Cal10n: {@linkplain http://cal10n.qos.ch/}

src/be/pw/jexif/internal/util/TagUtil.java

  ******************************************************************************/
 package be.pw.jexif.internal.util;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import be.pw.jexif.enums.DataType;
 import be.pw.jexif.enums.Errors;
 import be.pw.jexif.enums.tag.ExifIFD;
 import be.pw.jexif.enums.tag.IFD0;
 import be.pw.jexif.enums.tag.Tag;
 import be.pw.jexif.exception.JExifException;
-
-import java.util.HashMap;
-import java.util.Map;
+import be.pw.jexif.exception.JExifValidationException;
 
 /**
  * General utility class for working with Tags.
      * @param value the value to be written.
      * @throws JExifException if the given value is not formatted correctly.
      */
-    public static void validateValue(final Tag tag, final String value) throws JExifException {
+    public static void validateValue(final Tag tag, final String value) throws JExifValidationException {
         DataType type = tag.getType();
         Exception ex = null;
         Errors error = null;
                 throw new IllegalArgumentException(Cal10nUtil.get(Errors.UNKNOWN_DATATYPE, type.toString()));
         }
         if (error != null) {
-            throw new JExifException(Cal10nUtil.get(error, tag.getName(), type, value), ex);
+            throw new JExifValidationException(Cal10nUtil.get(error, tag.getName(), type, value), ex);
         }
     }
 

src/errorMessages.properties

 VALIDATION_NEGATIVE_UNSIGNED=Tag {0} has datatype {1} which is unsigned. The given value {2} is a negative value.
 VALIDATION_EXCEEDS_LIMITS=Tag {0} has datatype {1}. The value {2} exceeds the limits of this datatype.
 VALIDATION_RAT_FOR_INT=Tag {0} has datatype {1}. The value {2} however seems to be a rational.
-VALIDATION_INVALID_TYPE=Tag {0} has datatype {1}. The value {2} could not be parsed as being such.
+VALIDATION_INVALID_TYPE=Tag {0} has datatype {1}. The value {2} could not be parsed as being such.
+IO_FILE_NOT_VALID=There was a problem reading the file {0}

src/errorMessages_en.properties

 VALIDATION_EXCEEDS_LIMITS=Tag {0} has datatype {1}. The value {2} exceeds the limits of this datatype.
 VALIDATION_RAT_FOR_INT=Tag {0} has datatype {1}. The value {2} however seems to be a rational.
 VALIDATION_INVALID_TYPE=Tag {0} has datatype {1}. The value {2} could not be parsed as being such.
+IO_FILE_NOT_VALID=There was a problem reading the file {0}

src/errorMessages_nl.properties

 VALIDATION_EXCEEDS_LIMITS=Tag {0} heeft datatype {1}. De waarde {2} overschrijd de limieten van dit datatype.
 VALIDATION_RAT_FOR_INT=Tag {0} heeft datatype {1}. De waarde {2} is echter rationieel.
 VALIDATION_INVALID_TYPE=Tag {0} heeft datatype {1}. De waarde {2} kon niet als zo zijnde geparsed worden.
+IO_FILE_NOT_VALID=Er was een probleem met het uitlezen van het bestand {0}

src/jexiftool.properties

-jexiftool.version=0.0.1
+jexiftool.version=0.0.2
 exiftool.path=lib\\exiftool.exe
 exiftool.deadlock=4000

test/be/pw/jexif/internal/result/impl/TagReadParserTest.java

 import static org.fest.assertions.Assertions.assertThat;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import be.pw.jexif.internal.action.IAction;
 import be.pw.jexif.internal.action.impl.ActionFactory;
 
-/**
- * Created with IntelliJ IDEA. User: Phillip Date: 12/05/12 Time: 16:00
- */
 public class TagReadParserTest {
 
 	TagReadParser parser;
 	List<String> results;
 
 	@BeforeTest
-	public void setUp() {
+	public void setUp() throws IOException {
 		parser = new TagReadParser();
 		actionForTest = ActionFactory.createReadAction(new File("."), ExifIFD.APERTUREVALUE, ExifIFD.EXPOSURETIME);
 		results = new ArrayList<String>();