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 --> - - + diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/Native2Ascii.java b/src/main/org/apache/tools/ant/taskdefs/optional/Native2Ascii.java index 0f5d40c0a..743441e4e 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/Native2Ascii.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/Native2Ascii.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2004 The Apache Software Foundation + * Copyright 2000-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. @@ -22,6 +22,8 @@ import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.MatchingTask; +import org.apache.tools.ant.taskdefs.optional.native2ascii.Native2AsciiAdapter; +import org.apache.tools.ant.taskdefs.optional.native2ascii.Native2AsciiAdapterFactory; import org.apache.tools.ant.types.Commandline; import org.apache.tools.ant.types.Mapper; import org.apache.tools.ant.util.FileNameMapper; @@ -54,6 +56,15 @@ public class Native2Ascii extends MatchingTask { this.reverse = reverse; } + /** + * The value of the reverse attribute. + * + * @since Ant 1.6.3 + */ + public boolean getReverse() { + return reverse; + } + /** * Set the encoding to translate to/from. * If unset, the default encoding for the JVM is used. @@ -65,6 +76,15 @@ public class Native2Ascii extends MatchingTask { this.encoding = encoding; } + /** + * The value of the reverse attribute. + * + * @since Ant 1.6.3 + */ + public String getEncoding() { + return encoding; + } + /** * Set the source directory in which to find files to convert. * @@ -181,29 +201,15 @@ public class Native2Ascii extends MatchingTask { * @param srcName name of the input file. * @param destName name of the input file. */ - private void convert(String srcName, String destName) throws BuildException { - - Commandline cmd = new Commandline(); // Command line to run + private void convert(String srcName, String destName) + throws BuildException { File srcFile; // File to convert File destFile; // where to put the results - // Set up the basic args (this could be done once, but - // it's cleaner here) - if (reverse) { - cmd.createArgument().setValue("-reverse"); - } - - if (encoding != null) { - cmd.createArgument().setValue("-encoding"); - cmd.createArgument().setValue(encoding); - } - // Build the full file names srcFile = new File(srcDir, srcName); destFile = new File(destDir, destName); - cmd.createArgument().setFile(srcFile); - cmd.createArgument().setFile(destFile); // Make sure we're not about to clobber something if (srcFile.equals(destFile)) { throw new BuildException("file " + srcFile @@ -223,9 +229,9 @@ public class Native2Ascii extends MatchingTask { } log("converting " + srcName, Project.MSG_VERBOSE); - sun.tools.native2ascii.Main n2a - = new sun.tools.native2ascii.Main(); - if (!n2a.convert(cmd.getArguments())) { + Native2AsciiAdapter ad = + Native2AsciiAdapterFactory.getAdapter(null, this); + if (!ad.convert(this, srcFile, destFile)) { throw new BuildException("conversion failed"); } } diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/DefaultNative2Ascii.java b/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/DefaultNative2Ascii.java new file mode 100644 index 000000000..81d5ba363 --- /dev/null +++ b/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/DefaultNative2Ascii.java @@ -0,0 +1,93 @@ +/* + * 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 java.io.File; +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; + +/** + * encapsulates the handling common to diffent Native2Asciiadapter + * implementations. + * + * @since Ant 1.6.3 + */ +public abstract class DefaultNative2Ascii implements Native2AsciiAdapter { + + public DefaultNative2Ascii() { + } + + /** + * Splits the task into setting up the command line switches + * (delegated to {@link #setup setup}), adding the file names + * (delegated to {@link addFiles addFiles}) and running the tool + * (delegated to {@link #run run}). + */ + public final boolean convert(Native2Ascii args, File srcFile, + File destFile) throws BuildException { + Commandline cmd = new Commandline(); + setup(cmd, args); + addFiles(cmd, args, srcFile, destFile); + return run(cmd, args); + } + + /** + * Sets up the initial command line. + * + *

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