Moved com.google.zxing.client.result from core-ext to core, rewrote it for J2ME,...
[zxing.git] / core / src / com / google / zxing / client / result / EmailDoCoMoResult.java
1 /*
2  * Copyright 2007 Google Inc.
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 /**
20  * Implements the "MATMSG" email message entry format.
21  *
22  * Supported keys: TO, SUB, BODY
23  *
24  * @author srowen@google.com (Sean Owen)
25  */
26 public final class EmailDoCoMoResult extends AbstractDoCoMoResult {
27
28   private final String to;
29   private final String subject;
30   private final String body;
31
32   public EmailDoCoMoResult(String rawText) {
33     super(ParsedReaderResultType.EMAIL);
34     if (!rawText.startsWith("MATMSG:")) {
35       throw new IllegalArgumentException("Does not begin with MATMSG");
36     }
37     to = matchRequiredPrefixedField("TO:", rawText)[0];
38     if (!isBasicallyValidEmailAddress(to)) {
39       throw new IllegalArgumentException("Invalid email address: " + to);
40     }
41     subject = matchSinglePrefixedField("SUB:", rawText);
42     body = matchSinglePrefixedField("BODY:", rawText);
43   }
44
45   public String getTo() {
46     return to;
47   }
48
49   public String getSubject() {
50     return subject;
51   }
52
53   public String getBody() {
54     return body;
55   }
56
57   public String getDisplayResult() {
58     StringBuffer result = new StringBuffer(to);
59     maybeAppend(subject, result);
60     maybeAppend(body, result);
61     return result.toString();
62   }
63
64   /**
65    * This implements only the most basic checking for an email address's validity -- that it contains
66    * an '@' and a '.' somewhere after that, and that it contains no space.
67    * We want to generally be lenient here since this class is only intended to encapsulate what's
68    * in a barcode, not "judge" it.
69    */
70   static boolean isBasicallyValidEmailAddress(String email) {
71     int atIndex = email.indexOf('@');
72     return atIndex >= 0 && email.indexOf('.') > atIndex && email.indexOf(' ') < 0;
73   }
74
75 }