1 5 6 7 10 11 15 16 package org.openlaszlo.data; 17 18 import java.io.*; 19 import java.util.Properties ; 20 21 import javax.servlet.http.HttpServletRequest ; 22 import javax.servlet.http.HttpServletResponse ; 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 37 public class XMLConverter extends Converter { 38 39 private static Logger mLogger = Logger.getLogger(XMLConverter.class); 40 41 42 private DataCompiler mCompiler = new DataCompiler(); 43 44 47 public InputStream convertToSWF(Data data, HttpServletRequest req, 48 HttpServletResponse res) 49 throws ConversionException, IOException { 50 51 String 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 body = data.getAsString(); 62 63 if ("true".equals(LPS.getProperty("data.removenulls", "false"))) { 71 StringBuffer buf = new StringBuffer (); 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 String sendheaders = req.getParameter("sendheaders"); 84 StringBuffer headerbuf = new StringBuffer (); 85 headerbuf.append("<headers>\n"); 86 if (sendheaders == null || sendheaders.equals("true") ) { 87 data.appendResponseHeadersAsXML(headerbuf); 88 } 89 headerbuf.append("</headers>"); 90 String 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 119 public String chooseEncoding(HttpServletRequest req) { 120 121 String e = req.getParameter("enc"); 122 if (e == null || e.equals("false")) { 123 return null; 124 } 125 126 String enc = ContentEncoding.chooseEncoding(req); 127 mLogger.debug("Encoding: " + enc); 128 return enc; 129 } 130 131 } 132 | Popular Tags |