You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-dev@xmlgraphics.apache.org by Jiří Mareš <Ji...@svt.cz> on 2006/02/16 13:19:50 UTC

The userconfig support in ANT task

Hi all,

I have developed the support for userconfig into the ANT tasks. It was very easy and I have been inspired by the 
org.apache.fop.cli.CommandLineOptions method createUserConfig.

I don't know how to send you the sources therefore they are included in this file.

The changes are only in class FOPTaskStarter in file org.apache.fop.tools.anttasks.Fop (I leave only the methods where 
the changes are and the changes is marked by comments):

class FOPTaskStarter {

     /**
      * @see org.apache.fop.apps.Starter#run()
      */
     public void run() throws FOPException {
         //Setup configuration
/** --- MY CHANGES ---- START --- */
         Configuration userConfig = null;
/** --- MY CHANGES ---- END --- */
         if (task.getUserconfig() != null) {
/** --- MY CHANGES ---- START --- */
             if (task.getUserconfig() != null) {
                XMLReader parser = createParser();
                DefaultConfigurationBuilder configBuilder = new DefaultConfigurationBuilder(parser);
                try {
                   userConfig = configBuilder.buildFromFile(task.getUserconfig());
                } catch (SAXException e) {
                   throw new FOPException(e);
                } catch (ConfigurationException e) {
                   throw new FOPException(e);
                } catch (IOException e) {
                   throw new FOPException(e);
                }
            }
/** --- MY CHANGES ---- END --- */
         }

         //Set base directory
         if (task.getBasedir() != null) {
             try {
                 this.baseURL = task.getBasedir().toURL().toExternalForm();
             } catch (MalformedURLException mfue) {
                 logger.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) {
                 logger.error("Error creating base URL from XSL-FO input file", mfue);
             }
         }

         task.log("Using base URL: " + baseURL, Project.MSG_DEBUG);

         String outputFormat = normalizeOutputFormat(task.getFormat());
         String newExtension = determineExtension(outputFormat);

         // actioncount = # of fofiles actually processed through FOP
         int actioncount = 0;
         // skippedcount = # of fofiles which haven't changed (force = "false")
         int skippedcount = 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 if "force" flag is set OR
                 // OR output file doesn't exist OR
                 // output file is older than input file
                 if (task.getForce() || !outf.exists()
                     || (task.getFofile().lastModified() > outf.lastModified() )) {
/** --- MY CHANGES ---- START --- */
                     render(task.getFofile(), outf, outputFormat, userConfig);
/** --- MY CHANGES ---- END --- */
                     actioncount++;
                 } else if (outf.exists()
                         && (task.getFofile().lastModified() <= outf.lastModified() )) {
                     skippedcount++;
                 }
             }
         }

         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 (task.getRelativebase()) {
                         this.baseURL = f.getParentFile().toURL().
                                          toExternalForm();
                     }
                     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 if "force" flag is set OR
                 // OR output file doesn't exist OR
                 // output file is older than input file
                 if (task.getForce() || !outf.exists()
                     || (f.lastModified() > outf.lastModified() )) {
/** --- MY CHANGES ---- START --- */
                     render(f, outf, outputFormat, userConfig);
/** --- MY CHANGES ---- END --- */
                     actioncount++;
                 } else if (outf.exists() && (f.lastModified() <= outf.lastModified() )) {
                     skippedcount++;
                 }
             }
         }

         if (actioncount + skippedcount == 0) {
             task.log("No files processed. No files were selected by the filesets "
                 + "and no fofile was set." , Project.MSG_WARN);
         } else if (skippedcount > 0) {
             task.log(skippedcount + " xslfo file(s) skipped (no change found"
                 + " since last generation; set force=\"true\" to override)."
                 , Project.MSG_INFO);
         }
     }

/** --- MY CHANGES ---- START --- */
     private void render(File foFile, File outFile,
                         String outputFormat, Configuration userConfig) throws FOPException {
/** --- MY CHANGES ---- START --- */
         InputHandler inputHandler = new InputHandler(foFile);

         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 {
             FOUserAgent userAgent = new FOUserAgent();
/** --- MY CHANGES ---- START --- */
             userAgent.setUserConfig(userConfig);
/** --- MY CHANGES ---- END --- */
             userAgent.setBaseURL(this.baseURL);
             org.apache.fop.apps.Fop fop = new org.apache.fop.apps.Fop(
                     outputFormat, userAgent);
             fop.setOutputStream(out);
             inputHandler.render(fop);
         } catch (Exception ex) {
             throw new BuildException(ex);
         } finally {
             try {
                 out.close();
             } catch (IOException ioe) {
                 logger.error("Error closing output file", ioe);
             }
         }
     }

/** --- MY CHANGES ---- START --- */
     /**
      * Creates <code>XMLReader</code> object using default
      * <code>SAXParserFactory</code>
      * @return the created <code>XMLReader</code>
      * @throws FOPException if the parser couldn't be created or configured for proper operation.
      */
     private XMLReader createParser() throws FOPException {
         try {
             SAXParserFactory factory = SAXParserFactory.newInstance();
             factory.setNamespaceAware(true);
             return factory.newSAXParser().getXMLReader();
         } catch (Exception e) {
             throw new FOPException("Couldn't create XMLReader", e);
         }
     }
/** --- MY CHANGES ---- END --- */

}

-- 
Jiří Mareš (mailto:Jiri.Mares@svt.cz)
ČSAD SVT Praha, s.r.o. (http://www.svt.cz)
Czech Republic

Re: The userconfig support in ANT task

Posted by Jeremias Maerki <de...@jeremias-maerki.ch>.
Hi Jirí

Thanks for that. I've added your changes to the codebase: 
http://svn.apache.org/viewcvs?rev=378508&view=rev

For such a small thing, we can let your contribution go through like
this. However, since there's a lot of manual work involved like this, we
request that you send the changes as a DIFF file (created through the
"svn diff" command) next time. The patch file should then be submitted
through Bugzilla. More info: 
http://xmlgraphics.apache.org/fop/dev/index.html#patches
http://xmlgraphics.apache.org/fop/dev/tools.html#patches

On 16.02.2006 13:19:50 Jirí Mareš wrote:
> 
> Hi all,
> 
> I have developed the support for userconfig into the ANT tasks. It was very easy and I have been inspired by the 
> org.apache.fop.cli.CommandLineOptions method createUserConfig.
> 
> I don't know how to send you the sources therefore they are included in this file.
> 
> The changes are only in class FOPTaskStarter in file org.apache.fop.tools.anttasks.Fop (I leave only the methods where 
> the changes are and the changes is marked by comments):

<snip what="pseudo-patch"/>

Thanks,
Jeremias Maerki