--- /dev/null
+
+import java.io.*;
+
+import org.xml.sax.*;
+import com.apple.cocoa.foundation.NSData;
+
+//Avalon
+import org.apache.avalon.framework.ExceptionUtil;
+import org.apache.avalon.framework.logger.Logger;
+import org.apache.avalon.framework.logger.ConsoleLogger;
+
+//FOP
+import org.apache.fop.apps.Driver;
+import org.apache.fop.apps.FOPException;
+import org.apache.fop.messaging.MessageHandler;
+
+/**
+ * This class helps calling FOP from the Obj-C side.
+ * Based on "ExampleFO2PDF.java" from the FOP project.
+ */
+public class FOPWrapper {
+
+ String error;
+ boolean errorOccurred = false;
+
+ public NSData convert(String foString) throws IOException, FOPException {
+
+ //Construct driver
+ Driver driver = new Driver();
+
+ //Setup logger
+ Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
+ driver.setLogger(logger);
+ MessageHandler.setScreenLogger(logger);
+
+ //Setup Renderer (output format)
+ driver.setRenderer(Driver.RENDER_PDF);
+
+ //Setup output
+ ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
+ try {
+ driver.setOutputStream(out);
+
+ //Setup input
+ InputStream in = new java.io.ByteArrayInputStream(foString.getBytes("UTF-8"));
+ try {
+ driver.setInputSource(new InputSource(in));
+
+ //Process FO
+ driver.run();
+ } catch (Exception e) {
+ error = e.toString();
+ errorOccurred = true;
+ } finally {
+ in.close();
+ }
+
+ } finally {
+ out.close();
+ }
+
+ return new NSData(out.toByteArray());
+
+ }
+
+ public String getErrorMessage() {
+ return error;
+ }
+
+ public boolean errorOccurred() {
+ return errorOccurred;
+ }
+
+
+ /*
+ public static void main(String[] args) {
+ try {
+ System.out.println("FOP ExampleFO2PDF\n");
+ System.out.println("Preparing...");
+
+ //Setup directories
+ File baseDir = new File(".");
+ File outDir = new File(baseDir, "out");
+ outDir.mkdirs();
+
+ //Setup input and output files
+ File fofile = new File(baseDir, "xml/fo/helloworld.fo");
+ File pdffile = new File(outDir, "ResultFO2PDF.pdf");
+
+ System.out.println("Input: XSL-FO (" + fofile + ")");
+ System.out.println("Output: PDF (" + pdffile + ")");
+ System.out.println();
+ System.out.println("Transforming...");
+
+ FOPWrapper app = new FOPWrapper();
+ app.convertFO2PDF(fofile, pdffile);
+
+ System.out.println("Success!");
+ } catch (Exception e) {
+ System.err.println(ExceptionUtil.printStackTrace(e));
+ System.exit(-1);
+ }
+ }
+
+ */
+
+}
--- /dev/null
+//
+// XSLTProcessorJAXPBase.h
+// TestXSLT
+//
+// Created by Marc Liyanage on Mon Aug 18 2003.
+// Copyright (c) 2003 __MyCompanyName__. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "XSLTProcessor.h"
+
+
+
+@interface XSLTProcessorJAXPBase : XSLTProcessor {
+
+}
+
+- (NSString *) getJAXPProcessorName;
+
+
+@end
+
+
+// Java class declarations to keep the compiler happy
+
+@interface JAXPWrapper : NSObject
+{}
+- (int)getErrorSource;
+- (int)getErrorLine;
+- (NSString *)getErrorMessage;
+- (NSString *)getResult;
+- (BOOL)transform:(NSString *)processorClassName :(NSString *)xml :(NSString *)xslt :(NSString *)parameters :(NSString *)baseUri;
+@end
+
--- /dev/null
+//
+// XSLTProcessorJAXPBase.m
+// TestXSLT
+//
+// Created by Marc Liyanage on Mon Aug 18 2003.
+// Copyright (c) 2003 __MyCompanyName__. All rights reserved.
+//
+
+#import "XSLTProcessorJAXPBase.h"
+
+
+@implementation XSLTProcessorJAXPBase
+
+
+
+
+
+
+- (BOOL)processStrings:(NSString *)xmlCode withXslt:(NSString *)xsltCode andParameters:(const char **)params {
+
+ [self clearError];
+
+ NSString *jaxpBaseUri = @"";
+ if ([self baseUri] != nil) {
+ jaxpBaseUri = [self baseUri];
+ }
+
+
+ NSMutableString *paramBuffer = [[NSMutableString alloc] init];
+ int i = 0;
+ while (params && params[i]) {
+
+ [paramBuffer appendString:[NSString stringWithFormat:@"%s==_=!=_==%s", params[i], params[i+1]]];
+
+ if (params[i+2]) {
+ // more parameters to follow
+ [paramBuffer appendString:@"--_-!-_--"];
+ }
+
+ i += 2;
+ }
+
+ JAXPWrapper *jw = (JAXPWrapper *)[[NSClassFromString(@"JAXPWrapper") alloc] init];
+ BOOL swresult = [jw transform:[self getJAXPProcessorName] :xmlCode :xsltCode :paramBuffer :jaxpBaseUri];
+
+
+
+ if (swresult) {
+ [self setResult:[jw getResult]];
+ } else {
+ [self setError:[jw getErrorMessage] atLine:[jw getErrorLine] inSource:[jw getErrorSource]];
+ }
+
+
+ [jw release];
+
+ return ![self errorOccurred];
+
+}
+
+
+- (NSString *) getJAXPProcessorName {
+
+ NSLog(@"subclass must override getJAXPProcessorName in XSLTProcessorJAXPBase!");
+ return nil;
+
+}
+
+
+
+@end
--- /dev/null
+//
+// XSLTProcessorXalan_J.h
+// TestXSLT
+//
+// Created by Marc Liyanage on Mon Aug 18 2003.
+// Copyright (c) 2003 __MyCompanyName__. All rights reserved.
+//
+
+#import "XSLTProcessorJAXPBase.h"
+
+
+@interface XSLTProcessorXalan_J : XSLTProcessorJAXPBase {
+
+}
+
+@end
+
--- /dev/null
+//
+// XSLTProcessorXalan_J.m
+// TestXSLT
+//
+// Created by Marc Liyanage on Mon Aug 18 2003.
+// Copyright (c) 2003 __MyCompanyName__. All rights reserved.
+//
+
+#import "XSLTProcessorXalan_J.h"
+
+
+@implementation XSLTProcessorXalan_J
+
+
+- (int)processorType {
+
+ return PROCESSORTYPE_XALAN_J;
+
+}
+
+
+- (NSString *) getJAXPProcessorName {
+
+ return @"org.apache.xalan.processor.TransformerFactoryImpl";
+
+}
+
+
+@end
--- /dev/null
+//
+// XSL_FO_Renderer.h
+// TestXSLT
+//
+// Created by Marc Liyanage on Tue Aug 19 2003.
+// Copyright (c) 2003 __MyCompanyName__. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+
+@interface XSL_FO_Renderer : NSObject {
+
+}
+
+- (NSData *)render:(NSString *)foString;
+
+@end
+
+// Java class declarations to keep the compiler happy
+
+@interface FOPWrapper : NSObject
+{}
+/*
+- (int)getErrorSource;
+- (int)getErrorLine;
+- (NSString *)getResult;
+*/
+
+- (NSString *)getErrorMessage;
+- (BOOL)errorOccurred;
+- (NSData *)convert:(NSString *)foString;
+
+@end
--- /dev/null
+//
+// XSL_FO_Renderer.m
+// TestXSLT
+//
+// Created by Marc Liyanage on Tue Aug 19 2003.
+// Copyright (c) 2003 __MyCompanyName__. All rights reserved.
+//
+
+#import "XSL_FO_Renderer.h"
+
+
+@implementation XSL_FO_Renderer
+
+
+- (NSData *)render:(NSString *)foString {
+
+ FOPWrapper *fw = (FOPWrapper *)[[NSClassFromString(@"FOPWrapper") alloc] init];
+
+ NSData *resultData = [fw convert:foString];
+
+ // fixme: autorelease on resultData? creation on Java side?
+
+ [fw release];
+
+ //NSLog(@"data: %@", resultData);
+
+ return resultData;
+}
+
+
+
+@end