001: /**
002: * Servlet ServletEditor
003: * <P>
004: * @author: Stefanovic Nenad, Bojanic Sasa, Aleksandar Stojsavljevic
005: * @Date: August 2001 - May 2003 - May 2007
006: */package fops.presentation;
007:
008: import java.io.File;
009: import java.io.StringReader;
010: import java.util.Date;
011: import java.util.HashMap;
012: import java.util.Map;
013:
014: import javax.xml.parsers.DocumentBuilder;
015: import javax.xml.parsers.DocumentBuilderFactory;
016:
017: import org.apache.commons.logging.Log;
018: import org.apache.commons.logging.LogFactory;
019: import org.enhydra.util.fo.Fo2FopResponsePostProcessor;
020: import org.enhydra.xml.io.OutputOptions;
021: import org.w3c.dom.Document;
022: import org.xml.sax.InputSource;
023:
024: import com.lutris.appserver.server.httpPresentation.ClientPageRedirectException;
025: import com.lutris.appserver.server.httpPresentation.HttpPresentation;
026: import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
027: import com.lutris.appserver.server.httpPresentation.HttpPresentationException;
028:
029: public class ServletEditor implements HttpPresentation {
030: private Log log = null;
031:
032: private String DateHTML = null;
033:
034: private String NameHTML = null;
035:
036: private String SubjHTML = null;
037:
038: private String AppletHTML = null;
039:
040: private String xmlParam = null;
041:
042: private String xslParam = null;
043:
044: private String foParam = null;
045:
046: boolean noParams = false;
047:
048: public void run(HttpPresentationComms comms) throws Exception {
049: if (log == null) {
050: log = LogFactory.getLog(ServletEditor.class);
051: }
052:
053: xmlParam = comms.request.getParameter("xml");
054: xslParam = comms.request.getParameter("xsl");
055: foParam = comms.request.getParameter("fo");
056: DateHTML = comms.request.getParameter("date");
057: NameHTML = comms.request.getParameter("name");
058: SubjHTML = comms.request.getParameter("subject");
059: AppletHTML = comms.request.getParameter("context");
060:
061: try {
062: DocumentBuilderFactory factory = DocumentBuilderFactory
063: .newInstance();
064: DocumentBuilder builder = factory.newDocumentBuilder();
065:
066: if ((xmlParam != null) && (xslParam != null)) {
067: // Rendering a PDF from XML+XSL files
068: System.out.println("PDF from XML: " + xmlParam
069: + " and XSL: " + xslParam);
070: File content = new File(xmlParam);
071: Document page = builder.parse(content);
072: OutputOptions opt = getPostProcessingOptionsForXml();
073: comms.response.writeDOM(opt, page);
074:
075: } else if (foParam != null) {
076: // Rendering a PDF from FO file
077: File content = new File(foParam);
078: Document page = builder.parse(content);
079: OutputOptions opt = getPostProcessingOptions();
080: comms.response.writeDOM(opt, page);
081: } else if (DateHTML != null || NameHTML != null
082: || SubjHTML != null || AppletHTML != null) {
083: // Rendering a PDF from applet parameters
084: StringReader content;
085: content = inputText(DateHTML, NameHTML, SubjHTML,
086: AppletHTML);
087: Document page = builder.parse(new InputSource(content));
088: OutputOptions opt = getPostProcessingOptions();
089: comms.response.writeDOM(opt, page);
090: } else {
091: System.out
092: .println("There is no any request parameter given!!!");
093: StringReader content;
094:
095: content = defaultText();
096:
097: Document page = builder.parse(new InputSource(content));
098: OutputOptions opt = getPostProcessingOptions();
099: comms.response.writeDOM(opt, page);
100: }
101: } catch (NullPointerException e) {
102: throw new ClientPageRedirectException("IndexPO.po");
103: }
104: }
105:
106: public OutputOptions getPostProcessingOptions()
107: throws HttpPresentationException {
108: OutputOptions ret = new OutputOptions();
109: ret.setMIMEType("text/fo+fop-pdf");
110:
111: Map map = new HashMap();
112:
113: //TODO is this OK
114: map.put(Fo2FopResponsePostProcessor.AUTHOR_PARAM_NAME,
115: "fopApplet Demo");
116: map.put(Fo2FopResponsePostProcessor.CREATION_DATE_PARAM_NAME,
117: new Date());
118: map.put(Fo2FopResponsePostProcessor.CREATOR_PARAM_NAME,
119: "fopApplet");
120: map.put(Fo2FopResponsePostProcessor.KEYWORDS_PARAM_NAME,
121: "TAS TAF TDA fopApplet");
122: map.put(Fo2FopResponsePostProcessor.PRODUCER_PARAM_NAME,
123: "Together Application Framework");
124: map
125: .put(
126: Fo2FopResponsePostProcessor.TARGET_RESOLUTION_PARAM_NAME,
127: new Integer(300));
128: map.put(Fo2FopResponsePostProcessor.TITLE_PARAM_NAME,
129: "fopApplet Demo Document");
130: ret.setFreeformOptions(map);
131: return ret;
132: }
133:
134: public OutputOptions getPostProcessingOptionsForXml()
135: throws HttpPresentationException {
136: OutputOptions ret = new OutputOptions();
137: ret.setMIMEType("text/xml+fop-pdf");
138: ret.addFreeformOption("Xml2FoTemplatePath", xslParam);
139: return ret;
140: }
141:
142: /**
143: * Method inputText Creates a XSLFO file with inserted parameters from HTML.
144: * Noncomptibility: FOP 0.19 - <fo:page-sequence master-name="simple"> FOP
145: * 0.20.4 - <fo:page-sequence master-reference="simple">
146: *
147: * @return a StringReader
148: *
149: */
150: public StringReader inputText(String DateHTML, String NameHTML,
151: String SubjHTML, String AppletHTML) {
152: String text = new String();
153: text += "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
154: text += "<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">\n";
155: text += " <fo:layout-master-set>\n";
156: text += " <fo:simple-page-master master-name=\"simple\" page-height=\"29.7cm\" page-width=\"21cm\" "
157: + "margin-top=\"1cm\" margin-bottom=\"2cm\" margin-left=\"2.5cm\" margin-right=\"2.5cm\">\n";
158: text += " <fo:region-body margin-top=\"3cm\"/>\n";
159: text += " <fo:region-before extent=\"3cm\"/>\n";
160: text += " <fo:region-after extent=\"1.5cm\"/>\n";
161: text += " </fo:simple-page-master>\n";
162: text += " </fo:layout-master-set>\n";
163: text += " <fo:page-sequence master-reference=\"simple\">\n";
164: text += " <fo:flow flow-name=\"xsl-region-body\">\n";
165: text += " <fo:block font-size=\"18pt\" font-family=\"sans-serif\" line-height=\"24pt\" "
166: + "space-after.optimum=\"15pt\" background-color=\"blue\" color=\"white\" "
167: + "text-align=\"center\" padding-top=\"3pt\">\n";
168: text += " Demo PDF file from applet \n";
169: text += " </fo:block>\n";
170: text += " <fo:table font-family=\"Helvetica\" space-after.optimum=\"20pt\">\n";
171: text += " <fo:table-column column-width=\"20mm\" />\n";
172: text += " <fo:table-column column-width=\"150mm\" />\n";
173: text += " <fo:table-body>\n";
174: text += " <fo:table-row>\n";
175: text += " <fo:table-cell>\n";
176: text += " <fo:block color=\"blue\" font-weight=\"bold\">Date:</fo:block>\n";
177: text += " </fo:table-cell>\n";
178: text += " <fo:table-cell>\n";
179: text += " <fo:block>" + DateHTML
180: + "</fo:block>\n";
181: text += " </fo:table-cell>\n";
182: text += " </fo:table-row>\n";
183: text += " <fo:table-row>\n";
184: text += " <fo:table-cell>\n";
185: text += " <fo:block color=\"blue\" font-weight=\"bold\">Name:</fo:block>\n";
186: text += " </fo:table-cell>\n";
187: text += " <fo:table-cell>\n";
188: text += " <fo:block>" + NameHTML
189: + "</fo:block>\n";
190: text += " </fo:table-cell>\n";
191: text += " </fo:table-row>\n";
192: text += " <fo:table-row>\n";
193: text += " <fo:table-cell>\n";
194: text += " <fo:block color=\"blue\" font-weight=\"bold\">Subject:</fo:block>\n";
195: text += " </fo:table-cell>\n";
196: text += " <fo:table-cell>\n";
197: text += " <fo:block>" + SubjHTML
198: + "</fo:block>\n";
199: text += " </fo:table-cell>\n";
200: text += " </fo:table-row>\n";
201: text += " </fo:table-body>\n";
202: text += " </fo:table>\n";
203: text += AppletHTML;
204: text += " </fo:flow>\n";
205: text += " </fo:page-sequence>\n";
206: text += "</fo:root>\n";
207:
208: StringReader foText = new StringReader(text);
209:
210: return foText;
211: }
212:
213: public StringReader defaultText() {
214: String text = new String();
215: text += "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
216: text += "<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">\n";
217: text += " <fo:layout-master-set>\n";
218: text += " <fo:simple-page-master master-name=\"simple\" page-height=\"29.7cm\" page-width=\"21cm\" "
219: + "margin-top=\"1cm\" margin-bottom=\"2cm\" margin-left=\"2.5cm\" margin-right=\"2.5cm\">\n";
220: text += " <fo:region-body margin-top=\"3cm\"/>\n";
221: text += " <fo:region-before extent=\"3cm\"/>\n";
222: text += " <fo:region-after extent=\"1.5cm\"/>\n";
223: text += " </fo:simple-page-master>\n";
224: text += " </fo:layout-master-set>\n";
225: text += " <fo:page-sequence master-reference=\"simple\">\n";
226: text += " <fo:flow flow-name=\"xsl-region-body\">\n";
227: text += " <fo:block font-size=\"18pt\" font-family=\"sans-serif\" line-height=\"24pt\" "
228: + "space-after.optimum=\"15pt\" background-color=\"blue\" color=\"white\" "
229: + "text-align=\"center\" padding-top=\"3pt\">\n";
230: text += " Demo PDF file \n";
231: text += " </fo:block>\n";
232: text += " <fo:block font-size=\"12pt\" font-family=\"sans-serif\" line-height=\"18pt\" space-after.optimum=\"3pt\" text-align=\"center\" white-space-collapse=\"false\" language=\"en_US\" hyphenate=\"true\">\n";
233: text += " <fo:inline font-family=\"sans-serif\" font-size=\"18pt\" font-weight=\"bold\" >There is no any request parameter given!!!</fo:inline>\n";
234: text += " </fo:block>\n";
235: text += " <fo:block font-size=\"12pt\" font-family=\"sans-serif\" line-height=\"18pt\" space-after.optimum=\"3pt\" text-align=\"center\" white-space-collapse=\"false\" language=\"en_US\" hyphenate=\"true\">\n";
236: text += " <fo:inline font-family=\"sans-serif\" font-size=\"16pt\" ></fo:inline>\n";
237: text += " </fo:block>\n";
238: text += " <fo:block font-size=\"12pt\" font-family=\"sans-serif\" line-height=\"18pt\" space-after.optimum=\"3pt\" text-align=\"center\" white-space-collapse=\"false\" language=\"en_US\" hyphenate=\"true\">\n";
239: text += " <fo:inline font-family=\"sans-serif\" font-size=\"12pt\" >Please, see documentation for details.</fo:inline>\n";
240: text += " </fo:block>\n";
241: text += " </fo:flow>\n";
242: text += " </fo:page-sequence>\n";
243: text += "</fo:root>\n";
244:
245: StringReader defaultText = new StringReader(text);
246:
247: return defaultText;
248: }
249:
250: }
|