Wednesday, November 5, 2014

Serve IT Up!

Team. As I promised, this week we will serve up some (X)HTML through the use of a buffered file reader as the next step in completing our CABOOSE.

If you copy this source and compile it on your desktop, be certain that you supply the proper name and file path for the hypertext stencil. I am using the NetBeans integrated development environment. For simplicity, I placed it in the same directory as CAB.java which is the "cab" package directory of the "src" folder.

Practice compiling this and run it once or twice. Be careful that you properly copy the JAVA source placing it in a "CAB.java" file and the XHTML putting it in a "xstencil.xhtml" file. Removing the "package cab;" statement might be necessary before successfully compiling it.

Copy the output placing it in an ASCII text editor, and then save it as "view.html" on your desktop. Finally, open your output with a browser. For those of you learning JAVA, carefully review the code.

As promised, there is only minimal exception handling which simplifies coding for the time being. I will continue commenting on this source and our next step on Thursday and Friday. For extra scripting practice, you can make sure that the XHTML file is validated against the W3 Consortium Standard. Make any adjustments in it that are necessary. Rerun the program, and open the validated output in a browser. This should be enough for now.

Speak with you later. 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";
    //Simple response in the form of an HTML page. We will supply any response
    //via the servlet's Response object.
    String response = "<html><head><title>CABOOSE Embryo</title></head>" +
            "<body>#PROJECT#<p>#MODULE#<p>#CREATE_DATE#<p></body></html>";
   
    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 request ){
        java.util.Map<String,String> aMap = new java.util.Hashtable<String,String>();
        request = request.replace("?","");
        String[] theNameValuePairs = request.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 stencil, java.util.Map<String,String> aRequestMap ){
  
        stencil = stencil.replace("#PROJECT#",aRequestMap.get("project"));
        stencil = stencil.replace("#MODULE#",aRequestMap.get("module"));
        stencil = stencil.replace("#CREATE_DATE#",aRequestMap.get("create_date"));
       
        return( stencil );
       
    }
    /**
     * @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( "./src/cab/xstencil.xhtml" ), aRequestMap) );
        }catch( Exception e ){
            e.printStackTrace();
        }
    }
}
//END JAVA SOURCE

//BEGIN XHTML SOURCE
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>CABOOSE Embryo</title>
    </head>
    <body>
        <p>#PROJECT#</p>
        <p>#MODULE#</p>
        <p>#CREATE_DATE#</p>
    </body>
</html>
//END XHTML SOURCE

No comments:

Post a Comment