123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503 |
- /*
- * $Id: Fop.java,v 1.24 2003/03/07 10:09:30 jeremias Exp $
- * ============================================================================
- * The Apache Software License, Version 1.1
- * ============================================================================
- *
- * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modifica-
- * tion, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * 3. The end-user documentation included with the redistribution, if any, must
- * include the following acknowledgment: "This product includes software
- * developed by the Apache Software Foundation (http://www.apache.org/)."
- * Alternately, this acknowledgment may appear in the software itself, if
- * and wherever such third-party acknowledgments normally appear.
- *
- * 4. The names "FOP" and "Apache Software Foundation" must not be used to
- * endorse or promote products derived from this software without prior
- * written permission. For written permission, please contact
- * apache@apache.org.
- *
- * 5. Products derived from this software may not be called "Apache", nor may
- * "Apache" appear in their name, without prior written permission of the
- * Apache Software Foundation.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
- * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- * ============================================================================
- *
- * This software consists of voluntary contributions made by many individuals
- * on behalf of the Apache Software Foundation and was originally created by
- * James Tauber <jtauber@jtauber.com>. For more information on the Apache
- * Software Foundation, please see <http://www.apache.org/>.
- */
- package org.apache.fop.tools.anttasks;
-
- // Ant
- import org.apache.tools.ant.BuildException;
- import org.apache.tools.ant.DirectoryScanner;
- import org.apache.tools.ant.Project;
- import org.apache.tools.ant.Task;
- import org.apache.tools.ant.types.FileSet;
- import org.apache.tools.ant.util.GlobPatternMapper;
-
- // SAX
- import org.xml.sax.XMLReader;
-
- // Java
- import java.io.File;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.MalformedURLException;
- import java.util.List;
-
- // FOP
- import org.apache.fop.apps.Starter;
- import org.apache.fop.apps.InputHandler;
- import org.apache.fop.apps.FOInputHandler;
- import org.apache.fop.apps.Session;
- import org.apache.fop.apps.FOPException;
- import org.apache.fop.fo.FOUserAgent;
-
- // Avalon
- import org.apache.avalon.framework.logger.ConsoleLogger;
- import org.apache.avalon.framework.logger.Logger;
-
- /**
- * Wrapper for FOP which allows it to be accessed from within an Ant task.
- * Accepts the inputs:
- * <ul>
- * <li>fofile -> formatting objects file to be transformed</li>
- * <li>format -> MIME type of the format to generate ex. "application/pdf"</li>
- * <li>outfile -> output filename</li>
- * <li>baseDir -> directory to work from</li>
- * <li>userconfig -> file with user configuration (same as the "-c" command
- * line option)</li>
- * <li>messagelevel -> (error | warn | info | verbose | debug) level to output
- * non-error messages</li>
- * <li>logFiles -> Controls whether the names of the files that are processed
- * are logged or not</li>
- * </ul>
- */
- public class Fop extends Task {
-
- private File foFile;
- private List filesets = new java.util.ArrayList();
- private File outFile;
- private File outDir;
- private String format; //MIME type
- private File baseDir;
- private File userConfig;
- private int messageType = Project.MSG_VERBOSE;
- private boolean logFiles = true;
-
- /**
- * Sets the filename for the userconfig.xml.
- * @param userConfig Configuration to use
- */
- public void setUserconfig(File userConfig) {
- this.userConfig = userConfig;
- }
-
- /**
- * Returns the file for the userconfig.xml.
- * @return the userconfig.xml file
- */
- public File getUserconfig() {
- return this.userConfig;
- }
-
- /**
- * Sets the input XSL-FO file.
- * @param foFile input XSL-FO file
- */
- public void setFofile(File foFile) {
- this.foFile = foFile;
- }
-
- /**
- * Gets the input XSL-FO file.
- * @return input XSL-FO file
- */
- public File getFofile() {
- return foFile;
- }
-
- /**
- * Adds a set of XSL-FO files (nested fileset attribute).
- * @param set a fileset
- */
- public void addFileset(FileSet set) {
- filesets.add(set);
- }
-
- /**
- * Returns the current list of filesets.
- * @return the filesets
- */
- public List getFilesets() {
- return this.filesets;
- }
-
- /**
- * Sets the output file.
- * @param outFile File to output to
- */
- public void setOutfile(File outFile) {
- this.outFile = outFile;
- }
-
- /**
- * Gets the output file.
- * @return the output file
- */
- public File getOutfile() {
- return this.outFile;
- }
-
- /**
- * Sets the output directory.
- * @param outDir Directory to output to
- */
- public void setOutdir(File outDir) {
- this.outDir = outDir;
- }
-
- /**
- * Gets the output directory.
- * @return the output directory
- */
- public File getOutdir() {
- return this.outDir;
- }
-
- /**
- * Sets output format (MIME type).
- * @param format the output format
- */
- public void setFormat(String format) {
- this.format = format;
- }
-
- /**
- * Gets the output format (MIME type).
- * @return the output format
- */
- public String getFormat() {
- return this.format;
- }
-
- /**
- * Sets the message level to be used while processing.
- * @param messageLevel (error | warn| info | verbose | debug)
- */
- public void setMessagelevel(String messageLevel) {
- if (messageLevel.equalsIgnoreCase("info")) {
- messageType = Project.MSG_INFO;
- } else if (messageLevel.equalsIgnoreCase("verbose")) {
- messageType = Project.MSG_VERBOSE;
- } else if (messageLevel.equalsIgnoreCase("debug")) {
- messageType = Project.MSG_DEBUG;
- } else if (messageLevel.equalsIgnoreCase("err")
- || messageLevel.equalsIgnoreCase("error")) {
- messageType = Project.MSG_ERR;
- } else if (messageLevel.equalsIgnoreCase("warn")) {
- messageType = Project.MSG_WARN;
- } else {
- log("messagelevel set to unknown value \"" + messageLevel
- + "\"", Project.MSG_ERR);
- throw new BuildException("unknown messagelevel");
- }
- }
-
- /**
- * Returns the message type corresponding to Project.MSG_*
- * representing the current message level.
- * @see org.apache.tools.ant.Project
- */
- public int getMessageType() {
- return messageType;
- }
-
- /**
- * Sets the base directory; currently ignored.
- * @param baseDir File to use as a working directory
- */
- public void setBasedir(File baseDir) {
- this.baseDir = baseDir;
- }
-
- /**
- * Gets the base directory.
- * @return the base directory
- */
- public File getBasedir() {
- return (baseDir != null) ? baseDir : getProject().resolveFile(".");
- }
-
- /**
- * Controls whether the filenames of the files that are processed are logged
- * or not.
- * @param logFiles True if the feature should be enabled
- */
- public void setLogFiles(boolean logFiles) {
- this.logFiles = logFiles;
- }
-
- /**
- * Returns True if the filename of each file processed should be logged.
- * @return True if the filenames should be logged.
- */
- public boolean getLogFiles() {
- return this.logFiles;
- }
-
- /**
- * @see org.apache.tools.ant.Task#execute()
- */
- public void execute() throws BuildException {
- int logLevel = ConsoleLogger.LEVEL_INFO;
- switch (getMessageType()) {
- case Project.MSG_DEBUG : logLevel = ConsoleLogger.LEVEL_DEBUG; break;
- case Project.MSG_INFO : logLevel = ConsoleLogger.LEVEL_INFO; break;
- case Project.MSG_WARN : logLevel = ConsoleLogger.LEVEL_WARN; break;
- case Project.MSG_ERR : logLevel = ConsoleLogger.LEVEL_ERROR; break;
- case Project.MSG_VERBOSE: logLevel = ConsoleLogger.LEVEL_DEBUG; break;
- }
- Logger log = new ConsoleLogger(logLevel);
- try {
- Starter starter = new FOPTaskStarter(this);
- starter.enableLogging(log);
- starter.run();
- } catch (FOPException ex) {
- throw new BuildException(ex);
- }
-
- }
-
- }
-
- class FOPTaskStarter extends Starter {
-
- private Fop task;
- private String baseURL = null;
-
- FOPTaskStarter(Fop task) throws FOPException {
- this.task = task;
- }
-
- private int determineRenderer(String format) {
- if ((format == null)
- || format.equalsIgnoreCase("application/pdf")
- || format.equalsIgnoreCase("pdf")) {
- return Session.RENDER_PDF;
- } else if (format.equalsIgnoreCase("application/postscript")
- || format.equalsIgnoreCase("ps")) {
- return Session.RENDER_PS;
- } else if (format.equalsIgnoreCase("application/vnd.mif")
- || format.equalsIgnoreCase("mif")) {
- return Session.RENDER_MIF;
- } else if (format.equalsIgnoreCase("application/msword")
- || format.equalsIgnoreCase("application/rtf")
- || format.equalsIgnoreCase("rtf")) {
- return Session.RENDER_RTF;
- } else if (format.equalsIgnoreCase("application/vnd.hp-PCL")
- || format.equalsIgnoreCase("pcl")) {
- return Session.RENDER_PCL;
- } else if (format.equalsIgnoreCase("text/plain")
- || format.equalsIgnoreCase("txt")) {
- return Session.RENDER_TXT;
- } else if (format.equalsIgnoreCase("text/xml")
- || format.equalsIgnoreCase("at")
- || format.equalsIgnoreCase("xml")) {
- return Session.RENDER_XML;
- } else {
- String err = "Couldn't determine renderer to use: " + format;
- throw new BuildException(err);
- }
- }
-
- private String determineExtension(int renderer) {
- switch (renderer) {
- case Session.RENDER_PDF:
- return ".pdf";
- case Session.RENDER_PS:
- return ".ps";
- case Session.RENDER_MIF:
- return ".mif";
- case Session.RENDER_RTF:
- return ".rtf";
- case Session.RENDER_PCL:
- return ".pcl";
- case Session.RENDER_TXT:
- return ".txt";
- case Session.RENDER_XML:
- return ".xml";
- default:
- String err = "Unknown renderer: " + renderer;
- throw new BuildException(err);
- }
- }
-
- private File replaceExtension(File file, String expectedExt,
- String newExt) {
- String name = file.getName();
- if (name.toLowerCase().endsWith(expectedExt)) {
- name = name.substring(0, name.length() - expectedExt.length());
- }
- name = name.concat(newExt);
- return new File(file.getParentFile(), name);
- }
-
- /**
- * @see org.apache.fop.apps.Starter#run()
- */
- public void run() throws FOPException {
- //Setup configuration
- if (task.getUserconfig() != null) {
- /**@todo implement me */
- }
-
- //Set base directory
- if (task.getBasedir() != null) {
- try {
- this.baseURL = task.getBasedir().toURL().toExternalForm();
- } catch (MalformedURLException mfue) {
- getLogger().error("Error creating base URL from base directory", mfue);
- }
- } else {
- try {
- if (task.getFofile() != null) {
- this.baseURL = task.getFofile().getParentFile().toURL().
- toExternalForm();
- }
- } catch (MalformedURLException mfue) {
- getLogger().error("Error creating base URL from XSL-FO input file", mfue);
- }
- }
-
- task.log("Using base URL: " + baseURL, Project.MSG_DEBUG);
-
- int rint = determineRenderer(task.getFormat());
- String newExtension = determineExtension(rint);
-
- int actioncount = 0;
-
- // deal with single source file
- if (task.getFofile() != null) {
- if (task.getFofile().exists()) {
- File outf = task.getOutfile();
- if (outf == null) {
- throw new BuildException("outfile is required when fofile is used");
- }
- if (task.getOutdir() != null) {
- outf = new File(task.getOutdir(), outf.getName());
- }
- render(task.getFofile(), outf, rint);
- actioncount++;
- }
- }
-
- GlobPatternMapper mapper = new GlobPatternMapper();
- mapper.setFrom("*.fo");
- mapper.setTo("*" + newExtension);
-
- // deal with the filesets
- for (int i = 0; i < task.getFilesets().size(); i++) {
- FileSet fs = (FileSet) task.getFilesets().get(i);
- DirectoryScanner ds = fs.getDirectoryScanner(task.getProject());
- String[] files = ds.getIncludedFiles();
-
- for (int j = 0; j < files.length; j++) {
- File f = new File(fs.getDir(task.getProject()), files[j]);
-
- File outf = null;
- if (task.getOutdir() != null && files[j].endsWith(".fo")) {
- String[] sa = mapper.mapFileName(files[j]);
- outf = new File(task.getOutdir(), sa[0]);
- } else {
- outf = replaceExtension(f, ".fo", newExtension);
- if (task.getOutdir() != null) {
- outf = new File(task.getOutdir(), outf.getName());
- }
- }
-
- try {
- if (this.baseURL == null) {
- this.baseURL = fs.getDir(task.getProject()).toURL().
- toExternalForm();
- }
-
- } catch (Exception e) {
- task.log("Error setting base URL", Project.MSG_DEBUG);
- }
-
- render(f, outf, rint);
- actioncount++;
- }
- }
-
- if (actioncount == 0) {
- task.log("No files processed. No files were selected by the filesets "
- + "and no fofile was set." , Project.MSG_WARN);
- }
- }
-
- private void render(File foFile, File outFile,
- int renderer) throws FOPException {
- InputHandler inputHandler = new FOInputHandler(foFile);
- XMLReader parser = inputHandler.getParser();
- setParserFeatures(parser);
-
- OutputStream out = null;
- try {
- out = new java.io.FileOutputStream(outFile);
- } catch (Exception ex) {
- throw new BuildException("Failed to open " + outFile, ex);
- }
-
- if (task.getLogFiles()) {
- task.log(foFile + " -> " + outFile, Project.MSG_INFO);
- }
-
- try {
- Session session = new Session();
- setupLogger(session);
- session.initialize();
- FOUserAgent userAgent = new FOUserAgent();
- userAgent.setBaseURL(this.baseURL);
- userAgent.enableLogging(getLogger());
- session.setUserAgent(userAgent);
- session.setRenderer(renderer);
- session.setOutputStream(out);
- session.render(parser, inputHandler.getInputSource());
- } catch (Exception ex) {
- throw new BuildException(ex);
- } finally {
- try {
- out.close();
- } catch (IOException ioe) {
- getLogger().error("Error closing output file", ioe);
- }
- }
- }
-
- }
|