/*
 * This file is part of the DITA Open Toolkit project.
 *
 * Copyright 2010 IBM Corporation
 *
 * See the accompanying LICENSE file for applicable license.
 */
package org.dita.dost.module;

import static org.dita.dost.util.Constants.*;

import java.io.File;
import java.net.URI;
import java.util.Collection;

import org.dita.dost.exception.DITAOTException;
import org.dita.dost.pipeline.AbstractPipelineInput;
import org.dita.dost.pipeline.AbstractPipelineOutput;
import org.dita.dost.util.Job.FileInfo;
import org.dita.dost.util.Job.FileInfo.Filter;
import org.dita.dost.writer.ImageMetadataFilter;

/**
 * Image metadata module.
 *
 */
final class ImageMetadataModule extends AbstractPipelineModuleImpl {

    /**
     * Constructor.
     */
    public ImageMetadataModule() {
        super();
    }

    /**
     * Entry point of image metadata Module.
     * @param input Input parameters and resources.
     * @return null
     * @throws DITAOTException exception
     */
    @Override
    public AbstractPipelineOutput execute(final AbstractPipelineInput input)
            throws DITAOTException {
        if (logger == null) {
            throw new IllegalStateException("Logger not set");
        }
        final File outputDir = new File(input.getAttribute(ANT_INVOKER_EXT_PARAM_OUTPUTDIR));
        final ImageMetadataFilter writer = new ImageMetadataFilter(outputDir, job);
        writer.setLogger(logger);
        writer.setJob(job);
        final Filter<FileInfo> filter = fileInfoFilter != null
                ? fileInfoFilter
                : f -> !f.isResourceOnly && ATTR_FORMAT_VALUE_DITA.equals(f.format);
        for (final FileInfo f: job.getFileInfo(filter)) {
            writer.write(new File(job.tempDir, f.file.getPath()).getAbsoluteFile());
        }

        storeImageFormat(writer.getImages(), outputDir);

        return null;
    }

    private void storeImageFormat(final Collection<URI> images, final File outputDir) {
        final URI output = outputDir.toURI();
        final URI temp = job.tempDirURI;
        for (final URI f : images) {
            assert f.isAbsolute();
            URI rel = output.relativize(f);
            if (rel.isAbsolute()) {
                rel = temp.relativize(f);
            }
            final FileInfo fi = job.getFileInfo(rel);
            if (fi != null) {
                logger.debug("Set " + fi.uri + " format to " + ATTR_FORMAT_VALUE_IMAGE);
                job.add(new FileInfo.Builder(fi).format(ATTR_FORMAT_VALUE_IMAGE).build());
            }
        }
    }

}

@@ -255,16 +255,44 @@
 o.File;%0A
+import java.io.IOException;%0A
 import j
@@ -1866,16 +1866,202 @@
 tDir);%0A%0A
+        try %7B%0A            job.write();%0A        %7D catch (IOException e) %7B%0A            throw new DITAOTException(%22Failed to serialize job configuration: %22 + e.getMessage(), e);%0A        %7D%0A%0A
         

Store job configuration after modification in image metadata module