summaryrefslogtreecommitdiffstats
path: root/sched/src/com/android/sched/util/config/cli/TokenIterator.java
blob: b8ec22c1fcb229714f9a5910de9bcbc6c22da36f (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * 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 com.android.sched.util.config.cli;

import com.android.sched.util.file.CannotCreateFileException;
import com.android.sched.util.file.CannotReadException;
import com.android.sched.util.file.CannotSetPermissionException;
import com.android.sched.util.file.Directory;
import com.android.sched.util.file.FileAlreadyExistsException;
import com.android.sched.util.file.FileOrDirectory.ChangePermission;
import com.android.sched.util.file.FileOrDirectory.Existence;
import com.android.sched.util.file.FileOrDirectory.Permission;
import com.android.sched.util.file.InputStreamFile;
import com.android.sched.util.file.NoSuchFileException;
import com.android.sched.util.file.NotDirectoryException;
import com.android.sched.util.file.NotFileException;
import com.android.sched.util.file.NotFileOrDirectoryException;
import com.android.sched.util.file.WrongPermissionException;
import com.android.sched.util.location.LineLocation;
import com.android.sched.util.location.Location;
import com.android.sched.util.location.NoLocation;
import com.android.sched.util.log.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.util.NoSuchElementException;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/**
 * Iterator which tokenizes a list of {@link String}.
 *
 * This iterator manages implicitly references to files by automatically tokenizing the content of
 * these files.
 */
public class TokenIterator {
  @Nonnull
  private static final Logger logger = LoggerFactory.getLogger();

  @Nonnull
  private static final Entry NULL = new Entry();
  private static final char  DEFAULT_FILE_PREFIX = '@';

  private char    filePrefix = DEFAULT_FILE_PREFIX;
  private boolean allowFileRefInArray = true;
  private boolean allowFileRefInFile  = false;

  @CheckForNull
  private Directory baseDirectory = null;

  @Nonnull
  private final String[] args;
  private       int      index = 0;

  @Nonnull
  private Entry next = NULL;
  @Nonnull
  private Entry current = NULL;
  @CheckForNull
  private IOException pending = null;

  private class Sources {
    private class Source {
      @CheckForNull
      private final StreamTokenizer tokenizer;
      @Nonnull
      private final Location location;
      @CheckForNull
      private final InputStreamFile file;

      public Source(@Nonnull Location location) {
        this.tokenizer = null;
        this.file = null;
        this.location = location;
      }

      public Source(@Nonnull InputStreamFile file, @Nonnull StreamTokenizer tokenizer) {
        this.tokenizer = tokenizer;
        this.file = file;
        this.location = file.getLocation();
      }
    }

    @Nonnull
    private final Stack<Source> stack = new Stack<Source>();

    public void push(@Nonnull Location location) {
      stack.push(new Source(location));
    }

    public void push(@Nonnull String fileName) throws WrongPermissionException,
        NoSuchFileException, NotFileException {
      InputStreamFile file = new InputStreamFile(baseDirectory, fileName);

      stack.push(new Source(file, getTokenizer(file)));
    }

    public void pop() {
      InputStreamFile file = stack.pop().file;

      if (file != null) {
        try {
          file.getInputStream().close();
        } catch (IOException e) {
          logger.log(Level.FINE, "Cannot close " + file.getLocation().getDescription());
        }
      }
    }

    public void clear() {
      while (!stack.isEmpty()) {
        pop();
      }
    }

    @Nonnull
    public Location getCurrentLocation() {
      return stack.peek().location;
    }

    @CheckForNull
    public StreamTokenizer getCurrentTokenizer() {
      return stack.peek().tokenizer;
    }
  }

  private final Sources sources = new Sources();

  public TokenIterator (@Nonnull Location location, @Nonnull String... args) {
    this.args = args.clone();
    sources.push(location);
  }

  @Nonnull
  public TokenIterator withFilePrefix(char filePrefix) {
    this.filePrefix = filePrefix;

    return this;
  }

  @Nonnull
  public TokenIterator allowFileReferenceInFile() {
    this.allowFileRefInFile = true;

    return this;
  }

  @Nonnull
  public TokenIterator withFileRelativeTo(@Nonnull File directory) throws NotDirectoryException,
      WrongPermissionException, NoSuchFileException {
    try {
      this.baseDirectory = new Directory(directory.getPath(), null, Existence.MUST_EXIST,
          Permission.EXECUTE, ChangePermission.NOCHANGE);
    } catch (CannotSetPermissionException e) {
      // we're not changing the permissions
      throw new AssertionError(e);
    } catch (FileAlreadyExistsException e) {
      // we're not creating the directory
      throw new AssertionError(e);
    } catch (CannotCreateFileException e) {
      // we're not creating the directory
      throw new AssertionError(e);
    }

    return this;
  }

  @Nonnull
  public TokenIterator disallowFileReferenceInArray() {
    this.allowFileRefInArray = false;

    return this;
  }

  /**
   * @return {@code true} if there is more tokens, {@code false} otherwise.
   */
  public boolean hasNext() {
    if (next == NULL) {
      try {
        next = getNext();
      } catch (NoSuchElementException e) {
        return false;
      } catch (IOException e) {
        pending = e;
      }
    }

    return true;
  }

  /**
   * Return the next Token. It will become the current token.
   * In case of exceptions, the current token is discarded.
   *
   * @return the next token.
   */
  @Nonnull
  public String next()
      throws NoSuchElementException,
      WrongPermissionException,
      NoSuchFileException,
      NotFileOrDirectoryException,
      CannotReadException {
    throwIfPending();

    if (next == NULL) {
      try {
        current = getNext();
      } catch (NoSuchElementException e) {
        current = NULL;
        throw e;
      } catch (IOException e) {
        current = NULL;
        pending = e;
        throwIfPending();
        throw new AssertionError();
      }
    } else {
      current = next;
      next = NULL;
    }

    assert current.value != null;
    return current.value;
  }

  /**
   * @return the current token, or {@code null} if no current token exists.
   */
  @CheckForNull
  public String getToken() throws WrongPermissionException, NoSuchFileException,
      NotFileOrDirectoryException, CannotReadException {
    throwIfPending();

    return current.value;
  }

  /**
   * @return the location of the current token, or {@code null} if no current token exists.
   */
  @CheckForNull
  public Location getLocation() throws WrongPermissionException, NoSuchFileException,
      NotFileOrDirectoryException, CannotReadException {
    throwIfPending();

    return current.location;
  }

  private void throwIfPending() throws WrongPermissionException, NoSuchFileException,
      NotFileOrDirectoryException, CannotReadException {
    if (pending != null) {
      if (pending instanceof WrongPermissionException) {
        throw (WrongPermissionException) pending;
      } else if (pending instanceof NoSuchFileException) {
        throw (NoSuchFileException) pending;
      } else if (pending instanceof NotFileOrDirectoryException) {
        throw (NotFileOrDirectoryException) pending;
      } else if (pending instanceof CannotReadException) {
        throw (CannotReadException) pending;
      } else {
        throw new AssertionError();
      }
    }
  }

  @Nonnull
  private Entry getNext()
      throws NoSuchElementException,
      WrongPermissionException,
      NoSuchFileException,
      NotFileException,
      CannotReadException {

    while (true) {
      StreamTokenizer tokenizer = sources.getCurrentTokenizer();

      while (tokenizer != null) {
        try {
          if (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
            if (allowFileRefInFile && (!tokenizer.sval.isEmpty())
                && tokenizer.sval.charAt(0) == filePrefix) {
              // If it is a @<file_name>, create a tokenizer with this file, and set it to current
              sources.push(tokenizer.sval.substring(1));
              tokenizer = sources.getCurrentTokenizer();
              continue;
            } else {
              // If the current tokenizer has a next, return it
              return new Entry(tokenizer.sval, new LineLocation(sources.getCurrentLocation(),
                  tokenizer.lineno()));
            }
          }
        } catch (IOException e) {
          try {
            throw new CannotReadException(sources.getCurrentLocation());
          } finally {
            // Stop the iterator
            sources.clear();
            sources.push(new NoLocation());
            index = args.length;
          }
        }

        // Else, go to the next one
        sources.pop();
        tokenizer = sources.getCurrentTokenizer();
      }

      // If the is no current tokenizer, switch to arg
      if (index >= args.length) {
        // If all args have been already returned, it is the end
        throw new NoSuchElementException();
      }

      // Else, analyze the next arg
      if (allowFileRefInArray && (!args[index].isEmpty()) && args[index].charAt(0) == filePrefix) {
        // If it is a @<file_name>, create a tokenizer with this file, and set it to current
        sources.push(args[index].substring(1));
        index++;
      } else {
        // Else, return the arg
        return new Entry(args[index++], sources.getCurrentLocation());
      }
    }
  }

  @Nonnull
  protected StreamTokenizer getTokenizer(@Nonnull InputStreamFile file) {
    StreamTokenizer tokenizer;

    Reader reader = new InputStreamReader(file.getInputStream());
    tokenizer = new StreamTokenizer(reader);

    tokenizer.resetSyntax();
    tokenizer.wordChars(0, 255);
    tokenizer.whitespaceChars(' ', ' ');
    tokenizer.whitespaceChars('\t', '\t');
    tokenizer.whitespaceChars('\n', '\n');
    tokenizer.whitespaceChars('\r', '\r');
    tokenizer.quoteChar('\'');
    tokenizer.quoteChar('\"');
    tokenizer.commentChar('#');
    tokenizer.eolIsSignificant(false);
    tokenizer.slashSlashComments(false);
    tokenizer.slashStarComments(false);
    tokenizer.lowerCaseMode(false);

    return tokenizer;
  }

  private static class Entry {
    @CheckForNull
    private final String value;
    @CheckForNull
    private final Location location;

    private Entry() {
      this(null, null);
    }

    private Entry(@CheckForNull String value, @CheckForNull Location location) {
      this.value = value;
      this.location = location;
    }
  }
}