Project Report: fawkez

Packagesummary org.jcoderz.commons.taskdefs

org.jcoderz.commons.taskdefs.XsltBatchProcessor

LineHitsNoteSource
1  /*
2   * $Id: XsltBatchProcessor.java 1011 2008-06-16 17:57:36Z amandel $
3   *
4   * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5   *
6   * Redistribution and use in source and binary forms, with or without
7   * modification, are permitted provided that the following conditions are
8   * met:
9   *
10   *    * Redistributions of source code must retain the above copyright
11   *      notice, this list of conditions and the following disclaimer.
12   *    * Redistributions in binary form must reproduce the above
13   *      copyright notice, this list of conditions and the following
14   *      disclaimer in the documentation and/or other materials
15   *      provided with the distribution.
16   *    * Neither the name of the jCoderZ.org Project nor the names of
17   *      its contributors may be used to endorse or promote products
18   *      derived from this software without specific prior written
19   *      permission.
20   *
21   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   */
33  package org.jcoderz.commons.taskdefs;
34  
35  
36  import java.io.File;
37  import java.io.FileInputStream;
38  import java.io.FileOutputStream;
39  import java.io.IOException;
40  
41  import org.apache.tools.ant.BuildException;
42  import org.apache.tools.ant.DirectoryScanner;
43  import org.apache.tools.ant.Project;
44  import org.apache.tools.ant.Task;
45  import org.apache.tools.ant.types.FileSet;
46  import org.jcoderz.commons.util.IoUtil;
47  
48  
49  /**
50   * Ant task that performs XSL transformation for a set of input files.
51   *
52   * @author Michael Griffel
53   */
540 public class XsltBatchProcessor
55      extends Task
56  {
570     private String mStyleSheet = "default.xsl";
58  
590     private FileSet mFiles = new FileSet();
60  
610     private boolean mResolveExternalEntities = true;
62  
63      /** terminate ant build on error. */
640     private boolean mFailOnError = false;
65  
66      /**
67       * Set whether we should fail on an error.
68       *
69       * @param b Whether we should fail on an error.
70       */
71      public void setFailonerror (boolean b)
72      {
730         mFailOnError = b;
740     }
75  
76      /**
77       * Set the XSL Stylesheet to use.
78       *
79       * @param f The name of the XSL Stylesheet file.
80       * @see XsltBasedTask#getDefaultStyleSheet()
81       */
82      public void setXsl (String f)
83      {
840         mStyleSheet = f;
850     }
86  
87      /**
88       * XML files that are used as input documents for the
89       * transformation.
90       *
91       * @param fs fileset of XML files.
92       */
93      public void addFiles (FileSet fs)
94      {
950         mFiles = fs;
960     }
97  
98      /**
99       * {@inheritDoc}
100       */
101      public void execute ()
102          throws BuildException
103      {
104          try
105          {
1060             final XsltBasedTask xsltTask = new XsltBasedTask()
1070             {
108                  String getDefaultStyleSheet ()
109                  {
1100                     return mStyleSheet;
111                  }
112              };
1130             final Project myProject = getProject();
1140             final DirectoryScanner ds = mFiles.getDirectoryScanner(myProject);
1150             final String[] includedFiles = ds.getIncludedFiles();
1160             log("Transforming " + includedFiles.length + " files in directory "
117                  + ds.getBasedir());
1180             for (int i = 0; i < includedFiles.length; i++)
119              {
1200                 final String f = includedFiles[i];
1210                 final File orig = new File(ds.getBasedir(), f);
122                  final File out;
123                  try
124                  {
1250                     out = File.createTempFile("jcoderz", "tmp");
126                  }
1270                 catch (IOException e)
128                  {
1290                     throw new BuildException(
130                          "Failed to create temp file: " + e, e);
1310                 }
1320                 xsltTask.setProject(myProject);
1330                 xsltTask.setTaskName("xslt");
1340                 xsltTask.setIn(orig);
1350                 xsltTask.setOut(out);
1360                 xsltTask.setDestdir(myProject.getBaseDir());
1370                 xsltTask.setForce(true);
1380                 xsltTask.setFailonerror(mFailOnError);
1390                 xsltTask.setLogLevel(Project.MSG_VERBOSE);
1400                 xsltTask.resolveExternalEntities(mResolveExternalEntities);
1410                 log("Transforming file " + orig, Project.MSG_VERBOSE);
1420                 xsltTask.execute();
1430                 if (out.exists())
144                  {
1450                     if (!orig.delete())
146                      {
1470                         throw new BuildException("Failed to delete " + orig);
148                      }
1490                     if (!out.renameTo(orig))
150                      {
151                          // try copy && delete
152                          try
153                          {
1540                             safeMove(out, orig);
155                          }
1560                         catch (IOException e)
157                          {
1580                             throw new BuildException("Failed to move file "
159                                  + out, e);
1600                         }
161                      }
162                  }
163              }
164          }
1650         catch (BuildException e)
166          {
1670             if (mFailOnError)
168              {
1690                 throw e;
170              }
1710             log(e.getMessage(), Project.MSG_ERR);
1720         }
1730     }
174  
175      /**
176       * If set to <tt>false</tt>, external entities will not be
177       * resolved.
178       *
179       * @param b new value.
180       */
181      public void resolveExternalEntities (boolean b)
182      {
1830         mResolveExternalEntities = b;
1840     }
185  
186      private void safeMove (File source, File dest)
187          throws IOException
188      {
1890(1)        final FileInputStream in = new FileInputStream(source);
1900         final FileOutputStream out = new FileOutputStream(dest);
191          try
192          {
1930             IoUtil.copy(in, out);
1940             if (!source.delete())
195              {
1960                 throw new BuildException("Failed to delete " + source);
197              }
198          }
199          finally
200          {
2010             IoUtil.close(in);
2020             IoUtil.close(out);
2030         }
2040     }
205  }

Findings in this File

w (1) 189 : 0 Method org.jcoderz.commons.taskdefs.XsltBatchProcessor.safeMove(File, File) may fail to clean up stream or resource of type java.io.InputStream