Java Converting String to InputStream

This is the short tip of the day, this tip can be helpful to you if you are wishing, for example, to parse an XML String using the Java DOM APIs or in general if you are working with an API that does not expose an interface to accept the content as a String and it exposes an interface that accepts an input stream.

Using the class you can convert a String object to a byte array input stream as follows:

InputStream istream = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));

Generating Test Reports using JsTestDriver

JsTestDriver is one of the most powerful JavaScript unit testing tools. I wrote an article in developerWorks about efficient JavaScript unit testing using JsTestDriver, please read first it before reading this post.

In this post, I will show you how to generate test coverage reports using the JsTestDriver. First of all, Let’s understand what is meant by “Test Coverage”. “Test Coverage” is one of the measures of software testing. It describes how much the source code of the program is tested.

The coverage criteria can be on the function level (Function coverage), on the statement level (Statement coverage), and on the code branch level (Branch coverage).

JsTestDriver has a plugin that can generate code coverage for the JavaScript code. To configure this plugin, you should:

  • Download the “coverage.jar” from
  • Add the coverage plugin declaration to the configuration file (jsTestDriver.conf file) as follows:

    - name: "coverage" 
      jar: "plugins/coverage.jar" 
      module: ""
  • Specify the --testOutput [output_folder] flag in the test running command. This will get the test coverage files in the output_folder you specified.

Currently, The JsTestDriver generates the test coverage files in LCOV format. Unfortunately, JsTestDriver does not generate HTML reports directly as shown in the figure below.
Genarated LCOV files

To generate the HTML reports from the LCOV files, you can use the LCOV visualizer, check it here:

The LCOV visualizer tool is working on Linux Redhat environment. To generate the report from the tool, execute this command on the jsTestDriver.conf-coverage.dat file:

genhtml jsTestDriver.conf-coverage.dat

For more information about how to install the LCOV visualizer on Linux Redhat environment, and converting the LCOV files to HTML reports:

Finally for your reference, You can download the project from here, and the LCOV test coverage files from here.

I wish that this tip can be useful.

How to Create a Custom Dojo Component

I published an article about how to create custom Dojo components in Wazi. This article shows you how to create a custom Dojo component from scratch. It illustrates very important concepts like the dojo attach point, the widget structure, and the widget template. I wish you will enjoy this article:

Iterate over a JSON object

One of the most useful advantages of the JSON format is that it is browser friendly. You donot need to write complicated code for navigating or iterating over a JSON structure. In this post, I will show you how to iterate over a JSON object.

Assume that you are having the following JSON object.

<script type="text/javascript">
var personStr = "{\"Name\":\"Hazem Saleh\",\"Occupation\":\"Software Engineer\",\"Nationality\":\"Egyptian\",\"Age\":\"102\"}";

var personObject = eval("(" + personStr + ")");

You may notice that I put the JavaScript String between parentheses. This is mandatory to avoid the json invalid label browser error.

if you want to simply iterate over the JSON object you should use the standard JavaScript for in loop as follows.

<script type="text/javascript">
for (var key in personObject) {
  if (personObject.hasOwnProperty(key)) {
    alert(key + " -> " + personObject[key]);

I wish that this tip can be helpful to you.

Java Web Development Introductory Course For Beginners Slides

I had written a set of slides to be an introduction for guys who wish to start writing web applications with Java.

These slides cover the following topics in a simple way:

1. The Servlet Model.

2. Web Applications Structure.

3. The Web container Model.

4. JSP.

5. JSP Standard Actions, EL.

6. JSTL.

7. Classic Tag Libraries.

You will find samples for each lesson in the zip file. I wish you will learn from this course.

Java encoding similiar to JavaScript encodeURIComponent

You may have a REST service that returns an encoded output to a JavaScript client which decodes this output in the client side. Unfortunately, this scenario will not always work because of the mismatch between both the Java encode() API and the JavaScript encodeURIComponent().

To be able to make the encode() behavior equivalent to the JavaScript encodeURIComponent(), all what you should do is the following String replacements.

public static String encodeURIComponent(String component)   {     
	String result = null;      
	try {       
		result = URLEncoder.encode(component, "UTF-8")   
			   .replaceAll("\\%28", "(")                          
			   .replaceAll("\\%29", ")")   		
			   .replaceAll("\\+", "%20")                          
			   .replaceAll("\\%27", "'")   			   
			   .replaceAll("\\%21", "!")
			   .replaceAll("\\%7E", "~");     
	catch (UnsupportedEncodingException e) {       
		result = component;     
	return result;   

After that you can safely decode the output in the client side using the JavaScript decodeURIComponent().


CRIMC1017E Failed to locate ‘ {0}’ resolution

You may face this error when you are trying to install the IBM WebSphere Integration Designer V7.5.

CRIMC1017E: Failed to locate 
'zip org.eclipse.equinox.p2.bootstrap.repository.win32.win32.x86

This problem happens because the launcher could not find a specific jar file.

To resolve this error, just place all the extracted zip files of the setup source under the first folder "IID_V7.5_1_of_2" as shown in the figure below.

Hopefully, this tip can save you some time.

Refreshing Dijit tree in Dojo (code that works)

Refreshing the Dijit tree in Dojo with a new data store does not have a ready-made API in Dojo (Iam talking about Dojo 1.4). In this post, I will show a technique with example that will illustrate how to refresh the Dijit tree with a new data store.

The solution is about extending the dijit.Tree to support this feature as shown in the refreshModel method.

<script type="text/javascript">// <![CDATA[
	dojo.extend(dijit.Tree, {
		refreshModel: function () {

			// reset the itemNodes Map
			this._itemNodesMap = {};

			// reset the state of the rootNode
			this.rootNode.state = "UNCHECKED";

			// Nullify the tree.model's root-children
			this.model.root.children = null;

			// remove the rootNode
			if (this.rootNode) {

			// reload the tree
// ]]></script>

Let’s see now an example of refreshing the tree using this new introduced API.


<button id="refreshTreeButton" class="buttonClass" onclick="refreshTreeInformation();" type="button">Refresh Tree</button><script type="text/javascript">// <![CDATA[
	dojo.addOnLoad(function() {	
		var store = new{
			url: "sampleData.json"

		var treeModel = new dijit.tree.ForestStoreModel({
			store: store,
			query: {"type": "parent"},
			rootId: "root",
			rootLabel: "Groups",
			childrenAttrs: ["children"]

		var treeObject = new dijit.Tree({
			model: treeModel
		}, "treeDiv");

	function refreshTreeInformation() {		
		var treeObject = dijit.byId("treeDiv");
		var newStore = new{
			url: "sampleData2.json"
		}); = newStore;		
		treeObject.model.query = {"type": "parent"};	
		treeObject.rootId = "root";	
		treeObject.rootLabel = "Groups";	
		treeObject.childrenAttrs = ["children"];						

// ]]></script>

In the refreshTreeInformation function, the tree object is retrieved, a new store is loaded, and assigned to the tree model object. Finally calling the refreshModel method will make the displayed tree insync with the underlying model. you can download the code from here.

I wish this tip can help.

Getting the Selected Dijit Tree node in Dojo

This post shows you how to get the selected node in a Dijit tree.

Let’s assume we have the following dijit tree data store.

	"type": "parent",
	"name": "Countries",
	"id": "countries",
		{"type":"Leaf", "name":"Country 1", "id":"country1"},
		{"type":"Leaf", "name":"Country 2", "id":"country2"},
		{"type":"Leaf", "name":"Country 3", "id":"country3"},
		{"type":"Leaf", "name":"Country 4", "id":"country4"},
		{"type":"Leaf", "name":"Country 5", "id":"country5"},
		{"type":"Leaf", "name":"Country 6", "id":"country6"}

Using the following JavaScript code the tree is loaded with the desired hierarchy.

<script type="text/javascript">// <![CDATA[

var store = new{
	url: "sampleData.JSON"

var treeModel = new dijit.tree.ForestStoreModel({
	store: store,
	query: {"type": "parent"},
	rootId: "root",
	rootLabel: "Groups",
	childrenAttrs: ["children"]

var treeObject = new dijit.Tree({
	model: treeModel
}, "treeDiv");

// ]]></script>

In order to get the selected tree node object you should use the “selectedItem” attribute in the Dijit tree object as follows

dijit.byId("[Tree Node ID]").attr("selectedItem");

In our example to show the id of the selected leaf node when clicking the “Get Selected Node” button, the following code will do the job:

if (dijit.byId("treeDiv").attr("selectedItem") != null) {
	if (dijit.byId("treeDiv").attr("selectedItem").type == "Leaf") {
		document.getElementById("selectedNode").innerHTML =
                "Selected Node is " + dijit.byId("treeDiv").attr("selectedItem").id;

This code is tested in Dojo 1.4 environment. download the code from here.

Accessing parent window elements from child window in JavaScript

In JavaScript you can access the parent window elements from the child window. Let’s see an example. Let’s assume that the parent page code is as follows:

        <title>Parent Window</title>
	<input type="text" id="data" value="23444" />
	<a href="#" onclick="javascript:openChildWindow();">Open Child Popup window</a>
	function openChildWindow() {'child.htm','childWindow','width=400,height=400');		

The hyperlink in the parent window opens a new child window using the JavaScript API.

In the child window, Let’s see how to access the "data" input field of the parent window.

        <title>Child Window</title>
    <body onload="initializeMainDiv();">   
		<div id="mainDiv"></div>
	function initializeMainDiv() {
		document.getElementById("mainDiv").innerHTML = "Parent window data field value is: " + 

In the child window, when the page is initialized, the "data" input field of the parent window is accessed through the window.opener JavaScript API.