Wednesday, November 12, 2014

Serving IT up with XML.

I hope that your weeks are progressing well. This is the same source that serves up the XHTML, but it now has an XML configuration file which holds the name of the file being served. We will evolve this over the next few weeks so it determines how it should replace the stencil tags from the XML file also. The only new method is readingTheDirectoryXMLFile.

Read and process it carefully. It will be changing some in the coming weeks, but its basic function will be the same. NOTE: I place the directory.xml file in the same folder as the stencil XHTML file. We can adjust the CABOOSE system's file structure later. Continue your good work, Team. La-La.

//BEGIN JAVA SOURCE
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cab;
/**
 * The CAB will eventually for the kernel of the CABOOSE general purpose servlet
 *
 * @author Jody Sharpe
 */
public class CAB {
    //Simulated URL String. We will eventually get all of our input from the
    //the servlet,s Request object.
    String request = "?project=caboose&module=skeleton&create_date=10.30.2014";
    String gettingTheResponseStencil(String aViewFileName) throws Exception {
        java.io.BufferedReader aStencilReader = new java.io.BufferedReader(new java.io.FileReader(aViewFileName));
        String theStencilContent = "", theNextLine = null;
        while ((theNextLine = aStencilReader.readLine()) != null) {
            theStencilContent += theNextLine;
        }
        return (theStencilContent);
    }
    java.util.Map<String, String> mappingTheRequestNameValuePairs(String aRequest) {
        java.util.Map<String, String> aMap = new java.util.Hashtable<String, String>();
        aRequest = aRequest.replace("?", "");
        String[] theNameValuePairs = aRequest.split("&");
        for (String aNameValuePair : theNameValuePairs) {
            final int theName = 0, theValue = 1;
            String[] aTuple = aNameValuePair.split("=");
            aMap.put(aTuple[theName], aTuple[theValue]);
        }
        return aMap;
    }
    String preparingTheResponse(String aStencil, java.util.Map<String, String> aRequestMap) {
        aStencil = aStencil.replace("#PROJECT#", aRequestMap.get("project"));
        aStencil = aStencil.replace("#MODULE#", aRequestMap.get("module"));
        aStencil = aStencil.replace("#CREATE_DATE#", aRequestMap.get("create_date"));
        return (aStencil);
    }
    String readingTheDirectoryXMLFile(String theViewId) throws Exception {
        String aViewId = null;
        String aStencil = null;
       
        javax.xml.parsers.DocumentBuilderFactory theDocumentBuilderFactory =
                javax.xml.parsers.DocumentBuilderFactory.newInstance();
        javax.xml.parsers.DocumentBuilder theDocumentBuilder = theDocumentBuilderFactory.newDocumentBuilder();
       
        java.io.File aFile = new java.io.File("./src/cab/directory.xml");
       
        org.w3c.dom.Document theDocument = theDocumentBuilder.parse(aFile);
        theDocument.getDocumentElement().normalize();
        org.w3c.dom.NodeList theModelNodeList = theDocument.getElementsByTagName("model");
        for (int aModelNodeIndex = 0; aModelNodeIndex < theModelNodeList.getLength(); aModelNodeIndex++) {
           
            org.w3c.dom.Node aModelNode = theModelNodeList.item(aModelNodeIndex);
           
            if (aModelNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
               
                org.w3c.dom.Element aModelElement = (org.w3c.dom.Element) aModelNode;
                org.w3c.dom.NodeList theViewNodeList =
                        aModelElement.getElementsByTagName("view");
               
                for (int aViewNodeIndex = 0; aViewNodeIndex < theViewNodeList.getLength(); aViewNodeIndex++) {
                   
                    org.w3c.dom.Node aViewNode = (org.w3c.dom.Node) theViewNodeList.item(aViewNodeIndex);
                   
                    if (aViewNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
                       
                        org.w3c.dom.Element aViewElement = (org.w3c.dom.Element) aViewNode;
                        aViewId = aViewElement.getAttribute("id");
                       
                        if ( aViewId.equalsIgnoreCase( theViewId )){
                            aStencil = aViewElement.getAttribute("stencil");
                        }
                    }
                }
            }
        }
       
        return (aStencil);
       
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        CAB aCAB = new CAB();
        java.util.Map<String, String> aRequestMap = aCAB.mappingTheRequestNameValuePairs(aCAB.request);
        try {
            System.out.println(aCAB.preparingTheResponse(aCAB.gettingTheResponseStencil(aCAB.readingTheDirectoryXMLFile("test_page")), aRequestMap));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
//END JAVA SOURCE

//BEGIN XML SOURE
<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<model>
 <view id="test_page" stencil="./src/cab/xstencil.xhtml">
 </view>
</model>
//END XML SOURCE

No comments:

Post a Comment