Skip to content

Commit

Permalink
Make IncompatibleImageReferenceException checked
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonOellerer committed Dec 11, 2024
1 parent b6956e3 commit b593fad
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group 'com.docutools'
version = '7.0.0'
version = '8.0.0'

java {
toolchain {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,17 @@ private DefaultImageStrategy() {
}

@Override
public ImageReference load(Path path) throws IOException {
public ImageReference load(Path path) throws IOException, IncompatibleImageReferenceException {
log.trace("Loading image from '{}'", path);
var image = ImageIO.read(path.toFile());
if (image == null) {
throw new IncompatibleImageReferenceException("Could not read image %s".formatted(path));
}
return new DefaultImageReference(image);
}

@Override
public ImageReference scale(ImageReference original, double scaleBy) {
public ImageReference scale(ImageReference original, double scaleBy) throws IncompatibleImageReferenceException {
log.trace("Scaling image {} by factor {}", original, scaleBy);

if (original instanceof DefaultImageReference ref) {
Expand All @@ -66,7 +69,7 @@ public ImageReference scale(ImageReference original, double scaleBy) {
op.filter(source, target);
return new DefaultImageReference(target);
}
throw new IncompatibleImageReferenceException();
throw new IncompatibleImageReferenceException("Image reference {} not instance of DefaultImageReference %s".formatted(original.getId()));
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/docutools/jocument/image/ImageStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public interface ImageStrategy {
* @param path the path to the image file
* @return the reference to the in-memory image
*/
ImageReference load(Path path) throws IOException;
ImageReference load(Path path) throws IOException, IncompatibleImageReferenceException;

/**
* Scales an image by the given factor (can be > or < to 1.0) into a new in-memory image and returns it. The OG
Expand All @@ -40,7 +40,7 @@ public interface ImageStrategy {
* @param scaleBy the scaling factor (>= 0)
* @return the scaled image
*/
ImageReference scale(ImageReference original, double scaleBy);
ImageReference scale(ImageReference original, double scaleBy) throws IncompatibleImageReferenceException;

/**
* Gets the dimensions of the provided image.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
* @since 2021-03-23
* @author partschi
*/
public class IncompatibleImageReferenceException extends RuntimeException {
public class IncompatibleImageReferenceException extends Exception {
public IncompatibleImageReferenceException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.docutools.jocument.GenerationOptions;
import com.docutools.jocument.image.ImageReference;
import com.docutools.jocument.image.IncompatibleImageReferenceException;
import com.docutools.jocument.image.NoWriterFoundException;
import com.docutools.jocument.impl.word.CustomWordPlaceholderData;
import com.docutools.jocument.impl.word.WordImageUtils;
Expand Down Expand Up @@ -100,7 +101,7 @@ private Path applyOptions(GenerationOptions options) {
}
}
return saveImage(image);
} catch (IOException | NoWriterFoundException e) {
} catch (IOException | NoWriterFoundException | IncompatibleImageReferenceException e) {
logger.error(e);
return imagePath;
}
Expand Down

0 comments on commit b593fad

Please sign in to comment.