Fields Summary |
---|
private static final String | VIEWS_TAGtags 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_ATTRattributes 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_ATTRattribute used by the custom tag |
private static final String | DEFAULT_VIEW_NAMEthe name of the default view |
private ServletContext | contextthe servlet context |
private HashMap | viewsthe views, mapped by name |
Methods Summary |
---|
public java.lang.String | getNextPage(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.HashMap | parseXML(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 void | setContext(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());
}
|