1 16 package org.apache.cocoon.serialization; 17 18 import java.io.OutputStream ; 19 20 import org.apache.avalon.framework.configuration.Configurable; 21 import org.apache.avalon.framework.configuration.Configuration; 22 import org.apache.avalon.framework.configuration.ConfigurationException; 23 24 import org.apache.cocoon.caching.CacheableProcessingComponent; 25 26 import org.apache.excalibur.source.SourceValidity; 27 import org.apache.excalibur.source.impl.validity.NOPValidity; 28 29 import org.xml.sax.SAXException ; 30 31 import com.lowagie.text.Document; 32 import com.lowagie.text.PageSize; 33 import com.lowagie.text.Rectangle; 34 import com.lowagie.text.pdf.PdfWriter; 35 import com.lowagie.text.xml.SAXiTextHandler; 36 37 41 final public class iTextSerializer extends AbstractSerializer implements Configurable, CacheableProcessingComponent { 42 43 private final static boolean LANDSCAPE = true; 44 private final static boolean PORTRAIT = false; 45 46 private String mimetype = "application/pdf"; 47 private boolean setContentLength = true; 48 private Rectangle pageSize; 49 private boolean pageOrientation; 50 private Document document = null; 51 52 private Rectangle getPageSize(final String s) throws ConfigurationException { 53 if ("letter".equalsIgnoreCase(s)) { 55 return PageSize.LETTER; 56 } 57 else if ("a4".equalsIgnoreCase(s)) { 58 return PageSize.A4; 59 } 60 else if ("a5".equalsIgnoreCase(s)) { 61 return PageSize.A5; 62 } 63 else { 64 throw new ConfigurationException("page size [" + String.valueOf(s) + "] is not yet recognized"); 65 } 66 } 67 68 private boolean getOrientation(final String o) throws ConfigurationException { 69 if ("portrait".equalsIgnoreCase(o)) { 70 return PORTRAIT; 71 } 72 else if ("landscape".equalsIgnoreCase(o)) { 73 return LANDSCAPE; 74 } 75 else { 76 throw new ConfigurationException("orientation must be either portrait or landscape but is [" + String.valueOf(o) + "]"); 77 } 78 } 79 80 public void configure(Configuration conf) throws ConfigurationException { 81 this.setContentLength = conf.getChild("set-content-length").getValueAsBoolean(true); 82 this.mimetype = conf.getAttribute("mime-type"); 83 84 this.pageSize = getPageSize(conf.getAttribute("page-size","A4")); 85 this.pageOrientation = getOrientation(conf.getAttribute("page-orientation","portrait")); 86 87 if (pageOrientation == LANDSCAPE) { 88 pageSize = pageSize.rotate(); 89 } 90 91 getLogger().debug("iTextSerializer mime-type:" + mimetype); 92 } 93 94 public String getMimeType() { 95 return mimetype; 96 } 97 98 public void startDocument() throws SAXException { 99 getLogger().debug("starting PDF document"); 100 super.startDocument(); 101 } 102 103 public void endDocument() throws SAXException { 104 super.endDocument(); 105 getLogger().debug("finished PDF document"); 106 } 107 108 public void setOutputStream(OutputStream out) { 109 this.document = new Document(this.pageSize); 110 111 try { 112 PdfWriter.getInstance(document, out); 113 } 114 catch(Exception e) { 115 getLogger().error("cannot create pdf writer instance",e); 116 } 118 119 SAXiTextHandler handler = new SAXiTextHandler(document); 120 handler.setControlOpenClose(true); 121 this.contentHandler = handler; 122 } 123 124 public java.io.Serializable getKey() { 125 return "1"; 126 } 127 128 public SourceValidity getValidity() { 129 return NOPValidity.SHARED_INSTANCE; 130 } 131 132 public boolean shouldSetContentLength() { 133 return this.setContentLength; 134 } 135 } 136 | Popular Tags |