2 * Copyright 2007 Google Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.result;
19 import com.google.zxing.Result;
22 * Implements the "MATMSG" email message entry format.
24 * Supported keys: TO, SUB, BODY
26 * @author srowen@google.com (Sean Owen)
28 public final class EmailDoCoMoParsedResult extends AbstractDoCoMoParsedResult {
30 private final String to;
31 private final String subject;
32 private final String body;
34 private EmailDoCoMoParsedResult(String to, String subject, String body) {
35 super(ParsedReaderResultType.EMAIL);
37 this.subject = subject;
41 public static EmailDoCoMoParsedResult parse(Result result) {
42 String rawText = result.getText();
43 if (!rawText.startsWith("MATMSG:")) {
46 String[] rawTo = matchPrefixedField("TO:", rawText);
51 if (!isBasicallyValidEmailAddress(to)) {
54 String subject = matchSinglePrefixedField("SUB:", rawText);
55 String body = matchSinglePrefixedField("BODY:", rawText);
56 return new EmailDoCoMoParsedResult(to, subject, body);
59 public String getTo() {
63 public String getSubject() {
67 public String getBody() {
71 public String getDisplayResult() {
72 StringBuffer result = new StringBuffer(to);
73 maybeAppend(subject, result);
74 maybeAppend(body, result);
75 return result.toString();
79 * This implements only the most basic checking for an email address's validity -- that it contains
80 * an '@' and a '.' somewhere after that, and that it contains no space.
81 * We want to generally be lenient here since this class is only intended to encapsulate what's
82 * in a barcode, not "judge" it.
84 static boolean isBasicallyValidEmailAddress(String email) {
85 int atIndex = email.indexOf('@');
86 return atIndex >= 0 && email.indexOf('.') > atIndex && email.indexOf(' ') < 0;