1 11 package org.eclipse.help.internal.webapp.servlet; 12 13 import java.io.IOException ; 14 import java.util.Map ; 15 import java.util.WeakHashMap ; 16 17 import javax.servlet.ServletException ; 18 import javax.servlet.http.HttpServlet ; 19 import javax.servlet.http.HttpServletRequest ; 20 import javax.servlet.http.HttpServletResponse ; 21 import javax.xml.transform.TransformerException ; 22 23 import org.eclipse.help.internal.HelpPlugin; 24 import org.eclipse.help.internal.dynamic.DocumentWriter; 25 import org.eclipse.help.internal.index.Index; 26 import org.eclipse.help.internal.index.IndexContribution; 27 import org.eclipse.help.internal.webapp.data.UrlUtil; 28 29 38 public class IndexServlet extends HttpServlet { 39 40 private static final long serialVersionUID = 1L; 41 private Map responseByLocale; 42 private DocumentWriter writer; 43 44 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 45 throws ServletException , IOException { 46 String locale = UrlUtil.getLocale(req, resp); 47 req.setCharacterEncoding("UTF-8"); resp.setContentType("application/xml; charset=UTF-8"); 50 if (responseByLocale == null) { 51 responseByLocale = new WeakHashMap (); 52 } 53 String response = (String )responseByLocale.get(locale); 54 if (response == null) { 55 IndexContribution[] contributions = HelpPlugin.getIndexManager().getIndexContributions(locale); 56 try { 57 response = serialize(contributions, locale); 58 } 59 catch (TransformerException e) { 60 throw new ServletException (e); 61 } 62 responseByLocale.put(locale, response); 63 } 64 resp.getWriter().write(response); 65 } 66 67 public String serialize(IndexContribution[] contributions, String locale) throws TransformerException { 68 StringBuffer buf = new StringBuffer (); 69 buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); buf.append("<indexContributions>\n"); if (writer == null) { 72 writer = new DocumentWriter(); 73 } 74 for (int i=0;i<contributions.length;++i) { 75 IndexContribution contrib = contributions[i]; 76 buf.append("<indexContribution\n"); buf.append(" id=\"" + contrib.getId() + '"'); buf.append(" locale=\"" + contrib.getLocale() + "\">\n"); buf.append(writer.writeString((Index)contrib.getIndex(), false)); 80 buf.append("</indexContribution>\n"); } 82 buf.append("</indexContributions>\n"); return buf.toString(); 84 } 85 } 86 | Popular Tags |