📄 icalparser.java
字号:
// ignore leading blank lines } else { // Hmmm... should always start with this. if ( isParseStrict () ) { reportParseError ( new ParseError ( ln, "Data found outside VCALENDAR block", line ) ); } } break; case STATE_VCALENDAR: if ( lineUp.startsWith ( "BEGIN:VTIMEZONE" ) ) { state = STATE_VTIMEZONE; startLineNo = ln; // mark starting line number textLines.removeAllElements (); textLines.addElement ( line ); } else if ( lineUp.startsWith ( "BEGIN:VEVENT" ) ) { state = STATE_VEVENT; startLineNo = ln; // mark starting line number textLines.removeAllElements (); textLines.addElement ( line ); } else if ( lineUp.startsWith ( "BEGIN:VTODO" ) ) { state = STATE_VTODO; startLineNo = ln; // mark starting line number textLines.removeAllElements (); textLines.addElement ( line ); } else if ( lineUp.startsWith ( "BEGIN:VJOURNAL" ) ) { state = STATE_VJOURNAL; startLineNo = ln; // mark starting line number textLines.removeAllElements (); textLines.addElement ( line ); } else if ( lineUp.startsWith ( "BEGIN:VFREEBUSY" ) ) { state = STATE_VFREEBUSY; startLineNo = ln; // mark starting line number textLines.removeAllElements (); textLines.addElement ( line ); } else if ( lineUp.startsWith ( "END:VCALENDAR" ) ) { state = STATE_DONE; } else if ( lineUp.startsWith ( "VERSION" ) ) { if ( icalVersion != null && isParseStrict() ) { // only one of these allowed reportParseError ( new ParseError ( ln, "Only one VERSION token allowed", line ) ); } else { try { icalVersion = new Property ( line, getParseMethod() ); } catch ( ParseException e ) { reportParseError ( new ParseError ( ln, "Parse error in VERSION: " + e.toString(), line ) ); } } } else if ( lineUp.startsWith ( "PRODID" ) ) { if ( prodId != null && isParseStrict() ) { // only one of these allowed reportParseError ( new ParseError ( ln, "Only one PRODID token allowed", line ) ); } else { try { prodId = new Property ( line, getParseMethod() ); } catch ( ParseException e ) { reportParseError ( new ParseError ( ln, "Parse error in PRODID: " + e.toString(), line ) ); } } } else if ( lineUp.startsWith ( "CALSCALE" ) ) { try { calscale = new Property ( line, getParseMethod() ); } catch ( ParseException e ) { reportParseError ( new ParseError ( ln, "Parse error in CALSCALE: " + e.toString(), line ) ); } } else if ( lineUp.startsWith ( "METHOD" ) ) { try { method = new Property ( line, getParseMethod() ); } catch ( ParseException e ) { reportParseError ( new ParseError ( ln, "Parse error in CALSCALE: " + e.toString(), line ) ); } } else { // what else could this be??? if ( lineUp.trim().length() == 0 ) { // ignore blank lines } else if ( isParseStrict () ) { reportParseError ( new ParseError ( ln, "Unrecognized data found in VCALENDAR block", line ) ); } } break; case STATE_VTIMEZONE: textLines.addElement ( line ); if ( lineUp.startsWith ( "END:VTIMEZONE" ) ) { state = STATE_VCALENDAR; Timezone timezone = new Timezone ( this, startLineNo, textLines ); if ( timezone.isValid() ) { for ( int i = 0; i < dataStores.size(); i++ ) { DataStore ds = (DataStore) dataStores.elementAt ( i ); ds.storeTimezone ( timezone ); } } textLines.removeAllElements (); // truncate Vector } break; case STATE_VTODO: textLines.addElement ( line ); if ( lineUp.startsWith ( "END:VTODO" ) ) { state = STATE_VCALENDAR; // Send the Todo object to all DataStore objects /*** TODO - not yet implemented Uncomment this when Todo.java is implemented Todo todo = new Todo ( this, startLineNo, textLines ); if ( todo.isValid() ) { for ( int i = 0; i < dataStores.size(); i++ ) { DataStore ds = (DataStore) dataStores.elementAt ( i ); ds.storeTodo ( todo ); } } ***/ textLines.removeAllElements (); // truncate Vector } break; case STATE_VJOURNAL: textLines.addElement ( line ); if ( lineUp.startsWith ( "END:VJOURNAL" ) ) { state = STATE_VCALENDAR; // Send the Journal object to all DataStore objects /*** TODO - not yet implemented Uncomment this when Journal.java is implemented Journal journal = new Journal ( this, startLineNo, textLines ); if ( journal.isValid() ) { for ( int i = 0; i < dataStores.size(); i++ ) { DataStore ds = (DataStore) dataStores.elementAt ( i ); ds.storeJournal ( event ); } } ***/ textLines.removeAllElements (); // truncate Vector } break; case STATE_VEVENT: textLines.addElement ( line ); if ( lineUp.startsWith ( "END:VEVENT" ) ) { state = STATE_VCALENDAR; Event event = new Event ( this, startLineNo, textLines ); if ( event.isValid() ) { for ( int i = 0; i < dataStores.size(); i++ ) { DataStore ds = (DataStore) dataStores.elementAt ( i ); ds.storeEvent ( event ); } } else {System.out.println ( "ERROR: Invalid VEVENT found" ); } textLines.removeAllElements (); // truncate Vector } break; case STATE_VFREEBUSY: textLines.addElement ( line ); if ( lineUp.startsWith ( "END:VFREEBUSY" ) ) { state = STATE_VCALENDAR; // Send the Freebusy object to all DataStore objects /*** TODO - not yet implemented Uncomment this when Freebusy.java is implemented Freebusy fb = new Freebusy ( this, startLineNo, textLines ); if ( fb.isValid() ) { for ( int i = 0; i < dataStores.size(); i++ ) { DataStore ds = (DataStore) dataStores.elementAt ( i ); ds.storeFreebusy ( event ); } } ***/ textLines.removeAllElements (); // truncate Vector } break; case STATE_DONE: // should be nothing else after "END:VCALENDAR" if ( lineUp.trim().length() == 0 ) { // ignore blank lines at end of file } else if ( isParseStrict () ) { reportParseError ( new ParseError ( ln, "Data found after END:VCALENDAR", line ) ); } break; } if ( nextLine == null ) done = true; } r.close (); // Make sure PRODID and VERSION were specified since they are // required if ( icalVersion == null && isParseStrict() ) { reportParseError ( new ParseError ( ln, "No required VERSION attribute found", "n/a" ) ); } if ( prodId == null && isParseStrict() ) { reportParseError ( new ParseError ( ln, "No required PRODID attribute found", "n/a" ) ); } // iCal data is now in line // TODO: split into event, todo, timezone and journal strings return noErrors; } public String toIcal () { StringBuffer ret = new StringBuffer ( 1024 ); ret.append ( "BEGIN:VCALENDAR" ); ret.append ( CRLF ); ret.append ( "VERSION:2.0" ); ret.append ( CRLF ); // Should we use the PRODID we parsed on input. Since we are generating // output, I think we will use ours. // TODO: add version number in the following ret.append ( "PRODID:-//k5n.us//Java Calendar Tools//EN" ); ret.append ( CRLF ); // Include events Vector events = ((DataStore) getDataStoreAt ( 0 )).getAllEvents (); for ( int i = 0; i < events.size(); i++ ) { Event ev = (Event) events.elementAt ( i ); ret.append ( ev.toIcal () ); } ret.append ( "END:VCALENDAR" ); ret.append ( CRLF ); return ret.toString (); } // Test routine - will parse input string and then export back // into ical format. // Usage: java us.k5n.ical.IcalParser testfile.ics // public static void main ( String args[] ) { for ( int i = 0; i < args.length; i++ ) { java.io.File f = new java.io.File ( args[i] ); IcalParser a = new IcalParser ( PARSE_STRICT ); java.io.BufferedReader reader = null; if ( f.exists () ) { try { reader = new java.io.BufferedReader ( new java.io.FileReader ( f ) ); a.parse ( reader ); } catch ( IOException e ) { System.err.println ( "Error opening " + f + ": " + e ); System.exit ( 1 ); } } else { System.err.println ( "Usage: java IcalParser filename.ics" ); System.exit ( 1 ); } System.out.println ( "Filename:\n " + args[i] ); //System.out.println ( "\nFormatted output:\n\n" + a.toIcal () ); Vector errors = a.getAllErrors (); for ( int j = 0; j < errors.size (); j++ ) { ParseError err = (ParseError) errors.elementAt ( j ); System.out.println ( "Error # " + (j+1) + ":\n" + err.toString ( 2 ) ); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -