FileDocCategorySizeDatePackage
CompositeDispatcher.javaAPI DocExample4596Sat Nov 22 20:36:02 GMT 2003cvexample.controller

CompositeDispatcher

public class CompositeDispatcher extends Object implements Dispatcher
A dispatcher for dispatching composite views. This dispatcher uses the pages attribute of the request to choose a page described in the views.xml file, and generate a composite from the pieces it describes.

Fields Summary
private static final String
VIEWS_TAG
tags in the views.xml file
private static final String
VIEW_TAG
private static final String
CONTAINER_TAG
private static final String
INCLUDE_TAG
private static final String
TEMPLATE_ATTR
attributes in the views.xml file
private static final String
NAME_ATTR
private static final String
URL_ATTR
private static final String
PAGE_ATTR
private static final String
VIEW_ATTR
attribute used by the custom tag
private static final String
DEFAULT_VIEW_NAME
the name of the default view
private ServletContext
context
the servlet context
private HashMap
views
the views, mapped by name
Constructors Summary
Methods Summary
public java.lang.StringgetNextPage(javax.servlet.http.HttpServletRequest req)
Get the next page based on the page attribute in the request and corresponding entry in views.xml

        // read the page attribute from the request
        String page = (String) req.getParameter(PAGE_ATTR);
        
        // get the parsed view
        View v = (View) views.get(page);
        
        // if no views were found, use the default
        if (v == null) {
            v = (View) views.get(DEFAULT_VIEW_NAME);
        }
        
        // store the current view in the request, for use by the 
        // custom tags
        req.setAttribute(VIEW_ATTR, v);
        return (v.getTemplate());
    
private java.util.HashMapparseXML(java.io.InputStream is)
Parse the views.xml file

        // parse the XML
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder(); 
        Document doc = builder.parse(is);
       
        // find all the view tags
        NodeList viewList = doc.getElementsByTagName(VIEW_TAG);
        HashMap viewMap = new HashMap();
     
        // parse each tag
        for(int i = 0; i < viewList.getLength(); i++) {
            Element curView = (Element)viewList.item(i);
            String template = curView.getAttribute(TEMPLATE_ATTR);
            String curName = curView.getAttribute(NAME_ATTR);
            
            // create the view
            View newView = new View(template);
            viewMap.put(curName, newView);
            if (i == 0) {
                // the first view is the default
                viewMap.put(DEFAULT_VIEW_NAME, newView);
            }
            
            // parse all the containers in the view
            NodeList containerList = curView.getElementsByTagName(CONTAINER_TAG);
            for(int k = 0; k < containerList.getLength(); k++) {
                Element curCont = (Element)containerList.item(k);
                String contName = curCont.getAttribute(NAME_ATTR);
                
                // parse all the includes in the container
                NodeList includeList = curView.getElementsByTagName(INCLUDE_TAG);
                for(int l = 0; l < includeList.getLength(); l++) {
                    Element curInclude = (Element)includeList.item(l);
                    
                    String iName = curInclude.getAttribute(NAME_ATTR);
                    String iUrl = curInclude.getAttribute(URL_ATTR);
                    
                    // add the include to the view
                    newView.addInclude(contName, iName, iUrl);
                }
            }
        }
        
        return viewMap;
    
public voidsetContext(javax.servlet.ServletContext context)
Called by the front controller to set the context for this dispatcher. Parse the views.xml file.

      
                          
          
        this.context = context;
       
        // read the views.xml file & parse it
        InputStream is = context.getResourceAsStream("/views.xml");
        try {
            views = parseXML(is);
        } catch(Exception ex) {
            throw new IOException(ex.getMessage());
        }