FileDocCategorySizeDatePackage
XMLViewHandler.javaAPI DocExample14523Tue Jun 08 11:26:42 BST 2004com.mycompany.jsf.pl

XMLViewHandler

public class XMLViewHandler extends ClassViewHandler
This class is a JSF ViewHandler that works with views defined by a combination of a view specification file and a template file.
author
Hans Bergsten, Gefion Software
version
1.0

Fields Summary
Constructors Summary
public XMLViewHandler(javax.faces.application.ViewHandler origViewHandler)
Creates an instance and saves a reference to the previously registered ViewHandler.

        super(origViewHandler);
    
Methods Summary
protected javax.faces.component.UIViewRootcreateViewRoot(javax.faces.context.FacesContext context, java.lang.String viewId)
Returns the UIViewRoot for the specified view, by parsing the view specification file and processing the elements in the specification with an instance of the ViewSpecHandler class.


        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = null;
        try {
            saxParser = factory.newSAXParser();
        }
        catch (SAXException e) {
            throw new IllegalArgumentException(e.getMessage());
        }
        catch (ParserConfigurationException e) {
            throw new IllegalArgumentException(e.getMessage());
        }

        UIViewRoot viewRoot = new UIViewRoot();
        viewRoot.setViewId(viewId);
        ExternalContext ec = context.getExternalContext();
        InputStream viewSpecIS = null;

        DefaultHandler handler = 
            new ViewSpecHandler(context.getApplication(), viewRoot);
        try {
            viewSpecIS = context.getExternalContext().
                getResourceAsStream(viewId + ".view");
            saxParser.parse(viewSpecIS, handler);

        } catch (SAXParseException e) {
            String msg = "View spec parsing error: " + e.getMessage() +
                " at line=" + e.getLineNumber() +
                " col=" + e.getColumnNumber();
            throw new IllegalArgumentException(msg);
        } catch (Exception e) {
            String msg = "View spec parsing error: " + e.getMessage();
            throw new IllegalArgumentException(msg);
        } finally {
            try {
                if (viewSpecIS != null) {
                    viewSpecIS.close();
                }
            } catch (IOException e) {}
        }
        return viewRoot;
    
protected voidrenderResponse(javax.faces.context.FacesContext context, javax.faces.component.UIComponent component)
Renders the view represented by the provided root component by parsing the template file for the view and processing the elements in the template with an instance of the TemplateHandler class.


        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = null;
        try {
            saxParser = factory.newSAXParser();
        }
        catch (SAXException e) {
            throw new IllegalArgumentException(e.getMessage());
        }
        catch (ParserConfigurationException e) {
            throw new IllegalArgumentException(e.getMessage());
        }

        UIViewRoot root = (UIViewRoot) component;
        String viewId = root.getViewId();
        ExternalContext ec = context.getExternalContext();
        InputStream templIS = null;

        DefaultHandler handler = new TemplateHandler(context, root);
        try {
            templIS = context.getExternalContext().
                getResourceAsStream(viewId + ".html");
            saxParser.parse(templIS, handler);

        } catch (SAXParseException e) {
            String msg = "Template parsing error: " + e.getMessage() +
                " at line=" + e.getLineNumber() +
                " col=" + e.getColumnNumber();
            throw new IllegalArgumentException(msg);
        } catch (Exception e) {
            String msg = "Template parsing error: " + e.getMessage();
            throw new RuntimeException(msg, e);
        } finally {
            try {
                if (templIS != null) {
                    templIS.close();
                }
            } catch (IOException e) {}
        }