Monday, June 30, 2008
Simple RSS Reader
<html> <title>RSS Reader</title> <head> <script language="JavaScript">
var xmlHttp = null; var t_channel = 'channel'; var t_title = 'title'; var t_link = 'link'; var t_description = 'description';
var t_pubDate = 'pubDate' var t_lastBuildDate = 'lastBuildDate'
var t_item = 'item';
var t_tableHTML;
var objChannelNodeList;
// messages var NOT_A_VALID_RSS_FEED = 'Not a valid RSS Feed'; var GOT_ERROR = 'Got error please try again'; var GETTING_DATA = 'Getting Data Please wait';
// colors var CLR_PROCESSING="orange"; var CLR_ERROR="red"; var CLR_SUCCESS="green"; var CLR_BORDER="skyblue";
function getFirstChildText(objNode,tag){ if(objNode.getElementsByTagName(tag).length>0){ return objNode.getElementsByTagName(tag)[0].firstChild.nodeValue; }else{ return ' '; } }
function getRSSData(){ try{ if(xmlHttp.readyState==4){ t_tableHTML = '<table cellspacing="0" cellpadding="3" border="1" bordercolor='+CLR_BORDER+'>'; var resXML = xmlHttp.responseXML; if(!resXML){ throw NOT_A_VALID_RSS_FEED; } var objChannelNodeList = resXML.getElementsByTagName(t_channel); if(objChannelNodeList.length==0){ throw NOT_A_VALID_RSS_FEED; }
for(i=0;i<objChannelNodeList.length;i++){
var titleText = getFirstChildText(objChannelNodeList[i],t_title); var linkText = getFirstChildText(objChannelNodeList[i],t_link); var descText = getFirstChildText(objChannelNodeList[i],t_description); var publishDate = getFirstChildText(objChannelNodeList[i],t_pubDate); if(publishDate==''){ publishDate = getFirstChildText(objChannelNodeList[i],t_lastBuildDate); } if(publishDate!=''){ titleText = titleText + '[ <font color='+CLR_SUCCESS+'>' + publishDate + '</font> ]'; } constructRows(titleText,linkText,descText); for(j=0;j<objChannelNodeList[i].getElementsByTagName(t_item).length;j++){ var itemTitleText = getFirstChildText(objChannelNodeList[i].getElementsByTagName(t_item)[j],t_title); var itemLinkText = getFirstChildText(objChannelNodeList[i].getElementsByTagName(t_item)[j],t_link); var itemDescText = getFirstChildText(objChannelNodeList[i].getElementsByTagName(t_item)[j],t_description); constructRows(itemTitleText,itemLinkText,itemDescText);
}// end of : item for }// end of channel for
t_tableHTML = t_tableHTML + '</table>';
setText(t_tableHTML);
}else{// end of xml http ready state gettingDataPleaseWait(xmlHttp.readyState); } }catch(e){ gotErrorPleaseTry(e); } } function gettingDataPleaseWait(state){ t_tableHTML = '<table cellspacing="0" cellpadding="0" border="0">'; var t_txtMsg = GETTING_DATA; for(msgDots=0;msgDots<state;msgDots++){ t_txtMsg = t_txtMsg + ' ...'; } t_tableHTML = t_tableHTML + '<tr><td><font color='+CLR_PROCESSING+'>' + t_txtMsg + '</font></td></tr>'; t_tableHTML = t_tableHTML + '</table>';
setText(t_tableHTML); } function gotErrorPleaseTry(e){ t_tableHTML = '<table cellspacing="0" cellpadding="0" border="0">'; t_tableHTML = t_tableHTML + '<tr><td><font color='+CLR_ERROR+'>'; if(e){ if(e.message){ t_tableHTML = t_tableHTML + GOT_ERROR + ' [<b>' + e.message + '</b>]'; }else{ t_tableHTML = t_tableHTML + GOT_ERROR + ' [<b>' + e + '</b>]'; } }else{ t_tableHTML = t_tableHTML + GOT_ERROR; } t_tableHTML = t_tableHTML + '</font></td></tr>'; t_tableHTML = t_tableHTML + '</table>';
setText(t_tableHTML); } function constructRows(itemTitleText,itemLinkText,itemDescText){ t_tableHTML = t_tableHTML + '<tr>'; t_tableHTML = t_tableHTML + '<td><a href="' + itemLinkText + '">' + itemTitleText + '</a></td>'; if(!document.theForm.chkDebug.checked){ // trim images var t_tempDes = new String(itemDescText); itemDescText = t_tempDes.replace('<img','<imge'); }
t_tableHTML = t_tableHTML + '<td>' + itemDescText + '</td>'; t_tableHTML = t_tableHTML + '</tr>'; } function setText(strHtmlText){
document.getElementById('DisplayArea').innerHTML=''; document.getElementById('DisplayArea').innerHTML=strHtmlText;
} function startProcess() { try{ gettingDataPleaseWait(1); var runningUserAgent = new String(navigator.userAgent);
var URL=''; if(document.theForm.selURL.selectedIndex>0){ URL = document.theForm.selURL[document.theForm.selURL.selectedIndex].value; }else{ URL = document.theForm.txtURL.value; } // append some random number to avoid cache if(URL!=''){ URL = new String(URL) var curDate = new Date(); if(URL.indexOf("?")!=-1){ URL = URL + '&date=' + curDate.getTime(); }else{ URL = URL + '?date=' + curDate.getTime(); } }else{ throw 'Invalid URL' }
if(runningUserAgent.indexOf('MSIE')!=-1){//IE xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); if(!xmlHttp){ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } else if(runningUserAgent.indexOf('Firefox')!=-1){ netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); xmlHttp=new XMLHttpRequest(); }else{ throw 'Currently supports only IE or Firefox' }
xmlHttp.open("GET",URL,true); xmlHttp.send(null);
if(xmlHttp!=null){ xmlHttp.onreadystatechange=getRSSData; }
}catch(e){ gotErrorPleaseTry(e) } }// end of : ajaxFunction function </script> </head> <body> <form name="theForm"> <div id="RSS Source Area">
<table cellspacing="0" cellpadding="0" border="0"> <tr> <td align="left"> <select name="selURL"> <option value="">Select RSS Feed</option> <option value="http://www.ibnlive.com/xml/top.xml">IBN Top Stories</option> <option value="http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/latest_published_stories/rss.xml">BBC Latest</option>
<option value="http://www.ndtvprofit.com/RSS/SectionRssfeed.aspx?ts=Markets">NDTV Profit Business</option> </select> </td> <td> or </td> <td><input type="text" name="txtURL"></td> <td> </td> <td>Images : </td>
<td align="left"><input type="checkbox" name="chkDebug" value="1"></td> <td> </td> <td align="left"><input type="button" name="getRSS" value="Get Data" onClick="javaScript:startProcess();"/> </tr> </table> </div> <br/> <div id="DisplayArea"> </div>
</form> </body></html>
var xmlHttp = null; var t_channel = 'channel'; var t_title = 'title'; var t_link = 'link'; var t_description = 'description';
var t_pubDate = 'pubDate' var t_lastBuildDate = 'lastBuildDate'
var t_item = 'item';
var t_tableHTML;
var objChannelNodeList;
// messages var NOT_A_VALID_RSS_FEED = 'Not a valid RSS Feed'; var GOT_ERROR = 'Got error please try again'; var GETTING_DATA = 'Getting Data Please wait';
// colors var CLR_PROCESSING="orange"; var CLR_ERROR="red"; var CLR_SUCCESS="green"; var CLR_BORDER="skyblue";
function getFirstChildText(objNode,tag){ if(objNode.getElementsByTagName(tag).length>0){ return objNode.getElementsByTagName(tag)[0].firstChild.nodeValue; }else{ return ' '; } }
function getRSSData(){ try{ if(xmlHttp.readyState==4){ t_tableHTML = '<table cellspacing="0" cellpadding="3" border="1" bordercolor='+CLR_BORDER+'>'; var resXML = xmlHttp.responseXML; if(!resXML){ throw NOT_A_VALID_RSS_FEED; } var objChannelNodeList = resXML.getElementsByTagName(t_channel); if(objChannelNodeList.length==0){ throw NOT_A_VALID_RSS_FEED; }
for(i=0;i<objChannelNodeList.length;i++){
var titleText = getFirstChildText(objChannelNodeList[i],t_title); var linkText = getFirstChildText(objChannelNodeList[i],t_link); var descText = getFirstChildText(objChannelNodeList[i],t_description); var publishDate = getFirstChildText(objChannelNodeList[i],t_pubDate); if(publishDate==''){ publishDate = getFirstChildText(objChannelNodeList[i],t_lastBuildDate); } if(publishDate!=''){ titleText = titleText + '[ <font color='+CLR_SUCCESS+'>' + publishDate + '</font> ]'; } constructRows(titleText,linkText,descText); for(j=0;j<objChannelNodeList[i].getElementsByTagName(t_item).length;j++){ var itemTitleText = getFirstChildText(objChannelNodeList[i].getElementsByTagName(t_item)[j],t_title); var itemLinkText = getFirstChildText(objChannelNodeList[i].getElementsByTagName(t_item)[j],t_link); var itemDescText = getFirstChildText(objChannelNodeList[i].getElementsByTagName(t_item)[j],t_description); constructRows(itemTitleText,itemLinkText,itemDescText);
}// end of : item for }// end of channel for
t_tableHTML = t_tableHTML + '</table>';
setText(t_tableHTML);
}else{// end of xml http ready state gettingDataPleaseWait(xmlHttp.readyState); } }catch(e){ gotErrorPleaseTry(e); } } function gettingDataPleaseWait(state){ t_tableHTML = '<table cellspacing="0" cellpadding="0" border="0">'; var t_txtMsg = GETTING_DATA; for(msgDots=0;msgDots<state;msgDots++){ t_txtMsg = t_txtMsg + ' ...'; } t_tableHTML = t_tableHTML + '<tr><td><font color='+CLR_PROCESSING+'>' + t_txtMsg + '</font></td></tr>'; t_tableHTML = t_tableHTML + '</table>';
setText(t_tableHTML); } function gotErrorPleaseTry(e){ t_tableHTML = '<table cellspacing="0" cellpadding="0" border="0">'; t_tableHTML = t_tableHTML + '<tr><td><font color='+CLR_ERROR+'>'; if(e){ if(e.message){ t_tableHTML = t_tableHTML + GOT_ERROR + ' [<b>' + e.message + '</b>]'; }else{ t_tableHTML = t_tableHTML + GOT_ERROR + ' [<b>' + e + '</b>]'; } }else{ t_tableHTML = t_tableHTML + GOT_ERROR; } t_tableHTML = t_tableHTML + '</font></td></tr>'; t_tableHTML = t_tableHTML + '</table>';
setText(t_tableHTML); } function constructRows(itemTitleText,itemLinkText,itemDescText){ t_tableHTML = t_tableHTML + '<tr>'; t_tableHTML = t_tableHTML + '<td><a href="' + itemLinkText + '">' + itemTitleText + '</a></td>'; if(!document.theForm.chkDebug.checked){ // trim images var t_tempDes = new String(itemDescText); itemDescText = t_tempDes.replace('<img','<imge'); }
t_tableHTML = t_tableHTML + '<td>' + itemDescText + '</td>'; t_tableHTML = t_tableHTML + '</tr>'; } function setText(strHtmlText){
document.getElementById('DisplayArea').innerHTML=''; document.getElementById('DisplayArea').innerHTML=strHtmlText;
} function startProcess() { try{ gettingDataPleaseWait(1); var runningUserAgent = new String(navigator.userAgent);
var URL=''; if(document.theForm.selURL.selectedIndex>0){ URL = document.theForm.selURL[document.theForm.selURL.selectedIndex].value; }else{ URL = document.theForm.txtURL.value; } // append some random number to avoid cache if(URL!=''){ URL = new String(URL) var curDate = new Date(); if(URL.indexOf("?")!=-1){ URL = URL + '&date=' + curDate.getTime(); }else{ URL = URL + '?date=' + curDate.getTime(); } }else{ throw 'Invalid URL' }
if(runningUserAgent.indexOf('MSIE')!=-1){//IE xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); if(!xmlHttp){ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } else if(runningUserAgent.indexOf('Firefox')!=-1){ netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); xmlHttp=new XMLHttpRequest(); }else{ throw 'Currently supports only IE or Firefox' }
xmlHttp.open("GET",URL,true); xmlHttp.send(null);
if(xmlHttp!=null){ xmlHttp.onreadystatechange=getRSSData; }
}catch(e){ gotErrorPleaseTry(e) } }// end of : ajaxFunction function </script> </head> <body> <form name="theForm"> <div id="RSS Source Area">
<table cellspacing="0" cellpadding="0" border="0"> <tr> <td align="left"> <select name="selURL"> <option value="">Select RSS Feed</option> <option value="http://www.ibnlive.com/xml/top.xml">IBN Top Stories</option> <option value="http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/latest_published_stories/rss.xml">BBC Latest</option>
<option value="http://www.ndtvprofit.com/RSS/SectionRssfeed.aspx?ts=Markets">NDTV Profit Business</option> </select> </td> <td> or </td> <td><input type="text" name="txtURL"></td> <td> </td> <td>Images : </td>
<td align="left"><input type="checkbox" name="chkDebug" value="1"></td> <td> </td> <td align="left"><input type="button" name="getRSS" value="Get Data" onClick="javaScript:startProcess();"/> </tr> </table> </div> <br/> <div id="DisplayArea"> </div>
</form> </body></html>
Tuesday, June 17, 2008
Maven + Eclipse Java Project Setup Quick Steps
How to create a project .jar , .war file structure using Maven
1) Create a folder D:\mavenProject
2) Traverse to that Directory : CD D:\ mavenProject
3) Execute (for .jar): mvn archetype:create -DgroupId=com.abc.example -DartifactId=exampleLogic
4) Execute (for .war): mvn archetype:create -DgroupId=com.abc.example -DartifactId=exampleWeb -DarchetypeArtifactId=maven-archetype-webapp
Now we can independently build .jar and .war files by excecuting mvn celan install , command on respective folders.
i.e on D:\mavenProject\exampleLogic and D:\mavenProject\exampleWeb
The .jar file and .war file will be installed in the maven local repository.
Note : the maven local repository is configured in settings.xml located @ <maven Installaion directory>\conf
Say suppose our exampleLogic .jar is used in exampleWeb then we can add that as dependency in the .pom file of exampleWeb as follows
<dependency>
<groupId>com.abc.example</groupId>
<artifactId>exampleLogic</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
In this case first we have to build the .jar file and then we have to build the .war file
If the .jar file is not installed in the local maven repository and mvn clean install is executed on exampleWeb then we will receive an error.
If we want to run a single command to clean and install both the logic and web project we can create a .pom file in the D:\mavenProject as follows
<project>
<name>Maven Example</name>
<url>http://localhost:8080/</url>
<modelVersion>4.0.0</modelVersion>
<groupId>com.abc.example</groupId>
<version>1.0</version>
<artifactId>mavenExample</artifactId>
<packaging>pom</packaging>
<modules>
<module>exampleLogic</module>
<module>exampleWeb </module>
</modules>
</project>
How to configure the created project to work with eclipse.
1) Create a folder D:\workspaceExample
2) CD D:\mavenProject\exampleLogic : the following commands have to be executed from a folder having .pom file
3) Execute : mvn eclipse:add-maven-repo -Declipse.workspace=D:\workspaceExample
Note : This will create M2_REPO variable for the given workspace
4) Execute mvn -Dwtpversion=1.0 eclipse:eclipse from D:\mavenProject\exampleLogic
Note : This will create .class and .project files for Eclipse for ExampleLogic project.
5) Execute mvn -Dwtpversion=1.0 eclipse:eclipse from D:\mavenProject\exampleWeb
Note : This will create .class and .project files for Eclipse for ExampleLogic project.
6) Open the workspace (D:\workspaceExample) from eclipse
7) Select File > Import > General > Existing Projects into workspace
1) Create a folder D:\mavenProject
2) Traverse to that Directory : CD D:\ mavenProject
3) Execute (for .jar): mvn archetype:create -DgroupId=com.abc.example -DartifactId=exampleLogic
4) Execute (for .war): mvn archetype:create -DgroupId=com.abc.example -DartifactId=exampleWeb -DarchetypeArtifactId=maven-archetype-webapp
Now we can independently build .jar and .war files by excecuting mvn celan install , command on respective folders.
i.e on D:\mavenProject\exampleLogic and D:\mavenProject\exampleWeb
The .jar file and .war file will be installed in the maven local repository.
Note : the maven local repository is configured in settings.xml located @ <maven Installaion directory>\conf
Say suppose our exampleLogic .jar is used in exampleWeb then we can add that as dependency in the .pom file of exampleWeb as follows
<dependency>
<groupId>com.abc.example</groupId>
<artifactId>exampleLogic</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
In this case first we have to build the .jar file and then we have to build the .war file
If the .jar file is not installed in the local maven repository and mvn clean install is executed on exampleWeb then we will receive an error.
If we want to run a single command to clean and install both the logic and web project we can create a .pom file in the D:\mavenProject as follows
<project>
<name>Maven Example</name>
<url>http://localhost:8080/</url>
<modelVersion>4.0.0</modelVersion>
<groupId>com.abc.example</groupId>
<version>1.0</version>
<artifactId>mavenExample</artifactId>
<packaging>pom</packaging>
<modules>
<module>exampleLogic</module>
<module>exampleWeb </module>
</modules>
</project>
How to configure the created project to work with eclipse.
1) Create a folder D:\workspaceExample
2) CD D:\mavenProject\exampleLogic : the following commands have to be executed from a folder having .pom file
3) Execute : mvn eclipse:add-maven-repo -Declipse.workspace=D:\workspaceExample
Note : This will create M2_REPO variable for the given workspace
4) Execute mvn -Dwtpversion=1.0 eclipse:eclipse from D:\mavenProject\exampleLogic
Note : This will create .class and .project files for Eclipse for ExampleLogic project.
5) Execute mvn -Dwtpversion=1.0 eclipse:eclipse from D:\mavenProject\exampleWeb
Note : This will create .class and .project files for Eclipse for ExampleLogic project.
6) Open the workspace (D:\workspaceExample) from eclipse
7) Select File > Import > General > Existing Projects into workspace
Subscribe to Comments [Atom]