svn commit: r564615 - in /ofbiz/trunk/framework: common/webcommon/error.fo.ftl common/widget/CommonScreens.xml widget/src/org/ofbiz/widget/screen/ScreenFopViewHandler.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r564615 - in /ofbiz/trunk/framework: common/webcommon/error.fo.ftl common/widget/CommonScreens.xml widget/src/org/ofbiz/widget/screen/ScreenFopViewHandler.java

jacopoc
Author: jacopoc
Date: Fri Aug 10 08:21:35 2007
New Revision: 564615

URL: http://svn.apache.org/viewvc?view=rev&rev=564615
Log:
Implemented a standard XSL-FO template to display error messages returned while rendering XSL-FO reports.
This is probably a workaround (the ideal solution is to get the ViewHandlerException better treated by the view handler framwork) but it works to avoid the ugly 'not valid PDF stream' message returned before.

Added:
    ofbiz/trunk/framework/common/webcommon/error.fo.ftl   (with props)
Modified:
    ofbiz/trunk/framework/common/widget/CommonScreens.xml
    ofbiz/trunk/framework/widget/src/org/ofbiz/widget/screen/ScreenFopViewHandler.java

Added: ofbiz/trunk/framework/common/webcommon/error.fo.ftl
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/webcommon/error.fo.ftl?view=auto&rev=564615
==============================================================================
--- ofbiz/trunk/framework/common/webcommon/error.fo.ftl (added)
+++ ofbiz/trunk/framework/common/webcommon/error.fo.ftl Fri Aug 10 08:21:35 2007
@@ -0,0 +1,110 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<#--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+<#escape x as x?xml>
+<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"
+    <#-- inheritance -->
+    <#if defaultFontFamily?has_content>font-family="${defaultFontFamily}"</#if>
+>
+    <fo:layout-master-set>
+        <fo:simple-page-master master-name="simple-portrait"
+              page-width="8.5in" page-height="11in"
+              margin-top="0.3in" margin-bottom="0.3in"
+              margin-left="0.4in" margin-right="0.3in">
+            <fo:region-body margin-top="1in" margin-bottom="0.5in"/>
+            <fo:region-before extent="1in"/>
+            <fo:region-after extent="0.5in" />
+        </fo:simple-page-master>
+        <fo:simple-page-master master-name="simple-landscape"
+              page-width="11in" page-height="8.5in"
+              margin-top="0.3in" margin-bottom="0.3in"
+              margin-left="0.4in" margin-right="0.3in">
+            <fo:region-body margin-top="1in" margin-bottom="0.5in"/>
+            <fo:region-before extent="1in"/>
+            <fo:region-after extent="0.5in" />
+        </fo:simple-page-master>
+    </fo:layout-master-set>
+  
+    <fo:page-sequence master-reference="${pageLayoutName?default("simple-portrait")}" font-size="8pt">
+        <#-- Header -->
+        <#-- The elements it it are positioned using a table composed by one row
+             composed by two cells (each 50% of the total table that is 100% of the page):
+             in the left side cell we put the logo
+             in the right side cell we put the title, username and date
+        -->
+        <fo:static-content flow-name="xsl-region-before" font-size="${headerFontSize?default("8pt")}">
+            <fo:table>
+                <fo:table-column column-number="1" column-width="proportional-column-width(50)"/>
+                <fo:table-column column-number="2" column-width="proportional-column-width(50)"/>
+                <fo:table-body>
+                    <fo:table-row>
+                        <fo:table-cell>
+                        <#if logoImageUrl?exists>
+                            <fo:block>
+                                <fo:external-graphic src="${logoImageUrl}" overflow="hidden" height="40px"/>
+                            </fo:block>
+                        </#if>
+                        </fo:table-cell>
+                        <fo:table-cell>
+                            <#-- The title of the report -->
+                            <fo:block font-weight="bold" text-decoration="underline" space-after="0.03in">
+                            </fo:block>
+                            <#-- Username and date -->
+                            <fo:list-block provisional-distance-between-starts="1in">
+                                <fo:list-item>
+                                    <fo:list-item-label>
+                                        <fo:block font-weight="bold">${uiLabelMap.CommonUsername}</fo:block>
+                                    </fo:list-item-label>
+                                    <fo:list-item-body start-indent="body-start()">
+                                        <fo:block><#if userLogin?exists>${userLogin.userLoginId?if_exists}</#if></fo:block>
+                                    </fo:list-item-body>
+                                </fo:list-item>
+                                <fo:list-item>
+                                    <fo:list-item-label>
+                                        <fo:block font-weight="bold">${uiLabelMap.CommonDate}</fo:block>
+                                    </fo:list-item-label>
+                                    <fo:list-item-body start-indent="body-start()">
+                                        <fo:block>${nowTimestamp?if_exists}</fo:block>
+                                    </fo:list-item-body>
+                                </fo:list-item>
+                            </fo:list-block>
+                        </fo:table-cell>
+                    </fo:table-row>
+                </fo:table-body>
+            </fo:table>
+        </fo:static-content>
+        
+        <#-- Footer -->
+        <fo:static-content flow-name="xsl-region-after" font-size="${footerFontSize?default("8pt")}">
+            <fo:block text-align="center" border-top="thin solid black" padding="3pt">${uiLabelMap.CommonPage} <fo:page-number/> ${uiLabelMap.CommonOf} <fo:page-number-citation ref-id="theEnd"/></fo:block>
+        </fo:static-content>
+      
+        <#-- Body -->
+        <fo:flow flow-name="xsl-region-body" font-size="${bodyFontSize?default("8pt")}">
+            <fo:block font-weight="bold" text-decoration="underline" space-after="0.2in">
+                ${uiLabelMap.CommonFollowingErrorsOccurred}:
+            </fo:block>
+            <fo:block space-after="0.2in" color="red">
+                ${errorMessage?if_exists}
+            </fo:block>
+            <fo:block id="theEnd"/>
+        </fo:flow>
+    </fo:page-sequence>
+</fo:root>
+</#escape>

Propchange: ofbiz/trunk/framework/common/webcommon/error.fo.ftl
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/trunk/framework/common/webcommon/error.fo.ftl
------------------------------------------------------------------------------
    svn:keywords = "Date Rev Author URL Id"

Propchange: ofbiz/trunk/framework/common/webcommon/error.fo.ftl
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: ofbiz/trunk/framework/common/widget/CommonScreens.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/widget/CommonScreens.xml?view=diff&rev=564615&r1=564614&r2=564615
==============================================================================
--- ofbiz/trunk/framework/common/widget/CommonScreens.xml (original)
+++ ofbiz/trunk/framework/common/widget/CommonScreens.xml Fri Aug 10 08:21:35 2007
@@ -214,7 +214,6 @@
         </section>
     </screen>
 
-
     <!-- Simple fo decorator for pdf reports. -->
     <screen name="FoDecorator">
         <section>
@@ -225,6 +224,20 @@
             </actions>
             <widgets>
                 <platform-specific><html><html-template location="component://common/webcommon/includes/simple.fo.ftl"/></html></platform-specific>
+            </widgets>
+        </section>
+    </screen>
+
+    <!-- Default "error" page for XSL-FO based output. -->
+    <screen name="FoError">
+        <section>
+            <actions>
+                <property-map resource="CommonUiLabels" map-name="uiLabelMap" global="true"/>
+                <set field="logoImageUrl" value="http://localhost:8080/images/ofbiz_logo.jpg"/>
+                <!--<set field="defaultFontFamily" value="Arial"/>-->
+            </actions>
+            <widgets>
+                <platform-specific><html><html-template location="component://common/webcommon/error.fo.ftl"/></html></platform-specific>
             </widgets>
         </section>
     </screen>

Modified: ofbiz/trunk/framework/widget/src/org/ofbiz/widget/screen/ScreenFopViewHandler.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/screen/ScreenFopViewHandler.java?view=diff&rev=564615&r1=564614&r2=564615
==============================================================================
--- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/screen/ScreenFopViewHandler.java (original)
+++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/screen/ScreenFopViewHandler.java Fri Aug 10 08:21:35 2007
@@ -50,6 +50,8 @@
     protected ServletContext servletContext = null;
     protected FoScreenRenderer foScreenRenderer = new FoScreenRenderer();
 
+    private final String DEFAULT_ERROR_TEMPLATE = "component://common/widget/CommonScreens.xml#FoError";
+
     /**
      * @see org.ofbiz.webapp.view.ViewHandler#init(javax.servlet.ServletContext)
      */
@@ -94,6 +96,9 @@
             Reader reader = new StringReader(writer.toString());
             Source src = new StreamSource(reader);
 
+            if (Debug.verboseOn()) {
+                Debug.logVerbose("Transforming the following xsl-fo template: " + writer.toString(), module);
+            }
             /*
             try {
                 String buf = writer.toString();
@@ -112,24 +117,41 @@
                 // Transform the FOP XML source
                 transformer.transform(src, res);
 
-                // We don't want to cache the images that get loaded by the FOP engine
-                fopFactory.getImageFactory().clearCaches();
-
-                // set the content type and length
-                response.setContentType(contentType);
-                response.setContentLength(out.size());
-
-                // write to the browser
+            } catch (TransformerException e) {
+                Debug.logError("FOP transform failed: " + e, module );
+                Debug.logInfo("Rendering the error message using the default error template: " + DEFAULT_ERROR_TEMPLATE, module );
                 try {
-                    out.writeTo(response.getOutputStream());
-                    response.getOutputStream().flush();
-                } catch (IOException e) {
-                    throw new ViewHandlerException("Unable write to browser OutputStream", e);
+                    writer = new StringWriter();
+                    out = new ByteArrayOutputStream();
+                    fopFactory = ApacheFopFactory.instance();
+                    transFactory = TransformerFactory.newInstance();
+                    transformer = transFactory.newTransformer();
+                    fop = fopFactory.newFop(contentType, out);
+                    res = new SAXResult(fop.getDefaultHandler());
+                    ScreenRenderer screens = new ScreenRenderer(writer, null, foScreenRenderer);
+                    screens.populateContextForRequest(request, response, servletContext);
+                    screens.getContext().put("errorMessage", e.toString());
+                    screens.render(DEFAULT_ERROR_TEMPLATE);
+                    transformer.transform(new StreamSource(new StringReader(writer.toString())), res);
+                } catch (Throwable t) {
+                    // If we cannot even create the error page, then we return the original error
+                    throw new ViewHandlerException("Unable to transform FO to " + contentType, e);
                 }
+            }
 
-            } catch (TransformerException e) {
-                Debug.logError("FOP transform failed:" + e, module );
-                throw new ViewHandlerException("Unable to transform FO to " + contentType, e);
+            // We don't want to cache the images that get loaded by the FOP engine
+            fopFactory.getImageFactory().clearCaches();
+
+            // set the content type and length
+            response.setContentType(contentType);
+            response.setContentLength(out.size());
+
+            // write to the browser
+            try {
+                out.writeTo(response.getOutputStream());
+                response.getOutputStream().flush();
+            } catch (IOException e) {
+                throw new ViewHandlerException("Unable write to browser OutputStream", e);
             }
 
         } catch (TransformerConfigurationException e) {