2 // XSLTProcessorLibxslt.m
5 // Created by Marc Liyanage on Thu Aug 01 2002.
6 // Copyright (c) 2002 __MyCompanyName__. All rights reserved.
9 #import "XSLTProcessorLibxslt.h"
14 @implementation XSLTProcessorLibxslt
16 - (int)processorType {
18 return PROCESSORTYPE_LIBXSLT;
26 - (BOOL)processStrings:(NSString *)xmlCode withXslt:(NSString *)xsltCode andParameters:(const char **)params {
28 xmlChar *resultBuffer = NULL;
32 XMLParserLibxml *xmlParser = [[[XMLParserLibxml alloc] init] autorelease];
33 XMLParserLibxml *xsltParser = [[[XMLParserLibxml alloc] init] autorelease];
35 if ([self baseUri] != nil) {
36 [xsltParser setBaseUri:[self baseUri]];
39 xmlDocPtr resultDoc = NULL;
40 xsltStylesheetPtr stylesheet = NULL;
41 xsltTemplatePtr template = NULL;
47 xsltRegisterTestModule();
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.
55 xsltSetGenericErrorFunc(self, (xmlGenericErrorFunc)xsltErrorHandler);
58 if (![xmlParser parseString:xmlCode]) {
59 [self setError:[xmlParser errorMessage] atLine:[xmlParser errorLine] inSource:XSLT_ERROR_SOURCE_XML];
63 if (![xsltParser parseString:xsltCode]) {
64 [self setError:[xsltParser errorMessage] atLine:[xsltParser errorLine] inSource:XSLT_ERROR_SOURCE_XSLT];
71 stylesheet = xsltParseStylesheetDoc([xsltParser nativeDoc]);
74 [self setErrorSource:XSLT_ERROR_SOURCE_XSLT];
78 resultDoc = xsltApplyStylesheet(stylesheet, [xmlParser nativeDoc], params);
79 // resultDoc = xsltProfileStylesheet(stylesheet, [xmlParser nativeDoc], params, stderr);
82 [self setErrorSource:XSLT_ERROR_SOURCE_XSLT];
90 if (![self errorOccurred]) {
92 bytesWritten = xsltSaveResultToString(&resultBuffer, &resultSize, resultDoc, stylesheet);
93 [self setResult:[NSString stringWithCString:resultBuffer]];
97 template = stylesheet->templates;
98 while (template != NULL) {
100 if (template->match) {
101 NSLog(@"match: %s", template->match);
104 if (template->name) {
105 NSLog(@"name: %s", template->name);
108 if (template->mode) {
109 NSLog(@"mode: %s", template->mode);
112 NSLog(@"count: %d", template->nbCalls);
113 NSLog(@"time: %ld", template->time);
115 template = template->next;
123 xmlFreeDoc(resultDoc);
130 return ![self errorOccurred];
137 void xsltErrorHandler(id self, const char *message, ...) {
139 const char *end = message + (strlen(message) - 1);
140 char *pos = (char *)message;
141 char *errorstring = NULL;
143 NSString *errorMessage;
144 char completeMessage[512];
147 /* Do not overwrite previous error information so the
148 first error in a series will be preserved
150 if ([self errorOccurred]) {
155 NSLog(@"xslt error handler: %s", message);
157 va_start(args, message);
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);
173 va_start(args, message);
174 vsnprintf(completeMessage, 512, message, args);
177 errorMessage = [NSString stringWithCString:completeMessage];
178 [self setError:errorMessage atLine:errorLine inSource:0];