Package models.files

Source Code of models.files.EdbDatabaseReader

package models.files;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import models.Coordinates;
import models.StarStorage;
import models.coordinates.EquatorialCoordinates;
import views.Star;
import controllers.Messages;

public class EdbDatabaseReader implements IDatabaseReader {
 
  Pattern isDataline = Pattern.compile("^[a-zA-Z_0-9].*");
  Matcher isDatalineMatch;
 
  Pattern dataline = Pattern.compile("(.*?),(.*?),(.*?),(.*?),(.*?),.*");
  Matcher datalineMatch;
 
  Pattern pipe = Pattern.compile("(.*?)\\|(.*?)");
  Matcher pipeMatcher;
 
  EquatorialCoordinates c;
  private static final String fixedReferential = "f";
  private static final String starType = "[T\\|B\\|D\\|S\\|V]";

 
  @Override
  public void read(BufferedReader reader, StarStorage stars) throws IOException {
    String line;
    Star s;
    while ((line = reader.readLine()) != null) {
      s = readLine(line);
      if (s != null)
        stars.add(s);
    }
   
  }
 
  public Star readLine(String line) {
    if (isDataLine(line)) {
      datalineMatch = dataline.matcher(line);
      if (!datalineMatch.matches())
        return null;
      try {
        c = new EquatorialCoordinates(Coordinates.HoursMinutesSecondsToRadians(pipeTakeFirst(datalineMatch.group(3))),
                        Coordinates.DegreesMinutesSecondsToRadians(pipeTakeFirst(datalineMatch.group(4))));
        return new Star(datalineMatch.group(1),
                c,
                Double.parseDouble(datalineMatch.group(5)),
                spectralType(datalineMatch.group(2)));
      } catch (NumberFormatException e) {
        Messages.addError("Error while reading database : line "+line+" is malformed.");
        return null;
      } catch (WrongObjectTypeException e) {
        return null;
      }
    }
    return null;
  }
 
  private String pipeTakeFirst(String input) {
    pipeMatcher = pipe.matcher(input);
    if (pipeMatcher.matches())
      return pipeMatcher.group(1);
   
    return input;
  }
 
  private String spectralType(String specString) throws WrongObjectTypeException {
    String[] specs = specString.split("\\|");
    if (! specs[0].equalsIgnoreCase(fixedReferential))
      throw new WrongObjectTypeException("Object read is not a fixed object.");
    if (specs.length != 3)
      throw new WrongObjectTypeException();
    if (! specs[1].matches(starType))
      throw new WrongObjectTypeException("Object is not a star");
    return specs[2];
  }
 
  private boolean isDataLine(String line) {
    isDatalineMatch = isDataline.matcher(line);
    return isDatalineMatch.matches();
  }
 
}
TOP

Related Classes of models.files.EdbDatabaseReader

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.