KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > data > XMLConverter


1 /* Todo
2 + make search for null bytes optional
3
4 */

5
6
7 /* *****************************************************************************
8  * XMLConverter.java
9 * ****************************************************************************/

10
11 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
12 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
13 * Use is subject to license terms. *
14 * J_LZ_COPYRIGHT_END *********************************************************/

15
16 package org.openlaszlo.data;
17
18 import java.io.*;
19 import java.util.Properties JavaDoc;
20
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23
24 import org.apache.log4j.*;
25
26 import org.openlaszlo.xml.internal.DataCompiler;
27 import org.openlaszlo.xml.internal.DataCompilerException;
28 import org.openlaszlo.utils.ContentEncoding;
29
30 import org.openlaszlo.media.MimeType;
31 import org.openlaszlo.server.LPS;
32
33 /**
34  * XML Converter
35  *
36  */

37 public class XMLConverter extends Converter {
38     
39     private static Logger mLogger = Logger.getLogger(XMLConverter.class);
40
41     /** Data compiler */
42     private DataCompiler mCompiler = new DataCompiler();
43
44     /**
45      * Convert incoming XML to SWF, adding some HTTP info as well.
46      */

47     public InputStream convertToSWF(Data data, HttpServletRequest JavaDoc req,
48                                     HttpServletResponse JavaDoc res)
49        throws ConversionException, IOException {
50
51         String JavaDoc mimeType = data.getMimeType();
52
53         if (mimeType != null && mimeType.equals(MimeType.SWF)) {
54             return data.getInputStream();
55         }
56
57         if (mimeType == null || !mimeType.equals(MimeType.XML)) {
58             mLogger.warn("back-end mime-type is " + mimeType + ", treating as text/xml" );
59         }
60
61         String JavaDoc body = data.getAsString();
62
63         // TODO: [2003-04-26 bloch] perhaps there should be a config
64
// to turn this check on/off.
65
//
66
// Strip out possible null characters from input. Null
67
// characters are bad because they get passed by JGenerator
68
// through to Flash and really screw up zero-terminated string
69
// constants, causing corrupted Flash byte codes.
70
if ("true".equals(LPS.getProperty("data.removenulls", "false"))) {
71             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
72             int nchars = body.length();
73             for (int i = 0; i < nchars; i++) {
74                 char ch = body.charAt(i);
75                 if (ch != 0) {
76                     buf.append(ch);
77                 }
78             }
79             body = buf.toString();
80         }
81
82         // Get headers
83
String JavaDoc sendheaders = req.getParameter("sendheaders");
84         StringBuffer JavaDoc headerbuf = new StringBuffer JavaDoc();
85         headerbuf.append("<headers>\n");
86         if (sendheaders == null || sendheaders.equals("true") ) {
87             data.appendResponseHeadersAsXML(headerbuf);
88         }
89         headerbuf.append("</headers>");
90         String JavaDoc headers = headerbuf.toString();
91
92         if (mLogger.isDebugEnabled()) {
93             mLogger.info("Output:" + body.length());
94             mLogger.info("Output:\n" + body);
95             mLogger.info("Output Headers:" + headers.length());
96             mLogger.info("Output Headers:\n" + headers);
97         }
98
99         boolean trimWhitespace = "true".equals(req.getParameter("trimwhitespace"));
100
101         boolean compress = "true".equals(req.getParameter("compress"));
102
103         try {
104             return mCompiler.compile(body, headers, LPS.getSWFVersionNum(req),
105                                      true, trimWhitespace, compress);
106
107         } catch (DataCompilerException dce) {
108             throw new ConversionException(dce.getMessage());
109         } catch (IOException ie) {
110             throw new ConversionException(ie.getMessage());
111         }
112     }
113
114     /**
115      * @return the encoding that should be used when responding
116      * to this request or null for no encoding. For now, the only
117      * acceptable values besides null are "gzip" and "deflate".
118      */

119     public String JavaDoc chooseEncoding(HttpServletRequest JavaDoc req) {
120
121         String JavaDoc e = req.getParameter("enc");
122         if (e == null || e.equals("false")) {
123             return null;
124         }
125
126         String JavaDoc enc = ContentEncoding.chooseEncoding(req);
127         mLogger.debug("Encoding: " + enc);
128         return enc;
129     }
130
131 }
132
Popular Tags