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.toc.Toc; 26 import org.eclipse.help.internal.toc.TocContribution; 27 import org.eclipse.help.internal.webapp.data.UrlUtil; 28 29 38 public class TocServlet 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 TocContribution[] contributions = HelpPlugin.getTocManager().getTocContributions(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 private String serialize(TocContribution[] contributions, String locale) throws TransformerException { 68 StringBuffer buf = new StringBuffer (); 69 buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); buf.append("<tocContributions>\n"); if (writer == null) { 72 writer = new DocumentWriter(); 73 } 74 for (int i = 0; i < contributions.length; ++i) { 75 TocContribution contrib = contributions[i]; 76 buf.append("<tocContribution"); if (contrib.getCategoryId() != null) { 78 buf.append("\n categoryId=\"" + contrib.getCategoryId() + '"'); } 80 if (contrib.getContributorId() != null) { 81 buf.append("\n contributorId=\"" + contrib.getContributorId() + '"'); } 83 buf.append("\n id=\"" + contrib.getId() + '"'); buf.append("\n locale=\"" + contrib.getLocale() + '"'); buf.append("\n isPrimary=\"" + contrib.isPrimary() + "\">\n"); buf.append(writer.writeString((Toc)contrib.getToc(), false)); 87 String [] hrefs = contrib.getExtraDocuments(); 88 for (int j=0;j<hrefs.length;++j) { 89 buf.append(" <extraDocument HREF=\"" + hrefs[j] + "\"/>\n"); } 91 buf.append("</tocContribution>\n"); } 93 buf.append("</tocContributions>\n"); return buf.toString(); 95 } 96 } 97 | Popular Tags |