KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > native2ascii > DefaultNative2Ascii


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.taskdefs.optional.native2ascii;
19
20 import java.io.File JavaDoc;
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.ProjectComponent;
23 import org.apache.tools.ant.taskdefs.optional.Native2Ascii;
24 import org.apache.tools.ant.types.Commandline;
25
26 /**
27  * encapsulates the handling common to diffent Native2Asciiadapter
28  * implementations.
29  *
30  * @since Ant 1.6.3
31  */

32 public abstract class DefaultNative2Ascii implements Native2AsciiAdapter {
33
34     /** No-arg constructor. */
35     public DefaultNative2Ascii() {
36     }
37
38     /**
39      * Splits the task into setting up the command line switches
40      * @param args the native 2 ascii arguments.
41      * @param srcFile the source file.
42      * @param destFile the destination file.
43      * @return run if the conversion was successful.
44      * @throws BuildException if there is a problem.
45      * (delegated to {@link #setup setup}), adding the file names
46      * (delegated to {@link #addFiles addFiles}) and running the tool
47      * (delegated to {@link #run run}).
48      */

49     public final boolean convert(Native2Ascii args, File JavaDoc srcFile,
50                                  File JavaDoc destFile) throws BuildException {
51         Commandline cmd = new Commandline();
52         setup(cmd, args);
53         addFiles(cmd, args, srcFile, destFile);
54         return run(cmd, args);
55     }
56
57     /**
58      * Sets up the initial command line.
59      *
60      * <p>only the -encoding argument and nested arg elements get
61      * handled here.</p>
62      *
63      * @param cmd Command line to add to
64      * @param args provides the user-setting and access to Ant's
65      * logging system.
66      * @throws BuildException if there was a problem.
67      */

68     protected void setup(Commandline cmd, Native2Ascii args)
69         throws BuildException {
70         if (args.getEncoding() != null) {
71             cmd.createArgument().setValue("-encoding");
72             cmd.createArgument().setValue(args.getEncoding());
73         }
74         cmd.addArguments(args.getCurrentArgs());
75     }
76
77     /**
78      * Adds source and dest files to the command line.
79      *
80      * <p>This implementation adds them without any leading
81      * qualifiers, source first.</p>
82      *
83      * @param cmd Command line to add to
84      * @param log provides access to Ant's logging system.
85      * @param src the source file
86      * @param dest the destination file
87      * @throws BuildException if there was a problem.
88      */

89     protected void addFiles(Commandline cmd, ProjectComponent log, File JavaDoc src,
90                             File JavaDoc dest) throws BuildException {
91         cmd.createArgument().setFile(src);
92         cmd.createArgument().setFile(dest);
93     }
94
95     /**
96      * Executes the command.
97      *
98      * @param cmd Command line to execute
99      * @param log provides access to Ant's logging system.
100      * @return whether execution was successful
101      * @throws BuildException if there was a problem.
102      */

103     protected abstract boolean run(Commandline cmd, ProjectComponent log)
104         throws BuildException;
105 }
106
Popular Tags