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:(NSData *)xmlCode withXslt:(NSData *)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 parseData:xmlCode]) {
59 [self setError:[xmlParser errorMessage] atLine:[xmlParser errorLine] inSource:XSLT_ERROR_SOURCE_XML];
63 if (![xsltParser parseData: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);
94 [self setResult:[NSData dataWithBytes:resultBuffer length:resultSize]];
95 [self setResultEncodingFromData:xsltCode];
99 template = stylesheet->templates;
100 while (template != NULL) {
102 if (template->match) {
103 NSLog(@"match: %s", template->match);
106 if (template->name) {
107 NSLog(@"name: %s", template->name);
110 if (template->mode) {
111 NSLog(@"mode: %s", template->mode);
114 NSLog(@"count: %d", template->nbCalls);
115 NSLog(@"time: %ld", template->time);
117 template = template->next;
125 xmlFreeDoc(resultDoc);
132 return ![self errorOccurred];
139 void xsltErrorHandler(id self, const char *message, ...) {
141 const char *end = message + (strlen(message) - 1);
142 char *pos = (char *)message;
143 char *errorstring = NULL;
145 NSString *errorMessage;
146 char completeMessage[512];
149 /* Do not overwrite previous error information so the
150 first error in a series will be preserved
152 if ([self errorOccurred]) {
157 NSLog(@"xslt error handler: %s", message);
159 va_start(args, message);
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);
175 va_start(args, message);
176 vsnprintf(completeMessage, 512, message, args);
179 errorMessage = [NSString stringWithCString:completeMessage];
180 [self setError:errorMessage atLine:errorLine inSource:0];