git-svn-id: http://zxing.googlecode.com/svn/trunk@6 59b500cc-1b3d-0410-9834-0bbf25fbcc57
[zxing.git] / core-ext / 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 java.lang.reflect.Constructor;
20 import java.lang.reflect.InvocationTargetException;
21
22 /**
23  * @author srowen@google.com (Sean Owen)
24  */
25 public abstract class ParsedReaderResult {
26
27   private final ParsedReaderResultType type;
28
29   ParsedReaderResult(ParsedReaderResultType type) {
30     this.type = type;
31   }
32
33   public ParsedReaderResultType getType() {
34     return type;
35   }
36
37   public abstract String getDisplayResult();
38
39   public static ParsedReaderResult parseReaderResult(String rawText) {
40     for (ParsedReaderResultType type : ParsedReaderResultType.values()) {
41       Class<? extends ParsedReaderResult> resultClass = type.getResultClass();
42       try {
43         Constructor<? extends ParsedReaderResult> constructor =
44             resultClass.getConstructor(String.class);
45         return constructor.newInstance(rawText);
46       } catch (InvocationTargetException ite) {
47         Throwable cause = ite.getCause();
48         if (cause instanceof IllegalArgumentException) {
49           continue;
50         }
51         throw new RuntimeException(cause);
52       } catch (NoSuchMethodException nsme) {
53         throw new RuntimeException(nsme);
54       } catch (IllegalAccessException iae) {
55         throw new RuntimeException(iae);
56       } catch (InstantiationException ie) {
57         throw new RuntimeException(ie);
58       }
59     }
60     throw new IllegalStateException("TextResult should always work");
61   }
62
63   @Override
64   public String toString() {
65     return getDisplayResult();
66   }
67
68 }