Pages

Thursday, December 31, 2009

CDATA Tag in XML

It was once that I had to export the contents of all the (Action) Buttons in a lotus notes application into a DXL file, ( a Domino XML file) and parse them back to create Agents containing the same code as present in the Buttons.

The operation ran smoothly until I encountered a specific issue. And the issue was with certain characters like "<", ">", etc...

To give you a clear picture, let me say that I have a button named Source

The code in that button be,

...code fragment...
if(x<y) then
  ...do some operation
else if (x>y) then
   do some thing else
end if
...code fragment...

So my exporter shall export it into a DXL as follows (say)

<dxl>
 <buttons>
  <button>
   <name> Source </name>
   <code>
    ...code fragment...
    if(x<y) then
      ...do some operation
    else if (x>
y) then
       do some thing else
    end if
    ...code fragment...
   </code>
  <button>
 </buttons>
</dxl>


When I parse this resultant file to obtain the code, the code that has been highlighted in bold was considered as a tag accoring to the simple plain xml rule... any thing inbetween < and > is considered as a tag. And that created a lot of trouble for me when I attempted to parse the file.

So, eventually I ended up searching for a solution and ended up by discovering the usage of the CDATA tag.

Any thing that is put in between a CDATA tag is not parsed and thus it prevented my dxl from breaking up.

The bug fixed code will look like the following,

<dxl>
 <buttons>
  <button>
   <name> Source </name>
   <code>
    <![CDATA[
     ...code fragment...
     if(x<y) then
       ...do some operation
     else if (x>y) then
        do some thing else
     end if
     ...code fragment...
    ]]>

   </code>
  <button>
 </buttons>
</dxl>

Monday, December 28, 2009

Display a HTML element using javascript - A cross browser solution

function hideElement(divId) {
var element;
// get the element referenced by the parameter elementID
if (typeof elementID === "string") {
element = document.getElementById(divId);
} else {
element = divId
}

if ((typeof element == 'undefined') || (element == null)) {
throw new Error(
'No element with id "' + divId + '" is found
- in function hideElement(divId)');
}
tabContent.style.display = 'none';
}

Hide A HTML Element Using Javascript - A Cross Browser Solution

function hideElement(divId) {
var element;
// get the element referenced by the parameter elementID
if (typeof elementID === "string") {
element = document.getElementById(divId);
} else {
element = divId
}

if ((typeof element == 'undefined') || (element == null)) {
throw new Error(
'No element with id "' + divId + '" is found
- in function hideElement(divId)');
}
element.style.display = 'none';
}

Set value to a combobox - Javascript

/***********************************************************************************
@Author : Karthikeyan A
@Purpose : To set a particular value to a combo box, Provided the value is one of its options
@Type : Function
@Name : setComboValue
@Param : comboBoxId, ID of the combo box element
@Param : ValueToSet, The value to be set to the combo box
***********************************************************************************/
function setComboValue(comboBoxId,valueToSet){
var cmbElement=document.getElementById(comboBoxId);
if((typeof cmbElement=='undefined') || (cmbElement==null) || (typeof valueToSet=='undefined')) {
throw new Error('No element with id "'+ comboBoxId + '" is found <br> - in function getComboValue(comboBoxID)');
}
var cmbItr;
try {
for (cmbItr=0;cmbItr<cmbElement.options.length;cmbItr++) {
var currValue=document.all?cmbElement.options[cmbItr].text:cmbElement.options[cmbItr].value;
if (currValue==valueToSet) {
cmbElement.options.selectedIndex=cmbItr;
}
}
} catch(notCombobox) {
try {
cmbElement.value=valueToSet;
} catch (notFieldElement) {
throw new Error("The element corresponding to the id:\""+ comboBoxID+"\""+
" is neither a Combo Box, nor an Input Field or does not exist"+
"<br> - in function getComboValue(comboBoxID)");
}
}
}

Method to remove duplicate values in Array lists

/**
* Method to remove duplicate values in Array lists
* @param arrayList- The list from which the duplicates needs to be removed
* @return ArrayList
* @author karthikeyan_a
* @created 11-May-2009
*/
public ArrayList removeDuplicates(ArrayList arrayList) {

//Create a HashSet which allows no duplicates
HashSet hashSet = new HashSet(arrayList);

//Assign the HashSet to a new ArrayList
ArrayList resultArrayList = new ArrayList(hashSet) ;

//Ensure correct order, since HashSet doesn't
Collections.sort(resultArrayList);

return resultArrayList;
}

List Servers Indicated in Connection documents if Names.nsf

/**
* Method to return a list of server names present in the connection documents in names.nsf
* @param session- The Notes Session belonging to a particular user
* @return String Array
* @author karthikeyan_a
* @created 11-May-2009
* @see ArrayList removeDuplicates(ArrayList arrayList) ----> http://ozinisle.blogspot.com/2009/12/method-to-remove-duplicate-values-in.html
*/

public String[] getServerNames(Session session){
//initializing return type
String[] serverNames=null;
try {
//declaring variables and objects necessary for further manipulations
ArrayList serverNameList=new ArrayList();

Database addBook=null;
View connectionView=null;
ViewEntryCollection connectionEntries=null;
ViewEntry connectionEntry=null;
Document connectionDocument=null;

String connectionServerName=null;

int entryCount=0;
int serverNameCount=0;

//getting the handle for the names and address book in the local server
addBook=session.getDatabase("", "names.nsf");

//if the handle is not set then return the same
if (addBook==null) {
System.out.println("Names.nsf is not found");
return null;
}

// if the address book is not open before then open it again
if (!addBook.isOpen()) {
addBook.open();
}

//get the handle of the view which has the list of various connection documents in names.nsf
connectionView=addBook.getView("Adva_nced\\Connections");

//if the handle for the same is not set then return null
if (connectionView==null) {
System.out.println("Connections view is not found in names.nsf");
return null;
}

//get the handle of the collection of all entries present in the view
connectionEntries=connectionView.getAllEntries();

//if there are no entries found then return null
if (connectionEntries.getCount()==0) {
System.out.println("There are no connection documents found in names.nsf");
return null;
}
System.out.println("There are "+connectionEntries.getCount()+" connection documents found in names.nsf");

//set the handle for the first entry in the collection
connectionEntry=connectionEntries.getFirstEntry();

//loop through the entries in the collection and get the destination server's name
for (entryCount=0;entryCount<connectionEntries.getCount();entryCount++) { //start of entry collection for loop

//if the concerned entry is a document then proceed else skip
if (connectionEntry.isDocument()) {

//get the handle of the document associated with the current entry
connectionDocument=connectionEntry.getDocument();

//get the name of the server the connection document is associated with
connectionServerName=connectionDocument.getItemValueString("Destination");

//put that in the list of server names found so far
serverNameList.add(connectionServerName);
}

//proceed to the next entry in the collection
connectionEntry=connectionEntries.getNextEntry(connectionEntry);
} //end of entry collection for loop

//remove any duplicate entries present in the server list
serverNameList=removeDuplicates(serverNameList);

//convert the array list into a string array
serverNames=new String[serverNameList.size()];
for (serverNameCount=0;serverNameCount<serverNameList.size();serverNameCount++) {
serverNames[serverNameCount]=serverNameList.get(serverNameCount).toString();
}

} catch (NotesException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//return the resultant string array
return serverNames;
}

Custom Alert box in Java

/**
* Method to mimic the message box in lotus script/ javascript
* @param message- message to be displayed in the message box
* @param title- title to be displayed in the message box
* @return void
* @author Karthikeyan_A
* @since 04-May-2009
*/


public void msgbox(String message,String title) {

Object[] options = {"Ok"};
javax.swing.JFrame frame=new javax.swing.JFrame();
int n = javax.swing.JOptionPane.showOptionDialog(null,
message,
title,
javax.swing.JOptionPane.OK_OPTION,
javax.swing.JOptionPane.PLAIN_MESSAGE,
null,
options,
options[0]);
}

Capture on exit event of a TextField/JTextField in Java

/*==============================================================================
* Author : Karthikeyan A - MaargaSystems pvt.;td
* Created : 04-May-2009
* Purpose : illustrate the usage of on FoucsListener Class by Capturing on exit event of a TextField/JTextField
*==============================================================================
*/

import java.awt.BorderLayout;
import java.awt.Panel;
import java.awt.TextField;

import java.awt.Frame;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

//import javax.swing.*;
//import java.awt.*;
//import java.awt.event.*;
public class TextFieldTask {

public void onExit_TextField(TextField tf) {
tf.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent arg0) {
System.out.println("gained focus");
}

public void focusLost(FocusEvent arg0) {
System.out.println("lost focus");
msgbox("lost focus","captured on exit");

}
});
}

void msgbox(String message,String title) {

Object[] options = {"Ok"};
javax.swing.JFrame frame=new javax.swing.JFrame();
int n = javax.swing.JOptionPane.showOptionDialog(frame,
message,
title,
javax.swing.JOptionPane.OK_OPTION,
0,
null,
options,
options[0]);
}

public static void main(String args[]){
final TextFieldTask tft=new TextFieldTask();
//JPanel jp=new JPanel();
//JTextField tf1=new JTextField();
//JTextField tf2=new JTextField();

Panel p=new Panel();
TextField tf1=new TextField();
TextField tf2=new TextField();

tft.onExit_TextField(tf1);

//jp.setLayout(new BorderLayout());
//jp.add(tf1,BorderLayout.NORTH);
//jp.add(tf2,BorderLayout.SOUTH);
p.setLayout(new BorderLayout());
p.add(tf1,BorderLayout.NORTH);
p.add(tf2,BorderLayout.SOUTH);

//JFrame jf= new JFrame("Test");
//jf.add(jp);
//jf.setSize(100,100);
//jf.setVisible(true);
//jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Frame f=new Frame("test");
f.add(p);
f.setSize(100,100);
f.setVisible(true);

}
}

Create a Vector from a string array

public java.util.Vector createVectorFromStringArray( String items[]){
java.util.Vector v=new java.util.Vector();
int itr=0;
for(itr=0;itr<items.length;itr++){
v.add(items[itr]);
}
return v;
}

Method to assimilate the contents of a string Array List into a string array

<i>/**
* Method to assimilate the contents of a string Array List into a string array
* @param arrList - an array list with string objects
* @return String[]
* @author karthikeyan_a
* @since 30-April-2009
*/</i>
public String[] arrayListToStringArray(ArrayList arrList){
String[] strArray=null;
Object[] elements=arrList.toArray();
strArray=new String[elements.length];
int countItr=0;
for (countItr=0;countItr<elements.length;countItr++){
strArray[countItr]=elements[countItr].toString();
}
//recycle objects
elements=null;
arrList=null;

return strArray;
}