リスト5 CoveViewHandler.java

  1 package com.netpotlet.cove.application;
  2
  3 import java.io.IOException;
  4 import java.util.Iterator;
  5 import java.util.Locale;
  6
  7 import javax.faces.FacesException;
  8 import javax.faces.FactoryFinder;
  9 import javax.faces.application.ViewHandler;
 10 import javax.faces.component.UIComponent;
 11 import javax.faces.component.UIViewRoot;
 12 import javax.faces.context.FacesContext;
 13 import javax.faces.context.ResponseWriter;
 14 import javax.faces.render.RenderKit;
 15 import javax.faces.render.RenderKitFactory;
 16 import javax.faces.render.Renderer;
 17 import javax.servlet.http.HttpServletResponse;
 18 import javax.xml.parsers.ParserConfigurationException;
 19
 20 import org.xml.sax.SAXException;
 21
 22 import com.netpotlet.xul.element.XulStartWindow;
 23 import com.netpotlet.xul.element.URVisitor;
 24
 25 public class CoveViewHandler extends ViewHandler {
 26     public static String CONTENT_TYPE = "application/vnd.mozilla.xul+xml";
 27     public static String CHAR_ENCODING = "UTF-8";
 28
 29     public CoveViewHandler() {
 30         super();
 31     }
 32
 33     public Locale calculateLocale(FacesContext context) {
 34         return null;
 35     }
 36
 37     public String calculateRenderKitId(FacesContext context) {
 38         return null;
 39     }
 40
 41     public String getActionURL(FacesContext context, String arg1) {
 42         return null;
 43     }
 44
 45     public String getResourceURL(FacesContext context, String arg1) {
 46         return null;
 47     }
 48
 49     public void renderView(FacesContext context, UIViewRoot root)
 50         throws IOException, FacesException {
 51         RenderKitFactory factory =
 52             (RenderKitFactory) FactoryFinder.getFactory(
 53                 FactoryFinder.RENDER_KIT_FACTORY);
 54         RenderKit renderKit = factory.getRenderKit(context, "XUL_RENDER_KIT");
 55         HttpServletResponse response =
 56             (HttpServletResponse) context.getExternalContext().getResponse();
 57         response.setContentType(CONTENT_TYPE + "; charset=" + CHAR_ENCODING);
 58         ResponseWriter responseWriter =
 59             renderKit.createResponseWriter(
 60                 response.getWriter(),
 61                 CONTENT_TYPE,
 62                 CHAR_ENCODING);
 63         context.setResponseWriter(responseWriter);
 64         renderComponent(context, renderKit, root);
 65         responseWriter.endDocument();
 66         responseWriter.close();
 67     }
 68
 69     private void renderComponent(
 70         FacesContext context,
 71         RenderKit renderKit,
 72         UIComponent component)
 73         throws IOException {
 74         String family = component.getFamily();
 75         String rendererType = component.getRendererType();
 76         Renderer renderer = renderKit.getRenderer(family, rendererType);
 77         renderer.encodeBegin(context, component);
 78         Iterator iterator = component.getChildren().iterator();
 79         while (iterator.hasNext()) {
 80             renderComponent(context, renderKit, (UIComponent) iterator.next());
 81         }
 82         renderer.encodeEnd(context, component);
 83     }
 84
 85     public UIViewRoot createView(FacesContext context, String viewId) {
 86         return restoreView(context, viewId);
 87     }
 88
 89     public UIViewRoot restoreView(FacesContext context, String viewId) {
 90         String realPath =
 91             (
 92                 (javax.servlet.ServletContext) context
 93                     .getExternalContext()
 94                     .getContext())
 95                     .getRealPath(
 96                 viewId);
 97
 98         try {
 99             if (viewId.endsWith("xul")) {
100                 XulStartWindow window =
101                     new XulStartWindow(new java.io.File(realPath));
102                 XulVisitor visitor = new XulVisitor();
103                 URVisitor.traverse(window, visitor);
104                 UIViewRoot root = visitor.getUIViewRoot();
105                 root.setViewId(viewId);
106                 context.setViewRoot(root);
107                 return root;
108             } else {
109                 return null;
110             }
111         } catch (IOException e) {
112             throw new FacesException(e);
113         } catch (SAXException e) {
114             throw new FacesException(e);
115         } catch (ParserConfigurationException e) {
116             throw new FacesException(e);
117         }
118     }
119
120     public void writeState(FacesContext context) throws IOException {
121     }
122 }