summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/ojc/CommandArguments.java
blob: 81bc063d185ce15a8024b2dd494bdbbd4ec2692c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * CommandArguments.java
 *
 * comments here.
 *
 * @author   Michiaki Tatsubori
 * @version  %VERSION% %DATE%
 * @see      java.lang.Object
 *
 * COPYRIGHT 1998 by Michiaki Tatsubori, ALL RIGHTS RESERVED.
 */
package io.devnulllabs.openjava.ojc;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Vector;

/**
 * The class <code>CommandArguments</code>
 * <p>
 * For example
 * <pre>
 * </pre>
 * <p>
 *
 * @author   Michiaki Tatsubori
 * @version  1.0
 * @since    $Id: CommandArguments.java,v 1.4 2003/06/18 03:31:53 tatsubori Exp $
 * @see java.lang.Object
 */
public class CommandArguments {
    public static final int DEBUG_VERBOSE = 0x4;
    public static final int DEBUG_CALLER = 0x8;

    private final String[] originalArgs;
    private Hashtable options = new Hashtable();
    private File[] files;

    public CommandArguments(String args[]) throws IOException {
        originalArgs = args;
        files = initFiles();
        checkArguments();
    }

    public File[] getFiles() {
        return files;
    }

    public File[] initFiles() {
        if (originalArgs == null)
            return new File[0];

        //File[] result;
        //int file_num = originalArgs.length - countUpOptions( originalArgs );
        //result = new File[file_num];
        Vector srcfiles = new Vector();

        for (int i = 0, count = 0; i < originalArgs.length; ++i) {
            if (isOption(originalArgs[i])) {
                registerOption(originalArgs[i].substring(1));
            } else {
                //result[count++] = new File( originalArgs[i] );
                addFiles(originalArgs[i], srcfiles);
            }
        }

        File[] result = new File[srcfiles.size()];
        for (int i = 0; i < result.length; ++i) {
            result[i] = (File) srcfiles.elementAt(i);
        }
        return result;
    }

    private static void addFiles(String arg, Vector dest) {
        if (!arg.startsWith("@")) {
            dest.add(new File(arg));
            return;
        }
        FileReader fin;
        try {
            fin = new FileReader(arg.substring(1));
        } catch (IOException e) {
            System.err.println("Bad file name ignored : " + arg);
            return;
        }
        try {
            BufferedReader reader = new BufferedReader(fin);
            String line;
            while ((line = reader.readLine()) != null) {
                String filename = line.trim();
                if (filename.equals(""))
                    break;
                dest.add(new File(filename));
            }
        } catch (IOException e) {
            System.err.println("Bad file format : " + arg);
            return;
        }
    }

    private static boolean isOption(String arg) {
        return (arg != null && arg.startsWith("-"));
    }

    private static int countUpOptions(String[] args) {
        if (args == null)
            return 0;
        int result = 0;
        for (int i = 0; i < args.length; ++i) {
            if (isOption(args[i]))
                ++result;
        }
        return result;
    }

    public void registerOption(String str) {
        Vector v = (Vector) options.get(optionKind(str));
        if (v == null)
            v = new Vector();
        v.addElement(optionValue(str));
        options.put(optionKind(str), v);
    }

    private static String optionKind(String str) {
        int e = str.indexOf('=');
        return (e == -1) ? str : str.substring(0, e).trim();
    }

    private static String optionValue(String str) {
        int e = str.indexOf('=');
        return (e == -1) ? "" : str.substring(e + 1).trim();
    }

    public String getOption(String option_name) {
        Vector v = (Vector) options.get(option_name);
        if (v == null || v.isEmpty())
            return null;
        return (String) v.elementAt(0);
    }

    public String[] getOptions(String option_name) {
        Vector v = (Vector) options.get(option_name);
        String[] result = new String[(v == null) ? 0 : v.size()];
        for (int i = 0; i < result.length; ++i) {
            result[i] = (String) v.elementAt(i);
        }
        return result;
    }

    public String getOption(String opt1, String opt2) {
        String result = getOption(opt1);
        if (result == null)
            result = getOption(opt2);
        return result;
    }

    public int getDebugLevel() {
        int debug_level = 0;

        String level_str = getOption("g");
        if (level_str != null) {
            debug_level = Integer.valueOf(level_str).intValue();
        }
        if (getOption("verbose") != null)
            debug_level |= DEBUG_VERBOSE;

        return debug_level;
    }

    public JavaCompiler getJavaCompiler()
        throws
            ClassNotFoundException,
            InstantiationException,
            IllegalAccessException {
        String compiler_name = getOption("compiler", "c");
        if (compiler_name == null) {
            compiler_name = "jp.ac.tsukuba.openjava.SunJavaCompiler";
        }
        Class clazz = Class.forName(compiler_name);
        JavaCompiler result = (JavaCompiler) clazz.newInstance();
        return result;
    }

    public boolean callerTranslation() {
        if (getOption("calleroff") != null)
            return false;
        return true;
    }

    public boolean qualifyNameFirst() {
        String qualifyname_flag = getOption("callerfast");
        if (qualifyname_flag == null)
            qualifyname_flag = "true";
        if (qualifyname_flag.equals("false"))
            return false;
        return true;
    }

    private void checkArguments() throws IOException {
        /* checks files */
        File[] files = this.getFiles();
        if (files.length == 0) {
            System.err.println("Files are not specified.");
            throw new IOException();
        }
        for (int i = 0; i < files.length; ++i) {
            if (!files[i].canRead()) {
                System.err.println("cannot find file " + files[i]);
                throw new IOException();
            }
            if (!files[i].getName().endsWith(".oj")) {
                System.err.println("illegal file name " + files[i]);
                throw new IOException();
            }
        }
        String diststr = getOption("d");
        if (diststr != null && (!new File(diststr).isDirectory())) {
            System.err.println("Directory does not exist : " + diststr);
            throw new IOException();
        }
    }

}