Wednesday, November 19, 2014

Trimming the Fat

Team. This is the second stage of the source for this week. It has the redundant XML file reading removed. The logic is slightly different. The entire XML file is read and placed in a HashMap and class structure called a ViewModel which itself contains a mapping of stencil replacement tiles and renderer methods. Then the method which prepares the stencil content reads this and utilizes reflection for generating the final view. Remember that this is for a layered Schema-II Model-View-Controller architecture. Look over the source. If you have any questions, post them in the forum at https://java.net/projects/caboose. In future post, we will likely redefine any of the large methods so they consist of less than fifty lines of code. Also, we should improve the commenting. This source represents the crux of the controller's logic. Have a Wonderful Day. La-La.

//BEGIN CAB 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";
    java.util.HashMap<String,ViewModel> aDirectoryMap = null;
   
    private class ViewModel {
        private String theStencilName = null;
        private java.util.HashMap theTileRendererMappings = null;
        void settingTheStencilName( String aStencilName ){
            theStencilName = aStencilName;           
        }
        void settingTheTileRendererMappings( java.util.HashMap<String,String> aSetOfTileRendererMappings ){
            theTileRendererMappings = aSetOfTileRendererMappings;           
        }
        String gettingTheStencilName(){
            return( theStencilName );           
        }
        java.util.HashMap<String,String> gettingTheTileRendererMappings(){
            return( theTileRendererMappings );           
        }
    }
   
    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.HashMap<String, String> mappingTheRequestNameValuePairs(String aRequest) {
        java.util.HashMap<String, String> aMap = new java.util.HashMap<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 theViewId, java.util.HashMap<String, String> aRequestMap) throws Exception {
       
        ViewModel aViewModel = (ViewModel) aDirectoryMap.get( theViewId );
        if ( aViewModel == null ) throw new Exception("View Model Not Found");
       
        String [] aTileClassAndMethod = null;       
       
        String theStencilContent = gettingTheResponseStencil( aViewModel.gettingTheStencilName() );
        java.util.HashMap<String,String> someTileRenderMappings = aViewModel.gettingTheTileRendererMappings();
       
        for( String aTileId : someTileRenderMappings.keySet()){       
            aTileClassAndMethod = ((String)someTileRenderMappings.get( aTileId )).split(",");                                   
            //Start Reflection Activities
            Class aClass = Class.forName( aTileClassAndMethod[0] );
            Class [] theFullyQualifiedParameterTypeClasses = new Class[1];
            theFullyQualifiedParameterTypeClasses[0] = (new java.util.HashMap<String,String>()).getClass();
            Object [] theParameterObjectList= new Object [1];                                   
            theParameterObjectList[0] = aRequestMap;
            theStencilContent = theStencilContent.replace( aTileId , (String) ( aClass.getMethod(aTileClassAndMethod[1],theFullyQualifiedParameterTypeClasses) ).invoke((aClass.getConstructor()).newInstance(), theParameterObjectList));  
            //End Reflection Activities                        
        }
       
        return (theStencilContent);
    }
    void readingTheDirectoryXMLFile(String theViewId) throws Exception {
              
        String aViewId = null;
        String aStencil = null;
        String aTileId = null;
        String aTileClass = null;
        String aTileMethod = null;
       
        aDirectoryMap = new java.util.HashMap<String, ViewModel>();
       
        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;
                        ViewModel aViewModel = new ViewModel();
                       
                        aViewId = aViewElement.getAttribute("id");
                       
                        aStencil = aViewElement.getAttribute("stencil");
                       
                        aViewModel.settingTheStencilName(aStencil);
                        java.util.HashMap<String,String> someTileRendererMappings = new java.util.HashMap<String,String>();
                        org.w3c.dom.NodeList theTileNodeList = aViewElement.getElementsByTagName("tile");
                            for (int aTileNodeIndex = 0; aTileNodeIndex < theTileNodeList.getLength(); aTileNodeIndex++) {
                               
                                org.w3c.dom.Node aTileNode = (org.w3c.dom.Node) theTileNodeList.item(aTileNodeIndex);
                                if (aTileNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
                                    org.w3c.dom.Element aTileElement = (org.w3c.dom.Element) aTileNode;
                                   
                                    aTileId = aTileElement.getAttribute("id");
                                    aTileClass = aTileElement.getAttribute("class");
                                    aTileMethod = aTileElement.getAttribute("method");
                                   
                                    someTileRendererMappings.put( aTileId, aTileClass + "," + aTileMethod );
                                   
                                }
                               
                            }            
                            aViewModel.settingTheTileRendererMappings(someTileRendererMappings);
                                aDirectoryMap.put(aViewId, aViewModel);
                           
                    }
                }
            }
        }  
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        CAB aCAB = new CAB();
        java.util.HashMap<String, String> aRequestMap = aCAB.mappingTheRequestNameValuePairs(aCAB.request);
        try {
            aCAB.readingTheDirectoryXMLFile("test_page");
            System.out.println(aCAB.preparingTheResponse("test_page", aRequestMap));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
//END CAB JAVA Source

No comments:

Post a Comment