Various improvements to handling and detection of URLs in codes
[zxing.git] / core-ext / src / com / google / zxing / client / result / BookmarkDoCoMoResult.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.net.URI;
20 import java.net.URISyntaxException;
21
22 /**
23  * @author srowen@google.com (Sean Owen)
24  */
25 public final class BookmarkDoCoMoResult extends AbstractDoCoMoResult {
26
27   private final String title;
28   private final URI uri;
29
30   public BookmarkDoCoMoResult(String rawText) {
31     super(ParsedReaderResultType.BOOKMARK);
32     if (!rawText.startsWith("MEBKM:")) {
33       throw new IllegalArgumentException("Does not begin with MEBKM");
34     }
35     title = matchSinglePrefixedField("TITLE:", rawText);
36     String uriString = matchRequiredPrefixedField("URL:", rawText)[0];
37     try {
38       this.uri = new URI(uriString);
39     } catch (URISyntaxException urise) {
40       throw new IllegalArgumentException(urise.toString());
41     }
42   }
43
44   public String getTitle() {
45     return title;
46   }
47
48   public URI getURI() {
49     return uri;
50   }
51
52   @Override
53   public String getDisplayResult() {
54     if (title == null) {
55       return uri.toString();
56     } else {
57       return title + '\n' + uri;
58     }
59   }
60
61 }