Project Report: fawkez

Packagesummary org.jcoderz.commons.taskdefs

org.jcoderz.commons.taskdefs.Formatter

LineHitsNoteSource
1  /*
2   * $Id: Formatter.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  import java.io.File;
36  import java.util.Locale;
37  
38  import org.apache.fop.tools.anttasks.Fop;
39  import org.jcoderz.commons.taskdefs.XtremeDocs.FormatterInfoData;
40  
41  
42  /**
43   * Abstract class that defines the interface of a DocBook renderer.
44   *
45   * @author Michael Griffel
46   */
47  public abstract class Formatter
48  {
49     private final FormatterInfoData mInfoData;
50  
51     private Formatter (FormatterInfoData i)
520    {
530       mInfoData = i;
540    }
55  
56     /**
57      * Factory method to create a concrete instance of a formatter.
58      * @param f the meta data object initialized by the ant task.
59      * @return a concrete formatter instance.
60      */
61     public static Formatter getInstance (FormatterInfoData f)
62     {
630       if (f.getType() == null)
64        {
650(1)         throw new IllegalArgumentException("formatter type cannot be null");
66        }
670       final String typeAsLowercase = f.getType().toLowerCase(Locale.US);
68        final Formatter result;
690(2)(3)      if (typeAsLowercase.equals("html"))
70        {
710          result = new HtmlFormatter(f);
72        }
730(4)(5)      else if (typeAsLowercase.equals("pdf"))
74        {
750          result = new PdfFormatter(f);
76        }
77        else
78        {
790          throw new NoSuchMethodError("Unsupported type " + f.getType());
80        }
810       return result;
82     }
83  
84     /**
85      * Transforms the given DocBook input file and writes the
86      * result to <tt>out</tt>.
87      * @param parent the parent task.
88      * @param in the input file.
89      * @param out the ouput file.
90      */
91     public abstract void transform (XtremeDocs parent, File in, File out);
92  
93     /**
94      * Returns the filename extension.
95      * For example, the DocBook to HTML formatter will return <tt>"html"</tt>.
96      * @return teh filename extension.
97      */
98     public abstract String getFileExtension ();
99  
100     /**
101      * Returns the formatter's meta data.
102      * This data structure is set by the ant task.
103      * @return the formatter's meta data.
104      */
105     public FormatterInfoData getInfoData ()
106     {
1070       return mInfoData;
108     }
109  
110     protected void executeSaxon (XtremeDocs parent, File in, final File out)
111     {
1120       final SaxonTask task = new SaxonTask();
1130       task.setProject(parent.getProject());
1140       task.setTaskName("saxon");
1150       task.setCss(getInfoData().getCascadingStyleSheet());
1160       task.setXsl(getInfoData().getStyleSheet());
1170       task.setIn(in);
1180       task.setOut(out);
1190       task.setDir(in.getParentFile());
1200       task.setFailonerror(parent.failOnError());
1210       task.setClasspath(parent.getClassPath());
1220       task.setHaveRenderX(haveRenderX(parent));
1230       task.execute();
1240    }
125  
126     protected boolean haveRenderX (XtremeDocs parent)
127     {
1280       return parent.getXepHome() != null && parent.getXepHome().isDirectory();
129     }
130  
131     /**
132      * Transforms DocBook to HTML.
133      *
134      * @author Michael Griffel
135      */
136     private static class HtmlFormatter
137           extends Formatter
138     {
139        HtmlFormatter (FormatterInfoData i)
140        {
1410          super(i);
1420       }
143  
144        /** {@inheritDoc} */
145        public void transform (XtremeDocs parent, File in, File out)
146        {
1470          executeSaxon(parent, in, out);
1480          parent.log("Transformed " + in + " successfully to " + out);
1490       }
150  
151        /** {@inheritDoc} */
152        public String getFileExtension ()
153        {
1540          return "html";
155        }
156     }
157  
158     /**
159      * Transforms DocBook to PDF.
160      *
161      * @author Michael Griffel
162      */
163     private static class PdfFormatter
164           extends Formatter
165     {
166  
167        /**
168         * Constructor.
169         * @param i formatter's meta data.
170         */
171        PdfFormatter (FormatterInfoData i)
172        {
1730          super(i);
1740       }
175  
176        /** {@inheritDoc} */
177        public void transform (XtremeDocs parent, File in, File out)
178        {
1790          final File tmp = new File(out.getParentFile(), out.getName() + ".fo");
1800          executeSaxon(parent, in, tmp);
181  
1820          if (haveRenderX(parent))
183           {
1840             executeXep(parent, in, out, tmp);
185           }
186           else
187           {
1880             executeFop(parent, in, out, tmp);
189           }
1900          parent.log("Transformed " + tmp + " successfully to " + out);
1910       }
192  
193        private void executeXep (XtremeDocs parent, File in, File out, File tmp)
194        {
1950          final XepTask xep = new XepTask();
1960          xep.setProject(parent.getProject());
1970          xep.setTaskName("xep");
1980          xep.setFo(tmp);
1990          xep.setOut(out);
2000          xep.setXephome(parent.getXepHome());
2010          xep.execute();
2020       }
203  
204        private void executeFop (XtremeDocs parent, File in, File out, File tmp)
205        {
2060          final Fop fop = new Fop();
2070          fop.setProject(parent.getProject());
2080          fop.setTaskName("fop");
2090          fop.setFofile(tmp);
2100          fop.setOutfile(out);
2110          fop.setFormat("application/pdf");
2120          fop.setMessagelevel("info");
2130          fop.setBasedir(in.getParentFile());
2140          fop.execute();
2150       }
216  
217        /** {@inheritDoc} */
218        public String getFileExtension ()
219        {
2200          return "pdf";
221        }
222  
223     }
224  
225  }

Findings in this File

i (1) 65 : 0 method org.jcoderz.commons.taskdefs.Formatter.getInstance(XtremeDocs$FormatterInfoData) throws exception with static message string
i (2) 69 : 0 method org.jcoderz.commons.taskdefs.Formatter.getInstance(XtremeDocs$FormatterInfoData) makes literal string comparisons passing the literal as an argument
c (3) 69 : 11 Position literals first in String comparisons
i (4) 73 : 0 method org.jcoderz.commons.taskdefs.Formatter.getInstance(XtremeDocs$FormatterInfoData) makes literal string comparisons passing the literal as an argument
c (5) 73 : 16 Position literals first in String comparisons