1 | /*
|
---|
2 | * Copyright 2005 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | package org.apache.tools.ant.taskdefs.optional.native2ascii;
|
---|
18 |
|
---|
19 | import java.io.File;
|
---|
20 | import org.apache.tools.ant.BuildException;
|
---|
21 | import org.apache.tools.ant.ProjectComponent;
|
---|
22 | import org.apache.tools.ant.taskdefs.optional.Native2Ascii;
|
---|
23 | import org.apache.tools.ant.types.Commandline;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * encapsulates the handling common to diffent Native2Asciiadapter
|
---|
27 | * implementations.
|
---|
28 | *
|
---|
29 | * @since Ant 1.6.3
|
---|
30 | */
|
---|
31 | public abstract class DefaultNative2Ascii implements Native2AsciiAdapter {
|
---|
32 |
|
---|
33 | public DefaultNative2Ascii() {
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Splits the task into setting up the command line switches
|
---|
38 | * (delegated to {@link #setup setup}), adding the file names
|
---|
39 | * (delegated to {@link #addFiles addFiles}) and running the tool
|
---|
40 | * (delegated to {@link #run run}).
|
---|
41 | */
|
---|
42 | public final boolean convert(Native2Ascii args, File srcFile,
|
---|
43 | File destFile) throws BuildException {
|
---|
44 | Commandline cmd = new Commandline();
|
---|
45 | setup(cmd, args);
|
---|
46 | addFiles(cmd, args, srcFile, destFile);
|
---|
47 | return run(cmd, args);
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Sets up the initial command line.
|
---|
52 | *
|
---|
53 | * <p>only the -encoding argument and nested arg elements get
|
---|
54 | * handled here.</p>
|
---|
55 | *
|
---|
56 | * @param cmd Command line to add to
|
---|
57 | * @param args provides the user-setting and access to Ant's
|
---|
58 | * logging system.
|
---|
59 | */
|
---|
60 | protected void setup(Commandline cmd, Native2Ascii args)
|
---|
61 | throws BuildException {
|
---|
62 | if (args.getEncoding() != null) {
|
---|
63 | cmd.createArgument().setValue("-encoding");
|
---|
64 | cmd.createArgument().setValue(args.getEncoding());
|
---|
65 | }
|
---|
66 | cmd.addArguments(args.getCurrentArgs());
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Adds source and dest files to the command line.
|
---|
71 | *
|
---|
72 | * <p>This implementation adds them without any leading
|
---|
73 | * qualifiers, source first.</p>
|
---|
74 | *
|
---|
75 | * @param cmd Command line to add to
|
---|
76 | * @param log provides access to Ant's logging system.
|
---|
77 | * @param src the source file
|
---|
78 | * @param dest the destination file
|
---|
79 | */
|
---|
80 | protected void addFiles(Commandline cmd, ProjectComponent log, File src,
|
---|
81 | File dest) throws BuildException {
|
---|
82 | cmd.createArgument().setFile(src);
|
---|
83 | cmd.createArgument().setFile(dest);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Executes the command.
|
---|
88 | *
|
---|
89 | * @param cmd Command line to execute
|
---|
90 | * @param log provides access to Ant's logging system.
|
---|
91 | * @return whether execution was successful
|
---|
92 | */
|
---|
93 | protected abstract boolean run(Commandline cmd, ProjectComponent log)
|
---|
94 | throws BuildException;
|
---|
95 | }
|
---|