The XMLService Class
//: XMLService.java
package com.owensum.everest.xml;
import java.util.*;
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.apache.xml.serialize.XMLSerializer;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xerces.parsers.DOMParser;
/** Use this class to process xml requests. This class expects the xml request
* adheres to IFX
*
* @author Greg Thompson
* @version 1.0
*/
public class XMLService {
// the impl for xml document.
public final static String CLASS_NAME_DOCUMENT = "org.apache.xerces.dom.DocumentImpl";
// private constructor
private XMLService() {
// create new handlers hash map
_handlers = new HashMap();
// add currently known handlers
addElementHandler( CarrierInfoInqRqHandler.TAG_CARRIER_INFO_INQ_RQ, new CarrierInfoInqRqHandler() );
addElementHandler( EPLIFactorsInqRqHandler.TAG_EPLI_FACTORS_INQ_RQ, new EPLIFactorsInqRqHandler() );
.
.
.
}
/** This is the process request method. It is used to process an xml docuemnt.
* It expects that the document is in IFX format. It tries to login using the signonrq.
*
* @param doc the request document
* @return the response document
* @exception Exception
*/
public Document processRequest( Document request )
throws Exception
{
// login and get the user
User user = login( request );
// get root element
Element root = request.getDocumentElement();
// create the response doc
Document response = (Document) Class.forName( CLASS_NAME_DOCUMENT ).newInstance();
// handle the root element
Element newRoot = handleElement( user, response, root );
// append the new root
response.appendChild( newRoot );
// return the response
return response;
}
/** This method is to recursively handle the elements of a request document. For each child element
* of the given element, if there is a registered element handler, then it passes that element to
* the handler. If not, call handle element (recursive, drill down). If the element has no children,
* return null.
*
* The element handler should take a request transaction, and return the response.
*
* @param doc the request document
* @return the response document
* @exception Exception
*/
private Element handleElement( User user, Document doc, Element element )
throws Exception
{
// get child nodes of root element
NodeList nodes = element.getChildNodes();
// if no child nodes
if ( nodes.getLength() == 0 )
return null;
// create new element based on the same name as the given element
Element newElement = doc.createElement( element.getNodeName() );
// iterate over child nodes
for ( int i = 0; i < nodes.getLength(); i++ ) {
Node node = nodes.item( i );
// if the node is an element, then handle the element
if ( node.getNodeType() == node.ELEMENT_NODE ) {
//System.out.print( "Node: " + node.getNodeName() + " ");
Element e = (Element) node;
// handle the element
ElementHandler h = (ElementHandler) _handlers.get( node.getNodeName() );
Element ne = null;
// if there is a element handler, then call it
if ( h != null ) {
//System.out.println( "(ElementHandler: Yes)" );
ne = h.handleElement( user, doc, e );
}
// if not, drill down
else {
//System.out.println( "(ElementHandler: No)" );
ne = handleElement( user, doc, e );
}
// if the response is not null, add it
if ( ne != null )
newElement.appendChild( ne );
}
}
// return the new element ( response )
return newElement;
}
/** Use this method to create an xml document (dom) from a stream.
*
* @param inputStream the input stream of the xml document
* @return the document create from the stream
* @exception IOException
* @exception SAXException
*/
public Document createDocument( InputStream is )
throws IOException, SAXException
{
DOMParser parser = new DOMParser();
parser.parse( new InputSource( is ) );
return parser.getDocument();
}
.
.
.
}