1 64 65 package com.jcorporate.expresso.core.servlet.viewhandler; 66 67 import com.jcorporate.expresso.core.controller.ControllerException; 68 import com.jcorporate.expresso.core.controller.ControllerResponse; 69 import com.jcorporate.expresso.core.db.DBException; 70 import com.jcorporate.expresso.core.misc.StringUtil; 71 import com.jcorporate.expresso.ext.xml.dbobj.ControllerXSLMap; 72 import com.jcorporate.expresso.ext.xml.dbobj.UserAgent; 73 import com.jcorporate.expresso.services.dbobj.Setup; 74 import org.apache.log4j.Logger; 75 76 import javax.servlet.ServletConfig ; 77 import javax.servlet.ServletException ; 78 import javax.servlet.http.HttpServletRequest ; 79 import javax.servlet.http.HttpServletResponse ; 80 import javax.xml.transform.Transformer ; 81 import javax.xml.transform.TransformerConfigurationException ; 82 import javax.xml.transform.TransformerException ; 83 import javax.xml.transform.TransformerFactory ; 84 import javax.xml.transform.stream.StreamResult ; 85 import javax.xml.transform.stream.StreamSource ; 86 import java.io.File ; 87 import java.io.IOException ; 88 import java.io.PrintWriter ; 89 import java.io.StringReader ; 90 91 92 98 public class XMLViewHandler extends ViewHandler { 99 protected static final String thisClass = XMLViewHandler.class.getName() + "."; 100 private static Logger log = Logger.getLogger(XMLViewHandler.class); 101 private String mRootPath; 102 103 public XMLViewHandler() { 104 } 105 106 111 public void handleView(ControllerResponse con, 112 HttpServletRequest req, 113 HttpServletResponse res) 114 throws ControllerException, IOException , ServletException { 115 116 117 try { 118 String xsltName = StringUtil.notNull(con.getParameter("xsl")); 119 120 121 if (xsltName.equals("")) { 122 xsltName = Setup.getValueRequired(con.getDBName(), 123 "DefaultXSL"); 124 125 String userAgent = StringUtil.notNull(req.getHeader("User-agent")); 126 UserAgent ua = new UserAgent(); 127 ua.setDataContext(con.getDBName()); 128 129 132 int uaId = 0; 133 134 if (ua.getMatch(userAgent)) { 135 uaId = ua.getFieldInt("UserAgent"); 137 ControllerXSLMap myMap = new ControllerXSLMap(); 138 myMap.setDataContext(con.getDBName()); 139 140 if (myMap.getMatch(con.getUser(), con.getControllerClass(), 141 uaId)) { 142 xsltName = myMap.getField("XSLFileName"); 143 } 144 145 } 146 147 } 148 149 150 String mime = con.getParameter("mime"); 152 if (mime != null) { 153 res.setContentType(mime); 154 } else if (xsltName.equalsIgnoreCase("none")) { 155 res.setContentType("text/xml"); 156 } else { 157 159 String charset = con.getString("charset"); 160 161 if (charset != null) { 162 res.setContentType("text/html;charset=" + charset); 163 } else { 164 res.setContentType("text/html;charset=ISO-8859-1"); 165 } 166 } 167 168 PrintWriter out = res.getWriter(); 169 String responseString = con.toXML(); 170 171 if (xsltName.equalsIgnoreCase("none")) { 172 out.println("<?xml version=\"1.0\"?>"); 174 out.println(responseString); 175 } else { 176 177 File xslFile = new File (xsltName); 179 if (!xslFile.exists()) { 180 xslFile = new File (mRootPath, xsltName); 181 if (!xslFile.exists()) { 182 log.error("cannot find xsl file: " + xsltName); 183 } 184 } 185 186 TransformerFactory tFactory = TransformerFactory.newInstance(); 189 Transformer transformer = tFactory.newTransformer(new StreamSource (xslFile)); 190 191 transformer.transform(new StreamSource (new StringReader (responseString)), 193 new StreamResult (out)); 194 } 195 196 } catch (DBException de) { 198 res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 199 throw new ControllerException(de); 200 } catch (TransformerConfigurationException te1) { 201 res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 202 throw new ControllerException(te1); 203 } catch (TransformerException te2) { 204 res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 205 throw new ControllerException(te2); 206 } 207 } 208 209 214 public void init(ServletConfig sc) 215 throws ServletException { 216 super.init(sc); 217 mRootPath = sc.getServletContext().getRealPath("/"); 218 } 219 220 } | Popular Tags |