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