Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.xmlcalabash.util;
  
  import  org.apache.fop.apps.FOUserAgent;
  import  org.apache.fop.apps.Fop;
 import  org.apache.fop.apps.FopFactory;
 import  org.apache.fop.apps.MimeConstants;
 
 import java.util.Date;
Created by IntelliJ IDEA. User: ndw Date: 9/1/11 Time: 6:56 AM To change this template use File | Settings | File Templates.
 
 public class FoFOP implements FoProcessor {
     XProcRuntime runtime = null;
     FopFactory fopFactory = null;
     Properties options = null;
     XStep step = null;
     URIResolver resolver = null;
 
     public void initialize(XProcRuntime runtimeXStep stepProperties options) {
         this. = runtime;
         this. = step;
         this. = options;
 
          = FopFactory.newInstance();
          = runtime.getResolver();
 
         if ( != null) {
             .setURIResolver();
         }
 
         try {
             String s = getStringProp("BaseURL");
             if (s != null) {
                 .setBaseURL(s);
             }
 
             Boolean b = getBooleanProp("BreakIndentInheritanceOnReferenceAreaBoundary");
             if (b != null) {
                 .setBreakIndentInheritanceOnReferenceAreaBoundary(b);
             }
 
             s = getStringProp("FontBaseURL");
             if (s != null) {
                 .getFontManager().setFontBaseURL(s);
             }
 
             b = getBooleanProp("Base14KerningEnabled");
             if (b != null) {
                 .getFontManager().setBase14KerningEnabled(b);
             }
 
             s = getStringProp("HyphenBaseURL");
             if (s != null) {
                 .setHyphenBaseURL(s);
             }
 
             s = getStringProp("PageHeight");
             if (s != null) {
                 .setPageHeight(s);
             }
 
             s = getStringProp("PageWidth");
             if (s != null) {
                 .setPageWidth(s);
             }
 
             Float f = getFloatProp("SourceResolution");
             if (f != null) {
                 .setSourceResolution(f);
             }
 
             f = getFloatProp("TargetResolution");
             if (f != null) {
                 .setTargetResolution(f);
             }
 
             b = getBooleanProp("StrictUserConfigValidation");
             if (b != null) {
                 .setStrictUserConfigValidation(b);
             }
            b = getBooleanProp("StrictValidation");
            if (b != null) {
                .setStrictUserConfigValidation(b);
            }
            b = getBooleanProp("UseCache");
            if (b != null) {
                .getFontManager().setUseCache(b);
            }
            s = getStringProp("UserConfig");
            if (s != null) {
                .setUserConfig(s);
            }
          } catch (Exception e) {
            throw new XProcException(e);
        }
    }
    public void format(XdmNode docOutputStream outString 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(.getNode(), "Unsupported content-type on p:xsl-formatter: " + contentType);
        }
        try {
            InputSource fodoc = S9apiUtils.xdmToInputSource(doc);
            SAXSource source = new SAXSource(fodoc);
            Fop fop = .newFop(outputFormatout);
            FOUserAgent userAgent = fop.getUserAgent();
            Boolean b = getBooleanProp("Accessibility");
            if (b != null) {
                userAgent.setAccessibility(b);
            }
            String s = getStringProp("Author");
            if (s != null) {
                userAgent.setAuthor(s);
            }
            userAgent.setBaseURL(.getNode().getBaseURI().toString());
            s = getStringProp("BaseURL");
            if (s != null) {
                userAgent.setBaseURL(s);
            }
            b = getBooleanProp("ConserveMemoryPolicy");
            if (b != null) {
                userAgent.setConserveMemoryPolicy(b);
            }
            s = getStringProp("CreationDate");
            if (s != null) {
                DateFormat df = DateFormat.getDateInstance();
                Date d = df.parse(s);
                userAgent.setCreationDate(d);
            }
            s = getStringProp("Creator");
            if (s != null) {
                userAgent.setCreator(s);
            }
            s = getStringProp("Keywords");
            if (s != null) {
                userAgent.setKeywords(s);
            }
            b = getBooleanProp("LocatorEnabled");
            if (b != null) {
                userAgent.setLocatorEnabled(b);
            }
            s = getStringProp("Producer");
            if (s != null) {
                userAgent.setProducer(s);
            }
            s = getStringProp("Subject");
            if (s != null) {
                userAgent.setSubject(s);
            }
            Float f = getFloatProp("TargetResolution");
            if (f != null) {
                userAgent.setTargetResolution(f);
            }
            s = getStringProp("Title");
            if (s != null) {
                userAgent.setTitle(s);
            }
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.transform(sourcenew SAXResult(fop.getDefaultHandler()));
        } catch (Exception e) {
            throw new XProcException(.getNode(), "Failed to process FO document with FOP"e);
        }
    }
    private String getStringProp(String name) {
        return .getProperty(name);
    }
    private Float getFloatProp(String name) {
        String s = getStringProp(name);
        if (s != null) {
            try {
                float f = Float.parseFloat(s);
                return new Float(f);
            } catch (NumberFormatException nfe) {
                return null;
            }
        }
        return null;
    }
    private Boolean getBooleanProp(String name) {
        String s = .getProperty(name);
        if (s != null) {
            return "true".equals(s);
        }
        return null;
    }
New to GrepCode? Check out our FAQ X