OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [tools/] [gnu/] [classpath/] [tools/] [common/] [ClasspathToolParser.java] - Blame information for rev 781

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 779 jeremybenn
/* ClasspathToolParser.java -- Parser subclass for classpath tools
2
   Copyright (C) 2006 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version. */
37
 
38
 
39
package gnu.classpath.tools.common;
40
 
41
import java.io.BufferedReader;
42
import java.io.IOException;
43
import java.io.FileNotFoundException;
44
import java.io.FileReader;
45
import java.io.Reader;
46
import java.text.MessageFormat;
47
import java.util.ArrayList;
48
 
49
import gnu.classpath.Configuration;
50
import gnu.classpath.tools.getopt.FileArgumentCallback;
51
import gnu.classpath.tools.getopt.Option;
52
import gnu.classpath.tools.getopt.OptionException;
53
import gnu.classpath.tools.getopt.Parser;
54
 
55
/**
56
 * This is like the Parser class, but is specialized for use by
57
 * tools distributed with GNU Classpath.  In particular it automatically
58
 * computes the version string using the program's name.
59
 */
60
public class ClasspathToolParser
61
    extends Parser
62
{
63
  private static String getVersionString(String programName)
64
  {
65
    String fmt = (Messages.getString("ClasspathToolParser.VersionFormat")); //$NON-NLS-1$
66
    return MessageFormat.format(fmt,
67
                                new Object[]
68
                                  {
69
                                    programName,
70
                                    Configuration.CLASSPATH_VERSION
71
                                  });
72
  }
73
 
74
  public ClasspathToolParser(String programName)
75
  {
76
    this(programName, false);
77
  }
78
 
79
  public ClasspathToolParser(String programName, boolean longOnly)
80
  {
81
    super(programName, getVersionString(programName), longOnly);
82
    addFinal(new Option('J',
83
                        Messages.getString("ClasspathToolParser.JArgument"),//$NON-NLS-1$
84
                        Messages.getString("ClasspathToolParser.JName"), //$NON-NLS-1$
85
                        true)
86
             {
87
               public void parsed(String argument) throws OptionException
88
               {
89
                 // -J should be handled by the wrapper binary.
90
                 // We add it here so that it shows up in the --help output.
91
               }
92
             });
93
  }
94
 
95
  public void parse(String[] inArgs, FileArgumentCallback files,
96
                    boolean handleFileLists)
97
  {
98
    FileArgumentCallback cb;
99
 
100
    if (handleFileLists)
101
      cb = new AtFileArgumentCallback(files);
102
    else
103
      cb = files;
104
 
105
    parse(inArgs, cb);
106
  }
107
 
108
  public String[] parse(String[] inArgs, boolean handleFileLists)
109
  {
110
    final ArrayList<String> fileResult = new ArrayList<String>();
111
 
112
    final FileArgumentCallback cb = new FileArgumentCallback()
113
      {
114
        public void notifyFile(String fileArgument)
115
        {
116
          fileResult.add(fileArgument);
117
        }
118
      };
119
 
120
    if (handleFileLists)
121
      parse(inArgs, new AtFileArgumentCallback(cb));
122
    else
123
      parse(inArgs, cb);
124
 
125
    return fileResult.toArray(new String[fileResult.size()]);
126
  }
127
 
128
 
129
  /**
130
   * Simple function that takes the given {@link Reader}, treats it like
131
   * a textfile and reads all the whitespace separated entries from it
132
   * and adds them to the @{link FileArgumentCallback} instance.
133
   *
134
   * @param reader the reader to read from.
135
   * @param cb the callback to post the filenames to.
136
   * @throws OptionException if an error occurs reading the list.
137
   */
138
  public void parseFileList(Reader reader, FileArgumentCallback cb)
139
        throws OptionException
140
  {
141
    BufferedReader breader = new BufferedReader(reader);
142
    String line = null;
143
 
144
    try
145
      {
146
        while ((line = breader.readLine()) != null)
147
          parseLine(line, cb);
148
 
149
        reader.close();
150
      }
151
    catch (IOException ioe)
152
      {
153
        throw new OptionException("I/O error while reading a file list", ioe);
154
      }
155
 
156
  }
157
 
158
  /**
159
   * Parses whitespace separated file entries.
160
   *
161
   * Note: This is not coping with whitespace in files or quoting.
162
   *
163
   * @param line the line of the file to parse.
164
   * @param cb the callback to pass the parsed file to.
165
   * @throws IOException if an I/O error occurs.
166
   * @throws OptionException if an error occurs in the callback.
167
   */
168
  private void parseLine(String line, FileArgumentCallback cb)
169
    throws IOException, OptionException
170
  {
171
    final int length = line.length();
172
    int start = 0;
173
    int end = 0;
174
 
175
                // While not reached end of line ...
176
    while (start < length)
177
      {
178
        // Search for first non-whitespace character for the start of a word.
179
        while (Character.isWhitespace(line.codePointAt(start)))
180
          {
181
            start++;
182
 
183
            if (start == length)
184
              return;
185
          }
186
 
187
        end = start + 1;
188
 
189
        // Search for first whitespace character for the end of a word.
190
        while (end < length && !Character.isWhitespace(line.codePointAt(end)))
191
          end++;
192
 
193
        cb.notifyFile(line.substring(start, end));
194
 
195
        start = end + 1;
196
      }
197
  }
198
 
199
  /**
200
   * Implementation of {@link FileArgumentCallback} that handles
201
   * file arguments in {@link #notifyFile} starting with a <code>@</code>
202
   * through {@link ClasspathToolParser#parseFileList}.
203
   */
204
  class AtFileArgumentCallback extends FileArgumentCallback
205
  {
206
    FileArgumentCallback cb;
207
 
208
    AtFileArgumentCallback(FileArgumentCallback cb)
209
    {
210
      this.cb = cb;
211
    }
212
 
213
    @Override
214
    public void notifyFile(String fileArgument)
215
      throws OptionException
216
    {
217
      if (fileArgument.codePointAt(0) == '@')
218
        {
219
          FileReader fr = null;
220
 
221
          try
222
            {
223
              fr = new FileReader(fileArgument.substring(1));
224
            }
225
          catch (FileNotFoundException fnfe)
226
            {
227
              throw new OptionException("File not found: " + fileArgument.substring(1),
228
                                        fnfe);
229
            }
230
 
231
          ClasspathToolParser.this.parseFileList(fr, cb);
232
        }
233
      else
234
        cb.notifyFile(fileArgument);
235
    }
236
 
237
  }
238
 
239
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.