-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptureIndex.java
More file actions
152 lines (133 loc) · 3.72 KB
/
Copy pathScriptureIndex.java
File metadata and controls
152 lines (133 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//ScriptureIndex.java
//Emily Stuckey
//1-21-16
//Index.java adapted for scriptural indices
import java.util.*;
import java.util.regex.*;
import java.io.*;
public class ScriptureIndex extends ArrayList<ScriptureEntry>{
public ScriptureIndex(){
}
public ScriptureIndex(String sermon, String fileName){
try {
BufferedReader sermReader = new BufferedReader(new FileReader(fileName));
int para = 0;
String nextL = sermReader.readLine();
while (nextL != null){
Pattern p = Pattern.compile("^\\d+\\.?$");
Matcher m = p.matcher(nextL);
//if the only thing on the line is a digit
//set the para to that digit and advance
if (m.matches()){
para = Integer.parseInt(nextL);
}
//else add the scriptural reference on that line to the index
else
{
ScriptureEntry indEnt = findOrAdd(new ScriptCit(nextL));
indEnt.add(sermon, para);
}
nextL = sermReader.readLine();
}
} catch (IOException e){
System.out.println("IO exception: " + e.getMessage());
}
}
public ScriptureIndex(FileReader f1, FileReader f2){
this();
try {
BufferedReader file1 = new BufferedReader(f1);
BufferedReader file2 = new BufferedReader(f2);
String nextL1 = file1.readLine();
String nextL2 = file2.readLine();
while (nextL1 != null){
this.add(new ScriptureEntry(nextL1));
nextL1 = file1.readLine();
}
while (nextL2 != null){
this.add(new ScriptureEntry(nextL2));
nextL2 = file2.readLine();
}
} catch (IOException e){
System.out.println("problem in constructing merged index: " + e.getMessage());
}
}
/** Constructor to merge two indices
* @param ind1 The first Index
* @param ind2 The second Index
*/
public ScriptureIndex(ScriptureIndex ind1, ScriptureIndex ind2){
this();
for (ScriptureEntry ent : ind1){
add(ent);
}
for (ScriptureEntry ent2 : ind2){
add(ent2);
}
}
/** Constructor to read an index from a file (used for merging files)
* @param f FileReader object containing index in text form
*/
public ScriptureIndex(FileReader f){
this();
try {
BufferedReader file1 = new BufferedReader(f);
String nextL = file1.readLine();
while (nextL != null){
add(new ScriptureEntry(nextL));
nextL = file1.readLine();
}
} catch (IOException e){
System.out.println("problem creating an index from a file: " + e.getMessage());
}
}
public ScriptureEntry findOrAdd(ScriptCit newEnt){
if (this.size() == 0){
add(new ScriptureEntry(newEnt, null));
return this.get(0);
}
int i = 0;
boolean found = false;
ScriptureEntry cur = this.get(i);
while (i < this.size() && !found){
if (cur.getScript().compareTo(newEnt)==0){
found = true;
}
else if (cur.getScript().compareTo(newEnt) < 0){
i++;
if (i < this.size()){
cur = this.get(i);
}
}
else {
this.add(i, new ScriptureEntry(newEnt, null));
cur = this.get(i);
found = true;
}
}
if (i == this.size()){
this.add(i, new ScriptureEntry(newEnt, null));
cur = this.get(i);
}
return cur;
}
public boolean add(ScriptureEntry newEnt){
if (this.isEmpty()){
return super.add(newEnt);
}
else {
ScriptureEntry x = findOrAdd(newEnt.getScript());
x.add(newEnt.getCits());
return true;
}
}
public String toString(){
String result = "";
String ender = "";
for (ScriptureEntry blah : this){
result += ender + blah.toString();
ender = "\n";
}
return result;
}
}