Source code: com/eireneh/bible/book/VersewiseBible.java
1
2 package com.eireneh.bible.book;
3
4 import java.util.*;
5 import java.io.*;
6 import java.net.*;
7
8 import com.eireneh.util.*;
9 import com.eireneh.config.Config;
10 import com.eireneh.bible.util.Project;
11 import com.eireneh.bible.passage.*;
12 import com.eireneh.config.choices.TextViewChoice;
13
14 /**
15 * The VersewiseMutableBook class makes it easier to implement
16 * MutableBook by splitting the job up into a Verse by Verse effort.
17 * setDocument() is called once for every verse in the Bible in order,
18 * and flush() is called when done.
19 *
20 * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
21 * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
22 * Distribution Licence:<br />
23 * Project B is free software; you can redistribute it
24 * and/or modify it under the terms of the GNU General Public License,
25 * version 2 as published by the Free Software Foundation.<br />
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 * General Public License for more details.<br />
30 * The License is available on the internet
31 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
32 * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
33 * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
34 * The copyright to this program is held by it's authors.
35 * </font></td></tr></table>
36 * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
37 * @see docs.Licence
38 * @author Joe Walker
39 * @version D5.I4.T2
40 */
41 public abstract class VersewiseBible extends AbstractBible
42 {
43 /**
44 * Write the XML to disk. Children will almost certainly want to
45 * override this.
46 * @param verse The verse to write
47 * @param text The data to write
48 */
49 public abstract void setDocument(BibleEle text) throws BookException;
50
51 /**
52 * Save a list of found words. Children will probably want to
53 * override this.
54 * @param word The word to write
55 * @param ref The data to write
56 */
57 public abstract void foundPassage(String word, Passage ref) throws BookException;
58
59 /**
60 * Setup the Version information
61 * @param version The version that this Bible is becoming
62 */
63 public abstract void setVersion(Version version);
64
65 /**
66 * Flush the data written to disk
67 */
68 public void flush() throws BookException
69 {
70 }
71
72 /**
73 * Get a URL in which we can save a generation report
74 * @return A directory URL into which we can save files
75 */
76 public abstract URL getBaseURL();
77
78 /**
79 * This returns a simple Config, that displays the generation notes
80 */
81 public Config getProperties() throws BookException
82 {
83 try
84 {
85 URL dir = NetUtil.lengthenURL(getBaseURL(), "generate.log");
86
87 Config config = new Config("Bible Settings");
88 config.add("Notes.Log", new TextViewChoice(dir));
89
90 return config;
91 }
92 catch (MalformedURLException ex)
93 {
94 throw new BookException("book_prop", ex);
95 }
96 }
97
98 /**
99 * Read from the given source version to generate ourselves
100 * @param version The source
101 */
102 public void generate(Bible version) throws BookException
103 {
104 try
105 {
106 URL url = NetUtil.lengthenURL(getBaseURL(), "generate.log");
107 PrintWriter out = new PrintWriter(NetUtil.getOutputStream(url));
108
109 out.println("Generating Bible");
110 out.println("Dest driver = "+getClass().getName());
111 out.println("Dest url = "+getBaseURL());
112 out.println("Source name = "+version.getName());
113 out.println("Source driver = "+version.getClass().getName());
114 out.flush();
115
116 // Generate
117 setVersion(version.getVersion());
118 generatePassages(version);
119 generateText(version);
120 flush();
121 }
122 catch (IOException ex)
123 {
124 throw new BookException("book_gen", ex);
125 }
126 catch (NoSuchVerseException ex)
127 {
128 throw new LogicError(ex);
129 }
130 }
131
132 /**
133 * Read from the given source version to generate ourselves
134 * @param version The source
135 */
136 protected void generateText(Bible source) throws IOException, NoSuchVerseException, BookException
137 {
138 Passage temp = PassageFactory.createPassage(PassageFactory.SPEED);
139
140 // For every verse in the Bible
141 Enumeration en = whole.verseElements();
142 while (en.hasMoreElements())
143 {
144 // Create a Passage containing that verse alone
145 Verse verse = (Verse) en.nextElement();
146 temp.clear();
147 temp.add(verse);
148
149 // Fire a progress event?
150 fireProgressMade("Writing Verses:", 100 * verse.getOrdinal() / Books.versesInBible());
151
152 // Read the document from the original version
153 BibleEle doc = new BibleEle();
154 source.getDocument(doc, temp);
155
156 // Write the document to the mutable version
157 setDocument(doc);
158
159 // This could take a long time ...
160 Thread.currentThread().yield();
161 if (Thread.currentThread().isInterrupted())
162 break;
163 }
164 }
165
166 /**
167 * Read from the given source version to generate ourselves
168 * @param version The source
169 */
170 protected void generatePassages(Bible source) throws IOException, NoSuchVerseException, BookException
171 {
172 int count = 0;
173
174 // For every word in the word list
175 Enumeration en = source.listWords();
176 while (en.hasMoreElements())
177 {
178 // Read and write
179 String word = (String) en.nextElement();
180 Passage ref_source = source.findPassage(word);
181 foundPassage(word, ref_source);
182
183 // Fire a progress event?
184 fireProgressMade("Writing Words:", 100 * count++ / Version.GUESS_WORDS);
185
186 // This could take a long time ...
187 Thread.currentThread().yield();
188 if (Thread.currentThread().isInterrupted())
189 break;
190 }
191 }
192
193 /** The Whole Bible */
194 private static Passage whole = PassageFactory.getWholeBiblePassage();
195 }