root/trunk/src/java/org/jcoderz/commons/taskdefs/Formatter.java

Revision 1011, 6.4 kB (checked in by amandel, 4 years ago)

Aligned svn keyword settings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
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 */
33package org.jcoderz.commons.taskdefs;
34
35import java.io.File;
36import java.util.Locale;
37
38import org.apache.fop.tools.anttasks.Fop;
39import 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 */
47public abstract class Formatter
48{
49   private final FormatterInfoData mInfoData;
50
51   private Formatter (FormatterInfoData i)
52   {
53      mInfoData = i;
54   }
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   {
63      if (f.getType() == null)
64      {
65         throw new IllegalArgumentException("formatter type cannot be null");
66      }
67      final String typeAsLowercase = f.getType().toLowerCase(Locale.US);
68      final Formatter result;
69      if (typeAsLowercase.equals("html"))
70      {
71         result = new HtmlFormatter(f);
72      }
73      else if (typeAsLowercase.equals("pdf"))
74      {
75         result = new PdfFormatter(f);
76      }
77      else
78      {
79         throw new NoSuchMethodError("Unsupported type " + f.getType());
80      }
81      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   {
107      return mInfoData;
108   }
109
110   protected void executeSaxon (XtremeDocs parent, File in, final File out)
111   {
112      final SaxonTask task = new SaxonTask();
113      task.setProject(parent.getProject());
114      task.setTaskName("saxon");
115      task.setCss(getInfoData().getCascadingStyleSheet());
116      task.setXsl(getInfoData().getStyleSheet());
117      task.setIn(in);
118      task.setOut(out);
119      task.setDir(in.getParentFile());
120      task.setFailonerror(parent.failOnError());
121      task.setClasspath(parent.getClassPath());
122      task.setHaveRenderX(haveRenderX(parent));
123      task.execute();
124   }
125
126   protected boolean haveRenderX (XtremeDocs parent)
127   {
128      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      {
141         super(i);
142      }
143
144      /** {@inheritDoc} */
145      public void transform (XtremeDocs parent, File in, File out)
146      {
147         executeSaxon(parent, in, out);
148         parent.log("Transformed " + in + " successfully to " + out);
149      }
150
151      /** {@inheritDoc} */
152      public String getFileExtension ()
153      {
154         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      {
173         super(i);
174      }
175
176      /** {@inheritDoc} */
177      public void transform (XtremeDocs parent, File in, File out)
178      {
179         final File tmp = new File(out.getParentFile(), out.getName() + ".fo");
180         executeSaxon(parent, in, tmp);
181
182         if (haveRenderX(parent))
183         {
184            executeXep(parent, in, out, tmp);
185         }
186         else
187         {
188            executeFop(parent, in, out, tmp);
189         }
190         parent.log("Transformed " + tmp + " successfully to " + out);
191      }
192
193      private void executeXep (XtremeDocs parent, File in, File out, File tmp)
194      {
195         final XepTask xep = new XepTask();
196         xep.setProject(parent.getProject());
197         xep.setTaskName("xep");
198         xep.setFo(tmp);
199         xep.setOut(out);
200         xep.setXephome(parent.getXepHome());
201         xep.execute();
202      }
203
204      private void executeFop (XtremeDocs parent, File in, File out, File tmp)
205      {
206         final Fop fop = new Fop();
207         fop.setProject(parent.getProject());
208         fop.setTaskName("fop");
209         fop.setFofile(tmp);
210         fop.setOutfile(out);
211         fop.setFormat("application/pdf");
212         fop.setMessagelevel("info");
213         fop.setBasedir(in.getParentFile());
214         fop.execute();
215      }
216
217      /** {@inheritDoc} */
218      public String getFileExtension ()
219      {
220         return "pdf";
221      }
222
223   }
224
225}
Note: See TracBrowser for help on using the browser.