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.state; |
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.FileNotFoundException; |
24 | |
import java.io.RandomAccessFile; |
25 | |
import java.net.URI; |
26 | |
|
27 | |
import org.crosswire.common.util.FileUtil; |
28 | |
import org.crosswire.common.util.IOUtil; |
29 | |
import org.crosswire.common.util.NetUtil; |
30 | |
import org.crosswire.common.util.Reporter; |
31 | |
import org.crosswire.jsword.JSOtherMsg; |
32 | |
import org.crosswire.jsword.book.BookException; |
33 | |
import org.crosswire.jsword.book.BookMetaData; |
34 | |
import org.crosswire.jsword.book.sword.SwordConstants; |
35 | |
import org.crosswire.jsword.book.sword.SwordUtil; |
36 | |
import org.crosswire.jsword.versification.Testament; |
37 | |
import org.slf4j.Logger; |
38 | |
import org.slf4j.LoggerFactory; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 0 | public class RawBackendState extends AbstractOpenFileState { |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
RawBackendState(BookMetaData bookMetaData) throws BookException { |
61 | 0 | super(bookMetaData); |
62 | 0 | URI path = SwordUtil.getExpandedDataPath(bookMetaData); |
63 | |
|
64 | 0 | URI otPath = NetUtil.lengthenURI(path, File.separator + SwordConstants.FILE_OT); |
65 | 0 | otTextFile = new File(otPath.getPath()); |
66 | 0 | otIdxFile = new File(otPath.getPath() + SwordConstants.EXTENSION_VSS); |
67 | |
|
68 | 0 | URI ntPath = NetUtil.lengthenURI(path, File.separator + SwordConstants.FILE_NT); |
69 | 0 | ntTextFile = new File(ntPath.getPath()); |
70 | 0 | ntIdxFile = new File(ntPath.getPath() + SwordConstants.EXTENSION_VSS); |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | 0 | if (!otTextFile.canRead() && !ntTextFile.canRead()) { |
76 | 0 | BookException prob = new BookException(JSOtherMsg.lookupText("Missing data files for old and new testaments in {0}.", path)); |
77 | 0 | Reporter.informUser(this, prob); |
78 | 0 | throw prob; |
79 | |
} |
80 | |
|
81 | 0 | String fileMode = isWritable() ? FileUtil.MODE_WRITE : FileUtil.MODE_READ; |
82 | |
|
83 | 0 | if (otIdxFile.canRead()) { |
84 | |
try { |
85 | 0 | otIdxRaf = new RandomAccessFile(otIdxFile, fileMode); |
86 | 0 | otTextRaf = new RandomAccessFile(otTextFile, fileMode); |
87 | 0 | } catch (FileNotFoundException ex) { |
88 | |
|
89 | 0 | IOUtil.close(otIdxRaf); |
90 | 0 | IOUtil.close(otTextRaf); |
91 | |
|
92 | 0 | assert false : ex; |
93 | |
|
94 | 0 | LOGGER.error("Could not open OT", ex); |
95 | 0 | ntIdxRaf = null; |
96 | 0 | ntTextRaf = null; |
97 | 0 | } |
98 | |
} |
99 | |
|
100 | 0 | if (ntIdxFile.canRead()) { |
101 | |
try { |
102 | 0 | ntIdxRaf = new RandomAccessFile(ntIdxFile, fileMode); |
103 | 0 | ntTextRaf = new RandomAccessFile(ntTextFile, fileMode); |
104 | 0 | } catch (FileNotFoundException ex) { |
105 | |
|
106 | 0 | IOUtil.close(ntIdxRaf); |
107 | 0 | IOUtil.close(ntTextRaf); |
108 | |
|
109 | 0 | assert false : ex; |
110 | 0 | LOGGER.error("Could not open NT", ex); |
111 | 0 | ntIdxRaf = null; |
112 | 0 | ntTextRaf = null; |
113 | 0 | } |
114 | |
} |
115 | 0 | } |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
public boolean isWritable() { |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | 0 | if (otIdxFile.canRead() && (otIdxFile.canWrite() || !otTextFile.canWrite())) { |
126 | 0 | return false; |
127 | |
} |
128 | 0 | if (ntIdxFile.canRead() && (ntIdxFile.canWrite() || !ntTextFile.canWrite())) { |
129 | 0 | return false; |
130 | |
} |
131 | 0 | return otIdxFile.canRead() || ntIdxFile.canRead(); |
132 | |
} |
133 | |
|
134 | |
public void releaseResources() { |
135 | 0 | IOUtil.close(ntIdxRaf); |
136 | 0 | IOUtil.close(ntTextRaf); |
137 | 0 | IOUtil.close(otIdxRaf); |
138 | 0 | IOUtil.close(otTextRaf); |
139 | 0 | ntIdxRaf = null; |
140 | 0 | ntTextRaf = null; |
141 | 0 | otIdxRaf = null; |
142 | 0 | otTextRaf = null; |
143 | 0 | } |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
public RandomAccessFile getIdxRaf(Testament testament) { |
152 | 0 | return testament == Testament.NEW ? ntIdxRaf : otIdxRaf; |
153 | |
} |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
public RandomAccessFile getTextRaf(Testament testament) { |
162 | 0 | return testament == Testament.NEW ? ntTextRaf : otTextRaf; |
163 | |
} |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
public RandomAccessFile getOtTextRaf() { |
169 | 0 | return otTextRaf; |
170 | |
} |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
public RandomAccessFile getNtTextRaf() { |
176 | 0 | return ntTextRaf; |
177 | |
} |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
public File getTextFile(Testament testament) { |
186 | 0 | return testament == Testament.NEW ? ntTextFile : otTextFile; |
187 | |
} |
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
public File getIdxFile(Testament testament) { |
196 | 0 | return testament == Testament.NEW ? ntIdxFile : otIdxFile; |
197 | |
} |
198 | |
|
199 | |
protected RandomAccessFile otIdxRaf; |
200 | |
protected RandomAccessFile ntIdxRaf; |
201 | |
protected RandomAccessFile otTextRaf; |
202 | |
protected RandomAccessFile ntTextRaf; |
203 | |
protected File ntIdxFile; |
204 | |
protected File ntTextFile; |
205 | |
protected File otIdxFile; |
206 | |
protected File otTextFile; |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | 0 | private static final Logger LOGGER = LoggerFactory.getLogger(RawBackendState.class); |
212 | |
} |