Changed signature of parse() to take in more general Result
[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(Result)} 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     ParsedReaderResult result;
51     if ((result = BookmarkDoCoMoResult.parse(theResult)) != null) {
52       return result;
53     } else if ((result = AddressBookDoCoMoResult.parse(theResult)) != null) {
54       return result;
55     } else if ((result = EmailDoCoMoResult.parse(theResult)) != null) {
56       return result;
57     } else if ((result = EmailAddressResult.parse(theResult)) != null) {
58       return result;
59     } else if ((result = AddressBookAUResult.parse(theResult)) != null) {
60       return result;
61     } else if ((result = URLTOResult.parse(theResult)) != null) {
62       return result;
63     } else if ((result = URIParsedResult.parse(theResult)) != null) {
64       return result;
65     } else if ((result = UPCParsedResult.parse(theResult)) != null) {
66       return result;
67     }
68     return TextParsedResult.parse(theResult);
69   }
70
71   public String toString() {
72     return getDisplayResult();
73   }
74
75 }