1 | /* This file is part of Aard Dictionary for Android <http://aarddict.org>. |
2 | * |
3 | * This program is free software: you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License version 3 |
5 | * as published by the Free Software Foundation. |
6 | * |
7 | * This program is distributed in the hope that it will be useful, |
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
10 | * GNU General Public License <http://www.gnu.org/licenses/gpl-3.0.txt> |
11 | * for more details. |
12 | * |
13 | * Copyright (C) 2010 Igor Tkach |
14 | */ |
15 | |
16 | package aarddict; |
17 | |
18 | import java.net.URI; |
19 | import java.net.URISyntaxException; |
20 | |
21 | import android.util.Log; |
22 | |
23 | public class LookupWord { |
24 | |
25 | final static String TAG = LookupWord.class.getName(); |
26 | |
27 | public String nameSpace; |
28 | public String word; |
29 | public String section; |
30 | |
31 | public LookupWord() { |
32 | } |
33 | |
34 | public LookupWord(String nameSpace, String word, String section) { |
35 | this.nameSpace = nameSpace; |
36 | this.word = word; |
37 | this.section = section; |
38 | } |
39 | |
40 | void mergeNameSpace() { |
41 | if (!isEmpty(nameSpace)) { |
42 | word = nameSpace + ":" + word; |
43 | nameSpace = null; |
44 | } |
45 | } |
46 | |
47 | public static LookupWord splitWord(String word) { |
48 | if (word == null || word.equals("") || word.equals("#")) { |
49 | return new LookupWord(); |
50 | } |
51 | try { |
52 | return splitWordAsURI(word); |
53 | } catch (URISyntaxException e) { |
54 | Log.d(TAG, "Word is not proper URI: " + word); |
55 | return splitWordSimple(word); |
56 | } |
57 | } |
58 | |
59 | static LookupWord splitWordAsURI(String word) throws URISyntaxException { |
60 | URI uri = new URI(word); |
61 | String nameSpace = uri.getScheme(); |
62 | String lookupWord = uri.getSchemeSpecificPart(); |
63 | lookupWord = lookupWord.replace("_", " "); |
64 | String section = uri.getFragment(); |
65 | return new LookupWord(nameSpace, lookupWord, section); |
66 | } |
67 | |
68 | static LookupWord splitWordSimple(String word) { |
69 | String[] parts = word.split("#", 2); |
70 | String section = parts.length == 1 ? null : parts[1]; |
71 | String nsWord = (!isEmpty(parts[0]) || !isEmpty(section)) ? parts[0] : word; |
72 | String[] nsParts = nsWord.split(":", 2); |
73 | String lookupWord = nsParts.length == 1 ? nsParts[0] : nsParts[1]; |
74 | lookupWord = lookupWord.replace("_", " "); |
75 | String nameSpace = nsParts.length == 1 ? null : nsParts[0]; |
76 | return new LookupWord(nameSpace, lookupWord, section); |
77 | } |
78 | |
79 | @Override |
80 | public String toString() { |
81 | StringBuilder s = new StringBuilder(); |
82 | if (!isEmpty(nameSpace)) { |
83 | s.append(nameSpace); |
84 | s.append(":"); |
85 | } |
86 | s.append(word == null ? "" : word); |
87 | if (!isEmpty(section)) { |
88 | s.append("#"); |
89 | s.append(section); |
90 | } |
91 | return s.toString(); |
92 | } |
93 | |
94 | public boolean isEmpty() { |
95 | return isEmpty(word) && isEmpty(section); |
96 | } |
97 | |
98 | static boolean isEmpty(String s) { |
99 | return s == null || s.equals(""); |
100 | } |
101 | } |