001: /*
002: * Created on 29-Oct-2004
003: *
004: * TODO To change the template for this generated file go to
005: * Window - Preferences - Java - Code Style - Code Templates
006: */
007: package org.jical;
008:
009: import java.io.BufferedReader;
010: import java.io.BufferedWriter;
011: import java.io.FileInputStream;
012: import java.io.FileWriter;
013: import java.io.InputStreamReader;
014: import java.text.SimpleDateFormat;
015: import java.util.Date;
016: import java.util.LinkedList;
017: import java.util.StringTokenizer;
018:
019: /**
020: * @author sfg
021: *
022: * TODO To change the template for this generated type comment go to
023: * Window - Preferences - Java - Code Style - Code Templates
024: */
025: public class Text2ICal {
026:
027: public static void main(String[] args) {
028:
029: SimpleDateFormat touchformatter = new SimpleDateFormat(
030: "yyyyMMddHHmm.ss");
031:
032: // Parameters
033: // 1 - input file - ie archlog
034: // 2 - output cal file.
035: // 3 - field=row/startcol/endcol/parseString,field=row/startcol/endcol/parseString,...etc
036: // 4 - rows per event
037:
038: System.out.println("Input File is :" + args[0]);
039: String inputFile = args[0];
040: System.out.println("Output (Calendar) File is :" + args[1]);
041: String outputFile = args[1];
042: System.out.println("Field Details are :" + args[2]);
043: String fieldDetails = args[2];
044: System.out.println("Rows per event is :" + args[3]);
045:
046: Integer rowsPerEvent = new Integer(args[3]);
047:
048: ICalendar iCal = new ICalendar();
049: iCal.icalEventCollection = new LinkedList();
050: iCal.setProdId("JICAL-Text2ICal");
051: iCal.setVersion("2.0");
052: int iCtr = 0;
053:
054: // First get the fields and locations.
055: int row[] = new int[100];
056: int col[] = new int[100];
057: int endCol[] = new int[100];
058: String iCalField[] = new String[100];
059: String parseString[] = new String[100];
060:
061: int iFieldCtr = 0;
062: StringTokenizer st = new StringTokenizer(args[2], ",");
063: while (st.hasMoreTokens()) {
064: String fieldDetail = (String) st.nextToken();
065: StringTokenizer fdtok = new StringTokenizer(fieldDetail,
066: "=");
067: iCalField[iFieldCtr] = (String) fdtok.nextToken();
068: String rowColStr = (String) fdtok.nextToken();
069: StringTokenizer rowColTok = new StringTokenizer(rowColStr,
070: "/");
071: row[iFieldCtr] = new Integer(rowColTok.nextToken())
072: .intValue();
073: col[iFieldCtr] = new Integer(rowColTok.nextToken())
074: .intValue();
075: endCol[iFieldCtr] = new Integer(rowColTok.nextToken())
076: .intValue();
077: if (rowColTok.hasMoreTokens())
078: parseString[iFieldCtr] = (String) rowColTok.nextToken();
079:
080: iFieldCtr++;
081: }
082:
083: System.out.println("Found " + iFieldCtr + " fields");
084: System.out.println("Import: " + args[0]);
085:
086: try {
087: FileInputStream fin = new FileInputStream(args[0]);
088: BufferedReader myInput = null;
089:
090: myInput = new BufferedReader(new InputStreamReader(fin));
091:
092: String this Line = "";
093:
094: int iRowCtr = 0;
095: // There had better be a parm 4!!
096: if (rowsPerEvent.intValue() == 0) {
097: System.err
098: .println("You forgot parm 4 - rows per event");
099: return;
100: }
101: String storeRow[] = new String[rowsPerEvent.intValue() + 1];
102: /* Two loops, first joins lines together, second processes lines..
103: */
104: while ((this Line = myInput.readLine()) != null) {
105: iRowCtr++;
106: //System.out.println("ROW:"+thisLine);
107: storeRow[iRowCtr] = this Line;
108: if (iRowCtr == rowsPerEvent.intValue()) {
109: // Here we are, create an event..
110: ICalendarVEvent vevent = new ICalendarVEvent();
111: //SimpleDateFormat syslogformatter = new SimpleDateFormat("yyyy MMM dd HH:mm:ss");
112: for (int fieldIdx = 0; fieldIdx < iFieldCtr; fieldIdx++) {
113: Date workDate = new Date();
114: // Extract required row for this field.
115: String fieldValue = storeRow[row[fieldIdx]];
116: //System.out.println("Row is:"+fieldValue);
117:
118: if (fieldValue.length() < col[fieldIdx] - 1)
119: System.err.println("Ick, line too short:"
120: + fieldValue.length()
121: + " col starts:" + col[fieldIdx]
122: + " for field val: " + fieldValue);
123: else if (endCol[fieldIdx] - 1 > fieldValue
124: .length()) {
125: //System.out.println("option1:"+col[fieldIdx]);
126: fieldValue = fieldValue
127: .substring(col[fieldIdx] - 1);
128: } else {
129: //System.out.println("option2:"+col[fieldIdx]);
130: fieldValue = fieldValue.substring(
131: col[fieldIdx] - 1,
132: endCol[fieldIdx] - 1);
133: }
134:
135: //System.out.println("Field is :"+ iCalField[fieldIdx]+ " value is: "+fieldValue);
136:
137: // Process a field into the event.
138: if (iCalField[fieldIdx]
139: .equalsIgnoreCase("startdate")) {
140: // This should be done once only!
141: //System.out.println("Parse this:"+fieldValue+ " with this" +parseString[fieldIdx]);
142: SimpleDateFormat startDateFormatter = new SimpleDateFormat(
143: parseString[fieldIdx]);
144: //System.out.println("Parse this:"+fieldValue+ " with this" +parseString[fieldIdx]+" Parsed Results: "+startDateFormatter.parse(fieldValue));
145: vevent.setDateStart(startDateFormatter
146: .parse(fieldValue));
147: } else if (iCalField[fieldIdx]
148: .equalsIgnoreCase("enddate")) {
149: SimpleDateFormat endDateFormatter = new SimpleDateFormat(
150: parseString[fieldIdx]);
151: //System.out.println("Parse this:"+fieldValue+ " with this" +parseString[fieldIdx]+" Parsed Results: "+endDateFormatter.parse(fieldValue));
152: vevent.setDateEnd(endDateFormatter
153: .parse(fieldValue));
154: } else if (iCalField[fieldIdx]
155: .equalsIgnoreCase("summary")) {
156: vevent.setSummary(fieldValue);
157: } else if (iCalField[fieldIdx]
158: .equalsIgnoreCase("description")) {
159: vevent.setDescription(fieldValue);
160: } else {
161: System.err.println("Invalid field: "
162: + iCalField[fieldIdx]);
163: }
164:
165: if (vevent.getDateStart() != null
166: && vevent.getDateEnd() != null) {
167: vevent.setSequence(0);
168: vevent.setEventClass("PUBLIC");
169: vevent.setTransparency("OPAQUE");
170: vevent.setDateStamp(workDate);
171: vevent.setCreated(workDate);
172: vevent.setLastModified(workDate);
173: //vevent.setAttach(photoFile.toURL().toString());
174: vevent
175: .setOrganizer("MAILTO:sfg@eurekait.com");
176: iCtr++;
177: System.out.println(iCtr);
178: vevent.setUid("jical-"
179: + touchformatter.format(workDate)
180: + "-" + iCtr);
181: vevent.setPriority(3);
182:
183: //System.out.println(vevent.toVEvent());
184:
185: iCal.icalEventCollection.add(vevent);
186: }
187: }
188: iRowCtr = 0;
189: }
190:
191: }
192: } catch (Exception e) {
193: e.printStackTrace();
194: System.err.println("SomethingBad Happened:" + e);
195: }
196:
197: try {
198:
199: // Now write to string and view as file.
200: //System.out.println(iCal.getVCalendar());
201:
202: BufferedWriter out = new BufferedWriter(new FileWriter(
203: args[1]));
204: out.write(iCal.getVCalendar());
205: out.close();
206:
207: } catch (Exception e) {
208: e.printStackTrace();
209: System.err.println("SomethingBad Happened:" + e);
210: }
211:
212: System.out.println("Rendered new TEXT2ICAL calendar file: "
213: + args[1]);
214:
215: }
216: }
|