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;
20 * Implements the "MATMSG" email message entry format.
22 * Supported keys: TO, SUB, BODY
24 * @author srowen@google.com (Sean Owen)
26 public final class EmailDoCoMoResult extends AbstractDoCoMoResult {
28 private final String to;
29 private final String subject;
30 private final String body;
32 public EmailDoCoMoResult(String rawText) {
33 super(ParsedReaderResultType.EMAIL);
34 if (!rawText.startsWith("MATMSG:")) {
35 throw new IllegalArgumentException("Does not begin with MATMSG");
37 to = matchRequiredPrefixedField("TO:", rawText)[0];
38 if (!isBasicallyValidEmailAddress(to)) {
39 throw new IllegalArgumentException("Invalid email address: " + to);
41 subject = matchSinglePrefixedField("SUB:", rawText);
42 body = matchSinglePrefixedField("BODY:", rawText);
45 public String getTo() {
49 public String getSubject() {
53 public String getBody() {
57 public String getDisplayResult() {
58 StringBuffer result = new StringBuffer(to);
59 maybeAppend(subject, result);
60 maybeAppend(body, result);
61 return result.toString();
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.
70 static boolean isBasicallyValidEmailAddress(String email) {
71 int atIndex = email.indexOf('@');
72 return atIndex >= 0 && email.indexOf('.') > atIndex && email.indexOf(' ') < 0;