diff --git a/build.xml b/build.xml
index 07a57a6ca..e4689e49c 100644
--- a/build.xml
+++ b/build.xml
@@ -162,9 +162,8 @@
Kaffe for example -->
only the -encoding argument gets handled here.
+ * + * @param cmd Command line to add to + * @param args provides the user-setting and access to Ant's + * logging system. + */ + protected void setup(Commandline cmd, Native2Ascii args) + throws BuildException { + if (args.getEncoding() != null) { + cmd.createArgument().setValue("-encoding"); + cmd.createArgument().setValue(args.getEncoding()); + } + } + + /** + * Adds source and dest files to the command line. + * + *This implementation adds them without any leading + * qualifiers, source first.
+ * + * @param cmd Command line to add to + * @param log provides access to Ant's logging system. + * @param src the source file + * @param dest the destination file + */ + protected void addFiles(Commandline cmd, ProjectComponent log, File src, + File dest) throws BuildException { + cmd.createArgument().setFile(src); + cmd.createArgument().setFile(dest); + } + + /** + * Executes the command. + * + * @param cmd Command line to execute + * @param log provides access to Ant's logging system. + * @return whether execution was successful + */ + protected abstract boolean run(Commandline cmd, ProjectComponent log) + throws BuildException; +} \ No newline at end of file diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/KaffeNative2Ascii.java b/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/KaffeNative2Ascii.java new file mode 100644 index 000000000..02aacb372 --- /dev/null +++ b/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/KaffeNative2Ascii.java @@ -0,0 +1,54 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.tools.ant.taskdefs.optional.native2ascii; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.ProjectComponent; +import org.apache.tools.ant.taskdefs.ExecuteJava; +import org.apache.tools.ant.taskdefs.optional.Native2Ascii; +import org.apache.tools.ant.types.Commandline; + +/** + * Adapter to kaffe.tools.native2ascii.Native2Ascii. + * + * @since Ant 1.6.3 + */ +public final class KaffeNative2Ascii extends DefaultNative2Ascii { + + /** + * Identifies this adapter. + */ + public static final String IMPLEMENTATION_NAME = "kaffe"; + + protected void setup(Commandline cmd, Native2Ascii args) + throws BuildException { + if (args.getReverse()) { + throw new BuildException("-reverse is not supported by Kaffe"); + } + super.setup(cmd, args); + } + + protected boolean run(Commandline cmd, ProjectComponent log) + throws BuildException { + ExecuteJava ej = new ExecuteJava(); + cmd.setExecutable("kaffe.tools.native2ascii.Native2Ascii"); + ej.setJavaCommand(cmd); + ej.execute(log.getProject()); + // otherwise ExecuteJava has thrown an exception + return true; + } +} \ No newline at end of file diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/Native2AsciiAdapter.java b/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/Native2AsciiAdapter.java index 33e9bf04c..b712a5b5a 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/Native2AsciiAdapter.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/Native2AsciiAdapter.java @@ -14,11 +14,27 @@ * limitations under the License. * */ -package org.apache.tools.ant.taskdef.optional.native2ascii; +package org.apache.tools.ant.taskdefs.optional.native2ascii; import java.io.File; +import org.apache.tools.ant.BuildException; import org.apache.tools.ant.taskdefs.optional.Native2Ascii; +/** + * Interface for an adapter to a native2ascii implementation. + * + * @since Ant 1.6.3 + */ public interface Native2AsciiAdapter { - boolean convert(Native2Ascii args, File srcFile, File destFile); + /** + * Convert the encoding of srcFile writing to destFile. + * + * @param args Task that holds command line arguments and allows + * the implementation to send messages to Ant's logging system + * @param srcFile the source to convert + * @param destFile where to send output to + * @return whether the conversion has been successful. + */ + boolean convert(Native2Ascii args, File srcFile, File destFile) + throws BuildException; } \ No newline at end of file diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/Native2AsciiAdapterFactory.java b/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/Native2AsciiAdapterFactory.java new file mode 100644 index 000000000..c477ed087 --- /dev/null +++ b/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/Native2AsciiAdapterFactory.java @@ -0,0 +1,96 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.tools.ant.taskdefs.optional.native2ascii; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.ProjectComponent; +import org.apache.tools.ant.util.JavaEnvUtils; + +/** + * Creates the Native2AsciiAdapter based on the user choice and + * potentially the VM vendor. + * + * @since Ant 1.6.3 + */ +public class Native2AsciiAdapterFactory { + + /** + * Determines the default choice of adapter based on the VM + * vendor. + * + * @return the default choice of adapter based on the VM + * vendor + */ + public static String getDefault() { + if (JavaEnvUtils.isKaffe()) { + return KaffeNative2Ascii.IMPLEMENTATION_NAME; + } + return SunNative2Ascii.IMPLEMENTATION_NAME; + } + + /** + * Creates the Native2AsciiAdapter based on the user choice and * + * potentially the VM vendor. + * + * @param choice the user choice (if any). + * @param log a ProjectComponent instance used to access Ant's + * logging system. + * @return The adapter to use. + */ + public static Native2AsciiAdapter getAdapter(String choice, + ProjectComponent log) + throws BuildException { + if ((JavaEnvUtils.isKaffe() && choice == null) + || KaffeNative2Ascii.IMPLEMENTATION_NAME.equals(choice)) { + return new KaffeNative2Ascii(); + } else if (SunNative2Ascii.IMPLEMENTATION_NAME.equals(choice)) { + return new SunNative2Ascii(); + } else if (choice != null) { + return resolveClassName(choice); + } + + // This default has been good enough until Ant 1.6.3, so stick + // with it + return new SunNative2Ascii(); + } + + /** + * Tries to resolve the given classname into a native2ascii adapter. + * Throws a fit if it can't. + * + * @param className The fully qualified classname to be created. + * @throws BuildException This is the fit that is thrown if className + * isn't an instance of Native2AsciiAdapter. + */ + private static Native2AsciiAdapter resolveClassName(String className) + throws BuildException { + try { + Class c = Class.forName(className); + Object o = c.newInstance(); + return (Native2AsciiAdapter) o; + } catch (ClassNotFoundException cnfe) { + throw new BuildException("Can't load " + className, cnfe); + } catch (ClassCastException cce) { + throw new BuildException(className + + " is not a Native2Ascii adapter", cce); + } catch (Throwable t) { + // for all other possibilities + throw new BuildException(className + " caused an interesting " + + "exception.", t); + } + } +} \ No newline at end of file diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/SunNative2Ascii.java b/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/SunNative2Ascii.java new file mode 100644 index 000000000..e9c3ef0a4 --- /dev/null +++ b/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/SunNative2Ascii.java @@ -0,0 +1,49 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.tools.ant.taskdefs.optional.native2ascii; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.ProjectComponent; +import org.apache.tools.ant.taskdefs.optional.Native2Ascii; +import org.apache.tools.ant.types.Commandline; + +/** + * Adapter to sun.tools.native2ascii.Main. + * + * @since Ant 1.6.3 + */ +public final class SunNative2Ascii extends DefaultNative2Ascii { + + /** + * Identifies this adapter. + */ + public static final String IMPLEMENTATION_NAME = "sun"; + + protected void setup(Commandline cmd, Native2Ascii args) + throws BuildException { + if (args.getReverse()) { + cmd.createArgument().setValue("-reverse"); + } + super.setup(cmd, args); + } + + protected boolean run(Commandline cmd, ProjectComponent log) + throws BuildException { + sun.tools.native2ascii.Main n2a = new sun.tools.native2ascii.Main(); + return n2a.convert(cmd.getArguments()); + } +} \ No newline at end of file