1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
package org.crosswire.jsword.book.sword; |
21 | |
|
22 | |
import java.util.ArrayList; |
23 | |
import java.util.Iterator; |
24 | |
import java.util.List; |
25 | |
|
26 | |
import org.crosswire.jsword.JSOtherMsg; |
27 | |
import org.crosswire.jsword.book.BookException; |
28 | |
import org.crosswire.jsword.book.OSISUtil; |
29 | |
import org.crosswire.jsword.book.basic.AbstractBook; |
30 | |
import org.crosswire.jsword.book.filter.SourceFilter; |
31 | |
import org.crosswire.jsword.book.sword.processing.RawTextToXmlProcessor; |
32 | |
import org.crosswire.jsword.passage.DefaultKeyList; |
33 | |
import org.crosswire.jsword.passage.DefaultLeafKeyList; |
34 | |
import org.crosswire.jsword.passage.Key; |
35 | |
import org.crosswire.jsword.passage.NoSuchKeyException; |
36 | |
import org.jdom2.Content; |
37 | |
import org.jdom2.Element; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | public class SwordDictionary extends AbstractBook { |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
protected SwordDictionary(SwordBookMetaData sbmd, Backend backend) { |
53 | 0 | super(sbmd, backend); |
54 | |
|
55 | 0 | if (!(backend instanceof AbstractKeyBackend)) { |
56 | 0 | throw new IllegalArgumentException("AbstractBackend must be an AbstractKeyBackened"); |
57 | |
} |
58 | |
|
59 | 0 | this.filter = sbmd.getFilter(); |
60 | 0 | } |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public Iterator<Content> getOsisIterator(final Key key, boolean allowEmpty, final boolean allowGenTitles) throws BookException { |
66 | 0 | assert key != null; |
67 | |
|
68 | 0 | List<Content> content = new ArrayList<Content>(); |
69 | 0 | Element title = OSISUtil.factory().createGeneratedTitle(); |
70 | 0 | title.addContent(key.getName()); |
71 | 0 | content.add(title); |
72 | |
|
73 | 0 | String txt = getBackend().getRawText(key); |
74 | 0 | List<Content> osisContent = filter.toOSIS(this, key, txt); |
75 | 0 | content.addAll(osisContent); |
76 | |
|
77 | 0 | return content.iterator(); |
78 | |
} |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public String getRawText(Key key) throws BookException { |
84 | 0 | return getBackend().getRawText(key); |
85 | |
} |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
public boolean contains(Key key) { |
91 | 0 | Backend backend = getBackend(); |
92 | 0 | return backend != null && backend.contains(key); |
93 | |
} |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
@Override |
99 | |
public List<Content> getOsis(Key key, RawTextToXmlProcessor processor) throws BookException { |
100 | 0 | assert key != null; |
101 | |
|
102 | 0 | return getBackend().readToOsis(key, processor); |
103 | |
} |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
public boolean isWritable() { |
109 | 0 | return getBackend().isWritable(); |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public void setRawText(Key key, String rawData) throws BookException { |
116 | 0 | throw new BookException(JSOtherMsg.lookupText("This Book is read-only.")); |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public void setAliasKey(Key alias, Key source) throws BookException { |
123 | 0 | throw new BookException(JSOtherMsg.lookupText("This Book is read-only.")); |
124 | |
} |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
public Key getGlobalKeyList() { |
130 | 0 | return (AbstractKeyBackend) getBackend(); |
131 | |
} |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
public Key getValidKey(String name) { |
137 | |
try { |
138 | 0 | return getKey(name); |
139 | 0 | } catch (NoSuchKeyException e) { |
140 | 0 | return createEmptyKeyList(); |
141 | |
} |
142 | |
} |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
public Key getKey(String text) throws NoSuchKeyException { |
148 | 0 | AbstractKeyBackend keyBackend = (AbstractKeyBackend) getBackend(); |
149 | |
|
150 | 0 | int pos = keyBackend.indexOf(new DefaultLeafKeyList(text)); |
151 | 0 | if (pos < 0) { |
152 | 0 | if (keyBackend.getCardinality() > -pos - 1) { |
153 | 0 | return keyBackend.get(-pos - 1); |
154 | |
} |
155 | 0 | return keyBackend.get(keyBackend.getCardinality() - 1); |
156 | |
} |
157 | 0 | return keyBackend.get(pos); |
158 | |
} |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
public Key createEmptyKeyList() { |
164 | 0 | return new DefaultKeyList(); |
165 | |
} |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
private SourceFilter filter; |
171 | |
|
172 | |
} |