Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Monday, March 22, 2010

Setup decompiler for Eclipse debugging (Java, JAD & JadClipse)

Download JAD.exe and JadClipse*.jar from http://jadclipse.sourceforge.net/wiki/index.php/Main_Page

Choose a path on your local hard disk e.g. "C:\Decompiler\jad" and put jad.exe at this location

Then change environment variable "Path" to include the above path for jad.exe

Now its turn for JadClipse
put JAR file for JadClipse into eclipse's plugin directory e.g. "C:\eclipse\plugins"
Now restart Eclipse

Go to Eclipse -> Window -> Preferences -> Java ->JadClipse....
Give path to decompiler as "C:\Decompiler\jad\jad.exe"

Decompilation is all set using Eclipse plugin JadClipse

Monday, June 29, 2009

Show BUSY screen while submitting requests...Java/JS/CSS

This is two step process....You also need image "loading_bar.gif". The image I use is attached here on the right hand side (varieties can be downloaded from "http://images.google.com/")

Step 1: In you JSP ....
ONSUBMIT put this JS function call at the start....
showInProcess();
use this one to hide that BUSY SCREEN DIV
hideInProcess();
Given below is DIV code....HTML & CSS....put your path for the IMAGE......look for placeholder in code below named "IMG_PATH"

<div id="processStatus" style="position:absolute; display:none;"><img src="IMG_PATH/loading_bar.gif" ></div>
<div id="inProcess" class="opeque" style="position:absolute; border: solid #000000 1px; display:none; background:#DAEBE9;"><table border="1" width="100%" height="100%" align="center" ><tr height="1000%" ><td width="100%" colspan="3" align="center" valign="middle"> </td></tr></table></div>
CSS class opeque is defined as

.opeque{
filter:alpha(opacity=50); /* Internet Explorer */
-moz-opacity:0.5; /* Mozilla 1.6 and below */
opacity: 0.5; /* newer Mozilla and CSS-3 */
}
Step 2: In your JS file
function showInProcess(){
screenheight=window.screen.height;
screenwidth=window.screen.width;
document.getElementById('processStatus').style.top=(screenheight/2)-100;
document.getElementById('processStatus').style.left=(screenwidth/2)-50;
document.getElementById('processStatus').style.zIndex = 150;
document.getElementById('processStatus').style.display = "";
if (window.innerHeight && window.scrollMaxY) {// Firefox
screenheight = window.innerHeight + window.scrollMaxY;
screenwidth = window.innerWidth + window.scrollMaxX;
} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
screenheight = document.body.scrollHeight;
screenwidth = document.body.scrollWidth;
} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
screenheight = document.body.offsetHeight;
screenwidth = document.body.offsetWidth;
}
document.getElementById('inProcess').style.top=0;
document.getElementById('inProcess').style.left=0;
document.getElementById('inProcess').style.height=screenheight;
document.getElementById('inProcess').style.width=screenwidth;
document.getElementById('inProcess').style.zIndex = 100;
document.getElementById('inProcess').style.display = "";
}
function hideInProcess(){
document.getElementById('processStatus').style.display = "none";
document.getElementById('inProcess').style.display = "none";
}

Thursday, March 26, 2009

Java remove duplicates from Array / List

Method I

Set set = new HashSet(Arrays.asList(arr));
String[] array2 = (String[])(set.toArray(new String[set.size()]));

Method II

privateString[] removeduplicates(String[] arr) {
String[] newarr = new String[arr.length];
boolean addflag = true;
int elemcnt = 0;
for (String oldelem : arr) {
addflag = true;
for (String newelm : newarr) {
if (newelm != null && newelm.equals(oldelem))
addflag = false;
}
if (addflag) {
newarr[elemcnt] = oldelem;++elemcnt;
}
}
return newarr;
}

Java Sorting a List


  1. http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html
  2. Sample code

List e = new ArrayList(employees);

Collections.sort(e, new Comparator() {
public int compare(Employee e1, Employee e2) {
return e2.getFirstName().compareTo(e1.getFirstName());
}
}
);

Monday, January 21, 2008

Create XML doc from database and transform the same to any given template in XSLT

//Use this piece of code to generate XML file which has HEADER and BODY parts.
//Body can have many rows identified by "TR" elements
//Each TR element can have many columns e.g. CL1, CL2, CL3

The code is written using JAXP and SAX. The following imports needs to be included

import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

----------------------------------------------------------------------------

File xmlFile = new File("temp.xml");
OutputStream inputXML = new FileOutputStream(xmlFile);
StreamResult streamResult = new StreamResult(inputXML);
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();

// SAX2.0 ContentHandler.
TransformerHandler hd = tf.newTransformerHandler();
Transformer serializer = hd.getTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
hd.setResult(streamResult);
hd.startDocument();
AttributesImpl atts = new AttributesImpl();

// DOCUMENT Element
hd.startElement("", "", DOCUMENT, atts);

// Generate Header Row Elements
hd.startElement("", "", HEADER, atts);
hd.startElement("", "", HCL1, atts);
hd.characters(header.toCharArray(), 0, header.length());
hd.endElement("", "", HCL1);

hd.startElement("", "", HCL2, atts);
hd.characters(todayStr.toCharArray(), 0, todayStr.length());
hd.endElement("", "", HCL2);

hd.startElement("", "", HCL3, atts);
hd.characters(lastHdr.toCharArray(), 0, lastHdr.length());
hd.endElement("", "", HCL3);
hd.endElement("", "", HEADER);

// Generate actual data Elements within Body Elements
hd.startElement("", "", BODY, atts);

//Put this code into a loop - for or while till u have row/tuples from DB
// Content Row Elements
hd.startElement("", "", TR, atts);

hd.startElement("", "", CL1, atts);
hd.characters("ABC".toCharArray(), 0, "ABC".length());
hd.endElement("", "", CL1);

hd.startElement("", "", CL2, atts);
hd.characters("XYZ".toCharArray(), 0,"XYZ".length());
hd.endElement("", "", CL2);

hd.startElement("", "", CL3, atts);
hd.characters("PQR".toCharArray(), 0,"PQR".length());
hd.endElement("", "", CL3);
hd.endElement("", "", TR);
//Loop ends here

hd.endElement("", "", BODY);

hd.endElement("", "", DOCUMENT);
hd.endDocument();

//Now the following piece of code transforms above generated XML file into Output file with given template file

Source xmlSource = new StreamSource(xmlFile);
File finalOutput = new File("Output.doc");
OutputStream out = new FileOutputStream(finalOutput);
Result result = new StreamResult(out);
TransformerFactory transFact = TransformerFactory.newInstance();
Transformer trans;
InputStream xsltFile = null;
xsltFile = getXSLTInput(); //Get your input template file
Source xsltSource = new StreamSource(xsltFile);
trans = transFact.newTransformer(xsltSource);
trans.transform(xmlSource, result);


Function to read input template file as class via classLoader

private InputStream getXSLTInput() {
InputStream xsltFile = null;
ClassLoader cl = CreateXMLDoc.class.getClassLoader();
if (cl != null) {
xsltFile = cl.getResourceAsStream(XSLTTemplateFile.xsl);
}
if (xsltFile == null) {
cl = Thread.currentThread().getContextClassLoader();
if (cl != null) {
xsltFile = cl.getResourceAsStream(XSLTTemplateFile.xsl);
}
}
if (xsltFile == null) {
cl = ClassLoader.getSystemClassLoader();
if (cl != null) {
xsltFile = cl.getResourceAsStream(XSLTTemplateFile.xsl);
}
}
return xsltFile;
}

Ajax Components

1) Div pop-up relative to link.
divId - Id for the Div to be used as popup
lnk - Id for the link to be used as reference for popup position
xOffset - xOffset from the topleft of the ref link
yOffset - yOffset from the topleft of the ref link
function showPopUp(dispatch,lnk,xOffset, yOffset)

function showPopUp(divId,lnk,xOffset, yOffset)
{
var el = document.getElementById(lnk);
var ol=el.offsetLeft;
while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
var el = document.getElementById(lnk);
var ot=el.offsetTop;
while((el=el.offsetParent) != null) { ot += el.offsetTop; }

var x = ol+xOffset;
var y = ot+yOffset;
showMe(divId,x,y);
}


2) Div pop-up absolute.
divId - Id for the Div to be used as popup
xOffset - xOffset from the topleft
yOffset - yOffset from the topleft
function showMe(dispatch,xOffset,yOffset)

function showMe(divId,xOffset,yOffset){
var elm = document.getElementById(divId);
elm.style.top=yOffset;
elm.style.left=xOffset;
elm.style.display="";
elm.style.zIndex = 200;

}

3) Hide the div .
divid - Id for the div.
function hideMe(divid)

function hideMe(divid){
document.getElementById(divid).style.display="none";
}

4) Make an ajax request
url - URL of the server resource
data - Data to be used in case of post else empty string. Data passed is in the form of querystring.
method - get or post for ajax request submission
function serverData(url,data,method)

function serverData(url,data,method)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}

try {
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open(method,url,true);
xmlHttp.send(data);
showInProcess()
} catch (e) {
alert("Error Sending the Ajax Request."+e);
document.getElementById('inProcess').style.display = "none";
document.getElementById('processStatus').style.display = "none";
document.getElementById('result').style.display = "none";
}
}

5) Default response method which calls onAjaxResponse(responseText) on xmlHttp.readyState==4 and http_request.status == 200
function stateChanged()

function stateChanged()
{
if (xmlHttp.readyState==4)
{
if (xmlHttp.status == 200) {
onAjaxResponse(xmlHttp.responseText);
document.getElementById('inProcess').style.display = "none";
document.getElementById('processStatus').style.display = "none";
document.getElementById('result').style.display = "none";
} else {
alert('There was a problem with the request.');
document.getElementById('inProcess').style.display = "none";
document.getElementById('processStatus').style.display = "none";
document.getElementById('result').style.display = "none";
}
}
}

6) Gets the XmlHttp object based on the browser
function GetXmlHttpObject()

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

7) Shows the inProcessScreen
function showInProcess()

function showInProcess()
{
screenheight=window.screen.height;
screenwidth=window.screen.width;

document.getElementById('processStatus').style.top=(screenheight/2)-150;
document.getElementById('processStatus').style.left=screenwidth/2-100;
document.getElementById('processStatus').style.zIndex = 150;
document.getElementById('processStatus').style.display = "";


if (window.innerHeight && window.scrollMaxY) {// Firefox
screenheight = window.innerHeight + window.scrollMaxY;
screenwidth = window.innerWidth + window.scrollMaxX;
}
else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
screenheight = document.body.scrollHeight;
screenwidth = document.body.scrollWidth;
} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
screenheight = document.body.offsetHeight;
screenwidth = document.body.offsetWidth;
}
document.getElementById('inProcess').style.top=0;
document.getElementById('inProcess').style.left=0;
document.getElementById('inProcess').style.height=screenheight;
document.getElementById('inProcess').style.width=screenwidth;

document.getElementById('inProcess').style.zIndex = 100;
document.getElementById('inProcess').style.display = "";

}

8) Hides the inProcessScreen
function hideInProcess()

function hideInProcess()
{
document.getElementById('processStatus').style.display = "none";
document.getElementById('inProcess').style.display = "none";
}

9) Initializes the in process screen
processingColor - Color for the processing text message
filmColor - Color for the processing film to protect parent screen activities during processing
processingText - Color of the Text for Processing message
processingImgSrc - processing Img
function initScreenBlur(processingColor,filmColor,processingText,processingImgSrc)

function initScreenBlur(processingColor,filmColor,processingText,processingImgSrc)
{
document.write("");
document.write("");
document.write("");
document.write("");
}

10) Replaces the processing text message and image with the specified content
function replaceProcessingText(replaceText)

function replaceProcessingText(replaceText)
{
document.getElementById('processStatus').style.display = "none";
document.getElementById('result').style.top=document.getElementById('processStatus').style.top;
document.getElementById('result').style.left=document.getElementById('processStatus').style.left;
document.getElementById('result').style.zIndex = 150;

document.getElementById('result').innerHTML=replaceText;
document.getElementById('result').style.display = "";
}

usage
--------
initScreenBlur("red","gray","PROCESSING...","processing1.gif")
function onAjaxResponse(responseText)
{
//add your code here
}

test.jsp
---------
<% Thread.sleep(5000);//Some processing %>

This is the Ajax response