Implemented Paul's solution to the basic/regular build problem -- a sort of pseudo...
[zxing.git] / core / src / com / google / zxing / client / result / optional / MobileTagSimpleCalendarParsedResult.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.optional;
18
19 import com.google.zxing.BarcodeFormat;
20 import com.google.zxing.Result;
21 import com.google.zxing.client.result.ParsedReaderResultType;
22
23 /**
24  * <p>Represents a "simple calendar" result encoded according to section 4.9 of the
25  * MobileTag Reader International Specification.</p>
26  *
27  * @author srowen@google.com (Sean Owen)
28  */
29 public final class MobileTagSimpleCalendarParsedResult extends AbstractMobileTagParsedResult {
30
31   public static final String SERVICE_TYPE = "07";
32
33   private final String summary;
34   private final String start;
35   private final String end;
36   private final String location;
37   private final String attendee;
38   private final String title;
39
40   private MobileTagSimpleCalendarParsedResult(String summary,
41                                               String start,
42                                               String end,
43                                               String location,
44                                               String attendee,
45                                               String title) {
46     super(ParsedReaderResultType.MOBILETAG_SIMPLE_CALENDAR);
47     this.summary = summary;
48     this.start = start;
49     this.end = end;
50     this.location = location;
51     this.attendee = attendee;
52     this.title = title;
53   }
54
55   public static MobileTagSimpleCalendarParsedResult parse(Result result) {
56     if (!result.getBarcodeFormat().equals(BarcodeFormat.DATAMATRIX)) {
57       return null;
58     }
59     String rawText = result.getText();
60     if (!rawText.startsWith(SERVICE_TYPE)) {
61       return null;
62     }
63
64     String[] matches = matchDelimitedFields(rawText.substring(2), 6);
65     if (matches == null || !isDigits(matches[1], 10) || !isDigits(matches[2], 10)) {
66       return null;
67     }
68     String summary = matches[0];
69     String start = expandDateString(matches[1]);
70     String end = expandDateString(matches[2]);
71     String location = matches[3];
72     String attendee = matches[4];
73     String title = matches[5];
74
75     return new MobileTagSimpleCalendarParsedResult(summary, start, end, location, attendee, title);
76   }
77
78   public String getSummary() {
79     return summary;
80   }
81
82   /**
83    * <p>We would return the start and end date as a {@link java.util.Date} except that this code
84    * needs to work under JavaME / MIDP and there is no date parsing library available there, such
85    * as <code>java.text.SimpleDateFormat</code>.</p>
86    *
87    * <p>However we do translate the date from its encoded format of, say, "0602212156" to its full
88    * text representation of "20060221T215600Z", per the specification.</p>
89    */
90   public String getStart() {
91     return start;
92   }
93
94   /**
95    * @see #getStart()
96    */
97   public String getEnd() {
98     return end;
99   }
100
101   public String getLocation() {
102     return location;
103   }
104
105   public String getAttendee() {
106     return attendee;
107   }
108
109   public String getTitle() {
110     return title;
111   }
112
113   public String getDisplayResult() {
114     StringBuffer result = new StringBuffer(summary);
115     maybeAppend(start, result);
116     maybeAppend(end, result);
117     maybeAppend(location, result);
118     maybeAppend(attendee, result);
119     maybeAppend(title, result);
120     return result.toString();
121   }
122
123   private static String expandDateString(String date) {
124     if (date == null) {
125       return null;
126     }
127     // Input is of form YYMMddHHmmss, and needs to be YYYYMMdd'T'HHmmss'Z'
128     return "20" + date.substring(0, 6) + 'T' + date.substring(6) + "00Z";
129   }
130
131 }