Java tutorial
// Copyright 2007 the original author or authors. // site: http://www.bjmaxinfo.com // file: $Id: PdfResponseBuilder.java 3919 2008-01-16 04:36:52Z xf $ // created at:2007-07-31 // // Licensed 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. package corner.orm.tapestry.pdf.service; import java.io.CharArrayWriter; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import org.apache.hivemind.ApplicationRuntimeException; import org.apache.hivemind.Resource; import org.apache.hivemind.util.PropertyUtils; import org.apache.tapestry.IAsset; import org.apache.tapestry.IComponent; import org.apache.tapestry.IMarkupWriter; import org.apache.tapestry.IPage; import org.apache.tapestry.IRender; import org.apache.tapestry.IRequestCycle; import org.apache.tapestry.asset.AssetFactory; import org.apache.tapestry.services.ResponseBuilder; import org.apache.tapestry.util.exception.ExceptionDescription; import org.apache.tapestry.util.exception.ExceptionProperty; import org.apache.tapestry.web.WebResponse; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.pdf.PdfWriter; import corner.orm.tapestry.pdf.IPdfComponent; import corner.orm.tapestry.pdf.IPdfPage; import corner.orm.tapestry.pdf.PdfOutputPageEvent; import corner.orm.tapestry.pdf.PdfUtils; import corner.orm.tapestry.pdf.PdfWriterDelegate; /** * pdf?. * * @author <a href="mailto:jun.tsai@bjmaxinfo.com">Jun Tsai</a> * @author <a href=mailto:Ghostbb@bjmaxinfo.com>Ghostbb</a> * @version $Revision: 3919 $ * @since 2.3.7 */ public class PdfResponseBuilder implements ResponseBuilder { /** ?DocumentAttribute?? * */ public static final String PDF_DOCUMENT_ATTRIBUTE_NAME = "pdf-document-object"; private WebResponse response; /** pdf * */ private PdfWriterDelegate _writer; private Document _doc; /** * PdfResponseBuilder * * @param response * @param factory * @param string */ public PdfResponseBuilder(WebResponse response, AssetFactory factory, String string) { this.response = response; } public void renderResponse(IRequestCycle cycle) throws IOException { // ?? IPage pdfPage = cycle.getPage(); // PDF. OutputStream os = response.getOutputStream(IPdfPage.CONTENT_TYPE); // PDF _doc = new Document(); // PDF Writer PdfWriter pdfWriter = null; try { // PDF writer. pdfWriter = PdfWriter.getInstance(_doc, os); if (pdfPage instanceof IPdfPage) { // PDF Page?. // ? PdfOutputPageEvent event = new PdfOutputPageEvent((IPdfPage) pdfPage); pdfWriter.setPageEvent(event); // Pdf writer?. _writer = new PdfWriterDelegate(pdfWriter); // pdf _doc.open(); // PDF Writer??. cycle.setAttribute(PDF_DOCUMENT_ATTRIBUTE_NAME, _doc); // PDF? cycle.renderPage(this); // PDF??. cycle.removeAttribute(PDF_DOCUMENT_ATTRIBUTE_NAME); // PDF. _doc.close(); } else if (pdfPage instanceof org.apache.tapestry.pages.Exception) { // ??. ExceptionDescription[] exceptions = (ExceptionDescription[]) PropertyUtils.read(pdfPage, "exceptions"); if (exceptions == null) { throw new ApplicationRuntimeException("UNKOWN Exception!"); } else { // ???. StringBuffer sb = new StringBuffer(); for (int i = 0; i < exceptions.length; i++) { sb.append(exceptions[i].getMessage()).append("\n"); ExceptionProperty[] pros = exceptions[i].getProperties(); for (ExceptionProperty pro : pros) { sb.append(pro.getName()).append(" ").append(pro.getValue()).append("\n"); } String[] traces = exceptions[i].getStackTrace(); if (traces == null) { continue; } for (int j = 0; j < traces.length; j++) { sb.append(traces[j]).append("\n"); } } throw new Exception(sb.toString()); } } } catch (Throwable e) { // ?. if (pdfWriter == null) { e.printStackTrace(); return; } if (!_doc.isOpen()) { _doc.open(); } pdfWriter.setPageEvent(null); _doc.newPage(); // PDF???SO COOL! :) try { _doc.add(new Phrase("pdf?:", PdfUtils.createSongLightFont(12))); _doc.add(new Paragraph(e.getMessage())); _doc.add(new Paragraph(fetchStackTrace(e))); _doc.close(); } catch (DocumentException e1) { throw new ApplicationRuntimeException(e1); } } } //? String fetchStackTrace(Throwable e) { // ? CharArrayWriter writer = new CharArrayWriter(); PrintWriter printWriter = new PrintWriter(writer); e.printStackTrace(printWriter); return writer.toString(); } /** * * {@inheritDoc} */ public void render(IMarkupWriter writer, IRender render, IRequestCycle cycle) { if (writer == null) { writer = _writer; } if (render instanceof IPdfComponent) {// ?pdf //??? if (!((IPdfComponent) render).isTemplateFragment()) { render.render(writer, cycle); } } } // ================ ??ResponseBuilder??. public void beginBodyScript(IMarkupWriter writer, IRequestCycle cycle) { // do nothing } public boolean contains(IComponent target) { // do nothing return false; } public void endBodyScript(IMarkupWriter writer, IRequestCycle cycle) { // do nothing } public boolean explicitlyContains(IComponent target) { // do nothing return false; } public IMarkupWriter getWriter() { // do nothing return null; } public IMarkupWriter getWriter(String id, String type) { // do nothing return null; } public boolean isBodyScriptAllowed(IComponent target) { // do nothing return false; } public boolean isDynamic() { // do nothing return false; } public boolean isExternalScriptAllowed(IComponent target) { // do nothing return false; } public boolean isImageInitializationAllowed(IComponent target) { // do nothing return false; } public boolean isInitializationScriptAllowed(IComponent target) { // do nothing return false; } public void updateComponent(String id) { // do nothing } public void writeBodyScript(IMarkupWriter writer, IRequestCycle cycle) { // do nothing } public void writeBodyScript(IMarkupWriter writer, String script, IRequestCycle cycle) { // do nothing } public void writeExternalScript(IMarkupWriter writer, String url, IRequestCycle cycle) { // do nothing } public void writeImageInitializations(IMarkupWriter writer, String script, String preloadName, IRequestCycle cycle) { // do nothing } public void writeInitializationScript(IMarkupWriter writer) { // do nothing } public void writeInitializationScript(IMarkupWriter writer, String script) { // do nothing } public String getPreloadedImageReference(String url) { // do nothing return null; } public String getPreloadedImageReference(IComponent target, IAsset source) { // do nothing return null; } public String getPreloadedImageReference(IComponent target, String url) { // do nothing return null; } public void addBodyScript(String script) { // do nothing } public void addBodyScript(IComponent target, String script) { // do nothing } public void addExternalScript(Resource resource) { // do nothing } public void addExternalScript(IComponent target, Resource resource) { // do nothing } public void addInitializationScript(String script) { // do nothing } public void addInitializationScript(IComponent target, String script) { // do nothing } public String getUniqueString(String baseValue) { // do nothing return null; } public void addStatusMessage(IMarkupWriter writer, String category, String text) { // TODO Auto-generated method stub } public void flush() throws IOException { // TODO Auto-generated method stub } public void addScriptAfterInitialization(IComponent arg0, String arg1) { // TODO Auto-generated method stub } }