View | Details | Raw Unified | Return to bug 46926
Collapse All | Expand All

(-)documentation-sources/content/xdocs/tools/rasterizer.xml (+23 lines)
Lines 510-515 Link Here
510
            </td>
510
            </td>
511
            <td>No</td>
511
            <td>No</td>
512
          </tr>
512
          </tr>
513
          <tr>
514
            <td>flatten</td>
515
            <td>
516
              If this attribute is set to <code>true</code>, all generated
517
              files will be placed directly within the directory specified
518
              through the <code>destdir</code> attribute, without any
519
              intermediate subdirectories. When set to <code>false</code> the
520
              structure of the source tree is reproduced.
521
              This attribute is ignored when a nested <code>mapper</code>
522
              element is given. For backwards compatibility reasons, this
523
              attribute defaults to <code>true</code>.
524
            </td>
525
            <td>
526
              No.
527
            </td>
528
          </tr>
513
        </table>
529
        </table>
514
        <p>
530
        <p>
515
          You can use <code>fileset</code> elements to select input 
531
          You can use <code>fileset</code> elements to select input 
Lines 518-523 Link Here
518
          to use
534
          to use
519
          <a href="http://ant.apache.org/manual/CoreTypes/fileset.html">filesets</a>.
535
          <a href="http://ant.apache.org/manual/CoreTypes/fileset.html">filesets</a>.
520
        </p>
536
        </p>
537
        <p>Furthermore, you can use a <code>mapper</code> element to
538
          flexibly control the output file name for every input
539
          file. See the
540
          <a href="http://ant.apache.org/">Ant</a> documentation to learn how
541
          to use
542
          <a href="http://ant.apache.org/manual/CoreTypes/mapper.html">mappers</a>.
543
        </p>
521
      </section>
544
      </section>
522
545
523
      <section id="taskExamples">
546
      <section id="taskExamples">
524
  + build
547
  + build
(-)contrib/rasterizertask/sources/org/apache/tools/ant/taskdefs/optional/RasterizerTaskSVGConverter.java (+140 lines)
Line 0 Link Here
1
/*
2
3
   Licensed to the Apache Software Foundation (ASF) under one or more
4
   contributor license agreements.  See the NOTICE file distributed with
5
   this work for additional information regarding copyright ownership.
6
   The ASF licenses this file to You under the Apache License, Version 2.0
7
   (the "License"); you may not use this file except in compliance with
8
   the License.  You may obtain a copy of the License at
9
10
       http://www.apache.org/licenses/LICENSE-2.0
11
12
   Unless required by applicable law or agreed to in writing, software
13
   distributed under the License is distributed on an "AS IS" BASIS,
14
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
   See the License for the specific language governing permissions and
16
   limitations under the License.
17
18
 */
19
20
package org.apache.tools.ant.taskdefs.optional;
21
22
23
// -- Batik classes ----------------------------------------------------------
24
import org.apache.batik.apps.rasterizer.SVGConverter;
25
import org.apache.batik.apps.rasterizer.SVGConverterException;
26
import org.apache.batik.apps.rasterizer.SVGConverterFileSource;
27
import org.apache.batik.apps.rasterizer.SVGConverterSource;
28
29
// -- Ant classes ------------------------------------------------------------
30
import org.apache.tools.ant.Task;
31
32
// -- Java SDK classes -------------------------------------------------------
33
import java.io.File;
34
import java.util.ArrayList;
35
import java.util.List;
36
37
38
/**
39
 * An <code>SVGConverter</code> with an explicit destination for each source.
40
 *
41
 * @author <a href="mailto:Martin.vGagern@gmx.net">Martin von Gagern</a>
42
 * @version $Id$
43
 */
44
class RasterizerTaskSVGConverter extends SVGConverter {
45
46
    // -- Variables ----------------------------------------------------------
47
48
    /**
49
     * Ant task that is used to log messages.
50
     */
51
    private Task executingTask;
52
53
    /**
54
     * List of sources.
55
     * Elements are expected to be {@link File}s.
56
     * It is not possible (yet) to reference parts of a file or an URL
57
     * in this way.
58
     */
59
    private List sourceFiles;
60
61
    /**
62
     * List of destinations.
63
     * Elements are expected to be {@link File}s.
64
     * If <code>appendFileNames</code> is <code>true</code>, the items of
65
     * this list are interpreted as directories, and default file names
66
     * derived from the names of the corresponding source files will be
67
     * appended. Otherwise, the item will be interpreted as a file name
68
     * in its own right.
69
     */
70
    private List destinations;
71
72
    /**
73
     * Determines whether to append file names to destination paths.
74
     */
75
    private boolean appendFileNames;
76
77
78
    // -- Constructors -------------------------------------------------------
79
80
    /**
81
     * Sets the given Ant task to receive log messages.
82
     * The value can be <code>null</code> when log messages won't be written.
83
     *
84
     * @param task Ant task to receive log messages or <code>null</code>
85
     */
86
    public RasterizerTaskSVGConverter(Task task) {
87
        super(new RasterizerTaskSVGConverterController(task));
88
        executingTask = task;
89
    }
90
91
    // -- Public interface ---------------------------------------------------
92
93
    public void setSourceFiles(List sources) {
94
        this.sourceFiles = sources;
95
    }
96
97
    public void setDestinationFiles(List destinations) {
98
        this.destinations = destinations;
99
        appendFileNames = false;
100
    }
101
102
    public void setDestinationDirectories(List destinations) {
103
        this.destinations = destinations;
104
        appendFileNames = true;
105
    }
106
107
108
    // -- Overrides to protected interface -----------------------------------
109
110
    protected List computeSources() throws SVGConverterException {
111
        List sources = new ArrayList(sourceFiles.size());
112
        for (int i = 0; i < sourceFiles.size(); ++i) {
113
            File file = (File)sourceFiles.get(i);
114
            SVGConverterSource source;
115
            source = new SVGConverterFileSource(file);
116
            sources.add(source);
117
        }
118
        return sources;
119
    }
120
121
    protected List computeDstFiles(List sources)
122
    throws SVGConverterException {
123
        if (destinations.size() != sources.size())
124
            throw new IllegalStateException("Length mismatch: " +
125
                destinations.size() + " destinations for " +
126
                sources.size() + " sources");
127
        if (!appendFileNames)
128
            return destinations;
129
        List destFiles = new ArrayList(destinations.size());
130
        for (int i = 0; i < destinations.size(); ++i) {
131
            SVGConverterSource src = (SVGConverterSource)sources.get(i);
132
            File dir = (File)destinations.get(i);
133
            String name = getDestinationFile(src.getName());
134
            File file = new File(dir, name);
135
            destFiles.add(file);
136
        }
137
        return destFiles;
138
    }
139
140
}
(-)contrib/rasterizertask/sources/org/apache/tools/ant/taskdefs/optional/RasterizerTaskSVGConverterController.java (+5 lines)
Lines 83-88 Link Here
83
83
84
    public boolean proceedWithSourceTranscoding(SVGConverterSource source,
84
    public boolean proceedWithSourceTranscoding(SVGConverterSource source,
85
                                                File dest) {
85
                                                File dest) {
86
        if(executingTask != null) {
87
            executingTask.log("Converting '"
88
                + source.toString() + "' to '"
89
                + dest.toString() + "'.");
90
        }
86
        return true;
91
        return true;
87
    }
92
    }
88
93
(-)contrib/rasterizertask/sources/org/apache/tools/ant/taskdefs/optional/RasterizerTask.java (-35 / +104 lines)
Lines 26-35 Link Here
26
import org.apache.tools.ant.taskdefs.MatchingTask;
26
import org.apache.tools.ant.taskdefs.MatchingTask;
27
import org.apache.tools.ant.types.EnumeratedAttribute;
27
import org.apache.tools.ant.types.EnumeratedAttribute;
28
import org.apache.tools.ant.types.FileSet;
28
import org.apache.tools.ant.types.FileSet;
29
import org.apache.tools.ant.types.Mapper;
29
import org.apache.tools.ant.util.JAXPUtils;
30
import org.apache.tools.ant.util.JAXPUtils;
31
import org.apache.tools.ant.util.FileNameMapper;
30
32
31
// -- Batik classes ----------------------------------------------------------
33
// -- Batik classes ----------------------------------------------------------
32
import org.apache.batik.apps.rasterizer.SVGConverter;
33
import org.apache.batik.apps.rasterizer.DestinationType;
34
import org.apache.batik.apps.rasterizer.DestinationType;
34
import org.apache.batik.apps.rasterizer.SVGConverterException;
35
import org.apache.batik.apps.rasterizer.SVGConverterException;
35
import org.apache.batik.transcoder.image.JPEGTranscoder;
36
import org.apache.batik.transcoder.image.JPEGTranscoder;
Lines 47-52 Link Here
47
import java.util.Iterator;
48
import java.util.Iterator;
48
import java.util.ArrayList;
49
import java.util.ArrayList;
49
import java.util.List;
50
import java.util.List;
51
import java.util.Collections;
50
52
51
53
52
54
Lines 116-124 Link Here
116
    protected File destDir = null;
118
    protected File destDir = null;
117
    /** Contents of <code>fileset</code> elements. */
119
    /** Contents of <code>fileset</code> elements. */
118
    protected Vector filesets = new Vector();
120
    protected Vector filesets = new Vector();
121
    /** Contents of <code>mapper</code> elements. */
122
    protected Mapper mapperElement = null;
123
    /** Whether to strip directories from resulting paths. */
124
    protected boolean flatten = true;
119
125
120
    /** Converter object used to convert SVG images to raster images. */
126
    /** Converter object used to convert SVG images to raster images. */
121
    protected SVGConverter converter;
127
    protected RasterizerTaskSVGConverter converter;
122
128
123
129
124
130
Lines 127-133 Link Here
127
     * Initializes a new rasterizer task.
133
     * Initializes a new rasterizer task.
128
     */
134
     */
129
    public RasterizerTask() {
135
    public RasterizerTask() {
130
        converter = new SVGConverter(new RasterizerTaskSVGConverterController(this));
131
    }
136
    }
132
137
133
138
Lines 323-328 Link Here
323
    }
328
    }
324
329
325
    /**
330
    /**
331
     * Sets <code>flatten</code> attribute value.
332
     *
333
     * <p>If this attribute is set to <code>true</code>, all generated
334
     * files will be placed directly within the directory specified
335
     * through the <code>destdir</code> attribute, without any
336
     * intermediate subdirectories. When set to <code>false</code> the
337
     * structure of the source tree is reproduced.</p>
338
     *
339
     * <p>This attribute is ignored when a nested <code>mapper</code>
340
     * element is given. For backwards compatibility reasons, this
341
     * attribute defaults to <code>true</code>.</p>
342
     */
343
    public void setFlatten(boolean flatten) {
344
        this.flatten = flatten;
345
    }
346
347
    /**
326
     * Reads <code>fileset</code> elements.
348
     * Reads <code>fileset</code> elements.
327
     *
349
     *
328
     * <p><code>fileset</code> elements can be used when there are many files
350
     * <p><code>fileset</code> elements can be used when there are many files
Lines 335-340 Link Here
335
    }
357
    }
336
358
337
    /**
359
    /**
360
     * Creates <code>mapper</code> child element.
361
     *
362
     * <p>A <code>mapper</code> element can be used to specify
363
     * the output file(s) for every input file in a flexible way.</p>
364
     *
365
     * @return the <code>mapper</code> element to be configured by Ant
366
     */
367
    public Mapper createMapper() throws BuildException {
368
        if (mapperElement != null) {
369
            throw new BuildException("Cannot define more than one mapper",
370
                                     getLocation());
371
        }
372
        mapperElement = new Mapper(getProject());
373
        return mapperElement;
374
    }
375
376
    /**
377
     * Add a nested file name mapper.
378
     * @param fileNameMapper the mapper to add.
379
     */
380
    public void add(FileNameMapper fileNameMapper) {
381
        createMapper().add(fileNameMapper);
382
    }
383
384
    /**
338
     * Validates and sets input values and files, then starts the conversion 
385
     * Validates and sets input values and files, then starts the conversion 
339
     * process.
386
     * process.
340
     *
387
     *
Lines 345-352 Link Here
345
     */
392
     */
346
    public void execute() throws BuildException {
393
    public void execute() throws BuildException {
347
394
348
        String[] sources;        // Array of input files.
349
350
        // Store default XML parser information and set user class.
395
        // Store default XML parser information and set user class.
351
        String defaultParser = XMLResourceDescriptor.getXMLParserClassName();
396
        String defaultParser = XMLResourceDescriptor.getXMLParserClassName();
352
        // Throws BuildException.
397
        // Throws BuildException.
Lines 367-392 Link Here
367
                }
412
                }
368
            }
413
            }
369
414
415
            // Actually create our converter
416
            converter = new RasterizerTaskSVGConverter(this);
417
370
            // Throws BuildException.
418
            // Throws BuildException.
371
            setRasterizingParameters();
419
            setRasterizingParameters();
372
420
373
            // Get and set source(s).
421
            // Get and set source(s) and destination(s).
374
            sources = getSourceFiles();
422
            setFileLists();
375
            converter.setSources(sources);
376
423
377
            // Set destination.
378
            if(this.srcFile != null) {
379
                converter.setDst(this.destFile);
380
            } else {
381
                converter.setDst(this.destDir);
382
            }
383
384
            // Input filenames are stored in the converter and 
424
            // Input filenames are stored in the converter and 
385
            // everything is ready for the conversion.
425
            // everything is ready for the conversion.
386
426
427
            /*
387
            log("Rasterizing " + sources.length + 
428
            log("Rasterizing " + sources.length + 
388
                (sources.length == 1 ? " image " : " images ") + 
429
                (sources.length == 1 ? " image " : " images ") + 
389
                "from SVG to " + this.resultType.toString() + ".");
430
                "from SVG to " + this.resultType.toString() + ".");
431
            */
390
432
391
            try {
433
            try {
392
                converter.execute();
434
                converter.execute();
Lines 394-399 Link Here
394
                throw new BuildException(sce.getMessage());
436
                throw new BuildException(sce.getMessage());
395
            }
437
            }
396
        } finally {
438
        } finally {
439
            converter = null; // just to make sure it won't be reused
440
397
            // Restore default XML parser for the next execute.
441
            // Restore default XML parser for the next execute.
398
            XMLResourceDescriptor.setXMLParserClassName(defaultParser);
442
            XMLResourceDescriptor.setXMLParserClassName(defaultParser);
399
        }
443
        }
Lines 487-537 Link Here
487
     *
531
     *
488
     * @return Array of source filename strings.
532
     * @return Array of source filename strings.
489
     */
533
     */
490
    protected String[] getSourceFiles() {
534
    protected void setFileLists() {
491
535
492
        List inputFiles = new ArrayList(); // Input files in temp list.
493
494
        if(this.srcFile != null) {
536
        if(this.srcFile != null) {
495
            // Only one source and destination file have been set.
537
            // Only one source and destination file have been set.
496
            inputFiles.add(this.srcFile.getAbsolutePath());
538
            List srcs = Collections.singletonList(srcFile);
539
            List dests = Collections.singletonList(destFile);
540
            converter.setSourceFiles(srcs);
541
            converter.setDestinationFiles(dests);
497
        } else {
542
        } else {
498
            // Unknown number of files have to be converted. destdir 
543
            // Unknown number of files have to be converted. destdir 
499
            // attribute and either srcdir attribute or fileset element 
544
            // attribute and either srcdir attribute or fileset element 
500
            // have been set.
545
            // have been set.
501
546
547
            List srcs = new ArrayList(); // Input files
548
            List dests = new ArrayList(); // Output dirs or files
549
            List filesets = this.filesets; // local reference, modifyable
550
            FileNameMapper mapper = null;
551
            if (mapperElement != null)
552
                mapper = mapperElement.getImplementation();
553
502
            // Read source files from the child patterns.
554
            // Read source files from the child patterns.
503
            // The value of srcdir attribute overrides the dir attribute in
555
            // The value of srcdir attribute overrides the dir attribute in
504
            // fileset element.
556
            // fileset element.
505
            if(this.srcDir != null) {
557
            if(this.srcDir != null) {
506
                // fileset is declared in the super class.
558
                // fileset is declared in the super class.
507
                // Scan to get all the files in srcdir directory that 
559
                // Add to list of filesets in order to get all the files
508
                // should be in input files.
560
                // in srcdir directory that should be in input files.
509
                fileset.setDir(this.srcDir);
561
                fileset.setDir(this.srcDir);
510
                DirectoryScanner ds = fileset.getDirectoryScanner(project);
562
                filesets = new ArrayList(filesets); // local copy, just in case
511
                String[] includedFiles = ds.getIncludedFiles();
563
                filesets.add(fileset);
512
                // Add file and its path to the input file vector.
513
                for (int j = 0 ; j < includedFiles.length ; j++) {
514
                    File newFile = new File(srcDir.getPath(), includedFiles[j]);
515
                    inputFiles.add(newFile.getAbsolutePath());
516
                }
517
            }
564
            }
518
            // Read source files from child filesets.
565
566
            // Read source files from child filesets and child patterns.
519
            for (int i = 0 ; i < filesets.size() ; i++) {
567
            for (int i = 0 ; i < filesets.size() ; i++) {
520
                // Scan to get all the files in this fileset that 
568
                // Scan to get all the files in this fileset that 
521
                // should be in input files.
569
                // should be in input files.
522
                FileSet fs = (FileSet) filesets.elementAt(i);
570
                FileSet fs = (FileSet) filesets.get(i);
571
                File fsDir = fs.getDir(project);
523
                DirectoryScanner ds = fs.getDirectoryScanner(project);
572
                DirectoryScanner ds = fs.getDirectoryScanner(project);
524
                String[] includedFiles = ds.getIncludedFiles();
573
                String[] includedFiles = ds.getIncludedFiles();
525
                // Add file and its path to the input file vector.
574
                // Add file and its path to the input file vector.
526
                for (int j = 0 ; j < includedFiles.length ; j++) {
575
                for (int j = 0 ; j < includedFiles.length ; j++) {
527
                    File newFile = new File(fs.getDir(project).getPath(), includedFiles[j]);
576
                    File src = new File(fsDir, includedFiles[j]);
528
                    inputFiles.add(newFile.getAbsolutePath());
577
                    if (mapper != null) {
578
                        String[] mapped = mapper.mapFileName(includedFiles[j]);
579
                        if (mapped != null) {
580
                            for (int k = 0; k < mapped.length; ++k) {
581
                                File dest = new File(destDir, mapped[k]);
582
                                srcs.add(src);
583
                                dests.add(dest);
584
                            }
585
                        }
586
                    }
587
                    else if (flatten) {
588
                        dests.add(destDir);
589
                    }
590
                    else {
591
                        File dest = new File(destDir, includedFiles[j]);
592
                        srcs.add(src);
593
                        dests.add(dest.getParentFile()); // destination dir
594
                    }
529
                }
595
                }
530
            }
596
            }
597
598
            converter.setSourceFiles(srcs);
599
            if (mapper == null)
600
                converter.setDestinationDirectories(dests);
601
            else
602
                converter.setDestinationFiles(dests);
531
        }
603
        }
532
533
        // Convert List to array and return the array.
534
        return (String[])inputFiles.toArray(new String[0]);
535
    }
604
    }
536
605
537
    /**
606
    /**
(-)contrib/rasterizertask/build.xml (-2 / +16 lines)
Lines 33-39 Link Here
33
    <!-- == Global properties ============================================ -->
33
    <!-- == Global properties ============================================ -->
34
    <property name="root" value="${basedir}" />
34
    <property name="root" value="${basedir}" />
35
    <!-- root: Root directory of all the other directories. -->
35
    <!-- root: Root directory of all the other directories. -->
36
    <property name="lib" value="${root}/../../lib" />
36
    <property name="batik.root" value="${root}/../.." />
37
    <!-- batik.root: Root directory of the batik source tree. -->
38
39
    <property file="${root}/build.properties"/>
40
    <property file="${batik.root}/build.properties"/>
41
    <!-- allow properties to be overridden, for this part or all of batik. -->
42
43
    <property name="lib" value="${batik.root}/lib" />
37
    <!-- lib: Directory where the library files (jars etc.) are located. -->
44
    <!-- lib: Directory where the library files (jars etc.) are located. -->
38
    <property name="src" value="${root}/sources" />
45
    <property name="src" value="${root}/sources" />
39
    <!-- src: Directory for source files. -->
46
    <!-- src: Directory for source files. -->
Lines 45-50 Link Here
45
    <!-- doc: Directory for documentation. -->
52
    <!-- doc: Directory for documentation. -->
46
    <property name="doc.api" value="${doc}/javadoc" />
53
    <property name="doc.api" value="${doc}/javadoc" />
47
    <!-- doc.api: Directory for javadocs. -->
54
    <!-- doc.api: Directory for javadocs. -->
55
    <property name="debug" value="false" />
56
    <!-- debug: Include debug information in classes. -->
48
57
49
58
50
59
Lines 68-74 Link Here
68
        description="Compiles source files.">
77
        description="Compiles source files.">
69
        <mkdir dir="${build.classes}" />
78
        <mkdir dir="${build.classes}" />
70
        <!-- Compile code. -->
79
        <!-- Compile code. -->
71
        <javac srcdir="${src}" destdir="${build.classes}" />
80
        <javac srcdir="${src}" destdir="${build.classes}" debug="${debug}">
81
            <classpath>
82
                <pathelement location="${batik.root}/classes"/>
83
                <fileset dir="${lib}" includes="build/*.jar"/>
84
            </classpath>
85
        </javac>
72
    </target>
86
    </target>
73
87
74
    <target name="jar" depends="prepare, compile"
88
    <target name="jar" depends="prepare, compile"

Return to bug 46926