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 java.lang.reflect.Constructor;
20 import java.lang.reflect.InvocationTargetException;
23 * @author srowen@google.com (Sean Owen)
25 public abstract class ParsedReaderResult {
27 private final ParsedReaderResultType type;
29 ParsedReaderResult(ParsedReaderResultType type) {
33 public ParsedReaderResultType getType() {
37 public abstract String getDisplayResult();
39 public static ParsedReaderResult parseReaderResult(String rawText) {
40 for (ParsedReaderResultType type : ParsedReaderResultType.values()) {
41 Class<? extends ParsedReaderResult> resultClass = type.getResultClass();
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) {
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);
60 throw new IllegalStateException("TextResult should always work");
64 public String toString() {
65 return getDisplayResult();