// "PDF";
public void format(XdmNode doc, OutputStream out, String contentType) {
        String outputFormat = null;
        if (contentType == null || "application/pdf".equalsIgnoreCase(contentType)) {
            outputFormat = "application/pdf"; // "PDF";
        } else if ("application/PostScript".equalsIgnoreCase(contentType)) {
            outputFormat = "application/postscript"; //"PostScript";
        } else if ("application/afp".equalsIgnoreCase(contentType)) {
            outputFormat =  "application/x-afp";  //"AFP";
        } else if ("application/rtf".equalsIgnoreCase(contentType)) {
            outputFormat = "application/rtf";
        } else if ("text/plain".equalsIgnoreCase(contentType)) {
           outputFormat = "text/plain";
        } else {
            throw new XProcException(step.getNode(), "Unsupported content-type on p:xsl-formatter: " + contentType);
        }
        if (! ("1.x".equals(fopVersion) || "2.x".equals(fopVersion))) {
            throw new XProcException("Unexpected FOP version: " + fopVersion);
        }
        try {
            InputSource fodoc = S9apiUtils.xdmToInputSource(runtime, doc);
            SAXSource source = new SAXSource(fodoc);
            Object userAgent = null;
            Object fop = null;
            if ("1.x".equals(fopVersion)) {
                method = fopFactory.getClass().getMethod("newFop", String.class, OutputStream.class);
                fop = method.invoke(fopFactory, outputFormat, out);
                method = fop.getClass().getMethod("getUserAgent");
                userAgent = method.invoke(fop);
            } else {
                method = fopFactory.getClass().getMethod("newFOUserAgent");
                userAgent = method.invoke(fopFactory);
            }
            Class uaClass = userAgent.getClass();
            Boolean b = getBooleanProp("Accessibility");
            if (b != null) {
                method = uaClass.getMethod("setAccessibility", Boolean.class);
                method.invoke(userAgent, b);
            }
            String s = getStringProp("Author");
            if (s != null) {
                method = uaClass.getMethod("setAuthor", String.class);
                method.invoke(userAgent, s);
            }
            if ("1.x".equals(fopVersion)) {
                method = uaClass.getMethod("setBaseURL", String.class);
                method.invoke(userAgent, step.getNode().getBaseURI().toString());
                s = getStringProp("BaseURL");
                if (s != null) {
                    method.invoke(userAgent, s);
                }
            } else {
                // FIXME: how do I do this in 2.x?
            }
            b = getBooleanProp("ConserveMemoryPolicy");
            if (b != null) {
                method = uaClass.getMethod("setConserveMemoryPolicy", Boolean.class);
                method.invoke(userAgent, b);
            }
            s = getStringProp("CreationDate");
            if (s != null) {
                DateFormat df = DateFormat.getDateInstance();
                Date d = df.parse(s);
                method = uaClass.getMethod("setCreationDate", Date.class);
                method.invoke(userAgent, d);
            }
            s = getStringProp("Creator");
            if (s != null) {
                method = uaClass.getMethod("setCreator", String.class);
                method.invoke(userAgent, s);
            }
            s = getStringProp("Keywords");
            if (s != null) {
                method = uaClass.getMethod("setKeywords", String.class);
                method.invoke(userAgent, s);
            }
            b = getBooleanProp("LocatorEnabled");
            if (b != null) {
                method = uaClass.getMethod("setLocatorEnabled", Boolean.class);
                method.invoke(userAgent, b);
            }
            s = getStringProp("Producer");
            if (s != null) {
                method = uaClass.getMethod("setProducer", String.class);
                method.invoke(userAgent, s);
            }
            s = getStringProp("Subject");
            if (s != null) {
                method = uaClass.getMethod("setSubject", String.class);
                method.invoke(userAgent, s);
            }
            Float f = getFloatProp("TargetResolution");
            if (f != null) {
                method = uaClass.getMethod("setTargetResolution", Float.class);
                method.invoke(userAgent, f);
            }
            s = getStringProp("Title");
            if (s != null) {
                method = uaClass.getMethod("setTitle", String.class);
                method.invoke(userAgent, s);
            }
            if ("2.x".equals(fopVersion)) {
                method = uaClass.getMethod("newFop", String.class, OutputStream.class);
                fop = method.invoke(userAgent, outputFormat, out);
            }
            method = fop.getClass().getMethod("getDefaultHandler");
            Object defHandler = method.invoke(fop);
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.transform(source, new SAXResult((ContentHandler) defHandler));
        } catch (Exception e) {
            throw new XProcException(step.getNode(), "Failed to process FO document with FOP", e);
        }
    }
String outputFormat = null;
if (contentType == null || "application/pdf".equalsIgnoreCase(contentType)) {
outputFormat = "application/pdf"; // "PDF";
} else if ("application/PostScript".equalsIgnoreCase(contentType)) {
outputFormat = "application/postscript"; //"PostScript";
public void format(XdmNode doc, OutputStream out, String contentType) {
String outputFormat = null;
if (contentType == null || "application/pdf".equalsIgnoreCase(contentType)) {
outputFormat = "application/pdf"; // "PDF";
} else if ("application/PostScript".equalsIgnoreCase(contentType)) {
outputFormat = "application/postscript"; //"PostScript";
} else if ("application/afp".equalsIgnoreCase(contentType)) {
outputFormat = "application/x-afp"; //"AFP";
} else if ("application/rtf".equalsIgnoreCase(contentType)) {
outputFormat = "application/rtf";
} else if ("text/plain".equalsIgnoreCase(contentType)) {
outputFormat = "text/plain";
} else {
throw new XProcException(step.getNode(), "Unsupported content-type on p:xsl-formatter: " + contentType);
public void format(XdmNode doc, OutputStream out, String contentType) {
String outputFormat = null;
if (contentType == null || "application/pdf".equalsIgnoreCase(contentType)) {
outputFormat = "application/pdf"; // "PDF";
} else if ("application/PostScript".equalsIgnoreCase(contentType)) {
outputFormat = "application/postscript"; //"PostScript";
} else if ("application/afp".equalsIgnoreCase(contentType)) {
outputFormat = "application/x-afp"; //"AFP";
} else if ("application/rtf".equalsIgnoreCase(contentType)) {
outputFormat = "application/rtf";
} else if ("text/plain".equalsIgnoreCase(contentType)) {
outputFormat = "text/plain";
} else {
throw new XProcException(step.getNode(), "Unsupported content-type on p:xsl-formatter: " + contentType);
}
if (! ("1.x".equals(fopVersion) || "2.x".equals(fopVersion))) {
throw new XProcException("Unexpected FOP version: " + fopVersion);
}
try {
InputSource fodoc = S9apiUtils.xdmToInputSource(runtime, doc);
SAXSource source = new SAXSource(fodoc);
Object userAgent = null;
Object fop = null;
if ("1.x".equals(fopVersion)) {