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

Revision 1307, 35.8 kB (checked in by amandel, 3 years ago)

Small code style findings cleanup to test report diff.

  • 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
35
36import java.io.File;
37import java.io.FileInputStream;
38import java.io.FileOutputStream;
39import java.io.FilenameFilter;
40import java.io.IOException;
41import java.io.OutputStream;
42import java.util.ArrayList;
43import java.util.Iterator;
44import java.util.List;
45
46import javax.xml.transform.Transformer;
47
48import org.apache.batik.transcoder.TranscoderException;
49import org.apache.batik.transcoder.TranscoderInput;
50import org.apache.batik.transcoder.TranscoderOutput;
51import org.apache.batik.transcoder.XMLAbstractTranscoder;
52import org.apache.batik.transcoder.image.PNGTranscoder;
53import org.apache.tools.ant.BuildException;
54import org.apache.tools.ant.Project;
55import org.apache.tools.ant.Task;
56import org.apache.tools.ant.types.FileSet;
57import org.apache.tools.ant.types.Path;
58import org.jcoderz.commons.util.Constants;
59import org.jcoderz.commons.util.IoUtil;
60import org.jcoderz.commons.util.StringUtil;
61
62
63/**
64 * Xtreme Documentation Ant task.
65 *
66 * @author Michael Griffel
67 */
68public class XtremeDocs
69    extends Task
70{
71    private static final String FORMAT_PDF = "PDF";
72
73    private static final String FORMAT_HTML = "HTML";
74
75    private static final String FORMAT_ALL = "ALL";
76
77    private static final String FORMAT_NONE = "NONE";
78
79    private static final String TYPE_QUALITY_REPORT = "Quality-Report";
80   
81    private static final String TYPE_KPI_STATS = "KPI-Stats";
82   
83    private static final String TYPE_KPI_REPORT = "KPI-Report";
84
85    private static final String TYPE_RELEASE_NOTES = "Release-Notes";
86
87    private static final String TYPE_TEST_SPEC = "TestSpec";
88
89    private static final String TYPE_USE_CASE = "UseCase";
90
91    private static final String TYPE_SAD = "SAD";
92
93    private static final String IMAGE_DIR = "images";
94
95    private static final String APIDOC_DIR = "apidoc";
96
97    private static final String DEFAULT_COMPANY_NAME = "jCoderZ.org";
98
99    private static final String DEFAULT_COMPANY_LOGO = "jcoderz-org";
100
101    private static final boolean DEFAULT_VALIDATION_ONLY_FLAG = false;
102
103    /** The output directory. */
104    private File mOutDir;
105
106    /** XEP home directory. */
107    private File mXepHome;
108
109    /** The input file. */
110    private File mInFile;
111
112    /** terminate ant build on error. */
113    private boolean mFailOnError;
114
115    /** Type of document. (SAD|UseCase) */
116    private String mType;
117
118    /** Format of document. (NONE|HTML|PDF|ALL) */
119    private String mFormat;
120
121    private String mTypeLowerCase;
122
123    private final Path mDocletPath = new Path(getProject());
124
125    private final Path mClassPath = new Path(getProject());
126
127    private final List mFormatters = new ArrayList();
128
129    /** Source path - list of SourceDirectory. */
130    private final List mSources = new ArrayList();
131
132    /** Cruise Control label */
133    private String mCcLabel;
134
135    /** company name */
136    private String mCompanyName = DEFAULT_COMPANY_NAME;
137
138    /** company logo without suffix */
139    private String mCompanyLogo = DEFAULT_COMPANY_LOGO;
140
141    /** flag for execution of validation tasks only */
142    private boolean mValidationOnly = DEFAULT_VALIDATION_ONLY_FLAG;
143
144    /** List of Variables set as Properties in the Transformer context. */
145    private final List mTransformerProperties = new ArrayList();
146
147    private HibernateInfoData mHibernateInfoData;
148
149    /**
150     * Add the given property to be sent to the transformer.
151     *
152     * @param var the property to be sent to the transformer.
153     */
154    public void addParam (org.apache.tools.ant.types.Environment.Variable var)
155    {
156        mTransformerProperties.add(var);
157    }
158
159    void setXdocTransformerParams (Transformer transformer)
160    {
161        // TODO: Implement this in the corresponding first pass style
162        // sheets
163        // or remove this!
164        transformer.setParameter("basedir", getProject().getBaseDir()
165            .toString());
166        transformer.setParameter("cclabel", mCcLabel);
167        transformer.setParameter("user", System.getProperty("user.name"));
168        transformer.setParameter("companyname", mCompanyName);
169        transformer.setParameter("companylogo", mCompanyLogo);
170        final Iterator i = mTransformerProperties.iterator();
171        while (i.hasNext())
172        {
173            final org.apache.tools.ant.types.Environment.Variable var = (org.apache.tools.ant.types.Environment.Variable) i
174                .next();
175            transformer.setParameter(var.getKey(), var.getValue());
176        }
177    }
178
179    /**
180     * Sets the XML input file that contains the document.
181     *
182     * @param f the XML input file (log message info).
183     */
184    public void setIn (File f)
185    {
186        mInFile = f;
187    }
188
189    /**
190     * Set the destination directory into which the result files should
191     * be copied to. This parameter is required.
192     *
193     * @param dir the name of the destination directory.
194     */
195    public void setOut (File dir)
196    {
197        mOutDir = dir;
198    }
199
200    /**
201     * Set the XEP home directory.
202     *
203     * @param dir the name of the XEP home directory.
204     */
205    public void setXephome (File dir)
206    {
207        mXepHome = dir;
208    }
209
210    /**
211     * Set the document type.
212     *
213     * @param type the document type.
214     */
215    public void setType (String type)
216    {
217        mType = type;
218        mTypeLowerCase = type.toLowerCase(Constants.SYSTEM_LOCALE);
219    }
220
221    /**
222     * Set the document format.
223     *
224     * @param format the document format.
225     */
226    public void setFormat (String format)
227    {
228        mFormat = format;
229    }
230
231    /**
232     * Set the document type.
233     *
234     * @param label the cruise control label.
235     */
236    public void setCclabel (String label)
237    {
238        mCcLabel = label;
239    }
240
241    /**
242     * Set the name of the company or organisation.
243     *
244     * @param companyName The mCompanyName to set.
245     */
246    public void setCompanyName (String companyName)
247    {
248        mCompanyName = companyName;
249    }
250
251    /**
252     * Set the flag, whether only validation should be executed or not.
253     *
254     * @param validationOnly The mValidationOnly to set.
255     */
256    public void setValidationOnly (boolean validationOnly)
257    {
258        mValidationOnly = validationOnly;
259    }
260
261    /**
262     * Set the name of the company logo without suffix.
263     *
264     * @param companyLogo The mCompanyLogo to set.
265     */
266    public void setCompanyLogo (String companyLogo)
267    {
268        mCompanyLogo = companyLogo;
269    }
270
271    /**
272     * Set whether we should fail on an error.
273     *
274     * @param b Whether we should fail on an error.
275     */
276    public void setFailonerror (boolean b)
277    {
278        mFailOnError = b;
279    }
280
281    /**
282     * Additional path that is used to find the javadoc doclets.
283     *
284     * @return a path.
285     */
286    public Path createDocletpath ()
287    {
288        return mDocletPath;
289    }
290
291    /**
292     * The classpath that is used to find the classes for the DocBook
293     * formatters.
294     *
295     * @return a path.
296     */
297    public Path createClasspath ()
298    {
299        return mClassPath;
300    }
301
302    /**
303     * Creates a new FormatterInfoData object used as nested element to
304     * describe the DocBook formatters.
305     *
306     * @return a new FormatterInfoData object.
307     */
308    public FormatterInfoData createFormatter ()
309    {
310        final FormatterInfoData result = FormatterInfoData.create();
311        mFormatters.add(result);
312        return result;
313    }
314
315    /**
316     * Creates a new HibernateInfoData object used as nested element to
317     * describe the Hibernate Generator options.
318     *
319     * @return a new FormatterInfoData object.
320     */
321    public HibernateInfoData createHibernate ()
322    {
323        final HibernateInfoData hibernateInfoData = HibernateInfoData.create();
324        mHibernateInfoData = hibernateInfoData;
325        return hibernateInfoData;
326    }
327
328    /**
329     * Returns the classpath element.
330     *
331     * @return the classpath element.
332     */
333    public Path getClassPath ()
334    {
335        return mClassPath;
336    }
337
338    /**
339     * Returns <tt>true</tt> if this task (or subtasks) should fail on
340     * any error; <tt>false</tt> otherwise.
341     *
342     * @return <tt>true</tt> if this task (or subtasks) should fail on
343     *         any error; <tt>false</tt> otherwise.
344     */
345    public boolean failOnError ()
346    {
347        return mFailOnError;
348    }
349
350    /**
351     * Set the source path to be used for this task run.
352     *
353     * @param src an Ant FileSet object containing the compilation
354     *        source path.
355     */
356    public void addSrc (SourceDirectory src)
357    {
358        mSources.add(src);
359    }
360
361    /**
362     * Execute this task.
363     *
364     * @throws BuildException An building exception occurred.
365     */
366    public void execute ()
367        throws BuildException
368    {
369        try
370        {
371            checkAttributes();
372            log("Generating documentation into directory " + mOutDir);
373            final File imageDir = new File(mOutDir, IMAGE_DIR);
374            // convertPackageHtml2DocBook();
375            final File filePassOne = transformPassOne(mInFile);
376            if (TYPE_SAD.equals(mType))
377            {
378                generateApiDocs(filePassOne);
379                generateSadDiagrams(filePassOne);
380            }
381            else if (TYPE_USE_CASE.equals(mType))
382            {
383                if (!mValidationOnly)
384                {
385                    generateUseCaseDiagrams(filePassOne, imageDir);
386                    //exportToXmi(filePassOne, imageDir);
387                    AntTaskUtil.renderDotFiles(this, imageDir, mFailOnError);
388                    exportToHbCfg(filePassOne);
389                    exportToHbm(filePassOne);
390                }
391            }
392            else if (TYPE_KPI_STATS.equals(mType))
393            {
394                final File kpiFile = transformPassTwo(filePassOne);
395                File newFile = new File(AntTaskUtil.stripFileExtension(AntTaskUtil
396                    .stripFileExtension(kpiFile.getAbsolutePath())));
397                kpiFile.renameTo(newFile);
398            }
399            else if (TYPE_KPI_REPORT.equals(mType))
400            {
401                generateKeyPerformanceDiagrams(filePassOne, imageDir);
402                AntTaskUtil.renderGnuplotFiles(this, imageDir, mFailOnError);
403            }
404            else if (TYPE_TEST_SPEC.equals(mType))
405            {
406                // Nothing to do
407            }
408            else if (TYPE_QUALITY_REPORT.equals(mType))
409            {
410                // Nothing to do
411            }
412            else if (TYPE_RELEASE_NOTES.equals(mType))
413            {
414                // Nothing to do
415            }
416            else
417            {
418                throw new RuntimeException("Unsupported type " + mType);
419            }
420           
421            if (TYPE_KPI_STATS.equals(mType))
422            {
423                // do nothing
424            }
425            else
426            {
427                if (!mValidationOnly)
428                {
429                    if (isOutputEnabled(FORMAT_HTML))
430                    {
431                        rasterizeSvgFiles(imageDir);
432                    }
433                    if (isOutputEnabled(FORMAT_PDF))
434                    {
435                        scaleSvgImages(imageDir);
436                    }
437                }
438            }
439           
440            if (TYPE_TEST_SPEC.equals(mType))
441            {
442                renderDocbookFilesFromPassOne(filePassOne);
443            }
444            else if (TYPE_KPI_STATS.equals(mType))
445            {
446                // do nothing
447            }
448            else if (TYPE_KPI_REPORT.equals(mType))
449            {
450                final File docBookFile = transformPassTwo(filePassOne);
451                // rendering is placed here, thus PassTwo will also generate diagrams
452                AntTaskUtil.renderGnuplotFiles(this, imageDir, mFailOnError);
453                renderDocBook(docBookFile, mInFile);
454            }
455            else
456            {
457                final File docBookFile = transformPassTwo(filePassOne);
458                renderDocBook(docBookFile, mInFile);
459            }
460        }
461        catch (BuildException e)
462        {
463            if (mFailOnError)
464            {
465                throw e;
466            }
467            log(e.getMessage(), Project.MSG_ERR);
468        }
469    }
470
471    private boolean isOutputEnabled (String format)
472    {
473        final boolean result;
474        if (StringUtil.isEmptyOrNull(mFormat)
475            || mFormat.equals(FORMAT_ALL) || mFormat.equals(format))
476        {
477            result = true;
478        }
479        else if (mFormat.equals(FORMAT_NONE))
480        {
481            result = false;
482        }
483        else
484        {
485            result = true;
486        }
487        return result;
488    }
489
490    File getXepHome ()
491    {
492        return mXepHome;
493    }
494
495    private void renderDocBook (File docBookFile, File inFile)
496    {
497        for (final Iterator i = mFormatters.iterator(); i.hasNext();)
498        {
499            final FormatterInfoData f = (FormatterInfoData) i.next();
500            final Formatter formatter = Formatter.getInstance(f);
501            final File out = new File(docBookFile.getParentFile(), AntTaskUtil
502                .stripFileExtension(inFile.getName())
503                + "." + formatter.getFileExtension());
504            if (isOutputEnabled(formatter.getInfoData().getType()))
505            {
506                formatter.transform(this, docBookFile, out);
507            }
508        }
509    }
510
511    private void renderDocbookFilesFromPassOne (File filePassOne)
512    {
513        File docbookDir = new File(filePassOne.getParent());
514        transformPassTwo(filePassOne);
515        log("Search files to render in directory: " + docbookDir.getParent());
516        final File[] docbookFiles = docbookDir.listFiles(new FilenameFilter()
517        {
518            public boolean accept (File dir, String name)
519            {
520                final boolean result;
521                if (name.endsWith(".p2"))
522                {
523                    result = true;
524                }
525                else
526                {
527                    result = false;
528                }
529                return result;
530            }
531        });
532        if (docbookFiles != null)
533        {
534            for (int i = 0; i < docbookFiles.length; i++)
535            {
536                final File docbookFile = docbookFiles[i];
537                final File passOneFile = new File(AntTaskUtil
538                    .stripFileExtension(AntTaskUtil
539                        .stripFileExtension(docbookFile.getName())));
540                renderDocBook(docbookFile, passOneFile);
541                log("Will render file: " + docbookFile.getName(), 
542                    Project.MSG_VERBOSE);
543            }
544        }
545        else
546        {
547            log("No .xml files found to render", Project.MSG_VERBOSE);
548        }
549    }
550
551    private File transformPassOne (File in)
552    {
553        final XsltBasedTask task = new XsltBasedTask()
554        {
555            String getDefaultStyleSheet ()
556            {
557                return mTypeLowerCase + "-pass-one.xsl";
558            }
559
560            void setAdditionalTransformerParameters (Transformer transformer)
561            {
562                setXdocTransformerParams(transformer);
563            }
564        };
565        task.setProject(getProject());
566        task.setTaskName(mTypeLowerCase + "-p1");
567        task.setIn(in);
568        task.setForce(true); // FIXME
569        final File outFile = new File(mOutDir, in.getName() + ".p1");
570        task.setOut(outFile);
571        task.setFailonerror(mFailOnError);
572        task.setDestdir(outFile.getParentFile());
573        task.execute();
574        return outFile;
575    }
576
577    private File transformPassTwo (File filePassOne)
578    {
579        final XsltBasedTask task = new XsltBasedTask()
580        {
581            String getDefaultStyleSheet ()
582            {
583                return mTypeLowerCase + "-pass-two.xsl";
584            }
585
586            void setAdditionalTransformerParameters (Transformer transformer)
587            {
588                setXdocTransformerParams(transformer);
589            }
590        };
591        task.setProject(getProject());
592        task.setTaskName(mTypeLowerCase + "-p2");
593        task.setIn(filePassOne);
594        task.setForce(true); // FIXME
595        final File outFile = new File(mOutDir, filePassOne.getName() + ".p2");
596        task.setOut(outFile);
597        task.setFailonerror(mFailOnError);
598        task.setDestdir(outFile.getParentFile());
599        task.execute();
600        return outFile;
601    }
602
603    private void generateUseCaseDiagrams (File filePassOne, final File imageDir)
604    {
605        if (isOutputEnabled(FORMAT_PDF) || isOutputEnabled(FORMAT_HTML))
606        {
607            final XsltBasedTask task = new XsltBasedTask()
608            {
609                String getDefaultStyleSheet ()
610                {
611                    return "usecase_diagrams.xsl";
612                }
613
614                void setAdditionalTransformerParameters (
615                    Transformer transformer)
616                {
617                    transformer.setParameter(
618                        "basedir", getProject().getBaseDir().toString());
619                    transformer.setParameter("imagedir", imageDir.toString());
620                }
621            };
622            task.setProject(getProject());
623            task.setTaskName("diagrams");
624            task.setIn(filePassOne);
625            task.setForce(true); // FIXME
626            final File outFile
627                = new File(mOutDir, "use-case-diagrams" + ".tmp");
628            task.setOut(outFile);
629            task.setFailonerror(mFailOnError);
630            task.setDestdir(outFile.getParentFile());
631            task.execute();
632        }
633    }
634   
635    private void generateKeyPerformanceDiagrams (
636        File filePassOne, final File imageDir)
637    {
638        if (isOutputEnabled(FORMAT_PDF) || isOutputEnabled(FORMAT_HTML))
639        {
640            final XsltBasedTask task = new XsltBasedTask()
641            {
642                String getDefaultStyleSheet ()
643                {
644                    return "key-performance-diagrams.xsl";
645                }
646
647                void setAdditionalTransformerParameters (
648                    Transformer transformer)
649                {
650                    setXdocTransformerParams(transformer);
651                    transformer.setParameter("imagedir", imageDir.toString());
652                }
653            };
654            task.setProject(getProject());
655            task.setTaskName("diagrams");
656            task.setIn(filePassOne);
657            task.setForce(true); // FIXME
658            final File outFile
659                = new File(mOutDir, "key-performance-diagrams" + ".tmp");
660            task.setOut(outFile);
661            task.setFailonerror(mFailOnError);
662            task.setDestdir(outFile.getParentFile());
663            task.execute();
664        }
665    }
666
667    private void exportToXmi (File filePassOne, final File imageDir)
668    {
669        final XsltBasedTask task = new XsltBasedTask()
670        {
671            String getDefaultStyleSheet ()
672            {
673                return "usecase_xmi_export.xsl";
674            }
675
676            void setAdditionalTransformerParameters (Transformer transformer)
677            {
678                transformer.setParameter("basedir", getProject().getBaseDir()
679                    .toString());
680                transformer.setParameter("imagedir", imageDir.toString());
681            }
682        };
683        task.setProject(getProject());
684        task.setTaskName("uc-xmi");
685        task.setIn(filePassOne);
686        task.setForce(true); // FIXME
687        final File outFile = new File(mOutDir, "use-case-xmi" + ".tmp");
688        task.setOut(outFile);
689        task.setFailonerror(mFailOnError);
690        task.setDestdir(outFile.getParentFile());
691        task.execute();
692    }
693
694    private void exportToHbm (File filePassOne)
695    {
696        final String targetdir = buildTargetDir(filePassOne.getName());
697        File dir = new File(targetdir);
698        dir.mkdir();
699
700        final XsltBasedTask task = new XsltBasedTask()
701        {
702            String getDefaultStyleSheet ()
703            {
704                return "usecase_hbm_export.xsl";
705            }
706
707            void setAdditionalTransformerParameters (Transformer transformer)
708            {
709                transformer.setParameter("targetdir", targetdir);
710                transformer.setParameter("package-prefix", 
711                    mHibernateInfoData.getPackagePrefix());
712                transformer.setParameter("package-suffix", 
713                    mHibernateInfoData.getPackageSuffix());
714                transformer.setParameter("tablename-prefix", 
715                    mHibernateInfoData.getTableNamePrefix());
716                transformer.setParameter("tablename-suffix", 
717                    mHibernateInfoData.getTableNameSuffix());
718                transformer.setParameter("foreign-key-prefix", 
719                    mHibernateInfoData.getForeignKeyPrefix());
720                transformer.setParameter("foreign-key-suffix", 
721                    mHibernateInfoData.getForeignKeySuffix());
722            }
723        };
724        task.setProject(getProject());
725        task.setTaskName("uc-hbm");
726        task.setIn(filePassOne);
727        task.setForce(true); // FIXME
728        final File outFile = new File(mOutDir, "use-case-hbm" + ".tmp");
729        task.setOut(outFile);
730        task.setFailonerror(mFailOnError);
731        task.setDestdir(outFile.getParentFile());
732        task.execute();
733    }
734
735    private void exportToHbCfg (File filePassOne)
736    {
737        final String targetdir = buildTargetDir(filePassOne.getName());
738        File dir = new File(targetdir);
739        dir.mkdir();
740
741        final XsltBasedTask task = new XsltBasedTask()
742        {
743            String getDefaultStyleSheet ()
744            {
745                return "usecase_hbcfg_export.xsl";
746            }
747
748            void setAdditionalTransformerParameters (Transformer transformer)
749            {
750                transformer.setParameter("targetdir", targetdir);
751                transformer.setParameter("session-factory", 
752                    mHibernateInfoData.getSessionFactory());
753            }
754        };
755        task.setProject(getProject());
756        task.setTaskName("uc-hbm");
757        task.setIn(filePassOne);
758        task.setForce(true); // FIXME
759        final File outFile = new File(mOutDir, "use-case-hbcfg" + ".tmp");
760        task.setOut(outFile);
761        task.setFailonerror(mFailOnError);
762        task.setDestdir(outFile.getParentFile());
763        task.execute();
764    }
765
766    private String buildTargetDir (String name)
767    {
768        String strippedName = name.substring(0, name.lastIndexOf(".xml.p1"));
769        String dir = mOutDir + File.separator + strippedName
770                       + File.separator + "hibernate";
771        return dir;
772    }
773
774    private void generateSadDiagrams (File in)
775    {
776        final DiagramTask task = new DiagramTask();
777        task.setTaskName(DiagramTask.NAME);
778        task.setProject(getProject());
779        task.setIn(in);
780        for (final Iterator i = mSources.iterator(); i.hasNext();)
781        {
782            final SourceDirectory src = (SourceDirectory) i.next();
783            task.addSrc(src);
784        }
785        task.setFailonerror(mFailOnError);
786        final File out = new File(mOutDir, IMAGE_DIR);
787        task.setOut(out);
788        task.setDocletPath(mDocletPath);
789        task.execute();
790    }
791
792    private void scaleSvgImages (File dir)
793    {
794        final XsltBatchProcessor x = new XsltBatchProcessor();
795        x.setProject(getProject());
796        x.setTaskName("svg-scale");
797        x.setFailonerror(mFailOnError);
798        x.setXsl("svg-image-transform.xsl");
799        x.resolveExternalEntities(false);
800        final FileSet fs = new FileSet();
801        fs.setDir(dir);
802        fs.setIncludes("*.svg");
803        x.addFiles(fs);
804        x.execute();
805    }
806
807    private void generateApiDocs (File in)
808    {
809        final ApiDocTask task = new ApiDocTask();
810        task.setTaskName(ApiDocTask.NAME);
811        task.setProject(getProject());
812        task.setIn(in);
813        for (final Iterator i = mSources.iterator(); i.hasNext();)
814        {
815            final SourceDirectory src = (SourceDirectory) i.next();
816            task.addSrc(src);
817        }
818        task.setFailonerror(mFailOnError);
819        final File out = new File(mOutDir, APIDOC_DIR);
820        out.mkdirs();
821        task.setOut(out);
822        task.setDocletPath(mDocletPath);
823        task.execute();
824        // Transform to DocBook
825        final XsltBatchProcessor x = new XsltBatchProcessor();
826        x.setProject(getProject());
827        x.setTaskName("java2docbook");
828        x.setFailonerror(mFailOnError);
829        x.setXsl("java2docbook.xsl");
830        final FileSet fs = new FileSet();
831        fs.setDir(out);
832        fs.setIncludes("*.xml");
833        x.addFiles(fs);
834        x.execute();
835    }
836
837    /**
838     * Checks the attributes provided by this class.
839     *
840     * @throws BuildException
841     */
842    private void checkAttributes ()
843        throws BuildException
844    {
845        checkAttributeInFile();
846        XsltBasedTask.checkXercesVersion(this);
847    }
848
849    private void checkAttributeInFile ()
850    {
851        if (mInFile == null)
852        {
853            throw new BuildException("Missing mandatory attribute 'in'.",
854                getLocation());
855        }
856        if (!mInFile.exists())
857        {
858            throw new BuildException("Input file '" + mInFile + "' not found.",
859                getLocation());
860        }
861    }
862
863    private void rasterizeSvgFiles (File directory)
864    {
865        final File[] svgFiles = directory.listFiles(new FilenameFilter()
866        {
867            public boolean accept (File dir, String name)
868            {
869                final boolean result;
870                if (name.endsWith(".svg"))
871                {
872                    result = true;
873                }
874                else
875                {
876                    result = false;
877                }
878                return result;
879            }
880        });
881        log("Creating raster images for " + svgFiles.length
882            + " images", Project.MSG_INFO);
883        for (int i = 0; i < svgFiles.length; i++)
884        {
885            final File svgFile = svgFiles[i];
886            try
887            {
888                log("Creating raster image for '" + svgFile.getCanonicalPath()
889                    + "'", Project.MSG_VERBOSE);
890                /*
891                 * final String[] args = new String[] { "-maxw",
892                 * "700.0", "-scriptSecurityOff",
893                 * svgFile.getCanonicalPath()}; final Main conv = new
894                 * Main(args); // execute the conversion conv.execute();
895                 */
896                final File pngFile = new File(svgFile.getParentFile(), svgFile
897                    .getName().substring(0, svgFile.getName().indexOf('.'))
898                    + ".png");
899                Rasterizer.rasterize(svgFile, pngFile);
900            }
901            catch (Exception ex)
902            {
903                throw new BuildException(
904                    "Could not generate raster image for '" + svgFile.getName()
905                        + "' (" + ex + ")");
906            }
907        }
908    }
909
910    private static final class Rasterizer
911    {
912        private static final PNGTranscoder TRANSCODER;
913        static
914        {
915            TRANSCODER = new PNGTranscoder();
916            // force Xerces as XML Reader
917            TRANSCODER.addTranscodingHint(
918                XMLAbstractTranscoder.KEY_XML_PARSER_CLASSNAME,
919                "org.apache.xerces.parsers.SAXParser");
920        }
921
922        private Rasterizer ()
923        {
924            // utility class -- provides only static methods
925        }
926
927        public static void rasterize (File in, File out)
928            throws TranscoderException, IOException
929        {
930            final OutputStream ostream = new FileOutputStream(out);
931            try
932            {
933                // Create the transcoder input
934                final TranscoderInput input = new TranscoderInput(
935                    new FileInputStream(in));
936                input.setURI(in.toURL().toExternalForm());
937                // Create the transcoder output
938                final TranscoderOutput output = new TranscoderOutput(ostream);
939                // Transform the SVG document into a PNG image
940                TRANSCODER.transcode(input, output);
941                ostream.flush();
942            }
943            finally
944            {
945                IoUtil.close(ostream);
946            }
947        }
948    }
949
950    /**
951     * The Class FormatterInfoData.
952     */
953    public static class FormatterInfoData
954    {
955        private File mStyleSheet;
956
957        private File mCascadingStyleSheet;
958
959        private String mType;
960
961        /**
962         * Gets the cascading style sheet.
963         *
964         * @return the cascading style sheet
965         */
966        public File getCascadingStyleSheet ()
967        {
968            return mCascadingStyleSheet;
969        }
970
971        /**
972         * Sets the cascading style sheet.
973         *
974         * @param cascadingStyleSheet the new cascading style sheet
975         */
976        public void setCss (File cascadingStyleSheet)
977        {
978            mCascadingStyleSheet = cascadingStyleSheet;
979        }
980
981        /**
982         * Gets the style sheet.
983         *
984         * @return the style sheet
985         */
986        public File getStyleSheet ()
987        {
988            return mStyleSheet;
989        }
990
991        /**
992         * Sets the style.
993         *
994         * @param styleSheet the new style
995         */
996        public void setStyle (File styleSheet)
997        {
998            mStyleSheet = styleSheet;
999        }
1000
1001        /**
1002         * Gets the type.
1003         *
1004         * @return the type
1005         */
1006        public String getType ()
1007        {
1008            return mType;
1009        }
1010
1011        /**
1012         * Sets the type.
1013         *
1014         * @param type the new type
1015         */
1016        public void setType (String type)
1017        {
1018            mType = type;
1019        }
1020
1021        /**
1022         * Create the formatter info data.
1023         *
1024         * @return the formatter info data
1025         */
1026        public static FormatterInfoData create ()
1027        {
1028            return new FormatterInfoData();
1029        }
1030    }
1031
1032    /**
1033     * The Class HibernateInfoData.
1034     */
1035    public static class HibernateInfoData
1036    {
1037        private static final String DEFAULT_PACKAGE_PREFIX
1038            = "org.jcoderz.hibernate";
1039        private static final String DEFAULT_PACKAGE_SUFFIX = "";
1040
1041        private static final String DEFAULT_TABLE_NAME_PREFIX = "JC_";
1042        private static final String DEFAULT_TABLE_NAME_SUFFIX = "S";
1043
1044        private static final String DEFAULT_FOREIGN_KEY_PREFIX = "FK_";
1045        private static final String DEFAULT_FOREIGN_KEY_SUFFIX = "";
1046
1047        private static final String DEFAULT_HIBERNATE_SESSION_FACTORY
1048            = "Default";
1049
1050        /** Package prefix. */
1051        private String mPackagePrefix = DEFAULT_PACKAGE_PREFIX;
1052
1053        /** Package suffix. */
1054        private String mPackageSuffix = DEFAULT_PACKAGE_SUFFIX;
1055
1056        /** Table name prefix. */
1057        private String mTableNamePrefix = DEFAULT_TABLE_NAME_PREFIX;
1058
1059        /** Table name suffix. */
1060        private String mTableNameSuffix = DEFAULT_TABLE_NAME_SUFFIX;
1061
1062        /** Foreign key name prefix. */
1063        private String mForeignKeyPrefix = DEFAULT_FOREIGN_KEY_PREFIX;
1064
1065        /** Foreign key name suffix. */
1066        private String mForeignKeySuffix = DEFAULT_FOREIGN_KEY_SUFFIX;
1067
1068        /** Hibernate session factory. */
1069        private String mHibernateSessionFactory = DEFAULT_HIBERNATE_SESSION_FACTORY;
1070
1071        /**
1072         * Create the hibernate info data.
1073         *
1074         * @return the hibernate info data
1075         */
1076        public static HibernateInfoData create ()
1077        {
1078            return new HibernateInfoData();
1079        }
1080
1081        /**
1082         * Sets the session factory.
1083         *
1084         * @param hibernateSessionFactory the new session factory
1085         */
1086        public void setSessionFactory (
1087            String hibernateSessionFactory)
1088        {
1089            this.mHibernateSessionFactory = hibernateSessionFactory;
1090        }
1091
1092        /**
1093         * Gets the session factory.
1094         *
1095         * @return the session factory
1096         */
1097        public String getSessionFactory ()
1098        {
1099            return mHibernateSessionFactory;
1100        }
1101
1102        /**
1103         * Sets the package prefix.
1104         *
1105         * @param packagePrefix the new package prefix
1106         */
1107        public void setPackagePrefix (String packagePrefix)
1108        {
1109            mPackagePrefix = packagePrefix;
1110        }
1111
1112        /**
1113         * Gets the package prefix.
1114         *
1115         * @return the package prefix
1116         */
1117        public String getPackagePrefix ()
1118        {
1119            return mPackagePrefix;
1120        }
1121
1122        /**
1123         * Sets the package suffix.
1124         *
1125         * @param packageSuffix the new package suffix
1126         */
1127        public void setPackageSuffix (String packageSuffix)
1128        {
1129            mPackageSuffix = packageSuffix;
1130        }
1131
1132        /**
1133         * Gets the package suffix.
1134         *
1135         * @return the package suffix
1136         */
1137        public String getPackageSuffix ()
1138        {
1139            return mPackageSuffix;
1140        }
1141
1142        /**
1143         * Sets the table name prefix.
1144         *
1145         * @param mTableNamePrefix the new table name prefix
1146         */
1147        public void setTableNamePrefix (String tableNamePrefix)
1148        {
1149            mTableNamePrefix = tableNamePrefix;
1150        }
1151
1152        /**
1153         * Gets the table name prefix.
1154         *
1155         * @return the table name prefix
1156         */
1157        public String getTableNamePrefix ()
1158        {
1159            return mTableNamePrefix;
1160        }
1161
1162        /**
1163         * Sets the table name suffix.
1164         *
1165         * @param mTableNameSuffix the new table name suffix
1166         */
1167        public void setTableNameSuffix (String tableNameSuffix)
1168        {
1169            mTableNameSuffix = tableNameSuffix;
1170        }
1171
1172        /**
1173         * Gets the table name suffix.
1174         *
1175         * @return the table name suffix
1176         */
1177        public String getTableNameSuffix ()
1178        {
1179            return mTableNameSuffix;
1180        }
1181
1182        /**
1183         * Sets the foreign key prefix.
1184         *
1185         * @param mForeignKeyPrefix the new foreign key prefix
1186         */
1187        public void setForeignKeyPrefix (String foreignKeyPrefix)
1188        {
1189            mForeignKeyPrefix = foreignKeyPrefix;
1190        }
1191
1192        /**
1193         * Gets the foreign key prefix.
1194         *
1195         * @return the foreign key prefix
1196         */
1197        public String getForeignKeyPrefix ()
1198        {
1199            return mForeignKeyPrefix;
1200        }
1201
1202        /**
1203         * Sets the foreign key suffix.
1204         *
1205         * @param mForeignKeySuffix the new foreign key suffix
1206         */
1207        public void setForeignKeySuffix (String foreignKeySuffix)
1208        {
1209            mForeignKeySuffix = foreignKeySuffix;
1210        }
1211
1212        public String getForeignKeySuffix ()
1213        {
1214            return mForeignKeySuffix;
1215        }
1216
1217    }
1218
1219}
Note: See TracBrowser for help on using the browser.