Web フレームワークを作る!! test case

Posted 2011年8月22日 by

WebフレームワークのhtmlSrvクラスを少々改造

パラメータでどのパーサを使うかを選択できるようにします。

val sw = request.getParameter(“sw”)によってURLのパラメータを読み取ります。

 

	def gen(request : HttpServletRequest, response : HttpServletResponse) : Unit = {
		logger.debug("htmlSrv ################# start")
		println("htmlSrv ################# start")
		var htmlFile = ""
		request.setCharacterEncoding("utf-8")
		val sw = request.getParameter("sw")
   		val url = request.getRequestURL
		val uri = request.getRequestURI
		var spth:String = request.getServletPath
		// URLからパラメータ情報を取り出す
		spth = spth.replace("/","")
		val ur = uri.split("/")
		var fg = false
		ur.foreach({v =>
			if(fg){
				htmlFile += "/" + v
			}
			if(v.equals(spth))
				fg = true
		})
		logger.debug("uri="+uri+" htmlFile="+htmlFile)
		response.setContentType("text/html; charset=utf-8")
		var out = response.getWriter

		val ssp = sw match {
		  case null => new StraightSp(request, response, htmlPath + htmlFile)	// ファイルを読み込み、そのまま出力
		  case "1" => new XmlSp(request, response, htmlPath + htmlFile)			// HTMLファイルをXMLローダで読み込み、文字列変換して出力
		  case "2" => new Htmlparse2Sp(request, response, htmlPath + htmlFile)	// HTMLパーサーの解析結果を、文字列変換して出力
		  case "3" => new HtmlparseSp(request, response, htmlPath + htmlFile)	// HTMLパーサーの解析結果を、文字列変換して出力
		  case "4" => new Ssp(request, response, htmlPath + htmlFile)			// SEEDOによるクラス生成し、その出力結果を置換して出力
		  case _ => null
		}

   		val re = if(ssp.html != null){ssp.toString}else{message}
		out.println(re)	// output html data
		out.flush
		out.close
		logger.debug(" #### End")
		println(" #### End")
	}

 

テスト用のHTMLファイルにはtb関数の引数付きを追加します。
引数は、整数、実数、アスキー文字列、日本語文字列です。

<!doctype html>
<HTML>
<HEAD>
<title>Home</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</HEAD>
<BODY>
<h2>Example2</h2>
<div>The current time is <span class="seedo:ews.HelloWorld:tb">now</span>.</div>
<div>The current time is <span class="seedo:ews.HelloWorld:tb:123">now</span>.</div>
<div>The current time is <span class="seedo:ews.HelloWorld:tb:999999999">now</span>.</div>
<div>The current time is <span class="seedo:ews.HelloWorld:tb:8888.8888">now</span>.</div>
<div>The current time is <span class="seedo:ews.HelloWorld:tb:Abcdefgh">now</span>.</div>
<div>The current time is <span class="seedo:ews.HelloWorld:tb:こんばんわ">now</span>.</div>
</BODY>
</HTML>

結果

URLは、以下のようになります。
TOMCATのプロジェクトはtoroです。

http://127.0.0.1:8080/toro/html/helloworld2.html?sw=4

tb関数が引数の型により呼び出されていることが確認できます。

Post Details

  • Post Title: Web フレームワークを作る!! test case
  • Author: admin
  • Filed As: Framework, Scala
  • Tags:
  • You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

コメントを残す