XSLFOApp_Controller_FOP_MOD
// --------------------------------------------------------------------------- // This file has been generated partially by the Web Dynpro Code Generator. // MODIFY CODE ONLY IN SECTIONS ENCLOSED BY @@begin AND @@end. // ALL OTHER CHANGES WILL BE LOST IF THE FILE IS REGENERATED. // --------------------------------------------------------------------------- package com.cmc.test; // // IMPORTANT NOTE: // _ALL_ IMPORT STATEMENTS MUST BE PLACED IN THE FOLLOWING SECTION ENCLOSED // BY @@begin imports AND @@end. FURTHERMORE, THIS SECTION MUST ALWAYS CONTAIN // AT LEAST ONE IMPORT STATEMENT (E.G. THAT FOR IPrivateApacheFop). // OTHERWISE, USING THE ECLIPSE FUNCTION "Organize Imports" FOLLOWED BY // A WEB DYNPRO CODE GENERATION (E.G. PROJECT BUILD) WILL RESULT IN THE LOSS // OF IMPORT STATEMENTS. // //@@begin imports import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.StringWriter; import java.io.Writer; import java.net.URL; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.sax.SAXResult; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.stream.StreamSource; 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 com.cmc.foputil.MaterialsInputSource; import com.cmc.foputil.MaterialsXMLReader; import com.cmc.test.wdp.IPrivateApacheFop; import com.cmc.test.wdp.IPublicApacheFop.IMaterialsElement; import com.sap.tc.webdynpro.services.sal.url.api.IWDCachedWebResource; import com.sap.tc.webdynpro.services.sal.url.api.WDWebResource; import com.sap.tc.webdynpro.services.sal.url.api.WDWebResourceType; import com.sap.tc.webdynpro.services.session.api.WDScopeType; //@@end //@@begin documentation //@@end public class ApacheFop { /** * Logging location. */ private static final com.sap.tc.logging.Location logger = com.sap.tc.logging.Location.getLocation(ApacheFop.class); static { //@@begin id String id = "$Id$"; //@@end com.sap.tc.logging.Location.getLocation("ID.com.sap.tc.webdynpro").infoT(id); } /** * Private access to the generated Web Dynpro counterpart * for this controller class. </p> * * Use <code>wdThis</code> to gain typed access to the context, * to trigger navigation via outbound plugs, to get and enable/disable * actions, fire declared events, and access used controllers and/or * component usages. * * @see com.cmc.test.wdp.IPrivateApacheFop for more details */ private final IPrivateApacheFop wdThis; /** * Root node of this controller's context. </p> * * Provides typed access not only to the elements of the root node * but also to all nodes in the context (methods node<i>XYZ</i>()) * and their currently selected element (methods current<i>XYZ</i>Element()). * It also facilitates the creation of new elements for all nodes * (methods create<i>XYZ</i>Element()). </p> * * @see com.cmc.test.wdp.IPrivateApacheFop.IContextNode for more details. */ private final IPrivateApacheFop.IContextNode wdContext; /** * A shortcut for <code>wdThis.wdGetAPI()</code>. </p> * * Represents the generic API of the generic Web Dynpro counterpart * for this controller. </p> */ private final com.sap.tc.webdynpro.progmodel.api.IWDComponent wdControllerAPI; /** * A shortcut for <code>wdThis.wdGetAPI().getComponent()</code>. </p> * * Represents the generic API of the Web Dynpro component this controller * belongs to. Can be used to access the message manager, the window manager, * to add/remove event handlers and so on. </p> */ private final com.sap.tc.webdynpro.progmodel.api.IWDComponent wdComponentAPI; public ApacheFop(IPrivateApacheFop wdThis) { this.wdThis = wdThis; this.wdContext = wdThis.wdGetContext(); this.wdControllerAPI = wdThis.wdGetAPI(); this.wdComponentAPI = wdThis.wdGetAPI().getComponent(); } //@@begin javadoc:wdDoInit() /** Hook method called to initialize controller. */ //@@end public void wdDoInit() { //@@begin wdDoInit() /* put some values into the node. */ for (int i = 0; i < productData.length; i++) { IMaterialsElement element = wdContext.createMaterialsElement(); element.setId(productData[i][0]); element.setName(productData[i][1]); element.setDescription(productData[i][2]); element.setQtyinstock(productData[i][3]); element.setProducturl(productData[i][4]); element.setImgurl(productData[i][5]); wdContext.nodeMaterials().addElement(element); } //@@end } //@@begin javadoc:wdDoExit() /** Hook method called to clean up controller. */ //@@end public void wdDoExit() { //@@begin wdDoExit() //@@end } //@@begin javadoc:wdDoPostProcessing() /** * Hook called to handle data retrieval errors before rendering. * * After doModifyView(), the Web Dynpro Framework gets all context data needed * for rendering by validating the contexts (which in turn calls the supply * functions and supplying relation roles). In this hook, the application * should handle the errors which occurred during validation of the contexts. * * Using preorder depth-first traversal, this hook is called for all component * controllers starting with the current root component. * * Permitted operations: * - Flushing model queue * - Creating messages * - Reading context and model data * * Forbidden operations: * - Invalidating model data * - Manipulating the context * - Firing outbound plugs * - Creating components * - ... * * @param isCurrentRoot true if this is the root of the current request */ //@@end public void wdDoPostProcessing(boolean isCurrentRoot) { //@@begin wdDoPostProcessing() //@@end } //@@begin javadoc:wdDoBeforeNavigation() /** * Hook before the navigation phase starts. * * This hook allows you to flush the model queue and handle any * errors that occur. Firing outbound plugs is allowed in this hook. * * Using preorder depth-first traversal, this hook is called for all component * controllers starting with the current root component. * * @param isCurrentRoot true if this is the root of the current request */ //@@end public void wdDoBeforeNavigation(boolean isCurrentRoot) { //@@begin wdDoBeforeNavigation() //@@end } //@@begin javadoc:wdDoApplicationStateChange() /** * Hook that informs the application about a state change. * <p> * This hook is called e.g. to tell the application that will be * <ul> * <li>left via a suspend plug and therefore should go into a suspend/sleep * mode with minimal need of resources. errors that occur. Firing * outbound plugs is allowed in this hook. * <li>left due to a timeout and could write it's state to a data base if the * user comes back later on * </ul> * * The concrete reason is available via IWDApplicationStateChangeInfo * <p> * <b>Important</b>: This hook is called for the top level component only! * * @param stateChangeInfo contains the information about the nature of the state change * @param stateChangeReturn allows the application to ask for a different state change. * The framework is allowed to ignore it considering i.e. the current resources situation. */ //@@end public void wdDoApplicationStateChange(com.sap.tc.webdynpro.progmodel.api.IWDApplicationStateChangeInfo stateChangeInfo, com.sap.tc.webdynpro.progmodel.api.IWDApplicationStateChangeReturn stateChangeReturn) { //@@begin wdDoApplicationStateChange() //@@end } //@@begin javadoc:generatePDF() /** Declared method. */ //@@end public com.sap.tc.webdynpro.services.sal.url.api.IWDCachedWebResource generatePDF( ) { //@@begin generatePDF() File pdfFile = generatePDFFromNode(wdContext.nodeMaterials()); IWDCachedWebResource resource = null; if (pdfFile.length() != 0) { try { resource = WDWebResource.getPublicCachedWebResource( getBytesFromFile(pdfFile), WDWebResourceType.PDF, WDScopeType.CLIENTSESSION_SCOPE, wdThis .wdGetAPI() .getComponent() .getDeployableObjectPart(), pdfFile.getName()); //wdContext.currentContextElement().setVaPDFResource(resource); } catch (Exception e) { logMessage(getStackTrace(e)); } } logMessage("Finished PDF Build"); return resource; //@@end } //@@begin javadoc:generatePDFFromNode() /** Declared method. */ //@@end public java.io.File generatePDFFromNode( com.sap.tc.webdynpro.progmodel.api.IWDNode dataNode ) { //@@begin generatePDFFromNode() File pdfFile = new File("materials.pdf"); if (pdfFile.exists()) { pdfFile.delete(); } try { OutputStream out = new java.io.FileOutputStream(pdfFile); out = new BufferedOutputStream(out); /* do some FOP magic */ FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); TransformerFactory factory = TransformerFactory.newInstance(); /* get the stream source for openorder.xml */ InputStream is = null; String url = this .getClass() .getClassLoader() .getResource("materials.xml") .toExternalForm(); URL entryURL = new URL(url); is = entryURL.openStream(); /* do the XSLT transform magic */ Transformer transformer = factory.newTransformer(new StreamSource(is)); Source src = getTransformSource(dataNode); Result res = new SAXResult(fop.getDefaultHandler()); transformer.transform(src, res); out.close(); is.close(); } catch (IOException ioe) { logMessage(getStackTrace(ioe)); } catch (TransformerConfigurationException tce) { logMessage(getStackTrace(tce)); } catch (TransformerException te) { logMessage(getStackTrace(te)); } catch (Exception e){ logMessage(getStackTrace(e)); } return pdfFile; //@@end } //@@begin javadoc:getStackTrace() /** Declared method. */ //@@end public java.lang.String getStackTrace( java.lang.Exception e ) { //@@begin getStackTrace() final Writer result = new StringWriter(); final PrintWriter printWriter = new PrintWriter(result); e.printStackTrace(printWriter); return result.toString(); //@@end } //@@begin javadoc:logMessage() /** Declared method. */ //@@end public void logMessage( java.lang.String message ) { //@@begin logMessage() logger.debugT(message); //msgMgr.reportSuccess(message); //@@end } //@@begin javadoc:getTransformSource() /** Declared method. */ //@@end public javax.xml.transform.Source getTransformSource( com.sap.tc.webdynpro.progmodel.api.IWDNode node ) { //@@begin getTransformSource() return new SAXSource( new MaterialsXMLReader(), new MaterialsInputSource(wdContext.nodeMaterials())); //@@end } /* * The following code section can be used for any Java code that is * not to be visible to other controllers/views or that contains constructs * currently not supported directly by Web Dynpro (such as inner classes or * member variables etc.). </p> * * Note: The content of this section is in no way managed/controlled * by the Web Dynpro Designtime or the Web Dynpro Runtime. */ //@@begin others /* decompose a File into a byte array */ public static byte[] getBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); /* Get the size of the file */ long length = file.length(); if (length > Integer.MAX_VALUE) { throw new IOException( "Generated file size limit exceeded for : " + file.getName()); } /* Create the byte array to hold the data */ byte[] bytes = new byte[(int) length]; /* Read in the bytes */ int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } /* Ensure all the bytes have been read in */ if (offset < bytes.length) { throw new IOException( "Could not completely read file " + file.getName()); } /* Close the input stream and return bytes */ is.close(); return bytes; } private FopFactory fopFactory = FopFactory.newInstance(); private static final String productData[][] = { { "1", "EM406A", "GPS Chip", "10", "http://www.usglobalsat.com/p-46-em-406a-sirf-iii.aspx", "http://www.usglobalsat.com/images/PRODUCT/medium/46.jpg" }, { "2", "Diecimilia", "Arduino Diecimilia", "5", "http://www.arduino.cc", "http://arduino.cc/en/uploads/Main/arduino316.jpg" }, { "3", "Duemilanove", "Arduino Duemilanove", "100", "http://www.arduino.cc", "http://arduino.cc/en/uploads/Main/ArduinoDuemilanove.jpg" }, { "4", "NG", "Arduino NG", "1", "http://www.arduino.cc", "http://arduino.cc/en/uploads/Main/ArduinoDuemilanove.jpg" }, { "5", "Hitachi8888", "8 bit 2 line LCD Display", "56", "http://www.blah.com", "http://www.blah.com/img/Hitachi8888.jpg" } }; //@@end }
page revision: 3, last edited: 12 Mar 2009 19:46