1 7 package org.jical; 8 9 import java.io.BufferedReader ; 10 import java.io.BufferedWriter ; 11 import java.io.FileInputStream ; 12 import java.io.FileWriter ; 13 import java.io.InputStreamReader ; 14 import java.text.SimpleDateFormat ; 15 import java.util.Date ; 16 import java.util.LinkedList ; 17 import java.util.StringTokenizer ; 18 19 25 public class Text2ICal { 26 27 public static void main(String [] args) { 28 29 SimpleDateFormat touchformatter = new SimpleDateFormat ("yyyyMMddHHmm.ss"); 30 31 32 38 System.out.println("Input File is :"+args[0]); 39 String inputFile = args[0]; 40 System.out.println("Output (Calendar) File is :"+args[1]); 41 String outputFile = args[1]; 42 System.out.println("Field Details are :"+args[2]); 43 String fieldDetails= args[2]; 44 System.out.println("Rows per event is :"+args[3]); 45 46 Integer rowsPerEvent= new Integer (args[3]); 47 48 49 ICalendar iCal = new ICalendar(); 50 iCal.icalEventCollection = new LinkedList (); 51 iCal.setProdId("JICAL-Text2ICal"); 52 iCal.setVersion("2.0"); 53 int iCtr = 0; 54 55 56 int row[] = new int[100]; 58 int col[] = new int[100]; 59 int endCol[] = new int[100]; 60 String iCalField[] = new String [100]; 61 String parseString[] = new String [100]; 62 63 int iFieldCtr = 0; 64 StringTokenizer st = new StringTokenizer (args[2],","); 65 while(st.hasMoreTokens()) 66 { 67 String fieldDetail = (String )st.nextToken(); 68 StringTokenizer fdtok = new StringTokenizer (fieldDetail,"="); 69 iCalField[iFieldCtr] = (String )fdtok.nextToken(); 70 String rowColStr = (String )fdtok.nextToken(); 71 StringTokenizer rowColTok = new StringTokenizer (rowColStr,"/"); 72 row[iFieldCtr] = new Integer (rowColTok.nextToken()).intValue(); 73 col[iFieldCtr] = new Integer (rowColTok.nextToken()).intValue(); 74 endCol[iFieldCtr] = new Integer (rowColTok.nextToken()).intValue(); 75 if (rowColTok.hasMoreTokens()) 76 parseString[iFieldCtr] = (String )rowColTok.nextToken(); 77 78 iFieldCtr++; 79 } 80 81 System.out.println("Found "+iFieldCtr+ " fields"); 82 System.out.println("Import: "+args[0]); 83 84 try 85 { 86 FileInputStream fin = new FileInputStream (args[0]); 87 BufferedReader myInput = null; 88 89 myInput = new BufferedReader (new InputStreamReader (fin)); 90 91 String thisLine = ""; 92 93 int iRowCtr = 0; 94 if (rowsPerEvent.intValue() == 0) 96 { 97 System.err.println("You forgot parm 4 - rows per event"); 98 return; 99 } 100 String storeRow[] = new String [rowsPerEvent.intValue()+1]; 101 103 while((thisLine = myInput.readLine()) != null) 104 { 105 iRowCtr++; 106 storeRow[iRowCtr] = thisLine; 108 if (iRowCtr == rowsPerEvent.intValue()) 109 { 110 ICalendarVEvent vevent = new ICalendarVEvent(); 112 for (int fieldIdx = 0; fieldIdx < iFieldCtr; fieldIdx++) 114 { 115 Date workDate = new Date (); 116 String fieldValue =storeRow[row[fieldIdx]]; 118 120 if (fieldValue.length() < col[fieldIdx]-1) 121 System.err.println("Ick, line too short:" +fieldValue.length()+" col starts:"+col[fieldIdx]+ " for field val: "+fieldValue); 122 else 123 if (endCol[fieldIdx]-1 > fieldValue.length()) 124 { 125 fieldValue = fieldValue.substring(col[fieldIdx]-1); 127 } 128 else 129 { 130 fieldValue = fieldValue.substring(col[fieldIdx]-1,endCol[fieldIdx]-1); 132 } 133 134 136 if (iCalField[fieldIdx].equalsIgnoreCase("startdate")) 138 { 139 SimpleDateFormat startDateFormatter = new SimpleDateFormat (parseString[fieldIdx]); 142 vevent.setDateStart(startDateFormatter.parse(fieldValue)); 144 } 145 else 146 if (iCalField[fieldIdx].equalsIgnoreCase("enddate")) 147 { 148 SimpleDateFormat endDateFormatter = new SimpleDateFormat (parseString[fieldIdx]); 149 vevent.setDateEnd(endDateFormatter.parse(fieldValue)); 151 } 152 else 153 if (iCalField[fieldIdx].equalsIgnoreCase("summary")) 154 { 155 vevent.setSummary(fieldValue); 156 } 157 else if (iCalField[fieldIdx].equalsIgnoreCase("description")) 158 { 159 vevent.setDescription(fieldValue); 160 } 161 else 162 { 163 System.err.println("Invalid field: "+iCalField[fieldIdx]); 164 } 165 166 if (vevent.getDateStart()!=null 167 && vevent.getDateEnd() !=null) 168 { 169 vevent.setSequence(0); 170 vevent.setEventClass("PUBLIC"); 171 vevent.setTransparency("OPAQUE"); 172 vevent.setDateStamp(workDate); 173 vevent.setCreated(workDate); 174 vevent.setLastModified(workDate); 175 vevent.setOrganizer("MAILTO:sfg@eurekait.com"); 177 iCtr++; 178 System.out.println(iCtr); 179 vevent.setUid("jical-"+touchformatter.format(workDate)+"-"+iCtr); 180 vevent.setPriority(3); 181 182 184 iCal.icalEventCollection.add(vevent); 185 } 186 } 187 iRowCtr = 0; 188 } 189 190 } 191 } 192 catch(Exception e) 193 { 194 e.printStackTrace(); 195 System.err.println("SomethingBad Happened:"+e); 196 } 197 198 try{ 199 200 203 BufferedWriter out = new BufferedWriter (new FileWriter (args[1])); 204 out.write(iCal.getVCalendar()); 205 out.close(); 206 207 } 208 catch (Exception e) 209 { 210 e.printStackTrace(); 211 System.err.println("SomethingBad Happened:"+e); 212 } 213 214 System.out.println("Rendered new TEXT2ICAL calendar file: "+args[1]); 215 216 } 217 } 218 | Popular Tags |