Tuesday, June 22, 2010

jQuery: By Examples - Link Preview

About the example:
When a user hovers over a link, sneak preview of the link pops up. And when user hovers over a thumbnail, original size image pops up.

Link preview has been implemented through Ajax.load() function, where data for the link is loaded from one of the older posts. To know more about Ajax.load() function checkout
jQuery: By examples - Load data through Ajax


Demo:


Click the link to show code fragment



Monday, June 21, 2010

jQuery: By examples - Load data through Ajax -1

 This is another example in the series of jQuery: By Examples. In this example I will load data from one of the posts. Instead of loading complete post (including title) I will load data from a specific portion of the page. Following functions/methods are used in this example:
  1. .load( url, [ data ], [ complete(responseText, textStatus, XMLHttpRequest) ] )
  2. .hide()
  3. .appendTo()
  4. slideDown()
.load() method is the simplest way to get data from the server. Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol. Using the special syntax for url, you can get data from specif portion of the remote document. If the url has one or more spaces, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded. For example:
  • .load('http://myerrorslog.blogspot.com/2006/12/introduction.html div[class="post-body entry-content"]')
In this case I am fetching data from the DIV element whose CLASS attribute is equals to post-body entry-content,  in the remote document - http://myerrorslog.blogspot.com/2006/12/introduction.html.

Demo:

Click the link to show code fragment



Wednesday, June 9, 2010

Recursive call through arguments.callee

Is it possible for an anonymous function to refer itself in javascript?
When I read the question for the first time even I was totally blank. The very next thought came to my mind was "Is it really possible?".
Well the answer is YES. It is possible for an anonymous function to refer itself through the use of arguments.callee.
arguments is a local variable for a function object. callee is one of the properties of arguments. arguments.callee allows anonymous function to refer to itself, which is necessary for recursion. Here is an example:

Example: In this example, I will use arguments.callee within an anonymous function which is used as a callback for show() and hide() methods.

Demo:

Show the API list

  • ID Selector ("#id")
    Select an element based on the id value. For id selectors, jQuery uses the JavaScript function document.getElementById(), which is extremely efficient. When another selector is attached to the id selector, such as h2#pageTitle, jQuery performs an additional check before identifying the element as a match.
  • :first Selector
    Selects the first matched element. The :first pseudo-class is equivalent to :eq(0). It could also be written as :lt(1). While this matches only a single element, :first-child can match more than one: One for each parent.
  • :last Selector
    Selects the last matched element. Note that :last selects a single element by filtering the current jQuery collection and matching the last element within it.
  • Mouse Event - click()
    Bind an event handler to the "click" JavaScript event, or trigger that event on an element. The click event is sent to an element when the mouse pointer is over the element, and the mouse button is pressed and released. Any HTML element can receive this event.
  • show(duration, [ callback ] )
    Display the matched elements. When a duration is provided, .show() becomes an animation method. The .show() method animates the width, height, and opacity of the matched elements simultaneously. If supplied, the callback is fired once the animation is complete.
  • hide(duration, [ callback ] )
    Hide the matched elements. When a duration is provided, .hide() becomes an animation method. The .hide() method animates the width, height, and opacity of the matched elements simultaneously. When these properties reach 0, the display style property is set to none to ensure that the element no longer affects the layout of the page. If supplied, the callback is fired once the animation is complete.

Click the link to show code fragment



Tuesday, June 8, 2010

jQuery: By examples

I have been playing around with jQuery from last few days. And I AM LOVING IT. Well still I am in the learning phase, but I thought lets share what I have learnt so far. And the best way to share the knowledge is though EXAMPLES. So here is the first set of examples:

Example: In this example I will flip a box.

Demo: Click on the box to flip it

eM pliF
Filp Me





APIs used in the example (this section is also implemented through jQuery)

  • ID Selector ("#id")
    Select an element based on the id value. For id selectors, jQuery uses the JavaScript function document.getElementById(), which is extremely efficient. When another selector is attached to the id selector, such as h2#pageTitle, jQuery performs an additional check before identifying the element as a match.
  • :first Selector
    Selects the first matched element. The :first pseudo-class is equivalent to :eq(0). It could also be written as :lt(1). While this matches only a single element, :first-child can match more than one: One for each parent.
  • :last Selector
    Selects the last matched element. Note that :last selects a single element by filtering the current jQuery collection and matching the last element within it.
  • Mouse Event - click()
    Bind an event handler to the "click" JavaScript event, or trigger that event on an element. The click event is sent to an element when the mouse pointer is over the element, and the mouse button is pressed and released. Any HTML element can receive this event.
  • DOM Removal - remove()
    Remove the set of matched elements from the DOM. We use .remove() when we want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed.
  • DOM Insertion - appendTo()
    Insert every element in the set of matched elements to the end of the target. With .appendTo(), the content precedes the method, either as a selector expression or as markup created on the fly, and it is inserted into the target container.

Click the link to show code fragment




Friday, March 27, 2009

How to Unzip a File

Follow the steps given below to Unzip a file on Server:

1. Create a File object from the uplodaded data - If you have used Struts framwork to upload a Zip file, then first get the FormFile object (org.apache.struts.upload.FormFile).

FormFile formFile = form.getFileProperties();

From FormFile object get the InputStream.

InputStream inputStream = formFile.getInputStream();

In case we are getting the data within a hidden field in base-64 / zipped format, FormFile is null. In that case get the byte[] data using: sun.misc.BASE64Decoder().decodeBuffer() method if Enctype is *B64*

if(form.getEnctype().equals("*B64*"))
byte[] rawData = sun.misc.BASE64Decoder().decodeBuffer(form.Data());


Create ByteArryaIntputStream from byte[] data.

InputStream inputStream = new ByteArrayInputStream(rawData);

Now create a File object in which you want to read the InputStream.

File fileObj = new File("test.zip");

Create a FileOutputStream from the File object.

OutputStream outputStream = new FileOutputStream(fileObj);

Now read the IntputStream and write it to OutputStream.

2. Once the File object is created, next is to create a ZipFile (java.util.zip.ZipFile) object from it.

ZipFile zipFile = new ZipFile(fileObject);

3. Iterate through the ZipFile to read the zip and extract its contents.

Below is the complete soure code.

import org.apache.struts.upload.*;

import java.util.*;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
import java.io.*;

public class unzipUtility
{
public static final String B64String = "*B64*";

public void unzipFile(ActionForm form)
{
InputStream inputStream = null;
OutputStream outputStream = null;

File fileObj = new File("test.zip");

FormFile formFile = form.getFormFile();// not the standard method

if(formFile!=null)
{
inputStream = formFile.getInputStream();
}
else
{
byte[] rawData = null;

if(form.getEnctype().equals(B64String))
{
rawData = new sun.misc.BASE64Decoder().decodeBuffer(form.getData());
}

inputStream = new ByteArrayInputStream(
rawData);
}

outputStream = FileOutputStream(fileObj);
copyStream(inputStream, outputStream);
}

private byte[] getRawData(String content)
{

}// getRawData()

private void copyStream(InputStream input, OutputStream output)
{
int size;
byte[] buffer = new byte[1024];

while((size=input.read(buffer))>=0)
{
output.write(buffer, 0, size);
}//while

input.close();
output.close();
}//copyStream()

public void unzip(File fileObj)
{
Enumeration entries;
ZipFile zipFile;

try {
zipFile = new ZipFile(fileObj);

entries = zipFile.entries();

while(entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry)entries.nextElement();

if(entry.isDirectory()) {
// Assume directories are stored parents first then children.
System.err.println("Extracting directory: " + entry.getName());
// This is not robust, just for demonstration purposes.
(new File(entry.getName())).mkdir();
continue;
}

System.err.println("Extracting file: " + entry.getName());
copyInputStream(zipFile.getInputStream(entry),
new BufferedOutputStream(new FileOutputStream(entry.getName())));
}

zipFile.close();
} catch (IOException ioe) {
System.err.println("Unhandled exception:");
ioe.printStackTrace();
return;
}
}

}

Tuesday, February 24, 2009

Unable to run multiple instances of WEBLOGIC on the same machine

I have been facing this issue from couple of days. Searched alot on the net also, but not able to get any satisfactory results. What ever i have got was in bits-n-pieces. From hit and trial I have come up with the following solution:

Friday, May 25, 2007

What is Encapsulation?

Encapsulation refers to the bundling of data with the methods that operate on that data. Often that definition is misconstrued to mean that the data is somehow hidden. In Java, you can have encapsulated data that is not hidden at all.

Encapsulation or equivalently information hiding refers to the practice of including within an object everything it needs, and furthermore doing this in such a way that no other object need ever be aware of this internal structure.