/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Copyright (C) 2013 Marchand Eric <ricoh51@free.fr>
This file is part of Freegressi.
Freegressi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Freegressi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Freegressi. If not, see <http://www.gnu.org/licenses/>.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package freegressi.main;
import freegressi.tableur.Colonne;
import freegressi.tableur.Tableur;
import freegressi.tableur.Tableur.Angle;
import java.util.ArrayList;
import java.util.List;
/**
* Classe qui transforme le contenu d'un fichier regressi rw3 en
* une feuille de calcul.
* @author marchand, 12 mai 2013, 21:59:09
*/
public class RW3Handler {
private static String EOL = "\r\n";
// private static String regex = "[ £]";
// private static String regex2 = "[ &]";
// private static String regex3 = "[ ]";
private static String FIRSTLINE = "EVARISTE REGRESSI WINDOWS 1.0";
private static String ERRORFORMAT = "Le fichier RW3 n'est pas au bon format!";
private List<Tableur> sheets = new ArrayList<>();
private String comment;
private Angle angle = Angle.RADIAN;
private String[] lines;
private String[] tokens;
private int count;
private List<String> names = new ArrayList<>();
private List<String> units = new ArrayList<>();
public List<Tableur> getSheets(){
return sheets;
}
public void parse(String str) throws RuntimeException{
lines = str.split(EOL);
int n = lines.length;
if (n == 0){
throw new RuntimeException("Le fichier RW3 ne contient aucune donnée!");
}
if (!lines[0].equals(FIRSTLINE)){
throw new RuntimeException(ERRORFORMAT);
}
String line;
int i = 1;
while (i < n){
line = lines[i];
tokens = line.split("[ £&]");
if (getItem1Item2Count(tokens, "NOM", "VAR", 4, "£") != -1){
doNames(i + 1);
i = i + count + 1;
} else if (getItem1Item2Count(tokens, "UNITE", "VAR", 4, "£") != -1){
doUnits(i + 1);
i = i + count + 1;
} else if (getItem1Item2Count(tokens, "TRIGO", "", 3, "£") != -1){
doAngle(i + 1);
i = i + count + 1;
} else if (getItem1Item2Count(tokens, "PAGE", "COMMENT", 4, "£") != -1){
doSheet(i + 1);
i = i + count + 1;
} else if (getItem1Item2Count(tokens, "VALEUR", "VAR", 4, "&") != -1){
doValues(i + 1);
i = i + count + 1;
} else {
i++;
}
}
}
private void doNames(int index){
for (int i = 0; i < count; i++){
names.add(lines[index + i]);
}
}
private void doUnits(int index){
for (int i = 0; i < count; i++){
units.add(lines[index + i]);
}
}
private void doAngle(int index){
String str = lines[index];
try{
int a = Integer.parseInt(str);
angle = a == 0? Angle.RADIAN: Angle.DEGRE;
} catch (Exception e){
throw new RuntimeException(ERRORFORMAT);
}
}
private void doSheet(int index){
if (count > 0){
comment = lines[index];
}
}
private void doValues(int index){
ArrayList<Colonne> columns = new ArrayList<>();
int varCount = names.size();
for (int i = 0; i < varCount; i++){
Colonne c = new Colonne(names.get(i), units.get(i));
columns.add(c);
}
String line;
String[] tok;
double d;
for (int i = 0; i < count; i++){
line = lines[index + i].trim();
tok = line.split(" ");
for (int j = 0; j < varCount; j++){
try{
d = Double.parseDouble(tok[j]);
columns.get(j).ajoute(d);
} catch (Exception e){
throw new RuntimeException(ERRORFORMAT);
}
}
}
Tableur sheet = new Tableur(columns);
sheet.setAngle(angle);
sheet.setDescription(comment);
sheet.setName("Feuille " + (sheets.size() + 1));
sheet.calculeTout(count);
sheets.add(sheet);
}
private int getItem1Item2Count(String[] tokens, String item1, String item2,
int tokensCount, String firstToken){
count = -1;
if (tokens.length == tokensCount && item1.equals(tokens[2]) ){
if (tokens.length < 4 || item2.equals(tokens[3])){
try{
count = Integer.parseInt(tokens[1]);
} catch (Exception e){
throw new RuntimeException(ERRORFORMAT);
}
}
}
return count;
}
}