/* * Wrapper class for running the IpCOS Cookbook examples. * * This abstract class provides a wrapper to run pCOS Cookbook example code over * one or multiple files. The user of this class must implement the * example_code function. * * The wrapper class is written without an explicit reference to a specific * PDFlib GmbH product. Instead it uses the IpCOS Java interface that * exposes the pCOS-related functions. * * To create the actual object to be used for calling pCOS functions Java * reflection is used. This makes it possible to compile the pCOS Cookbook * without tying it to a specific PDFlib product. This is not recommended * for production code. In production code always the concrete object * should be created by normal Java means. */ package com.pdflib.cookbook.pcos; import com.pdflib.IpCOS; public abstract class pcos_cookbook_example { private static final int HEADER_LINE_LENGTH = 65; private String[] argv; private String readable_name; private String search_path; /** * Constructor taking data for banner, search path and usage. * * @param argv * array with input file names * @param readable_name * one or two word human readable description of example * @param search_path * this directory will be passed to pCOS as search path */ public pcos_cookbook_example(String[] argv, String readable_name, String search_path) { this.argv = argv; this.readable_name = readable_name; this.search_path = search_path; } /** * Execute the wrapper that calls example_code for each file. * * This method is intended to be called from the derived class' * main routine. It will check the args array and * prints a usage message if it is empty. * * Otherwise it will create a IpCOS object, open each filename * argument to obtain a document handle and call the * example_code routine for each document handle. * * If any document provokes an exception the * execute function will terminate and the remaining documents * will not be processed. */ protected void execute() { String class_name = getClass().getSimpleName(); if (argv.length == 0) { System.err.println("usage: " + class_name + " ..."); return; } String banner = readable_name + " (" + class_name + ".java): "; System.out.print(banner); int dash_length = HEADER_LINE_LENGTH - banner.length(); if (dash_length > 0) { StringBuffer buffer = new StringBuffer(dash_length); for (int i = 0; i < dash_length; i += 1) { buffer.append('-'); } System.out.print(buffer.toString()); } System.out.println(); IpCOS p = null; // PDFlib product classes that have the IpCOS interface String[] pcos_classes = new String[] { "com.pdflib.pdflib", "com.pdflib.TET", "com.pdflib.plop" }; try { int i, used_class_index; for (used_class_index = i = 0; p == null && i < pcos_classes.length; i += 1) { try { Class c = Class.forName(pcos_classes[i]); p = (IpCOS) c.getDeclaredConstructor().newInstance(); // Remember index for output below used_class_index = i; } catch (ClassNotFoundException class_not_found) { // continue search } } if (p != null) { p.set_option("SearchPath=" + search_path); boolean class_info_printed = false; for (i = 0; i < argv.length; i += 1) { int doc = p.pcos_open_document(argv[i], "requiredmode=minimum"); if (doc == -1) throw new Exception("Error: " + p.get_errmsg()); if (!class_info_printed) { /* Print once the class used and the pCOS interface number */ int pcosinterface = (int) p.pcos_get_number(0, "pcosinterface"); System.out.println("Using class '" + pcos_classes[used_class_index] + "' for running Cookbook example (pCOS Interface " + pcosinterface + ")"); class_info_printed = true; } example_code(p, doc); p.pcos_close_document(doc, ""); } } else { System.err.println("No PDFlib product found for calling pCOS functions!"); System.err.println("Please add the JAR file of one of the following PDFlib products to the classpath:"); System.err.println("PDFlib, TET, PLOP"); } } catch (Exception e) { System.err.println("Error: " + e.getMessage()); } finally { if (p != null) { p.delete(); } } } /** * The function that executes the actual Cookbook example code. * * The wrapper calls this method once for each file provided on the command * line. The wrapper passes a IpCOS object created in the * execute function and a valid document handle. * * @param p * a valid IpCOS object * @param doc * document handle of the current input document * @throws Exception * the example code throws an exeption if it detects an error */ public abstract void example_code(IpCOS p, int doc) throws Exception; }