01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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 org.apache.tools.ant.BuildException;
21: import org.apache.tools.ant.ProjectComponent;
22: import org.apache.tools.ant.taskdefs.ExecuteJava;
23: import org.apache.tools.ant.taskdefs.optional.Native2Ascii;
24: import org.apache.tools.ant.types.Commandline;
25:
26: /**
27: * Adapter to kaffe.tools.native2ascii.Native2Ascii.
28: *
29: * @since Ant 1.6.3
30: */
31: public final class KaffeNative2Ascii extends DefaultNative2Ascii {
32:
33: // sorted by newest Kaffe version first
34: private static final String[] N2A_CLASSNAMES = new String[] {
35: "gnu.classpath.tools.native2ascii.Native2Ascii",
36: // pre Kaffe 1.1.5
37: "kaffe.tools.native2ascii.Native2Ascii", };
38:
39: /**
40: * Identifies this adapter.
41: */
42: public static final String IMPLEMENTATION_NAME = "kaffe";
43:
44: /** {@inheritDoc} */
45: protected void setup(Commandline cmd, Native2Ascii args)
46: throws BuildException {
47: if (args.getReverse()) {
48: throw new BuildException(
49: "-reverse is not supported by Kaffe");
50: }
51: super .setup(cmd, args);
52: }
53:
54: /** {@inheritDoc} */
55: protected boolean run(Commandline cmd, ProjectComponent log)
56: throws BuildException {
57: ExecuteJava ej = new ExecuteJava();
58: Class c = getN2aClass();
59: if (c == null) {
60: throw new BuildException(
61: "Couldn't load Kaffe's Native2Ascii" + " class");
62: }
63:
64: cmd.setExecutable(c.getName());
65: ej.setJavaCommand(cmd);
66: ej.execute(log.getProject());
67: // otherwise ExecuteJava has thrown an exception
68: return true;
69: }
70:
71: /**
72: * tries to load Kaffe Native2Ascii and falls back to the older
73: * class name if necessary.
74: *
75: * @return null if neither class can get loaded.
76: */
77: private static Class getN2aClass() {
78: for (int i = 0; i < N2A_CLASSNAMES.length; i++) {
79: try {
80: return Class.forName(N2A_CLASSNAMES[i]);
81: } catch (ClassNotFoundException cnfe) {
82: // Ignore
83: }
84: }
85: return null;
86: }
87:
88: }
|