Pages

Showing posts with label codes. Show all posts
Showing posts with label codes. Show all posts

Labs

avro-log4j   -  serialization mechanism to provide a layout for log4j

firetester   -  A simple RESTful services testing tool written in Groovy Griffon framework

gitter - Publishes github activities to Twitter

jfilemagic (jfm) is an utility for identifying files using magic numbers or signatures

cometd-chat - a comet based chatter for fun



Debug open social gadgets : Error codes

I was playing around with Apache shindig to develop a simple open social application.
I find it easy to use the error codes available in open social specification.Use this sample gadget for testing purposes.In the specification http://code.google.com/apis/opensocial/docs/0.8/reference/#opensocial.ResponseItem.getErrorCode

it says like to get error code from getErrorCode function. Its returning an enumeration.So its easy to get error codes from Error class.I tried for response.getErrorCode().. hopeless .. its undefined while response.hadError() was working !! I think spec document should be clear with examples ...mmm try the code ..





<script type="text/javascript" >
function getData() {
var req = opensocial.newDataRequest();
req.add(req.newFetchPersonRequest('VIEWER'), 'viewer');
req.send(callback);
}


function callback(response){


if(!response.hadError()){
alert("Success");
var html = "Test OK"
document.getElementById('message').innerHTML = html;

}else {
alert(response.getErrorMessage());
var viewer_resp_item = response.get('viewer')
switch(viewerresp.getErrorCode())
{
case opensocial.ResponseItem.Error.INTERNAL_ERROR :
/*The request encountered an unexpected condition that prevented it from fulfilling the request*/
alert('There was an error on the server.');
break;
case opensocial.ResponseItem.Error.UNAUTHORIZED :
/*The gadget does not have access to the requested data.*/
alert('There was a permissions issue: the app is not allowed to show the data.');
break;
case opensocial.ResponseItem.Error.BAD_REQUEST :
/*The request was invalid.Parameters are wrong etc.*/
alert('There was an error in the Container.');
break;
case opensocial.ResponseItem.Error.FORBIDDEN :
/*The gadget can never have access to the requested data.*/
alert('The Container was unable to contact the server.');
break;
case opensocial.ResponseItem.Error.NOT_IMPLEMENTED :
/*This container does not support the request that was made.
Different version of implementations*/
alert('did not implement this particular OpenSocial interface.');
break;
case opensocial.ResponseItem.Error.LIMIT_EXCEEDED :
/*The gadget exceeded a quota on the request.*/
alert('Limit exceeded.');
break;


}

}
}


gadgets.util.registerOnLoadHandler(getData);

</script>





A simple RSS parser

Various XML parsing technologies made the content delivery on web very flexible.I am experimenting with various rss parsing methods.One of the straight forward parsing technology is XML Pull Parsing.Xml Pull Parser (in short XPP) is a streaming pull XML parser and should be used when there is a need to process quickly and efficiently all input elements.The method is very flexible and there is no need for xml validation.I think this form is very useful for parsing simple xml documents.The parsing is designed by programmer itself requesting the parsing for each elements.He can omit elements and its subtree which are not needed.RSS are syndications through xml data.So a simple RSS parsing can be done by XPP method.The limitation that restricts XPP to a subset of XML documents is that it does not support entities, comments, or processing instructions in the document. XPP creates a document structure consisting only of elements, attributes (including Namespaces), and content text. This is a serious limitation for some types of applications.Anyway I try here to make a sample code.In this code an rss feed is read and create a string as a content inside div tag , so that the string can attach as innerhtml data for a dynamic page.I want to make it as a webservice, so that the content delivery can happen as a service.

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace SampleWebService
{
public class Parser
{
StringBuilder sbString = new StringBuilder();
StringBuilder sbTempString = null;

public string GetXMLData()
{

XmlTextReader xtrReader = new XmlTextReader("http://rss.cnn.com/rss/cnn_world.rss");
xtrReader.WhitespaceHandling = WhitespaceHandling.None;
xtrReader.MoveToContent();


while (xtrReader.Read())
{
switch (xtrReader.NodeType)
{

case XmlNodeType.Element:
BuildElements(xtrReader);
break;

case XmlNodeType.EndElement:
BuildEndElements(xtrReader);
break;
}

}

return sbString.ToString();

}

private void BuildElements(XmlTextReader xtrReader)
{
switch (xtrReader.Name.ToLower())
{

case "item":
sbTempString = new StringBuilder();
sbTempString.Append("<div>");
break;

case "title":
if (sbTempString != null && sbTempString.Length > 0)
{
xtrReader.Read();
sbTempString.Append("<a>").Append(xtrReader.Value).Append( "</a><br />");
}
break;

case "link":
if (sbTempString != null && sbTempString.Length > 0)
{
xtrReader.Read();
sbTempString.Insert(sbTempString.ToString().LastIndexOf("<a") + 2, " href=\"" + xtrReader.Value +"\"");
}
break;

case "description":
if (sbTempString != null && sbTempString.Length > 0)
{
xtrReader.Read();
sbTempString.Append("<p>").Append(xtrReader.Value);
}
break;

}
}


private void BuildEndElements(XmlTextReader xtrReader)
{
switch (xtrReader.Name.ToLower())
{

case "item":
if (sbTempString != null && sbTempString.Length > 0)
{
sbTempString.Append("</div>");
sbString.Append(sbTempString);
}
break;

case "title":

break;

case "link":
break;

case "description":
if (sbTempString != null && sbTempString.Length > 0)
{
sbTempString.Append("</p>");
}
break;

}
}


}
}