001/*
002 *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 *  This file is licensed to You under the Eclipse Public License (EPL);
005 *  You may not use this file except in compliance with the License. You
006 *  may obtain a copy of the License at
007 *
008 *      http://www.opensource.org/licenses/eclipse-1.0.php
009 *
010 *  See the COPYRIGHT.txt file distributed with this work for information
011 *  regarding copyright ownership.
012 */
013package org.jikesrvm.tools.oth;
014
015import java.io.BufferedReader;
016import java.io.FileNotFoundException;
017import java.io.FileReader;
018import java.io.IOException;
019import java.util.StringTokenizer;
020
021public class DefaultFileAccess implements FileAccess {
022
023  @Override
024  public final String[] readOptionStringFromFile(String fileName) throws IOException {
025    BufferedReader in = getBufferedReaderForFile(fileName);
026    StringBuilder s = getStringBuilderToHoldFileContents();
027    readContentIntoStringBuilder(in, s);
028    String[] options = tokenizeInput(s.toString());
029    return options;
030  }
031
032  protected StringBuilder getStringBuilderToHoldFileContents() {
033    return new StringBuilder();
034  }
035
036  protected BufferedReader getBufferedReaderForFile(String fileName) throws FileNotFoundException {
037    return new BufferedReader(new FileReader(fileName));
038  }
039
040  protected final void readContentIntoStringBuilder(BufferedReader in, StringBuilder s) throws IOException {
041    String line = "";
042    while (in.ready() && line != null) {
043      line = in.readLine();
044      if (line != null) {
045        line = line.trim();
046        if (isNonCommentLine(line)) {
047          s.append(line);
048          s.append(" ");
049        }
050      }
051    }
052    in.close();
053  }
054
055  protected final String[] tokenizeInput(String s) {
056    StringTokenizer t = new StringTokenizer(s);
057    String[] av = new String[t.countTokens()];
058    for (int j = 0; j < av.length; j++) {
059      av[j] = t.nextToken();
060    }
061    return av;
062  }
063
064  protected final boolean isNonCommentLine(String line) {
065    return !line.startsWith("#");
066  }
067
068}