1
20 package org.crosswire.jsword.book.sword;
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.crosswire.jsword.JSMsg;
27 import org.crosswire.jsword.book.BookException;
28 import org.crosswire.jsword.book.BookMetaData;
29 import org.crosswire.jsword.book.sword.state.GenBookBackendState;
30 import org.crosswire.jsword.book.sword.state.OpenFileStateManager;
31 import org.crosswire.jsword.passage.DefaultKeyList;
32 import org.crosswire.jsword.passage.Key;
33 import org.crosswire.jsword.passage.TreeKey;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37
43 public class GenBookBackend extends AbstractBackend<GenBookBackendState> {
44
49 public GenBookBackend(SwordBookMetaData sbmd) {
50 super(sbmd);
51 index = new TreeKeyIndex(sbmd);
52 }
53
54 public GenBookBackendState initState() throws BookException {
55 return OpenFileStateManager.instance().getGenBookBackendState(getBookMetaData());
56 }
57
58
61 @Override
62 public boolean contains(Key key) {
63 return getRawTextLength(key) > 0;
64 }
65
66
69 @Override
70 public int getRawTextLength(Key key) {
71 try {
72 TreeNode node = find(key);
73
74 if (node == null) {
75 return 0;
76 }
77
78 byte[] userData = node.getUserData();
79
80 if (userData.length == 8) {
82 return SwordUtil.decodeLittleEndian32(userData, 4);
83 }
84
85 return 0;
86
87 } catch (IOException e) {
88 return 0;
89 }
90 }
91
92
95 public String readRawContent(GenBookBackendState state, Key key) throws IOException, BookException {
96 TreeNode node = find(key);
97
98 if (node == null) {
99 throw new BookException(JSMsg.gettext("No entry for '{0}' in {1}.", key.getName(), getBookMetaData().getInitials()));
104 }
105
106 byte[] userData = node.getUserData();
107
108 if (userData.length == 8) {
110 int start = SwordUtil.decodeLittleEndian32(userData, 0);
111 int size = SwordUtil.decodeLittleEndian32(userData, 4);
112 byte[] data = SwordUtil.readRAF(state.getBdtRaf(), start, size);
113 decipher(data);
114 return SwordUtil.decode(key.getName(), data, getBookMetaData().getBookCharset());
115 }
116
117 return "";
118 }
119
120
128 private TreeNode find(Key key) throws IOException {
129 List<String> path = new ArrayList<String>();
132 for (Key parentKey = key; parentKey != null && parentKey.getName().length() > 0; parentKey = parentKey.getParent()) {
133 path.add(parentKey.getName());
134 }
135
136 TreeNode node = index.getRoot();
137
138 node = index.getFirstChild(node);
139
140 for (int i = path.size() - 1; i >= 0; i--) {
141 String name = path.get(i);
142
143 while (node != null && !name.equals(node.getName())) {
145 if (node.hasNextSibling()) {
146 node = index.getNextSibling(node);
147 } else {
148 log.error("Could not find {}", name);
149 node = null;
150 }
151 }
152
153 if (node != null && name.equals(node.getName()) && i > 0) {
156 node = index.getFirstChild(node);
157 }
158 }
159
160 if (node != null && node.getName().equals(key.getName())) {
163 return node;
164 }
165
166 return null;
167 }
168
169 @Override
170 public Key readIndex() {
171 BookMetaData bmd = getBookMetaData();
172 Key reply = new DefaultKeyList(null, bmd.getName());
173
174 try {
175 TreeNode node = index.getRoot();
176 reply = new TreeKey(node.getName(), null);
177 doReadIndex(node, reply);
178 } catch (IOException e) {
179 log.error("Could not get read GenBook index", e);
180 }
181
182 return reply;
183 }
184
185
188 public void setAliasKey(GenBookBackendState state, Key alias, Key source) throws IOException {
189 throw new UnsupportedOperationException();
190 }
191
192
195 public void setRawText(GenBookBackendState rafBook, Key key, String text) throws BookException, IOException {
196 throw new UnsupportedOperationException();
197 }
198
199
207 private void doReadIndex(TreeNode parentNode, Key parentKey) throws IOException {
208 TreeNode currentNode = parentNode;
209 if (currentNode.hasChildren()) {
210 TreeNode childNode = index.getFirstChild(currentNode);
211 do {
212 TreeKey childKey = new TreeKey(childNode.getName(), parentKey);
213 parentKey.addAll(childKey);
214
215 doReadIndex(childNode, childKey);
217
218 if (!childNode.hasNextSibling()) {
219 break;
220 }
221
222 childNode = index.getNextSibling(childNode);
223 } while (true);
224 }
225 }
226
227
230 private final TreeKeyIndex index;
231
232
235 private static final Logger log = LoggerFactory.getLogger(GenBookBackend.class);
236 }
237