Initial revision
[TestXSLT.git] / XSLTProcessorSablotron.m
1 //
2 //  XSLTProcessorSablotron.m
3 //  TestXSLT
4 //
5 //  Created by Marc Liyanage on Thu Aug 01 2002.
6 //  Copyright (c) 2002 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "XSLTProcessorSablotron.h"
10
11
12 @implementation XSLTProcessorSablotron
13
14
15
16 - (int)processorType {
17
18         return PROCESSORTYPE_SABLOTRON;
19
20 }
21
22
23
24 +(BOOL)checkWellFormed:(NSString *)xmlCode {
25
26         return NO;
27
28 }
29
30
31
32
33
34 - (id)init {
35
36         if (self = [super init]) {
37
38                 [self clearResult];
39                 [self clearError];
40
41                 SablotCreateSituation(&S);
42                 SablotCreateProcessorForSituation(S, &processor);
43                 
44                 memset(&msgHandlerPtr, 0, sizeof(MessageHandler));
45
46                 msgHandlerPtr.makeCode = (MessageHandlerMakeCode *)rawMakeCodeHandler;
47                 msgHandlerPtr.log = (MessageHandlerLog *)rawLogHandler;
48                 msgHandlerPtr.error = (MessageHandlerError *)rawErrorHandler;
49                 SablotRegHandler(processor, HLR_MESSAGE, &msgHandlerPtr, self);
50
51         }
52
53         return self;
54
55 }
56
57
58
59
60 - (void)dealloc {
61
62         SablotDestroyProcessor(processor);
63         SablotDestroySituation(S);
64
65         [super dealloc];
66 }
67
68
69
70
71
72
73
74
75
76
77
78 - (BOOL)processStrings:(NSString *)xmlCode withXslt:(NSString *)xsltCode andParameters:(const char **)params {
79
80         char *resultBuffer;
81
82         int resultCode = 0, i=0;
83
84
85         [self clearError];
86
87
88         if ([self baseUri] != nil) {
89                 SablotSetBase(processor, [[NSString stringWithFormat:@"%@/", [[self baseUri] stringByDeletingLastPathComponent]] cString]);
90         }
91
92         SablotAddArgBuffer(S, processor, "xslt", (char *)[xsltCode cString]);
93         SablotAddArgBuffer(S, processor, "xml", (char *)[xmlCode cString]);
94
95         while (params && params[i]) {
96                 SablotAddParam(S, processor, params[i], params[i+1]);
97                 i += 2;
98         }
99
100         
101 //      resultCode = SablotRunProcessor(processor, "arg:/xslt", "arg:/xml", "arg:/result", params, args);
102         resultCode = SablotRunProcessorGen(S, processor, "arg:/xslt", "arg:/xml", "arg:/result");
103
104         if ([self errorOccurred]) {
105                 return NO;
106         }
107
108         SablotGetResultArg(processor, "arg:/result", &resultBuffer);
109
110         [self setResult:[NSString stringWithCString:resultBuffer]];
111                  
112         SablotFree(resultBuffer);
113
114         return YES;
115         
116 }
117
118
119
120 - (BOOL)processStringsOld:(NSString *)xmlCode withXslt:(NSString *)xsltCode andParameters:(const char **)params {
121
122         char *resultBuffer;
123         const char *args[7];
124
125         int resultCode = 0;
126
127         args[0] = "/xml";
128         args[1] = (char *)[xmlCode cString];
129         args[2] = "/xslt";
130         args[3] = (char *)[xsltCode cString];
131         args[4] = "/result";
132         args[5] = (char *)&resultBuffer;
133         args[6] = NULL;
134
135         [self clearError];
136
137
138         if ([self baseUri] != nil) {
139                 SablotSetBase(processor, [[self baseUri] cString]);
140         }
141         
142         resultCode = SablotRunProcessor(processor, "arg:/xslt", "arg:/xml", "arg:/result", params, args);
143
144         if ([self errorOccurred]) {
145                 return NO;
146         }
147
148         SablotGetResultArg(processor,"arg:/result", &resultBuffer);
149
150         [self setResult:[NSString stringWithCString:resultBuffer]];
151                  
152         SablotFree(resultBuffer);
153
154         return YES;
155         
156 }
157
158
159
160 MH_ERROR rawErrorHandler(id self, SablotHandle processor_, MH_ERROR code, MH_LEVEL level, char **fields) {
161
162         int i = 0;
163         NSString *currentField;
164         NSRange colonRange, keyRange, valueRange;
165         NSString *key;
166         NSString *value;
167
168         NSString *errorMessage;
169         int errorLine;
170         int errorSource;
171
172         for (i = 0; fields[i] != NULL; i++) {
173
174                 currentField = [NSString stringWithCString:fields[i]];
175
176                 colonRange = [currentField rangeOfString:@":"];
177                 keyRange = NSMakeRange(0, colonRange.location);
178                 valueRange = NSMakeRange(colonRange.location + 1, [currentField length] - (colonRange.location + 1));
179
180                 key = [currentField substringWithRange:keyRange];
181                 value = [currentField substringWithRange:valueRange];
182
183                 if ([key isEqual:@"msg"]) {
184                         errorMessage = value;
185                 }
186
187                 if ([key isEqual:@"line"]) {
188                         errorLine = [value intValue];
189                 }
190
191                 if ([key isEqual:@"URI"]) {
192
193                         if ([value isEqual:@"arg:/xml"]) {
194                                 errorSource = XSLT_ERROR_SOURCE_XML;
195                         } else if ([value isEqual:@"arg:/xslt"]) {
196                                 errorSource = XSLT_ERROR_SOURCE_XSLT;
197                         }
198                 }
199
200         }
201
202         [self setError:errorMessage atLine:errorLine inSource:errorSource];
203
204         return 0;
205
206 }
207
208
209 MH_ERROR rawMakeCodeHandler(id self, SablotHandle processor_, int severity, unsigned short facility, unsigned short code) {
210
211         //      NSLog(@"makeCode, proc %p, sev %d, fac %d, code %d", processor_, severity, facility, code);
212         return 0;
213 }
214
215
216 MH_ERROR rawLogHandler(id self, SablotHandle processor_, MH_ERROR code, MH_LEVEL level, char **fields) {
217
218         int i = 0;
219
220         for (i = 0; fields[i] != NULL; i++) {
221                 //              NSLog(@"Sablotron Log: %s", fields[i]);
222         }
223
224         return 0;
225
226 }
227
228
229
230
231
232 @end