new files for 2.9
[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:(NSData *)xmlCode withXslt:(NSData *)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 parseData:xmlCode]) {
59                         [self setError:[xmlParser errorMessage] atLine:[xmlParser errorLine] inSource:XSLT_ERROR_SOURCE_XML];
60                         break;
61                 }
62                 
63                 if (![xsltParser parseData: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
94                 [self setResult:[NSData dataWithBytes:resultBuffer length:resultSize]];
95                 [self setResultEncodingFromData:xsltCode];
96
97                 
98                 /*
99                 template = stylesheet->templates;
100                 while (template != NULL) {
101
102                         if (template->match) {
103                                 NSLog(@"match: %s", template->match);
104                         }
105
106                         if (template->name) {
107                                 NSLog(@"name: %s", template->name);
108                         }
109
110                         if (template->mode) {
111                                 NSLog(@"mode: %s", template->mode);
112                         }
113
114                         NSLog(@"count: %d", template->nbCalls);
115                         NSLog(@"time: %ld", template->time);
116
117                         template = template->next;
118                 }
119                 */
120                 
121                 
122         }
123         
124         if (resultDoc) {
125                 xmlFreeDoc(resultDoc);
126         }
127
128         if (resultBuffer) {
129                 free(resultBuffer);
130         }
131
132         return ![self errorOccurred];
133 }
134
135
136
137
138
139 void xsltErrorHandler(id self, const char *message, ...) {
140
141         const char *end = message + (strlen(message) - 1);
142         char *pos = (char *)message;
143         char *errorstring = NULL;
144         int errorLine = 0;
145         NSString *errorMessage;
146         char completeMessage[512];
147         va_list args;
148
149         /*      Do not overwrite previous error information so the
150                 first error in a series will be preserved
151          */
152         if ([self errorOccurred]) {
153                 return;
154         }
155
156
157         NSLog(@"xslt error handler: %s", message);
158         
159         va_start(args, message);
160
161         while (pos <= end) {
162
163                 if (!strncmp(pos, "%s", 2)) {
164                         errorstring = va_arg(args, char *);
165                 } else if (!strncmp(pos, "%d", 2)) {
166                         errorLine = va_arg(args, int);
167                 }
168
169                 pos++;
170
171         }
172
173     va_end(args);
174
175         va_start(args, message);
176         vsnprintf(completeMessage, 512, message, args);
177     va_end(args);
178
179         errorMessage = [NSString stringWithCString:completeMessage];
180         [self setError:errorMessage atLine:errorLine inSource:0];
181
182
183 }
184
185
186
187
188
189
190
191
192
193
194 @end