Initial revision
[TestXSLT.git] / XSLTProcessorLibxslt.m
1 //
2 //  XSLTProcessorLibxslt.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 "XSLTProcessorLibxslt.h"
10
11 #define MAX_PATHS 64
12
13
14 @implementation XSLTProcessorLibxslt
15
16 - (int)processorType {
17
18         return PROCESSORTYPE_LIBXSLT;
19
20 }
21
22
23
24
25
26 - (BOOL)processStrings:(NSString *)xmlCode withXslt:(NSString *)xsltCode andParameters:(const char **)params {
27
28         xmlChar *resultBuffer = NULL;
29         int resultSize = 0;
30         int bytesWritten = 0;
31
32         XMLParserLibxml *xmlParser = [[[XMLParserLibxml alloc] init] autorelease];
33         XMLParserLibxml *xsltParser = [[[XMLParserLibxml alloc] init] autorelease];
34
35         if ([self baseUri] != nil) {
36                 [xsltParser setBaseUri:[self baseUri]];
37         }
38
39         xmlDocPtr resultDoc = NULL;
40         xsltStylesheetPtr stylesheet = NULL;
41         xsltTemplatePtr template = NULL;
42
43
44         [self clearError];
45         
46         exsltRegisterAll();
47         xsltRegisterTestModule();
48
49
50         
51
52         /* There might be a race condition here as the error handler seems to be set globally.
53          * Multiple Documents processing at the same time could interfere.
54          */
55         xsltSetGenericErrorFunc(self, (xmlGenericErrorFunc)xsltErrorHandler);
56         while (1) {
57
58                 if (![xmlParser parseString:xmlCode]) {
59                         [self setError:[xmlParser errorMessage] atLine:[xmlParser errorLine] inSource:XSLT_ERROR_SOURCE_XML];
60                         break;
61                 }
62                 
63                 if (![xsltParser parseString:xsltCode]) {
64                         [self setError:[xsltParser errorMessage] atLine:[xsltParser errorLine] inSource:XSLT_ERROR_SOURCE_XSLT];
65                         break;
66                 }
67
68                 
69
70                 
71                 stylesheet = xsltParseStylesheetDoc([xsltParser nativeDoc]);
72
73                 if (!stylesheet) {
74                         [self setErrorSource:XSLT_ERROR_SOURCE_XSLT];
75                         break;
76                 }
77
78                 resultDoc = xsltApplyStylesheet(stylesheet, [xmlParser nativeDoc], params);
79 //              resultDoc = xsltProfileStylesheet(stylesheet, [xmlParser nativeDoc], params, stderr);
80
81                 if (!resultDoc) {
82                         [self setErrorSource:XSLT_ERROR_SOURCE_XSLT];
83                         break;
84                 }
85
86                 break;
87         }
88
89
90         if (![self errorOccurred]) {
91
92                 bytesWritten = xsltSaveResultToString(&resultBuffer, &resultSize, resultDoc, stylesheet);
93                 [self setResult:[NSString stringWithCString:resultBuffer]];
94
95
96                 /*
97                 template = stylesheet->templates;
98                 while (template != NULL) {
99
100                         if (template->match) {
101                                 NSLog(@"match: %s", template->match);
102                         }
103
104                         if (template->name) {
105                                 NSLog(@"name: %s", template->name);
106                         }
107
108                         if (template->mode) {
109                                 NSLog(@"mode: %s", template->mode);
110                         }
111
112                         NSLog(@"count: %d", template->nbCalls);
113                         NSLog(@"time: %ld", template->time);
114
115                         template = template->next;
116                 }
117                 */
118                 
119                 
120         }
121         
122         if (resultDoc) {
123                 xmlFreeDoc(resultDoc);
124         }
125
126         if (resultBuffer) {
127                 free(resultBuffer);
128         }
129
130         return ![self errorOccurred];
131 }
132
133
134
135
136
137 void xsltErrorHandler(id self, const char *message, ...) {
138
139         const char *end = message + (strlen(message) - 1);
140         char *pos = (char *)message;
141         char *errorstring = NULL;
142         int errorLine = 0;
143         NSString *errorMessage;
144         char completeMessage[512];
145         va_list args;
146
147         /*      Do not overwrite previous error information so the
148                 first error in a series will be preserved
149          */
150         if ([self errorOccurred]) {
151                 return;
152         }
153
154
155         NSLog(@"xslt error handler: %s", message);
156         
157         va_start(args, message);
158
159         while (pos <= end) {
160
161                 if (!strncmp(pos, "%s", 2)) {
162                         errorstring = va_arg(args, char *);
163                 } else if (!strncmp(pos, "%d", 2)) {
164                         errorLine = va_arg(args, int);
165                 }
166
167                 pos++;
168
169         }
170
171     va_end(args);
172
173         va_start(args, message);
174         vsnprintf(completeMessage, 512, message, args);
175     va_end(args);
176
177         errorMessage = [NSString stringWithCString:completeMessage];
178         [self setError:errorMessage atLine:errorLine inSource:0];
179
180
181 }
182
183
184
185
186
187
188
189
190
191
192 @end