2 // XSLTProcessorSablotron.m
5 // Created by Marc Liyanage on Thu Aug 01 2002.
6 // Copyright (c) 2002 __MyCompanyName__. All rights reserved.
9 #import "XSLTProcessorSablotron.h"
12 @implementation XSLTProcessorSablotron
16 - (int)processorType {
18 return PROCESSORTYPE_SABLOTRON;
24 +(BOOL)checkWellFormed:(NSString *)xmlCode {
36 if (self = [super init]) {
41 SablotCreateSituation(&S);
42 SablotCreateProcessorForSituation(S, &processor);
44 memset(&msgHandlerPtr, 0, sizeof(MessageHandler));
46 msgHandlerPtr.makeCode = (MessageHandlerMakeCode *)rawMakeCodeHandler;
47 msgHandlerPtr.log = (MessageHandlerLog *)rawLogHandler;
48 msgHandlerPtr.error = (MessageHandlerError *)rawErrorHandler;
49 SablotRegHandler(processor, HLR_MESSAGE, &msgHandlerPtr, self);
62 SablotDestroyProcessor(processor);
63 SablotDestroySituation(S);
78 - (BOOL)processStrings:(NSData *)xmlCode withXslt:(NSData *)xsltCode andParameters:(const char **)params {
82 int resultCode = 0, i=0;
88 if ([self baseUri] != nil) {
89 SablotSetBase(processor, [[NSString stringWithFormat:@"%@/", [[self baseUri] stringByDeletingLastPathComponent]] cString]);
92 char *xmlBuffer = malloc([xmlCode length] + 1);
93 [xmlCode getBytes:xmlBuffer];
94 xmlBuffer[[xmlCode length]] = '\0';
96 char *xsltBuffer = malloc([xsltCode length] + 1);
97 [xsltCode getBytes:xsltBuffer];
98 xsltBuffer[[xsltCode length]] = '\0';
100 SablotAddArgBuffer(S, processor, "xml", (char *)xmlBuffer);
101 SablotAddArgBuffer(S, processor, "xslt", (char *)xsltBuffer);
103 while (params && params[i]) {
104 SablotAddParam(S, processor, params[i], params[i+1]);
109 // resultCode = SablotRunProcessor(processor, "arg:/xslt", "arg:/xml", "arg:/result", params, args);
110 resultCode = SablotRunProcessorGen(S, processor, "arg:/xslt", "arg:/xml", "arg:/result");
115 if ([self errorOccurred]) {
119 SablotGetResultArg(processor, "arg:/result", &resultBuffer);
121 [self setResult:[NSData dataWithBytes:resultBuffer length:strlen(resultBuffer)]];
122 [self setResultEncodingFromData:xsltCode];
123 SablotFree(resultBuffer);
131 MH_ERROR rawErrorHandler(id self, SablotHandle processor_, MH_ERROR code, MH_LEVEL level, char **fields) {
134 NSString *currentField;
135 NSRange colonRange, keyRange, valueRange;
139 NSString *errorMessage;
143 for (i = 0; fields[i] != NULL; i++) {
145 currentField = [NSString stringWithCString:fields[i]];
147 colonRange = [currentField rangeOfString:@":"];
148 keyRange = NSMakeRange(0, colonRange.location);
149 valueRange = NSMakeRange(colonRange.location + 1, [currentField length] - (colonRange.location + 1));
151 key = [currentField substringWithRange:keyRange];
152 value = [currentField substringWithRange:valueRange];
154 if ([key isEqual:@"msg"]) {
155 errorMessage = value;
158 if ([key isEqual:@"line"]) {
159 errorLine = [value intValue];
162 if ([key isEqual:@"URI"]) {
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;
173 [self setError:errorMessage atLine:errorLine inSource:errorSource];
180 MH_ERROR rawMakeCodeHandler(id self, SablotHandle processor_, int severity, unsigned short facility, unsigned short code) {
182 // NSLog(@"makeCode, proc %p, sev %d, fac %d, code %d", processor_, severity, facility, code);
187 MH_ERROR rawLogHandler(id self, SablotHandle processor_, MH_ERROR code, MH_LEVEL level, char **fields) {
191 for (i = 0; fields[i] != NULL; i++) {
192 // NSLog(@"Sablotron Log: %s", fields[i]);