Add iCal support, plus many small changes suggested by code inspection -- mostly...
[zxing.git] / core / src / com / google / zxing / client / result / VEventResultParser.java
1 /*
2  * Copyright 2008 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.client.result;
18
19 import com.google.zxing.Result;
20
21 /**
22  * Partially implements the iCalendar format's "VEVENT" format for specifying a
23  * calendar event. See RFC 2445. This supports SUMMARY, DTSTART and DTEND fields.
24  *
25  * @author srowen@google.com (Sean Owen)
26  */
27 final class VEventResultParser extends ResultParser {
28
29   private VEventResultParser() {
30   }
31
32   public static CalendarParsedResult parse(Result result) {
33     String rawText = result.getText();
34     if (rawText == null) {
35       return null;
36     }
37     int vEventStart = rawText.indexOf("BEGIN:VEVENT");
38     int vEventEnd = rawText.indexOf("END:VEVENT");
39     if (vEventStart < 0 || vEventEnd < 0) {
40       return null;
41     }
42     rawText = rawText.substring(vEventStart + 14, vEventEnd); // skip over BEGIN:VEVENT\r\n at start
43
44     String summary = VCardResultParser.matchSingleVCardPrefixedField("SUMMARY", rawText);
45     String start = VCardResultParser.matchSingleVCardPrefixedField("DTSTART", rawText);
46     String end = VCardResultParser.matchSingleVCardPrefixedField("DTEND", rawText);
47     try {
48       return new CalendarParsedResult(summary, start, end, null, null, null);
49     } catch (IllegalArgumentException iae) {
50       return null;
51     }
52   }
53
54 }