upgraded to native target
[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:(NSData *)xmlCode withXslt:(NSData *)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         char *xmlBuffer = malloc([xmlCode length] + 1);
93         [xmlCode getBytes:xmlBuffer];
94         xmlBuffer[[xmlCode length]] = '\0';
95         
96         char *xsltBuffer = malloc([xsltCode length] + 1);
97         [xsltCode getBytes:xsltBuffer];
98         xsltBuffer[[xsltCode length]] = '\0';
99
100         SablotAddArgBuffer(S, processor, "xml", (char *)xmlBuffer);
101         SablotAddArgBuffer(S, processor, "xslt", (char *)xsltBuffer);
102
103         while (params && params[i]) {
104                 SablotAddParam(S, processor, params[i], params[i+1]);
105                 i += 2;
106         }
107
108         
109 //      resultCode = SablotRunProcessor(processor, "arg:/xslt", "arg:/xml", "arg:/result", params, args);
110         resultCode = SablotRunProcessorGen(S, processor, "arg:/xslt", "arg:/xml", "arg:/result");
111
112         free(xmlBuffer);
113         free(xsltBuffer);
114         
115         if ([self errorOccurred]) {
116                 return NO;
117         }
118
119         SablotGetResultArg(processor, "arg:/result", &resultBuffer);
120         
121         [self setResult:[NSData dataWithBytes:resultBuffer length:strlen(resultBuffer)]];
122         [self setResultEncodingFromData:xsltCode];
123         SablotFree(resultBuffer);
124
125         return YES;
126         
127 }
128
129
130
131 MH_ERROR rawErrorHandler(id self, SablotHandle processor_, MH_ERROR code, MH_LEVEL level, char **fields) {
132
133         int i = 0;
134         NSString *currentField;
135         NSRange colonRange, keyRange, valueRange;
136         NSString *key;
137         NSString *value;
138
139         NSString *errorMessage;
140         int errorLine;
141         int errorSource;
142
143         for (i = 0; fields[i] != NULL; i++) {
144
145                 currentField = [NSString stringWithCString:fields[i]];
146
147                 colonRange = [currentField rangeOfString:@":"];
148                 keyRange = NSMakeRange(0, colonRange.location);
149                 valueRange = NSMakeRange(colonRange.location + 1, [currentField length] - (colonRange.location + 1));
150
151                 key = [currentField substringWithRange:keyRange];
152                 value = [currentField substringWithRange:valueRange];
153
154                 if ([key isEqual:@"msg"]) {
155                         errorMessage = value;
156                 }
157
158                 if ([key isEqual:@"line"]) {
159                         errorLine = [value intValue];
160                 }
161
162                 if ([key isEqual:@"URI"]) {
163
164                         if ([value isEqual:@"arg:/xml"]) {
165                                 errorSource = XSLT_ERROR_SOURCE_XML;
166                         } else if ([value isEqual:@"arg:/xslt"]) {
167                                 errorSource = XSLT_ERROR_SOURCE_XSLT;
168                         }
169                 }
170
171         }
172
173         [self setError:errorMessage atLine:errorLine inSource:errorSource];
174
175         return 0;
176
177 }
178
179
180 MH_ERROR rawMakeCodeHandler(id self, SablotHandle processor_, int severity, unsigned short facility, unsigned short code) {
181
182         //      NSLog(@"makeCode, proc %p, sev %d, fac %d, code %d", processor_, severity, facility, code);
183         return 0;
184 }
185
186
187 MH_ERROR rawLogHandler(id self, SablotHandle processor_, MH_ERROR code, MH_LEVEL level, char **fields) {
188
189         int i = 0;
190
191         for (i = 0; fields[i] != NULL; i++) {
192                 //              NSLog(@"Sablotron Log: %s", fields[i]);
193         }
194
195         return 0;
196
197 }
198
199
200
201
202
203 @end