KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > serialization > iTextSerializer


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.serialization;
17
18 import java.io.OutputStream JavaDoc;
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 JavaDoc;
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 /**
38  * @author <a HREF="mailto:tcurdt@dff.st">Torsten Curdt</a>
39  * @version CVS $Id: iTextSerializer.java 30932 2004-07-29 17:35:38Z vgritsenko $
40  */

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 JavaDoc 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 JavaDoc s) throws ConfigurationException {
53         // TC: we could use reflection here instead
54
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 JavaDoc 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 JavaDoc getMimeType() {
95         return mimetype;
96     }
97
98     public void startDocument() throws SAXException JavaDoc {
99         getLogger().debug("starting PDF document");
100         super.startDocument();
101     }
102
103     public void endDocument() throws SAXException JavaDoc {
104         super.endDocument();
105         getLogger().debug("finished PDF document");
106     }
107
108     public void setOutputStream(OutputStream JavaDoc out) {
109         this.document = new Document(this.pageSize);
110
111         try {
112             PdfWriter.getInstance(document, out);
113         }
114         catch(Exception JavaDoc e) {
115             getLogger().error("cannot create pdf writer instance",e);
116             //TC: FIXME! shouldn't we throw an exception here? what kind?
117
}
118
119         SAXiTextHandler handler = new SAXiTextHandler(document);
120         handler.setControlOpenClose(true);
121         this.contentHandler = handler;
122     }
123
124     public java.io.Serializable JavaDoc 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