001package org.cpsolver.studentsct; 002 003import java.io.BufferedReader; 004import java.io.File; 005import java.io.FileReader; 006import java.io.FileWriter; 007import java.io.PrintWriter; 008import java.util.ArrayList; 009import java.util.Iterator; 010import java.util.List; 011 012/** 013 * Process all choice files (files choices.csv) in all subfolders of the given 014 * folder and create a CSV (comma separated values text file) combining all 015 * choices (one column for each choice file) of the found choices files. 016 * 017 * @author Tomáš Müller 018 * @version StudentSct 1.3 (Student Sectioning)<br> 019 * Copyright (C) 2007 - 2014 Tomáš Müller<br> 020 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 021 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 022 * <br> 023 * This library is free software; you can redistribute it and/or modify 024 * it under the terms of the GNU Lesser General Public License as 025 * published by the Free Software Foundation; either version 3 of the 026 * License, or (at your option) any later version. <br> 027 * <br> 028 * This library is distributed in the hope that it will be useful, but 029 * WITHOUT ANY WARRANTY; without even the implied warranty of 030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 031 * Lesser General Public License for more details. <br> 032 * <br> 033 * You should have received a copy of the GNU Lesser General Public 034 * License along with this library; if not see 035 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 036 */ 037public class GetChoices { 038 039 public static void getChoicesFile(File folder, List<List<String>> choices, String prefix) { 040 File choicesFile = new File(folder, "choices.csv"); 041 if (choicesFile.exists()) { 042 System.out.println("Reading " + choicesFile + " ..."); 043 try { 044 List<String> prefixes = null; 045 if (choices.isEmpty()) { 046 prefixes = new ArrayList<String>(); 047 choices.add(prefixes); 048 } else { 049 prefixes = choices.get(0); 050 } 051 prefixes.add(prefix); 052 BufferedReader reader = new BufferedReader(new FileReader(choicesFile)); 053 String line = null; 054 for (int idx = 1; (line = reader.readLine()) != null; idx++) { 055 List<String> cx = null; 056 if (choices.size() <= idx) { 057 cx = new ArrayList<String>(); 058 choices.add(cx); 059 } else { 060 cx = choices.get(idx); 061 } 062 cx.add(line); 063 } 064 reader.close(); 065 } catch (Exception e) { 066 System.err.println("Error reading file " + choicesFile + ", message: " + e.getMessage()); 067 } 068 } 069 } 070 071 public static void getChoices(File folder, List<List<String>> choices, String prefix) { 072 System.out.println("Checking " + folder + " ..."); 073 File[] files = folder.listFiles(); 074 getChoicesFile(folder, choices, (prefix == null ? "" : prefix)); 075 for (int i = 0; i < files.length; i++) 076 if (files[i].isDirectory()) 077 getChoices(files[i], choices, (prefix == null ? "" : prefix + "/") + files[i].getName()); 078 } 079 080 public static void writeChoices(List<List<String>> choices, File file) { 081 try { 082 System.out.println("Writing " + file + " ..."); 083 PrintWriter writer = new PrintWriter(new FileWriter(file, false)); 084 for (List<String> cx : choices) { 085 for (Iterator<String> f = cx.iterator(); f.hasNext();) { 086 String s = f.next(); 087 writer.print(s); 088 if (f.hasNext()) 089 writer.print(","); 090 } 091 writer.println(); 092 } 093 writer.flush(); 094 writer.close(); 095 } catch (Exception e) { 096 System.err.println("Error writing file " + file + ", message: " + e.getMessage()); 097 } 098 } 099 100 public static void main(String[] args) { 101 try { 102 File folder = new File(args[0]); 103 List<List<String>> choices = new ArrayList<List<String>>(); 104 getChoices(folder, choices, null); 105 if (!choices.isEmpty()) 106 writeChoices(choices, new File(folder, "all-choices.csv")); 107 } catch (Exception e) { 108 e.printStackTrace(); 109 } 110 } 111}