executing Javascript in Java in Oracle ADF

Requirment- Sometime we want to execute javascript from Java in ADF.How to achieve this.below code tell you about this.

import javax.faces.context.FacesContext;
import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
import org.apache.myfaces.trinidad.util.Service;

FacesContext fctxObj = FacesContext.getCurrentInstance();
ExtendedRenderKitService eRKS =
Service.getRenderKitService(fctxObj , ExtendedRenderKitService.class);
String javaScriptCode = "alert( "+"Het you executed javascript in Java .Hurray vinay"+");";
eRKS.addScript(fctxObj , javaScriptCode );


Thats it. enjoy. Happy coding with Techartifact with Vinay Kumar….. πŸ™‚

Why we can’t override the static method? or Overriding Vs Hiding

Can I override a static method? – No you cant

Many people have heard that you can’t override a static method. This is true – you can’t. However it is possible to write code like this:

class test {
    public static void method() {
        System.out.println("in test");
    }
}

class example extends test{
    public static void method() {
        System.out.println("in example");
    }
}

This compiles and runs just fine. Isn’t it an example of a static method overriding another static method? The answer is no – it’s an example of a static method hiding another static method. If you try to override a static method, the compiler doesn’t actually stop you – it just doesn’t do what you think it does.

lets try it

Briefly, when you override a method, you still get the benefits of run-time polymorphism, and when you hide, you don’t. So what does that mean? Take a look at this code:

class test{
    public static void classMethod() {
        System.out.println("classMethod() in test");
    }

    public void instanceMethod() {
        System.out.println("instanceMethod() in test");
    }
}

class example extends test {
    public static void classMethod() {
        System.out.println("classMethod() in example");
    }

    public void instanceMethod() {
        System.out.println("instanceMethod() in example");
    }
}
 
class Test {
    public static void main(String[] args) {
        test f = new example();
        f.instanceMethod();
        f.classMethod();
    }
}



If you run this, the output is

instanceMethod() in example
classMethod() in test

Now we should understand what is overriding and hiding-

Overriding
An instance method overrides all accessible instance methods with the same signature in superclasses [JLS 8.4.8.1], enabling dynamic dispatch; in other words, the VM chooses which overriding to invoke based on an instance’s run-time type [JLS 15.12.4.4]. Overriding is fundamental to object-oriented programming and is the only form of name reuse that is not generally discouraged:

class Base {
    public void f() { }
}

class Derived extends Base {
    public void f() { } // overrrides Base.f()
}

Hiding
A field, static method, or member type hides all accessible fields, static methods, or member types, respectively, with the same name (or, for methods, signature) in supertypes. Hiding a member prevents it from being inherited.

class Base {
    public static void f() { }
}

class Derived extends Base {
    public static void f() { } // hides Base.f()
}

Instance methods and class methods have this important difference in behavior, we use different terms – “overriding” for instance methods and “hiding” for class methods – to distinguish between the two cases. And when we say you can’t override a static method, what that means is that even if you write code that looks like it’s overriding a static method (like the first test and example at the top of this page) – it won’t behave like an overridden method.

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 … πŸ˜€