001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.serialization;
018:
019: import java.io.OutputStream;
020:
021: import org.apache.avalon.framework.configuration.Configurable;
022: import org.apache.avalon.framework.configuration.Configuration;
023: import org.apache.avalon.framework.configuration.ConfigurationException;
024:
025: import org.apache.cocoon.caching.CacheableProcessingComponent;
026:
027: import org.apache.excalibur.source.SourceValidity;
028: import org.apache.excalibur.source.impl.validity.NOPValidity;
029:
030: import org.xml.sax.SAXException;
031:
032: import com.lowagie.text.Document;
033: import com.lowagie.text.PageSize;
034: import com.lowagie.text.Rectangle;
035: import com.lowagie.text.pdf.PdfWriter;
036: import com.lowagie.text.xml.SAXiTextHandler;
037:
038: /**
039: * @author <a href="mailto:tcurdt@dff.st">Torsten Curdt</a>
040: * @version CVS $Id: iTextSerializer.java 433543 2006-08-22 06:22:54Z crossley $
041: */
042: final public class iTextSerializer extends AbstractSerializer implements
043: Configurable, CacheableProcessingComponent {
044:
045: private final static boolean LANDSCAPE = true;
046: private final static boolean PORTRAIT = false;
047:
048: private String mimetype = "application/pdf";
049: private boolean setContentLength = true;
050: private Rectangle pageSize;
051: private boolean pageOrientation;
052: private Document document = null;
053:
054: private Rectangle getPageSize(final String s)
055: throws ConfigurationException {
056: // TC: we could use reflection here instead
057: if ("letter".equalsIgnoreCase(s)) {
058: return PageSize.LETTER;
059: } else if ("a4".equalsIgnoreCase(s)) {
060: return PageSize.A4;
061: } else if ("a5".equalsIgnoreCase(s)) {
062: return PageSize.A5;
063: } else {
064: throw new ConfigurationException("page size ["
065: + String.valueOf(s) + "] is not yet recognized");
066: }
067: }
068:
069: private boolean getOrientation(final String o)
070: throws ConfigurationException {
071: if ("portrait".equalsIgnoreCase(o)) {
072: return PORTRAIT;
073: } else if ("landscape".equalsIgnoreCase(o)) {
074: return LANDSCAPE;
075: } else {
076: throw new ConfigurationException(
077: "orientation must be either portrait or landscape but is ["
078: + String.valueOf(o) + "]");
079: }
080: }
081:
082: public void configure(Configuration conf)
083: throws ConfigurationException {
084: this .setContentLength = conf.getChild("set-content-length")
085: .getValueAsBoolean(true);
086: this .mimetype = conf.getAttribute("mime-type");
087:
088: this .pageSize = getPageSize(conf
089: .getAttribute("page-size", "A4"));
090: this .pageOrientation = getOrientation(conf.getAttribute(
091: "page-orientation", "portrait"));
092:
093: if (pageOrientation == LANDSCAPE) {
094: pageSize = pageSize.rotate();
095: }
096:
097: getLogger().debug("iTextSerializer mime-type:" + mimetype);
098: }
099:
100: public String getMimeType() {
101: return mimetype;
102: }
103:
104: public void startDocument() throws SAXException {
105: getLogger().debug("starting PDF document");
106: super .startDocument();
107: }
108:
109: public void endDocument() throws SAXException {
110: super .endDocument();
111: getLogger().debug("finished PDF document");
112: }
113:
114: public void setOutputStream(OutputStream out) {
115: this .document = new Document(this .pageSize);
116:
117: try {
118: PdfWriter.getInstance(document, out);
119: } catch (Exception e) {
120: getLogger().error("cannot create pdf writer instance", e);
121: //TC: FIXME! shouldn't we throw an exception here? what kind?
122: }
123:
124: SAXiTextHandler handler = new SAXiTextHandler(document);
125: handler.setControlOpenClose(true);
126: this .contentHandler = handler;
127: }
128:
129: public java.io.Serializable getKey() {
130: return "1";
131: }
132:
133: public SourceValidity getValidity() {
134: return NOPValidity.SHARED_INSTANCE;
135: }
136:
137: public boolean shouldSetContentLength() {
138: return this.setContentLength;
139: }
140: }
|