This repository has been archived by the owner on Oct 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLSLookup.m
178 lines (150 loc) · 4.35 KB
/
LSLookup.m
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// LSLookup.m
// Verba
//
// Created by Joshua Hayes on 7/5/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "LSLookup.h"
@implementation LSLookup
//Private methods
//Setters for pointer vars (all of which are essentially private)
- (void) setXMLData: (NSString*)input
{
[XMLData autorelease];
XMLData = [input retain];
}
- (void) setHeadword: (NSString*)input
{
[headword autorelease];
headword = [input retain];
}
- (void) setHTMLData: (NSString*)input
{
[HTMLData autorelease];
HTMLData = [input retain];
}
//transforms the XML already loaded into our obj
//TODO - fix Greek text display
- (NSString*) transformXML{
if([XMLData length] < 1){
//something's wrong
NSLog(@"Tried to tranformXML, but it's empty!");
return @"There was an error; please retry your search.";
}
// find XSLT code
NSString *xsltPath = [[NSBundle mainBundle]
pathForResource:@"tei" ofType:@"xsl"];
// transform through XSLT
NSError *err=nil;
//turn our XML string into a NSXMLDocument
NSXMLDocument* XMLDataAsNSDoc = [[NSXMLDocument alloc]
initWithXMLString:XMLData options:nil error:&err];
NSData* entryData = [XMLDataAsNSDoc objectByApplyingXSLTAtURL:[NSURL fileURLWithPath:xsltPath]
arguments:nil // no extra XSLT parameters needed
error:&err];
[XMLDataAsNSDoc release];
return [entryData description];
}
//Accessors
-(int) totalEntries{
return totalEntries;
}
-(int) currentEntryID{
return currentEntryID;
}
-(NSString*) headword{
return headword;
}
-(NSString*) HTMLData{
return HTMLData;
}
//Methods
-(BOOL) getEntryHTMLWithHeadwordString: (NSString*) requestString{
//reset the totalEntries count etc.
totalEntries = 0;
currentEntryID = 0;
//replace every j/J with i and every v with u
NSMutableString* mutableString = [NSMutableString stringWithString:requestString];
[mutableString replaceOccurrencesOfString:@"j"withString:@"i"
options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutableString length])];
[mutableString replaceOccurrencesOfString:@"v"withString:@"u"
options:nil range:NSMakeRange(0, [mutableString length])];
//A LIKE query means not case sensitive; not noticeably slower for now.
FMResultSet *rs =
[db executeQuery:@"SELECT headwords.*,entries.entry FROM headwords,entries WHERE headwords.headword LIKE ? AND headwords.id = entries.id", mutableString];
while ([rs next]) {
//This is because I want to know how many rows returned,
//but only the data from the first.
if(currentEntryID == 0){
currentEntryID = [rs intForColumn:@"id"];
[self setHeadword:[rs stringForColumn:@"headword"]];
[self setXMLData:[rs stringForColumn:@"entry"]];
}
totalEntries++;
}
// close the result set.
[rs close];
if(totalEntries > 0){
//fill in the rest of my data
[self setHTMLData:[self transformXML]];
return YES;
}else{
NSString* notFound = [NSString stringWithFormat:@"<em>%@</em> was not found.", requestString];
[self setHTMLData:notFound];
return NO;
}
}
-(BOOL) getEntryHTMLWithID: (int) requestID{
NSLog(@"Get entry HTML with id = %d", requestID);
FMResultSet *rs =
[db executeQuery:@"SELECT headwords.*,entries.entry FROM headwords,entries WHERE headwords.id = ? AND headwords.id = entries.id LIMIT 1",
[NSNumber numberWithInt:requestID]];
if ([rs next]) {
currentEntryID = [rs intForColumn:@"id"];
[self setHeadword:[rs stringForColumn:@"headword"]];
[self setXMLData:[rs stringForColumn:@"entry"]];
[rs close];
[self setHTMLData:[self transformXML]];
return YES;
}else{
// close the result set.
[rs close];
//if we can't find it, then this must be set to 0
currentEntryID = 0;
totalEntries = 0;
return NO;
}
}
//init and dealloc
- (id) init
{
if ( self = [super init] )
{
NSLog(@"Initializing %@", [self description]);
totalEntries = 0;
currentEntryID = 0;
NSString* dbPath = [[NSBundle mainBundle]
pathForResource:@"Lewis_Short" ofType:@"db"];
db = [FMDatabase databaseWithPath:dbPath];
if ([db open]) {
NSLog(@"Successfully opened db: %@", [db description]);
}else{
NSLog(@"Error: could not open db!");
}
//keep this db connection around please
[db retain];
}
return self;
}
//Hurray for no memory leaks!
- (void) dealloc
{
[db close];
[db release];
[XMLData release];
[headword release];
[HTMLData release];
[super dealloc];
}
@end