package com.pdflib.cookbook.pcos.interchange;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

import com.pdflib.pCOS;
import com.pdflib.pCOSException;
import com.pdflib.cookbook.pcos.pcos_cookbook_example;

/** 
 * Retrieve the XMP metadata for all images in the document.
 * <p>
 * The metadata is written to a separate file for each image. The naming
 * convention for the generated files is:
 * <p>
 * &lt;basename of input document&gt;_images[&lt;n&gt;]_Metadata.txt
 * <p>
 * with &lt;n&gt; being the index of the image in the "images" array.
 * <p>
 * Required software: pCOS interface 3 (pCOS 2.x, PDFlib 7.x, TET 2.2,
 * PLOP 3.x)<br>
 * Required data: PDF document with images that have XMP metadata attached
 * 
 * @version  $Id: image_metadata.java,v 1.5 2008/07/08 13:00:07 stm Exp $
 */
public class image_metadata extends pcos_cookbook_example {
    
    /* This is where the data files are. Adjust as necessary. */
    private final static String SEARCH_PATH = "../input";

    public void example_code(pCOS p, String filename) throws pCOSException, Exception {
        /* Open the PDF document */
        int doc = p.open_document(filename, "");
        if (doc == -1)
            throw new Exception("Error: " + p.get_errmsg());
        
        System.out.println("File name: " + p.get_string(doc, "filename"));
        
        /*
         * Loop over all images in the document and check whether there is
         * a Metadata stream attached. If a Metadata stream is found,
         * the contents are dumped to a file.
         */
        int imagecount = (int) p.get_number(doc, "length:images");
        for (int i = 0; i < imagecount; i += 1) {
            String image_path = "images[" + i + "]/Metadata";
            
            String objtype = p.get_string(doc, "type:" + image_path);
            
            if (objtype.equals("stream")) {
                xmp_dump(p, doc, filename, i, image_path);
            }
        }
        
        p.close_document(doc);
    }
    
    private void xmp_dump(pCOS p, int doc, String filename,
                             int i, String image_path) throws Exception {
        /*
         * Strip relative pathname and ".pdf" suffix from input filename.
         */
        File path = new File(filename);
        String basename = strip_suffix(path.getName());
        
        /*
         * Create an output filename that has blanks and forward slashes
         * replaced with underscores
         */
        String output_filename =
            (basename + "_" + image_path + ".txt").replaceAll("[ \\/]", "_");
        
        System.out.println("Writing metadata of image " + i + " to file \""
            + output_filename + "\" ...");
        
        byte[] contents = p.get_stream(doc, "", image_path);
        
        PrintStream output_file = new PrintStream(new FileOutputStream(output_filename));
        output_file.write(contents);
        output_file.close();
    }

    private String strip_suffix(String filename) {
        int dot_index = filename.lastIndexOf('.');
        
        return dot_index > 0 ? filename.substring(0, dot_index) : filename;
    }

    public image_metadata(String[] argv, String readable_name,
        String search_path, String full_rcs_file_name, String revision) {
        super(argv, readable_name, search_path, full_rcs_file_name, revision);
    }

    public static void main(String argv[]) {
        image_metadata example = new image_metadata(argv,
            "Image XMP metadata", SEARCH_PATH,
            "$RCSfile: image_metadata.java,v $", "$Revision: 1.5 $");
        example.execute();
    }
}
