git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@277561 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -162,9 +162,8 @@ | |||||
| Kaffe for example --> | Kaffe for example --> | ||||
| <selector id="needs.sun.tools"> | <selector id="needs.sun.tools"> | ||||
| <or> | <or> | ||||
| <filename name="${optional.package}/Native2Ascii*"/> | |||||
| <filename name="${optional.package}/Javah*"/> | <filename name="${optional.package}/Javah*"/> | ||||
| <filename name="${optional.package}/native2ascii/*"/> | |||||
| <filename name="${optional.package}/native2ascii/Sun*"/> | |||||
| </or> | </or> | ||||
| </selector> | </selector> | ||||
| @@ -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"); | * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
| * you may not use this file except in compliance with 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.DirectoryScanner; | ||||
| import org.apache.tools.ant.Project; | import org.apache.tools.ant.Project; | ||||
| import org.apache.tools.ant.taskdefs.MatchingTask; | 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.Commandline; | ||||
| import org.apache.tools.ant.types.Mapper; | import org.apache.tools.ant.types.Mapper; | ||||
| import org.apache.tools.ant.util.FileNameMapper; | import org.apache.tools.ant.util.FileNameMapper; | ||||
| @@ -54,6 +56,15 @@ public class Native2Ascii extends MatchingTask { | |||||
| this.reverse = reverse; | 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. | * Set the encoding to translate to/from. | ||||
| * If unset, the default encoding for the JVM is used. | * If unset, the default encoding for the JVM is used. | ||||
| @@ -65,6 +76,15 @@ public class Native2Ascii extends MatchingTask { | |||||
| this.encoding = encoding; | 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. | * 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 srcName name of the input file. | ||||
| * @param destName 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 srcFile; // File to convert | ||||
| File destFile; // where to put the results | 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 | // Build the full file names | ||||
| srcFile = new File(srcDir, srcName); | srcFile = new File(srcDir, srcName); | ||||
| destFile = new File(destDir, destName); | destFile = new File(destDir, destName); | ||||
| cmd.createArgument().setFile(srcFile); | |||||
| cmd.createArgument().setFile(destFile); | |||||
| // Make sure we're not about to clobber something | // Make sure we're not about to clobber something | ||||
| if (srcFile.equals(destFile)) { | if (srcFile.equals(destFile)) { | ||||
| throw new BuildException("file " + srcFile | throw new BuildException("file " + srcFile | ||||
| @@ -223,9 +229,9 @@ public class Native2Ascii extends MatchingTask { | |||||
| } | } | ||||
| log("converting " + srcName, Project.MSG_VERBOSE); | 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"); | throw new BuildException("conversion failed"); | ||||
| } | } | ||||
| } | } | ||||
| @@ -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. | |||||
| * | |||||
| * <p>only the -encoding argument gets handled here.</p> | |||||
| * | |||||
| * @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. | |||||
| * | |||||
| * <p>This implementation adds them without any leading | |||||
| * qualifiers, source first.</p> | |||||
| * | |||||
| * @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; | |||||
| } | |||||
| @@ -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; | |||||
| } | |||||
| } | |||||
| @@ -14,11 +14,27 @@ | |||||
| * limitations under the License. | * 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 java.io.File; | ||||
| import org.apache.tools.ant.BuildException; | |||||
| import org.apache.tools.ant.taskdefs.optional.Native2Ascii; | import org.apache.tools.ant.taskdefs.optional.Native2Ascii; | ||||
| /** | |||||
| * Interface for an adapter to a native2ascii implementation. | |||||
| * | |||||
| * @since Ant 1.6.3 | |||||
| */ | |||||
| public interface Native2AsciiAdapter { | 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; | |||||
| } | } | ||||
| @@ -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); | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -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()); | |||||
| } | |||||
| } | |||||