1 19 20 package org.netbeans.modules.xsl.transform; 21 22 import java.io.IOException ; 23 import java.io.PrintWriter ; 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import java.net.UnknownHostException ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Vector ; 30 import javax.servlet.ServletConfig ; 31 import javax.servlet.ServletException ; 32 import javax.servlet.http.HttpServlet ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 import javax.xml.transform.Result ; 36 import javax.xml.transform.Source ; 37 import javax.xml.transform.TransformerException ; 38 import javax.xml.transform.stream.StreamResult ; 39 import org.netbeans.api.xml.cookies.CookieMessage; 40 import org.netbeans.api.xml.cookies.CookieObserver; 41 import org.netbeans.api.xml.cookies.TransformableCookie; 42 import org.netbeans.api.xml.cookies.XMLProcessorDetail; 43 import org.netbeans.modules.xsl.utils.TransformUtil; 44 import org.netbeans.spi.xml.cookies.DefaultXMLProcessorDetail; 45 import org.openide.filesystems.FileObject; 46 import org.openide.filesystems.FileSystem; 47 import org.openide.filesystems.Repository; 48 import org.openide.filesystems.URLMapper; 49 import org.xml.sax.SAXParseException ; 50 51 55 public class TransformServlet extends HttpServlet { 56 private static final long serialVersionUID = 1632869007241230624L; 57 58 private static TransformableCookie transformable; 59 60 private static Source xmlSource; 61 62 private static Source xslSource; 63 64 65 public static void prepare (TransformableCookie trans, Source xml, Source xsl) { 66 transformable = trans; 67 xmlSource = xml; 68 xslSource = xsl; 69 } 70 71 73 public void init (ServletConfig config) throws ServletException { 74 super.init (config); 75 } 76 77 79 public void destroy () { 80 xmlSource = null; 81 xslSource = null; 82 } 83 84 88 protected void processRequest (HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 89 PrintWriter out = response.getWriter(); 90 Result outputResult = new StreamResult (out); 91 92 Observer notifier = new Observer(); 93 try { 94 String guessOutputExt = TransformUtil.guessOutputExt (xslSource); 95 String mimeType; 96 if (guessOutputExt.equals("txt")) { mimeType = "text/plain"; } else if (guessOutputExt.equals("xml")) { mimeType = "text/xml"; } else if (guessOutputExt.equals("html")) { mimeType = "text/html"; } else { 103 mimeType = null; 104 } 105 106 if ( mimeType != null ) { 107 response.setContentType (mimeType); 108 } 109 110 if ( Util.THIS.isLoggable() ) { 111 Util.THIS.debug ("[TransformServlet] Response MIME Type: '" + mimeType + "'"); 112 Util.THIS.debug (" xmlSource.getSystemId() = " + xmlSource.getSystemId()); 113 Util.THIS.debug (" transformable = " + transformable); 114 Util.THIS.debug (" xslSource.getSystemId() = " + xslSource.getSystemId()); 115 } 116 117 TransformUtil.transform (xmlSource, transformable, xslSource, outputResult, notifier); 118 } catch (Exception exc) { 119 if ( Util.THIS.isLoggable() ) Util.THIS.debug (" EXCEPTION!!!: " + exc.getClass().getName(), exc); 120 121 CookieMessage message = null; 123 124 if ( exc instanceof TransformerException ) { 125 } else if ( exc instanceof SAXParseException ) { 127 message = new CookieMessage 128 (TransformUtil.unwrapException (exc).getLocalizedMessage(), 129 CookieMessage.FATAL_ERROR_LEVEL, 130 new DefaultXMLProcessorDetail ((SAXParseException ) exc) 131 ); 132 } else { 133 message = new CookieMessage 134 (exc.getLocalizedMessage(), 135 CookieMessage.FATAL_ERROR_LEVEL 136 ); 137 } 138 139 if ( Util.THIS.isLoggable() ) { 140 Util.THIS.debug (" message = " + message); 141 Util.THIS.debug (" notifier = " + notifier); 142 } 143 144 if ( message != null ) { 145 notifier.receive (message); 146 } 147 148 response.setContentType ("text/html"); 150 151 out.println ("<html><head>"); 152 out.println (" <title>" + Util.THIS.getString ("MSG_error_html_title") + "</title>"); 153 out.println (" <style>" + Util.THIS.getString ("MSG_error_html_style") + "</style>"); 154 out.println ("</head><body>"); 155 out.println (" <h2>" + Util.THIS.getString ("MSG_error_page_title") + "</h2>"); 156 out.println (" <p>" + Util.THIS.getString ("MSG_error_page_message") + "</p>"); 157 out.println (" <hr size=\"1\" noshade=\"\" />\n" + generateReport (notifier.getList()) + "<hr size=\"1\" noshade=\"\" />"); 158 out.println (" <p>" + Util.THIS.getString ("MSG_error_bottom_message") + "</p>"); 159 out.println ("</body></html>"); 160 } finally { 161 out.close(); 162 } 163 } 164 165 169 protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 170 processRequest (request, response); 171 } 172 173 177 protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 178 processRequest (request, response); 179 } 180 181 183 public String getServletInfo () { 184 return "XSL Transformation Preview Servlet"; 185 } 186 187 public static URL getServletURL () throws MalformedURLException , UnknownHostException { 188 189 URL base = getSampleHTTPServerURL(); 190 URL root = new URL (base.getProtocol(), base.getHost(), base.getPort(), "/servlet/" + TransformServlet.class.getName() + "/"); 192 193 return root; 194 } 195 196 private static URL getSampleHTTPServerURL() { 197 FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 198 FileObject fo = fs.findResource("HTTPServer_DUMMY"); 199 if (fo == null) { 200 return null; 201 } 202 URL u = URLMapper.findURL(fo, URLMapper.NETWORK); 203 return u; 204 } 205 206 private String generateReport (List msgList) { 207 StringBuffer sb = new StringBuffer (); 208 209 try { 210 211 Iterator it = msgList.iterator(); 212 while ( it.hasNext() ) { 213 CookieMessage msg = (CookieMessage) it.next(); 214 XMLProcessorDetail detail = (XMLProcessorDetail) msg.getDetail (XMLProcessorDetail.class); 215 216 sb.append (" <font class=\"").append (levelName (msg.getLevel())).append ("\">").append (msg.getMessage()).append ("</font>"); 219 if ( detail != null ) { 220 String systemId = preferFileName (detail.getSystemId()); 222 if ( systemId != null ) { 223 sb.append (" (<font class=\"system-id\">"); 224 boolean isFile = systemId.startsWith ("file:"); 225 if ( isFile ) { 226 sb.append ("<a HREF=\"").append (systemId).append ("\">"); 227 } 228 sb.append (systemId); 229 if ( isFile ) { 230 sb.append ("</a>"); 231 } 232 sb.append ("</font>\n"); sb.append (" [<font class=\"line-number\">").append (detail.getLineNumber()).append ("</font>])<br>"); } 236 } 237 } 238 239 } catch (Exception exc) { 240 if ( Util.THIS.isLoggable() ) Util.THIS.debug (exc); 241 } 242 243 return sb.toString(); 244 } 245 246 247 private String preferFileName (String systemId) { 248 String name = systemId; 249 250 try { 251 URL url = new URL (systemId); 252 FileObject fo = URLMapper.findFileObject(url); 253 if (fo != null) { 254 name = TransformUtil.getURLName (fo); 255 } 256 } catch (Exception exc) { 257 259 if ( Util.THIS.isLoggable() ) Util.THIS.debug (exc); 260 } 261 262 return name; 263 } 264 265 private String levelName (int level) { 266 if ( level == CookieMessage.FATAL_ERROR_LEVEL ) { 267 return "fatal-error"; } else if ( level == CookieMessage.ERROR_LEVEL ) { 269 return "error"; } else if ( level == CookieMessage.WARNING_LEVEL ) { 271 return "warning"; } else { return "informational"; } 275 } 276 277 278 282 private static class Observer implements CookieObserver { 283 284 private final List msgList; 285 286 public Observer () { 287 msgList = new Vector (); 288 } 289 290 public void receive (CookieMessage msg) { 291 msgList.add (msg); 292 } 293 294 public List getList () { 295 return msgList; 296 } 297 } 298 } 299 | Popular Tags |