1 11 package org.eclipse.help.internal.webapp.servlet; 12 13 import java.io.IOException ; 14 import java.io.Writer ; 15 16 import javax.servlet.ServletException ; 17 import javax.servlet.http.HttpServlet ; 18 import javax.servlet.http.HttpServletRequest ; 19 import javax.servlet.http.HttpServletResponse ; 20 21 import org.eclipse.help.IContext; 22 import org.eclipse.help.IHelpResource; 23 import org.eclipse.help.internal.HelpPlugin; 24 import org.eclipse.help.internal.Topic; 25 import org.eclipse.help.internal.context.Context; 26 import org.eclipse.help.internal.webapp.data.UrlUtil; 27 28 34 public class ContextServlet extends HttpServlet { 35 36 private static final long serialVersionUID = 1L; 37 private static final String PARAMETER_ID = "id"; 39 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 40 throws ServletException , IOException { 41 String locale = UrlUtil.getLocale(req, resp); 42 req.setCharacterEncoding("UTF-8"); resp.setContentType("application/xml; charset=UTF-8"); String id = req.getParameter(PARAMETER_ID); 45 if (id != null) { 46 IContext context = HelpPlugin.getContextManager().getContext(id, locale); 47 if (context != null) { 48 serialize(context, resp.getWriter()); 49 } 50 else { 51 resp.sendError(404); 52 } 53 } 54 else { 55 resp.sendError(400); } 57 } 58 59 private void serialize(IContext context, Writer out) throws IOException { 60 out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); out.write('<' + Context.NAME + ">\n"); out.write(" <description>" + context.getText() + "</description>\n"); IHelpResource[] topics = context.getRelatedTopics(); 64 for (int i=0;i<topics.length;++i) { 65 out.write(" <" + Topic.NAME); if (topics[i].getLabel() != null) { 67 out.write("\n " + Topic.ATTRIBUTE_LABEL + "=\"" + topics[i].getLabel() + '"'); } 69 if (topics[i].getHref() != null) { 70 out.write("\n " + Topic.ATTRIBUTE_HREF + "=\"" + topics[i].getHref() + '"'); } 72 out.write(">\n </topic>"); } 74 out.write("</" + Context.NAME + ">\n"); } 76 } 77 | Popular Tags |