Web Development with JavaServer Pages

Скачать в pdf «Web Development with JavaServer Pages»


Node parent = child.getParentNode(); if (parent == null) { return null;


}


String parentName = parent.getNodeName(); if (parentName.equals(named))    {


return parent;


} else {


return findAncestor(parent, named);


}


}


This method recursively walks through all of the ancestors of the supplied child node, checking the name of each parent—retrieved via the parent node’s getNode-Name() method—to see if it matches the named parameter. If a matching node is found, it is returned. If not, the findAncestor() utility method is called recursively, with the current child’s parent acting as the child for the next attempt. If the original inner tag node was not properly nested, eventually this method will reach the root element of the XML document, having checked each enclosing tag in turn and failing to find one whose name matches the sought-for outer tag. The root


element has no parent, so in this case findAncestor() will return a null value to validateInnerNode() , resulting in the creation of a ValidationMessage for notifying the JSP container that the page is not valid.


The complete source code for the NestingTVL validator is presented in abbreviated form in listing 20.7.


Listing 20.7 The NestingTLV tag library validator


package com.taglib.wdj sp.mut;


import javax.servlet.jsp.tagext.TagLibraryValidator;


import javax.servlet.jsp.tagext.ValidationMessage;


import javax.servlet.jsp.tagext.PageData;


import javax.xml.parsers.DocumentBuilderFactory;


import javax.xml.parsers. DocumentBuilder;


import org.w3c.dom.Document;


import org.w3c.dom.NodeList;


import org.w3c.dom.Node;


import org.w3c.dom.NamedNodeMap;


import org.w3c.dom.Attr;


import java.io.InputStream;


import java.util.Map;


import java.util.ArrayList;


import java.io.IOException; import org.xml.sax.SAXException;


import javax.xml.parsers.ParserConfigurationException;

Скачать в pdf «Web Development with JavaServer Pages»