Play framework tutorial for Starters

What is play-Play! is very different compared to your average Java web application. Play! doesn’t follow many of the J2EE conventions or standards. Instead the developers of Play! decided to make the most efficient web framework possible. It’s not a full Java Enterprise stack and Play! expressly states that it is built for web developers. This humble developer started out very skeptical that it would be useful and some of the design choices made little sense to my “Java Enterprise” mindset.

So what is Play!? – let’s take a look at what it offers at a glance.

Completely stateless – matching the HTTP standard
Non-blocking IO – reactive, fast
Hot-reload of Java classes – more on this later
Compiled views – which can be tested
Symmetrical simple routes – created with purpose and design
Both Scala and Java – Built with Scala but Java is a first class alternative
Large third-party module repository – growing daily
Commercial Support – by TypeSafe and Zenexity
Download and install the Play binary package from http://www.playframework.com/download .

To check if play is working or not, open the command prompt and type ‘play help’.

C:\>play help

you should get below screen.

Creting a new Application
step 1 : C:\>play new myFirstApp
step 2 : Confirm the application name.Press enter.
Step 3: type 2 for creating a simple java application.

Creating a working eclipse project from a play application
step 1 : cd to the new application directory
C:\>cd myFirstApp
step 2 : play
C:\myFirstApp>play
step 3 : eclipsify
[myFirstApp] $ eclipsify

Import the project in eclipse.

The app/ directory contains executables. Below are 3 packages
app/controllers
app/models
app/views

The public/ directory contains static assets. Has 3 subdirectories
images
css
javascript

The conf/ directory contains applications configuration files.There are 2 configuration files.
application.conf – standard configuration parameters.
routes – routes defination file

The lib/ directory – libraries/jar files

The project/ directory – sbt build definitions
Plugins.sbt
Build.scala – application build script

The target/ directory – contains things generated by build system

Typical .gitignore file – Generated folders should be ignored by your version control system
logs
project/project
project/target
target
tmp

To run the newly created application

[myFirstApp] $ run

On the browser address type
localhost:9000

The below screen gets displayed.

Modify the source code as below.

Application.java

package controllers;

import model.Task;
import play.*;
import play.data.Form;
import play.mvc.*;

import views.html.*;
import views.*;

public class Application extends Controller {
	
  public static Result index() {
	
	  Form<Task> form = form(Task.class);
    return ok(index.render(form));
  }
  
  public static Result sayHello() {
	  Form<Task> taskForm = form(Task.class).bindFromRequest();
	  if(taskForm.hasErrors())
		  return badRequest(index.render(taskForm));
	  else
	  {
	  Task data = taskForm.get();
	  
	  return ok(sayHello.render(data.name, data.age));
	  }
	  
		  
  }

}

Task.java

package model;

import java.util.*;
import play.data.validation.Constraints.*;
public class Task {
  
	@Required
  public String name;
  
	@Required
  public String age;
    
}
Index.scala
@(form1: Form[model.Task])
@import helper._

@main(title = "The 'helloworld' application") {
    
    <h1>Configure your 'Hello world':</h1>
  @form(action = routes.Application.sayHello, args = 'id -> "helloform") {
        
        @inputText(
            field = form1("name"),
            args = '_label -> "What's your name?"
        )
        
        @inputText(
            field = form1("age"),
            args = '_label -> "What's your age?", 'size -> 3
        )
        
         <p class="buttons">
            <input type="submit">
        <p>
       } 
}


[/sourcecode ]

You can write the code for creating form in HTML also if not in Scala.



 <form action= @routes.Application.sayHello method=GET >
  <table>
  <tr>
  <td>Name   :</td>
  <td><input type="text" name="name"   required="required" > </td></tr>
 <tr><td> Age :</td><td><input type="text" name="age" > </td></tr>

  <tr><td><input type="submit" value="Submit" /></td></tr>
      
  </table>
main.scala

@(title: String)(content: Html)
 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>@title</title>		
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
     
    </head>
    <body>
        @content 
    </body>
</html>

sayHello.scala

@(name: String, age: String)
@main("Details are : "){
<h1>Hello @name!</h1>
<h1>Your age is @age</h1> 
<a href="@{routes.Application.index()}">Back to form</a>
}
</body>
</html>

routes.conf
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Home page
GET / controllers.Application.index()

GET /sayHello controllers.Application.sayHello()
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path=”/public”, file)

Refresh the browser.
You will see the below screen.

Test your application.

Happy coding with Techartifact

XML or String comparison tutorial using google-diff-match-patch and XMLUnit API

Requirment– To compare the two xml or string and find out the difference. I need to show the difference with color as well, if it is added then its in green otherwise it should be in red.
Different from normal ADF stuff. It’s a pure java work. Let find out, how to achieve it.We can try few open source .Following are the different xml diff libraries
http://www.java-tips.org/java-libraries/xml-diff-libraries/

DIffXML-Defines an interface for comparing two XML files. It enables two XML files to be compared to check for their equivalence. It provides the objects to display the differences, if any, in a graphical format. The differences can also be represented as XSL. The corresponding XSL stylesheet with the differences can be generated as a file or an XMLDocument object. The first XML file can be transformed into the second XML file by using the XSL stylesheet generated.
XMLDiff
http://docs.oracle.com/cd/B12037_01/appdev.101/b12024/oracle/xml/differ/XMLDiff.html
First i tried DiffXML api from oracle.I read the documentation in oracle site and I am impressed with the documentation.I tried to download the jar from oracle site. I don’t know what went wrong , I found everything but no option to download.strange . 😛

After spending few hours , I drop this idea, I tried another one api i.e XMLUNIT

XMLUNIT– XMLUnit for Java provides two JUnit extension classes, XMLAssert and XMLTestCase, and a set of supporting classes (e.g. Diff, DetailedDiff,Transform,SimpleXpathEngine,Validator,NodeTest) that allow assertions to be made about:
• The differences between two pieces of XML
• The outcome of transforming a piece of XML using XSLT
• The evaluation of an XPath expression on a piece of XML
• The validity of a piece of XML
• Individual nodes in a piece of XML that are exposed by DOM Traversal

XMLUnit for Java can also treat HTML content (even badly-formed HTML) as valid XML to allow these assertions to be made about the content of web pages too. You can download the code and jar from below url – URL: http://xmlunit.sourceforge.net/

Finally ,I found the jar.I added the in the project library in build path using project properties.

I created a file called comparsionTest.java

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.List;
 
import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.Difference;
import org.custommonkey.xmlunit.XMLUnit;
import org.xml.sax.SAXException;
 
public class ComparisonTest {
 
    public static void main(String[] args) {
        URL url1 = ComparisonTest.class.getResource("comparison.xml");
        URL url2 = ComparisonTest.class.getResource("reference.xml");
        FileReader fr1 = null;
        FileReader fr2 = null;
        try {
            fr1 = new FileReader(url1.getPath());
            fr2 = new FileReader(url2.getPath());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
 
        //Add init code here.
 
        try {
            Diff diff = new Diff(fr1, fr2);
            System.out.println(diff.similar());
            System.out.println(diff.identical());
 
            DetailedDiff detDiff = new DetailedDiff(diff);
            List differences = detDiff.getAllDifferences();
            for (Object object : differences) {
                Difference difference = (Difference)object;
                System.out.println("***********************");
                System.out.println(difference);
                System.out.println("***********************");
            }
 
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Add the reference and comparison.xml in resource folder of project like

Reference.xml

<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book>
        <name>Angels &amp; Demons</name>
        <isbn>9971-5-0210-0</isbn>
        <author>Dan Brown</author>
        <category></category>
    </book>
    <book>
        <name>You can win</name>
        <isbn>9971-5-0222-0</isbn>
        <author>Vinay</author>
    </book>
</books>

comparison.xml

<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book>
        <name>Angels &amp; Demons</name>
        <isbn>9971-5-0210-0</isbn>
        <author>Dan Brown</author>
        <title>I am great</title>
        <category></category>
    </book>
    <book>
        <name>You can win</name>
        <isbn>9971-5-0222-0</isbn>
        <author>Shiv Khera</author>
    </book>
</books>

Run the file as application in eclipse and see output as below

false
false
***********************
Expected number of child nodes '11' but was '9' - comparing <book...> at /books[1]/book[1] to <book...> at /books[1]/book[1]
***********************
***********************
Expected presence of child node 'title' but was 'null' - comparing <title...> at /books[1]/book[1]/title[1] to  at null
***********************
***********************
Expected text value '
        ' but was '
    ' - comparing <book ...>
        </book> at /books[1]/book[1]/text()[5] to <book ...>
    </book> at /books[1]/book[1]/text()[5]
***********************
***********************
Expected sequence of child nodes '9' but was '7' - comparing <category...> at /books[1]/book[1]/category[1] to <category...> at /books[1]/book[1]/category[1]
***********************
***********************
Expected sequence of child nodes '10' but was '8' - comparing <book ...>
    </book> at /books[1]/book[1]/text()[6] to <book ...>
    </book> at /books[1]/book[1]/text()[5]
***********************
***********************
Expected text value 'Shiv Khera' but was 'Vinay' - comparing <author ...>Shiv Khera</author> at /books[1]/book[2]/author[1]/text()[1] to <author ...>Vinay</author> at /books[1]/book[2]/author[1]/text()[1]
***********************

Detailed explanation of xmlUnit you can found on http://technicalmumbojumbo.wordpress.com/2010/01/31/xml-comparison-tutorial-using-xmlunit/

************************************************************************************************************************************************************************

google-diff-match-patch=Compare two blocks of plain text and efficiently return a list of differences.

After trying this xmlUnit, it does not solve my purpose 🙁 .i need something else.so i found one more api google-diff-match-patch and you can
find in http://code.google.com/p/google-diff-match-patch/

create new project in eclipse and copy diff_match_patch.java from source of above code. And create your new class and implement as below

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.ListIterator;



public class LineComparsion {

public static void main(String args[]) {
	String text1 ="hello i am best. where are you";// readFile("rahul.txt");
	String text2 ="hello i am best good.i am here";
	
 compareFile(text1,text2);
System.gc();


}



private static  String compareFile(String text1,String text2) {
	
	 diff_match_patch dmp = new diff_match_patch();
	 dmp.Diff_Timeout = 0;

	 // Execute one reverse diff as a warmup.
	 LinkedList difference= dmp.diff_main(text2, text1, false);

	 ListIterator itr = difference.listIterator();

	 String abc=dmp.diff_prettyHtml(difference);
	 System.out.println(abc);
	 
	
	 
	 long start_time = System.currentTimeMillis();
	 dmp.diff_main(text1, text2, false);
	 long end_time = System.currentTimeMillis();
//	 System.out.printf("Elapsed time: %f\n", ((end_time - start_time) / 1000.0));
	return text2;
	 // Read a file from disk and return the text contents.
	
	}



private static String readFile(String filename) {
 // Read a file from disk and return the text contents.
 StringBuffer strbuf = new StringBuffer();
 try {
   FileReader input = new FileReader(filename);
   BufferedReader bufRead = new BufferedReader(input);
   String line = bufRead.readLine();
   while (line != null) {
     strbuf.append(line);
     strbuf.append('\n');
     line = bufRead.readLine();
   }
   
   bufRead.close();
  
 } catch (IOException e) {
   e.printStackTrace();
 }
 return strbuf.toString();
}
}

That all and you output in string with html tag i.e .You have use diff_prettyHtml method to get html output.there are multiple method and you can test or play with them

hello i am best good.i am where are you

If you run as html you see as below

This is really great and what I wanted. So I can say google-diff-match-patch is best api to compare string or xml with great output.

Happy coding with techartifact … 😀