Sunday, August 24, 2008

Gita Slokas


Wednesday, July 9, 2008

Thought of the day




As the candlelight flame,
Ur life may always be happiness' claim;
As the mountain high,
U move without sigh;
like the white linen flair,
Purity is always an affair;
As sunshine creates morning glory,
fragrance fills years as flory;
with the immaculate eternal smile,
attached to u mile after mile;
All darkness is far away,
As light is on its way;
Wish all of u a very happy diwali.


Monday, July 7, 2008

IT Jobs



Sapient - Bangalore

Technology professionals with minimun 4+ years of experience in areas of Java / J2EE / Asp.Net, Core Java / .Net, Moss Architect, C++, Sharepoint & ATG eCommerce.

QA professionals with minimun 4+ years of experience in areas of performance testing, QTP and Silk test.

Location Gurgaon | Noida | Bangalore

Send resumes at openings@sapient.com

Friday, July 4, 2008

Ajax Tutorial (Asynchronous Javascript And XML)





Brief history



Ajax is only a name given to a set of tools that were previously existing.

The main part is XMLHttpRequest, a class usable in JavaScript , that was implemented
into Internet Explorer since the 4.0 version.

The same concept was named XMLHTTP some times, before the Ajax name becomes commonly
used.

The use of XMLHttpRequest in 2005 by Google, in Gmail and GoogleMaps has contributed
to the success of this format. But this is the name Ajax itself that made the technology
so popular.



Why to use Ajax?



Mainly to build a fast, dynamic website, but also to save resources.

For improving sharing of resources, it is better to use the power of all the client
computers rather than just an unique server and network. Ajax allows to perform
processing on client computer (in JavaScript) with data taken from the server.


The processing of web page formerly was only server-side, using web services or
PHP scripts, before the whole page was sent within the network.

But Ajax can selectively modify a part of a page displayed by the browser, and update
it without the need to reload the whole document with all images, menus, etc...

For example, fields of forms, choices of user, may be processed and the result displayed
immediately into the same page.



What is Ajax in depth?



Ajax is a set of technologies, supported by a web browser, including these elements:


  • HTML and CSS for presenting.


  • JavaScript (ECMAScript) for local processing, and DOM (Document Object Model) to
    access data inside the page or to access elements of XML file read on the server
    (with the getElementByTagName method for example)...

  • The XMLHttpRequest class read or send data on the server asynchronously.

optionally...


  • The DomParser class may be used
  • PHP or another scripting language may be used on the server.

  • XML and XSLT to process the data if returned in XML form.

  • SOAP may be used to dialog with the server.



The "Asynchronous" word, means that the response of the server while be
processed when available, without to wait and to freeze the display of the page.



How does it works?



Ajax uses a programming model with display and events. These events are user actions,
they call functions associated to elements of the web page.

Interactivity is achieved with forms and buttons. DOM allows to link elements of
the page with actions and also to extract data from XML files provided by the server.

To get data on the server, XMLHttpRequest provides two methods:

- open: create a connection.

- send: send a request to the server.

Data furnished by the server will be found in the attributes of the XMLHttpRequest
object:

- responseXml   for an XML file or

- responseText   for a plain text.



Take note that a new XMLHttpRequest object has to be created for each new file to
load.



We have to wait for the data to be available to process it, and in this purpose,
the state of availability of data is given by the readyState attribute of
XMLHttpRequest.



States of readyState follow (only the last one is really useful):

0: not initialized.

1: connection established.

2: request received.

3: answer in process.

4: finished.





Ajax and DHTML



DHTML has same purpose and is also, as Ajax, a set of standards:

- HTML,

- CSS,

- JavaScript.

DHTML allows to change the display of the page from user commands or from text typed
by the user.

Ajax allows also to send requests asynchronously and load data from the server.



The XMLHttpRequest class



Allows to interact with the servers, thanks to its methods and attributes.



Attributes
























readyState

the code successively changes value from 0 to 4 that means for "ready".

status

200 is OK

404 if the page is not found.

responseText

holds loaded data as a string of characters.

responseXml

holds an XML loaded file, DOM's method allows to extract data.

onreadystatechange

property that takes a function as value that is invoked when the readystatechange
event is dispatched.


Methods













open(mode, url,
boolean)

mode: type of request, GET or POST

url: the location of the file, with a path.

boolean: true (asynchronous) / false (synchronous).

optionally, a login and a password may be added to arguments.

send("string")

null for a GET command.


Building a request, step by step



First step: create an instance



This is just a classical instance of class, but two options must be tried, for browser
compatibility.


if (window.XMLHttpRequest)     // Object
of the current windows

{
xhr = new XMLHttpRequest();     // Firefox,
Safari, ...

}
else
if (window.ActiveXObject)   // ActiveX version
{
xhr = new ActiveXObject("Microsoft.XMLHTTP"); // Internet Explorer

}


or exceptions may be used instead:


try {
xhr = new ActiveXObject("Microsoft.XMLHTTP"); // Trying Internet
Explorer

}
catch(e) // Failed
{
xhr = new XMLHttpRequest()
}


Second step: wait for the response



The response and further processing are included in a function and the return of
the function will be assigned to the onreadystatechange attribute of the
object previously created.


xhr.onreadystatechange = function() { // instructions to process the response };         face="arial" size="2">

if (xhr.readyState == 4)
{
// Received, OK
} else
{
// Wait...
}


Third step: make the request itself



Two methods of XMLHttpRequest are used:

- open: command GET or POST, URL of the document, true for asynchronous.

- send: with POST only, the data to send to the server.

The request below read a document on the server.


xhr.open('GET', 'http://www.xul.fr/somefile.xml', true);                  
xhr.send(null);


Examples



Get a text



<html>
<head>
<script>
function submitForm()
{
var xhr;
try { xhr = new ActiveXObject('Msxml2.XMLHTTP'); }
catch (e)
{
try { xhr = new ActiveXObject('Microsoft.XMLHTTP'); }
catch (e2)
{
try { xhr = new XMLHttpRequest(); }
catch (e3) { xhr = false; }
}
}

 xhr.onreadystatechange = function()
{
if(xhr.readyState == 4)
{
if(xhr.status == 200)
document.ajax.dyn="Received:" + xhr.responseText;
else
document.ajax.dyn="Error code " + xhr.status;
}
};

  xhr.open(GET, "data.txt", true);
xhr.send(null);
}
</script>
</head>

<body>
<FORM method="POST" name="ajax" action="">
<INPUT type="BUTTON" value="Submit" ONCLICK="submitForm()">
 <INPUT type="text" name="dyn" value="">
</FORM>
</body>
</html>


Syntax of form using Ajax



Comments on the code:



new ActiveXObject(Microsoft.XMLHTTP)

   This constructor is for Internet Explorer.



new XMLHttpRequest()

   This constructor is for any other browser including Firefox.




http.onreadystatechange


  An anonymous function is assigned to the event indicator.




http.readyState == 4

   The 4 state means for the response is ready and sent by the server.




http.status == 200 face="arial" size="2" color="#666666">

   This status means ok, otherwise
some error code is returned, 404 for example.



http.open( "POST", "data.xml", true);


   POST or GET

   URL of the script to execute.

   true for asynchronous (false for synchronous).



http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");


   This is for POST only.



http.send(document.getElementById("TYPEDTEXT").value);



   Send data to the server. Data comes from the "TYPEDTEXT"
variable filled throught the form by the user.



Get from XML



To get data from an XML file we have just to replace this line:




document.ajax.dyn="Received:" + xhr.responseText; 


by this code:


var doc = xhr.responseXML;   // Assign the XML
file to a var

var element = doc.getElementsByTagName('root').item(0);   //
Read the first element

document.ajax.dyn.value= element.firstChild.data;    //
Assign the content to the form


How to build an Ajax website?



You need for some wrapper. A short list of frameworks is provided below.

Your JavaScript program, integrated into a web page, sends request to the server
to load files for rebuilding of pages. The received documents are processed with
Dom's methods or XML parsers and the data are used to update the pages.



Drawbacks of Ajax



- If JavaScript is not activated, Ajax can't works. The user must be asked to set
JavaScript from within options of the browser, with the "noscript" tag.


- Since data to display are loaded dynamically, they are not part of the page, and
the keywords inside are not used by search engines.

- The asynchronous mode may change the page with delays (when the processing on
the server take some times), this may be disturbing.

- The back button may be deactivated (this is not the case in examples provided here). This may be overcomed.

Globalization & Localization in .Net



Introduction

The .NET Framework provides support for globalization and localization. Localization is the customization of data and resources for specific ‘locales’ or languages. Whereas in pre-Internet days it wasn’t unusual for an application to be designed for use in a single country, now an Internet application can be accessed by users from anywhere in the world.

A locale categorizes a collection of data and rules specific to a language and geographical area. These include information on sorting rules, date and time formatting, numeric and monetary conventions and symbols and character encoding. Thus you can see localization isn’t simply about language and translating the text of the user interface.

ASP.NET uses Unicode which makes our life easier. The use of Unicode with culture encodings allows us to tailor response data. The pertinent namespaces for Localization are System.Globalization and System.Threading.
Dealing with Globalization
When writing an application for multiple locations around the world you could write completely different sets of source code for each location. That wouldn’t be a very efficient approach however, with much overlap between each application. More sensibly you would write one set of source code and build into the solution the ability to customize the application for different locations. This is achieved in VB.NET via techniques such as locale-aware formatting functions and resource files.
Microsoft divides the process of preparing a ‘world ready application’ into three phases:

Globalization - is the identification of all the localizable resources in the application and separating them from the executable code so that they can be modified easily.
Localizability - is checking whether the changes needed for a given location will require design changes to the application.
Localization - is the actual customization of the application for the designated locales consisting primarily of translating resources that you identified during the globalization phase.

What will typically be localized?
The obvious item is that any text should be translated; others are:
form layouts: some languages will simply occupy more space than others on average
display formats: dates, times, currency, numbers
data input fields: e.g. zip codes (US) versus post codes (UK)
graphics with local content, e.g. maps
shortcut keys
calendars
alphabetical order
Careful judgment should be used to assess how far to proceed with localization … it can become a very time consuming and expensive enterprise where the costs start outweighing the benefits.
System.Globalization
The System.Globalization namespace provides most of the support in .NET for localization in VB.NET applications. Key concepts are cultures and resource files. A culture is an identifier for a particular locale. A resource file is a place where you can place culture-dependent resources that .NET cannot handle automatically.
Pertinent class libraries include various calendar classes including GregorianCalendar, HebrewCalendar, JulianCalendar, etc. as well as the CultureInfo class. The latter class provides all the basic functionality for changing the response encoding format, which in turn controls structures such as the language, writing systems and calendar used by a particular culture.

In particular, the CultureInfo class maintains the values for CurrentCulture and CurrentUICulture.
CurrentCulture indicates the encoding culture used for locale-dependent formatting such as with dates. CurrentUICulture indicates the encoding culture used for resource lookups.
The current settings can be obtained via the properties:
CultureInfo.CurrentCulture.EnglishName
and
CultureInfo.CurrentUICulture.EnglishName
You have a host of other information additionally at your disposal via the CultureInfo class, including date and time format, number format and calendar information. Thus you can see that use of the CultureInfo class will be key to localizing applications.
There’s one more culture to know about – the invariant culture. This is a special culture used where there is no interaction with end users: Its use can be categorized into 2 areas:
to interact with other software such as other application components
storing data in a culture-independent format
Finally, the .NET framework handles localization on a thread by thread basis where each thread has accessible CurrentCulture and CurrentUICulture properties via the Thread.CurrentThread object. I don’t want to go into further detail regarding threading in this article. Suffice to say that multithreading support is a good thing and this is why you’ll see and use references to the threading namespace in localization code.
An Example
Time for some code to reinforce the concepts introduced thus far and to see what the allied code looks like. I’ve implemented an ASP.NET web form to allow you to select a specific culture and view corresponding information about it.

In terms of setting the culture for each user you can do so based on their system configuration or you can let them explicitly set their culture. Things become a mite more complicated with ASP.NET as normally the .NET Framework would automatically default to the culture currently in use on the system on which it is running. In the web scenario of course this would be the web server on which your application is running which wouldn’t be much good!
You can however code your ASP.NET application to retrieve the user’s languages as set up in the users web browser software via request.UserLanguages with the first language of the returned array being the default language of the browser. Thus we have automated culture detection! There are a few caveats that mean you may want to opt for a non-automated culture selection solution however:
web browsers aren’t required to specify a user language when sending an HTTP request for a web page
even if a web browser specifies one or more acceptable languages there is no guarantee that any of those languages will exactly match an available culture
the user may be using a web browser whose language does not match that of the user
Working with Resource Files
The .NET framework offers support for user interface localization through the ability to select a set of user interface resources at runtime. These resources are contained in assembly resource files which are XML structured files that contain the pertinent text versions.
Visual Studio .NET provides strong support for working with resource files, allowing you to work directly with assembly resource files. I won’t go into great detail here but briefly amongst the items you may add to the project via the ‘Add New Item’ dialog are resource file templates which are created with the .resx extension. The tool that opens such resource files allows you to enter names and values to identify all of the text strings in the user interface. You would create one of these resource files for each language / culture you wish to support.
In your code you’ll need to add a reference to the System.Resources namespace. You then may create and use a ResourceManager object which allows you to retrieve text from the assembly resource files based on the current setting of CurrentUICulture. You may then use the GetString method of the ResourceManager object to retrieve the text values associated with the name you’ve previously defined.
You don’t have to use assembly resource files by the way – resources can be stored in text files as well. For those without Visual Studio .NET the .NET Framework provides the ResGen utility which takes a .txt or .resx resource file and creates a .resources file which allow direct access to resource files. If interested in exploring further take a look at the SDK documentation.
Character Encodings
There are many different approaches to representing characters of a language as numeric codes within computer memory. For example, the ASCII encoding represents common Latin characters via numeric codes 0 through 127. .NET provides support for this and other encodings via the classes of System.Text.Encoding.
Windows and .NET support Unicode encoding. The .NET Framework’s encoding of choice is UTF-16 – the 16bit Unicode standard providing the ability to represent approximately 65,000 distinct characters. Other Unicode standards are even more powerful.
Hopefully, you won’t need to worry about conversion to other encoding standards. If you do meet this as a requirement, the System.Text namespace provides a number of classes to support conversion to and from UTF-16. When might you need to look at this? If you need to connect or interact with other systems, or their data, which use more restrictive encodings.
Mirroring
A further issue that relates to localization that we haven’t covered thus far is mirroring. The requirement for mirroring occurs because some languages are read from right to left rather than left to right. Arabic is an example. Hence a key facet to mirroring is reversing the presentation of text strings. However, you’d like to reverse (or mirror) the entire UI if possible for complete consistency. .NET provides partial and indirect support for mirroring in ASP.NET via the HTML dir attribute which is simply added to the documents HTML tag.
If you do this you will see that partial mirroring occurs: controls will fill from left to right as you enter text and drop down lists, buttons, checkboxes etc. will reverse their appearance. The mirroring is imperfect however as menus and buttons won’t change position automatically and controls will not be mirrored to the opposite position on the form from their initial design. These you will have to implement in code yourself if so desired.
Validating non-Latin User Input
This is our final topic looking at globalization and localization. Further code changes may be required in dealing with different alphabets when it comes to string indexing and data sorting.
String indexing
String indexing is extracting a single character from a longer string. Remembering that our textual data is being held in UTF-16 in .NET you might think you could simply iterate through the data 16 bits at a time, treating each 16 bit sequence as separate character data for comparison. Unfortunately matters aren’t that simple.
Unicode supports surrogate pairs and combining character sequences:
A surrogate pair is a set of two 16 bit codes that represent a single character from the extended 32-bit Unicode character space.
A combining character sequence is a set of more than one 16-bit character codes that represent a single character. Typically this facility is used to combine marks such as accents with a base character.
So, how to we deal with this non-determinism? We use the inherent support provided by the .NET framework. In this instance the System.Globalization.StringInfo class is designed to be able to iterate through such string elements.
The GetTextElementEnumerator of the StringInfo class provides a mechanism for iterating through a string that properly handles both surrogate pairs and combining characters. The Current property of the class returns the single character at the current position for comparison.
Comparing and Sorting Data
Different cultures use different alphabetical orders to sort strings and different cultures also compare strings differently. Fortunately the culture aware .NET Framework will handle most situations automatically for you via the following features:
The String.Compare method compares strings according to the rules of the CultureInfo referenced by the CurrentCulture property.
The CultureInfo.CompareInfo object can search for substrings according to the comparison rules of the current culture.
The Array.Sort method sorts the members of an array by the alphabetical order rules of the current culture.
The SortKey.Compare method also compares strings according to the rules of the current culture.