Source code: com/eireneh/bible/book/jdbc/JDBCBibleUtil.java
1
2 package com.eireneh.bible.book.jdbc;
3
4 import com.eireneh.util.*;
5
6 /**
7 * JDBCBible was getting a bit long winded, so I took all the static
8 * methods and parcled them off here.
9 *
10 * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
11 * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
12 * Distribution Licence:<br />
13 * Project B is free software; you can redistribute it
14 * and/or modify it under the terms of the GNU General Public License,
15 * version 2 as published by the Free Software Foundation.<br />
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.<br />
20 * The License is available on the internet
21 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
22 * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
24 * The copyright to this program is held by it's authors.
25 * </font></td></tr></table>
26 * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
27 * @see docs.Licence
28 * @author Joe Walker
29 * @version D0.I0.T0
30 */
31 public class JDBCBibleUtil
32 {
33 /**
34 * Ensure that we can't be instansiated
35 */
36 private JDBCBibleUtil()
37 {
38 }
39
40 /**
41 * Removes a character from a String
42 * @param orig The string to parse.
43 * @param x The char to remove
44 * @param y The char to replace it with
45 * @return The string with the char replaced
46 */
47 protected static String swapChar(String orig, char x, char y)
48 {
49 StringBuffer retcode = new StringBuffer(orig);
50 int start = 0;
51
52 while (true)
53 {
54 start = orig.indexOf(x, start+1);
55 if (start == -1) break;
56 retcode.setCharAt(start, y);
57 }
58
59 return ""+retcode;
60 }
61
62 /**
63 * Some fancy footwork we need to do to get rid of strongs numbers
64 */
65 protected static String processText(String text)
66 {
67 text = StringUtil.chop(text, "{", "}");
68 text = chop(text, "<", ">");
69 text = chop(text, "(", ")");
70 text = StringUtil.removeChar(text, '[');
71 text = StringUtil.removeChar(text, ']');
72
73 text = StringUtil.swap(text, " ", " ");
74 text = StringUtil.swap(text, " ", " ");
75 text = StringUtil.swap(text, " ", " ");
76
77 text = StringUtil.swap(text, " ,", ",");
78 text = StringUtil.swap(text, " .", ".");
79 text = StringUtil.swap(text, " !", "!");
80 text = StringUtil.swap(text, " ?", "?");
81 text = StringUtil.swap(text, " :", ":");
82 text = StringUtil.swap(text, " ;", ";");
83 text = StringUtil.swap(text, " '", "'");
84 text = StringUtil.swap(text, " )", ")");
85 text = StringUtil.swap(text, " -", "-");
86
87 text = text.trim();
88
89 return text;
90 }
91
92 /**
93 * Strips the text between a pair of delimitters. For example:
94 * <code>chop("123(456)789", "(", ")") = "123789"</code>
95 * Delimiters currently do not nest. So:
96 * <code>chop("12(34(56)78)9", "(", ")") = Exception</code>
97 */
98 protected static String chop(String orig, String start_delim, String end_delim)
99 {
100 try
101 {
102 int skip_start = 0;
103 int skip_end = 0;
104
105 while (true)
106 {
107 // Find the next start and end delimitters, ensure that
108 // the end delimitters is after the start one.
109 int next_start = orig.indexOf(start_delim, skip_start);
110 skip_end = Math.max(skip_end, next_start);
111 int next_end = orig.indexOf(end_delim, skip_end);
112
113 // If there are no more give up
114 if (next_start == -1 || next_end == -1)
115 break;
116
117 // The text to be considered for chopping out
118 String chopped_text = orig.substring(next_start+start_delim.length(),
119 next_end);
120
121 // Check to see that what we are chopping out really is a number
122 try
123 {
124 Integer.parseInt(chopped_text);
125
126 orig = orig.substring(0, next_start) +
127 orig.substring(next_end+end_delim.length());
128 }
129 catch (NumberFormatException ex)
130 {
131 // It is not a number so we best leave it in
132 skip_start = next_start + 1;
133 skip_end = next_end + 1;
134 }
135 }
136
137 return orig;
138 }
139 catch (StringIndexOutOfBoundsException ex)
140 {
141 log.warning("orig="+orig+" end_delim="+end_delim+" end_delim="+end_delim);
142 Reporter.informUser(JDBCBibleUtil.class, ex);
143 return "Error";
144 }
145 }
146
147 /** The log stream */
148 protected static Logger log = Logger.getLogger("bible.book");
149 }