KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jical > Text2ICal


1 /*
2  * Created on 29-Oct-2004
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */

7 package org.jical;
8
9 import java.io.BufferedReader JavaDoc;
10 import java.io.BufferedWriter JavaDoc;
11 import java.io.FileInputStream JavaDoc;
12 import java.io.FileWriter JavaDoc;
13 import java.io.InputStreamReader JavaDoc;
14 import java.text.SimpleDateFormat JavaDoc;
15 import java.util.Date JavaDoc;
16 import java.util.LinkedList JavaDoc;
17 import java.util.StringTokenizer JavaDoc;
18
19 /**
20  * @author sfg
21  *
22  * TODO To change the template for this generated type comment go to
23  * Window - Preferences - Java - Code Style - Code Templates
24  */

25 public class Text2ICal {
26
27     public static void main(String JavaDoc[] args) {
28         
29         SimpleDateFormat JavaDoc touchformatter = new SimpleDateFormat JavaDoc("yyyyMMddHHmm.ss");
30
31         
32         // Parameters
33
// 1 - input file - ie archlog
34
// 2 - output cal file.
35
// 3 - field=row/startcol/endcol/parseString,field=row/startcol/endcol/parseString,...etc
36
// 4 - rows per event
37

38         System.out.println("Input File is :"+args[0]);
39         String JavaDoc inputFile = args[0];
40         System.out.println("Output (Calendar) File is :"+args[1]);
41         String JavaDoc outputFile = args[1];
42         System.out.println("Field Details are :"+args[2]);
43         String JavaDoc fieldDetails= args[2];
44         System.out.println("Rows per event is :"+args[3]);
45         
46         Integer JavaDoc rowsPerEvent= new Integer JavaDoc(args[3]);
47         
48
49         ICalendar iCal = new ICalendar();
50         iCal.icalEventCollection = new LinkedList JavaDoc();
51         iCal.setProdId("JICAL-Text2ICal");
52         iCal.setVersion("2.0");
53         int iCtr = 0;
54         
55         
56         // First get the fields and locations.
57
int row[] = new int[100];
58         int col[] = new int[100];
59         int endCol[] = new int[100];
60         String JavaDoc iCalField[] = new String JavaDoc[100];
61         String JavaDoc parseString[] = new String JavaDoc[100];
62         
63         int iFieldCtr = 0;
64         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(args[2],",");
65         while(st.hasMoreTokens())
66         {
67             String JavaDoc fieldDetail = (String JavaDoc)st.nextToken();
68             StringTokenizer JavaDoc fdtok = new StringTokenizer JavaDoc(fieldDetail,"=");
69             iCalField[iFieldCtr] = (String JavaDoc)fdtok.nextToken();
70             String JavaDoc rowColStr = (String JavaDoc)fdtok.nextToken();
71             StringTokenizer JavaDoc rowColTok = new StringTokenizer JavaDoc(rowColStr,"/");
72             row[iFieldCtr] = new Integer JavaDoc(rowColTok.nextToken()).intValue();
73             col[iFieldCtr] = new Integer JavaDoc(rowColTok.nextToken()).intValue();
74             endCol[iFieldCtr] = new Integer JavaDoc(rowColTok.nextToken()).intValue();
75             if (rowColTok.hasMoreTokens())
76                 parseString[iFieldCtr] = (String JavaDoc)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 JavaDoc fin = new FileInputStream JavaDoc(args[0]);
87             BufferedReader JavaDoc myInput = null;
88             
89             myInput = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(fin));
90             
91             String JavaDoc thisLine = "";
92             
93             int iRowCtr = 0;
94             // There had better be a parm 4!!
95
if (rowsPerEvent.intValue() == 0)
96             {
97                 System.err.println("You forgot parm 4 - rows per event");
98                 return;
99             }
100             String JavaDoc storeRow[] = new String JavaDoc[rowsPerEvent.intValue()+1];
101             /* Two loops, first joins lines together, second processes lines..
102              */

103             while((thisLine = myInput.readLine()) != null)
104             {
105                 iRowCtr++;
106                 //System.out.println("ROW:"+thisLine);
107
storeRow[iRowCtr] = thisLine;
108                 if (iRowCtr == rowsPerEvent.intValue())
109                 {
110                     // Here we are, create an event..
111
ICalendarVEvent vevent = new ICalendarVEvent();
112                     //SimpleDateFormat syslogformatter = new SimpleDateFormat("yyyy MMM dd HH:mm:ss");
113
for (int fieldIdx = 0; fieldIdx < iFieldCtr; fieldIdx++)
114                     {
115                         Date JavaDoc workDate = new Date JavaDoc();
116                         // Extract required row for this field.
117
String JavaDoc fieldValue =storeRow[row[fieldIdx]];
118                         //System.out.println("Row is:"+fieldValue);
119

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                             //System.out.println("option1:"+col[fieldIdx]);
126
fieldValue = fieldValue.substring(col[fieldIdx]-1);
127                         }
128                         else
129                         {
130                             //System.out.println("option2:"+col[fieldIdx]);
131
fieldValue = fieldValue.substring(col[fieldIdx]-1,endCol[fieldIdx]-1);
132                         }
133                         
134                         //System.out.println("Field is :"+ iCalField[fieldIdx]+ " value is: "+fieldValue);
135

136                         // Process a field into the event.
137
if (iCalField[fieldIdx].equalsIgnoreCase("startdate"))
138                         {
139                             // This should be done once only!
140
//System.out.println("Parse this:"+fieldValue+ " with this" +parseString[fieldIdx]);
141
SimpleDateFormat JavaDoc startDateFormatter = new SimpleDateFormat JavaDoc(parseString[fieldIdx]);
142                             //System.out.println("Parse this:"+fieldValue+ " with this" +parseString[fieldIdx]+" Parsed Results: "+startDateFormatter.parse(fieldValue));
143
vevent.setDateStart(startDateFormatter.parse(fieldValue));
144                         }
145                         else
146                         if (iCalField[fieldIdx].equalsIgnoreCase("enddate"))
147                         {
148                             SimpleDateFormat JavaDoc endDateFormatter = new SimpleDateFormat JavaDoc(parseString[fieldIdx]);
149                             //System.out.println("Parse this:"+fieldValue+ " with this" +parseString[fieldIdx]+" Parsed Results: "+endDateFormatter.parse(fieldValue));
150
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.setAttach(photoFile.toURL().toString());
176
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                             //System.out.println(vevent.toVEvent());
183

184                             iCal.icalEventCollection.add(vevent);
185                         }
186                     }
187                     iRowCtr = 0;
188                 }
189                 
190             }
191         }
192         catch(Exception JavaDoc e)
193         {
194             e.printStackTrace();
195             System.err.println("SomethingBad Happened:"+e);
196         }
197
198         try{
199
200         // Now write to string and view as file.
201
//System.out.println(iCal.getVCalendar());
202

203             BufferedWriter JavaDoc out = new BufferedWriter JavaDoc(new FileWriter JavaDoc(args[1]));
204             out.write(iCal.getVCalendar());
205             out.close();
206             
207         }
208         catch (Exception JavaDoc 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