Refactorings to allow raw bytes to be passed back with reader result, where applicable
[zxing.git] / core / src / com / google / zxing / client / result / ParsedReaderResult.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 import com.google.zxing.Result;
20
21 /**
22  * <p>Abstract class representing the result of decoding a barcode, as more than
23  * a String -- as some type of structured data. This might be a subclass which represents
24  * a URL, or an e-mail address. {@link #parseReaderResult(String)} will turn a raw
25  * decoded string into the most appropriate type of structured representation.</p>
26  *
27  * <p>Thanks to Jeff Griffin for proposing rewrite of these classes that relies less
28  * on exception-based mechanisms during parsing.</p>
29  *
30  * @author srowen@google.com (Sean Owen)
31  */
32 public abstract class ParsedReaderResult {
33
34   private final ParsedReaderResultType type;
35
36   public ParsedReaderResult(ParsedReaderResultType type) {
37     this.type = type;
38   }
39
40   public ParsedReaderResultType getType() {
41     return type;
42   }
43
44   public abstract String getDisplayResult();
45
46   public static ParsedReaderResult parseReaderResult(Result theResult) {
47     // This is a bit messy, but given limited options in MIDP / CLDC, this may well be the simplest
48     // way to go about this. For example, we have no reflection available, really.
49     // Order is important here.
50     String rawText = theResult.getText();
51     ParsedReaderResult result;
52     if ((result = BookmarkDoCoMoResult.parse(rawText)) != null) {
53       return result;
54     } else if ((result = AddressBookDoCoMoResult.parse(rawText)) != null) {
55       return result;
56     } else if ((result = EmailDoCoMoResult.parse(rawText)) != null) {
57       return result;
58     } else if ((result = EmailAddressResult.parse(rawText)) != null) {
59       return result;
60     } else if ((result = AddressBookAUResult.parse(rawText)) != null) {
61       return result;
62     } else if ((result = URLTOResult.parse(rawText)) != null) {
63       return result;
64     } else if ((result = URIParsedResult.parse(rawText)) != null) {
65       return result;
66     } else if ((result = UPCParsedResult.parse(rawText)) != null) {
67       return result;
68     }
69     return TextParsedResult.parse(rawText);
70   }
71
72   public String toString() {
73     return getDisplayResult();
74   }
75
76 }