Archive for the 'Tech' Category

Parsing XML Using Castor

Castor provides 3 ways to parse an xml file into java objects. I have used two of those, and am writing this post to give an introduction into the method.

The first way is to create java classes for each of the elements and then use the marshall and unmarshall methods to parse the xml file. Castor uses introspection techniques to map elements with fields of the java class. I have not used this method, and will not go any further.

The second and perhaps the easiest way is to give Castor the schema file and let it generate the java classes. Say you have the following schema file:

If you let Castor generate the java source files itself, the following files are generated: The Plgen class for the root element:

The Playlist class which is the child of Plgen:

And finally, the ExtensionType enum:

But I ran into trouble when I wanted to add additional behavior to the classes generated by Castor. As an example, say, I want my Playlist class to implement an interface XYZ which has a method doXYZprocess(). If I am to modify the Playlist class to make it implement the said interface, then if I make any change to the schema file in future I’ll be in trouble. I’ll have to redo all the additions. Now this is a painful task.

An alternative was to create wrapper class around the Castor generated classes and use the wrappers instead. Well, this would work, but I wanted something smarter rather than such duplication.

This is where the third method of parsing comes into picture. It addresses the exact issue as mentioned above. The idea is to use user written classes for marshalling rather than Castor generated ones. One could write a class having lots of methods and fields, bind it to an element of an xml file and have it populated by Castor while parsing the xml. Castor provides this functionality through a Mapping.

A Mapping is another xml file which instructs Castor about the Java classes and the fields that are to be used for parsing. A great degree of freedom is provided in the writing of mapping. One could specifically tell Castor to use certain getters and setters, or leave it to Castor to populate the class fields by introspection.

An example of mapping file for the above schema file would be:

Castor interprets this mapping as the root element being plgen which would get mapped to the class Plgen. This root element can contain a list of Playlist elements. So the getter and setter that Castor calls will be

public ArrayList<Playlist> getPlaylist();

public void setPlaylist(ArrayList<Playlist>);

So our Plgen class above needs to contain these two methods. Similarly, the Playlist class needs to have the following two methods to set the extensionType field:

public String getExtensionType();

public void setExtensionType(String);

Note that I am taking the enum value as a string and will use the setter to set the Enum from a string. My getter likewise will return the Enum.toString().

If you do not want the default getters and setters to be used by Castor, then you have to specify the getter and setter as attributes of the field element in t he mapping.

Castor has compiled a help page to understand the mapping option. You may like to read it here:

Castor XML Mapping

I really liked the mapping feature. It gives me a lot of freedom to write Java classes and then use the mapping to parse any xml file into these classes. This obviates the need to understand the DOM parsing model, of iterating through the xml tree and extracting the values required. The game has been simplified to writing a mapping file and letting Castor create the java objects.

I hate xmls’ NO more. :)

Popularity: 24% [?]

Ubuntu Lucid: Upgrading To Lynx In Virtualbox

I have Virtualbox 3.1.4 r57640 installed on Windows XP as the host, Ubuntu 9.10 as the guest. And since the ubuntu is not being used as an active development currently, I decided to upgrade it to Ubuntu 10.04 Beta 1. Having read a lot of positive reviews for Lucid Lynx, I wanted to try it out as soon as possible.

The upgrade took more than a couple of hours. It downloaded a GB of package data from repositories, and then took 90 minutes to setup the new system. Usually my ubuntu upgrades have been quite a painful process, having to manually configure a few things and set another few right. But then these things are to be expected if you always jump the ship in the Beta mode.

Immediately upon restart I was faced with my first problem. The xserver failed to load and instead threw a dialog box saying it was switching to low graphics mode for the session. I let it do so. Logging in into the system, as usual, my VirtualBox addons needed re-installation. But even that did not solve the problem. The same error popped up again upon restart. Also, my mouse integration did not work.

The first problem was occurring due to the new kernal 2.6.32-16-generic. Looking up in the forums, I found that virtualbox will incorporated the fix in their next maintenance release. However, one could apply the patch manually. It required modifying the file:

/usr/src/vboxvideo-3.1.4/vboxvideo_drm.c

The fix is r27248. You can read up about the ticket here.

To get the cursor integration working, we need to create xorg.conf. Ubuntu has done away with xorg and are using hal now. I could not clearly understand the reason behind the problem, but re-creating the xorg.conf file does the trick. Here is the forum where this issue has been discussed:

Cursor Control in Ubuntu 10.04

I do not seem to be having any more issues. I’ll keep consolidating any issues I run into and how to fix them so that I don’t have to google them up every time.

Popularity: 42% [?]

Amarok 2.3.0 “Clear Light” Is Beautiful

The latest edition of Amarok is too good. I was using 2.2.0 prior to this one. The new amarok has a couple of features which makes the experience even more enriching.

The 2.3.0 sees a new toolbar. The left edge has pause or play button, the right end has a sound control and the remaining width is made up of the progress bar. Above the progress bar in the middle is shown the details of the song being played currently. To either side of it are songs before/after this one, serving the purpose of jumping to next/previous song. The beauty of this design is that if you unlock your layout, remove the media sources, playlist and context panels, then you are only left with the toolbar. Resize the window to cover just the space needed for the toolbar. Now you have got an ipod like player. Flick above the progress bar to go to the next/previous song. This is one cool feature. You can have a look at it from this video:

Amarok New Toolbar In Action

Another feature I liked was the bookmarking of timeline for a given song. If there is an awesome guitar solo that you like in a particular song, you can bookmark that point and play it from there whenever you want to. This feature has been in Amarok for quite a while now.

But installing Amarok 2.3.0 in Ubuntu needed more than just apt-get install amarok. The default amarok package available in the Ubuntu 9.10 repositories is that of 2.2.0. To install 2.3.0 you need to follow the steps mentioned here:

Installing Amarok 2.3.0 in Ubuntu

But there was a problem with the installation on my computer. The application would crash every time I opened it. The application would just not start up. A few searches and I found that the info applet was causing the application to crash because of some fault with qt [read this comment]. The simplest solution is to remove the info applet. To do that, open the ~/.kde/share/config/amarok_homerc file and remove the info applet from plugins. The application should start now.

Another problem I encountered was that my local collections was not being displayed or stored. It would scan the files alright, but then it would not add them to the collections list. And if you do not have the collection added to amarok, it takes away half the fun. You need them for dynamic playlist and all.

Again googling around and I found the solution. Here is the link to the forum where this topic has been discussed:

Amarok Not Recognizing Media

For me just installing the below mentioned packages did the trick:

libhtml-template-perl (2.9-1)
mysql-server (5.1.41-3ubuntu6)
mysql-server-5.1 (5.1.41-3ubuntu6)
mysql-server-core-5.1 (5.1.41-3ubuntu6)

Well, I have my Amarok 2.3.0 running now, and am liking it already. Yay !!

But a couple of things are not working for me. They are:

  • I am not getting the mood toolbar even though I have enabled it from the options.
  • I have enabled the display tooltip for songs options, but do not see the tooltip song description.

Popularity: 42% [?]

Svn Repository Backup

Ever since I started using svn I have found it an indispensable tool. I have created a local repository on my hard disk and all my personal projects are using this repository. And having had lots of hard disk crashes last year, I fear for the loss of my code files. So, I thought about a regular backup system.

I also have Ubuntu installed as a virtual machine. And Ubuntu has this Ubuntu One feature, which gives you a 2gb space online. So I went ahead to backup my repository to this space.

I have my repository mounted in Ubuntu at /mnt/WinRepository. The following script takes a dump of the repository:

svnadmin dump /mnt/WinRepository --incremental > ~/Ubuntu\ One/WinRepo.bak

The advantage of using incremental option is that I can specify revision range to be backed up into different files.

svnadmin dump /mnt/WinRepository --revision 0:100 > ~/Ubuntu\ One/WinRepo1.bak
svnadmin dump /mnt/WinRepository --revision 101:200 --incremental > ~/Ubuntu\ One/WinRepo2.bak
svnadmin dump /mnt/WinRepository --revision 201:HEAD --incremental > ~/Ubuntu\ One/WinRepo3.bak

And since the files are being saved in Ubuntu One folder, they get automatically backed up to the Ubuntu One online space.

This does not compress the files. But since I have 2 gb of space available, I am not really concerned about space.

Popularity: 9% [?]

Adding Startup And Shutdown Scripts in Ubuntu

I have my Ubuntu installed in Virtual Box. The very first thing that I needed to do was to setup my Ubuntu to recognize Windows drives. I setup two bash scripts to run at startup and shutdown. The following is what I have done.

anu@sw:/etc/init.d$ sudo vi vboxStartup
#!/bin/bash

# Mount the Win virtual drives - MyDocument, CShared, PidginWin
sudo mount -t vboxsf MyDocuments /mnt/MyDocuments
sudo mount -t vboxsf CShared /mnt/CShared
sudo mount -t vboxsf PidginWin /mnt/PidginWin

# Sync the files from PidginWin/logs to purple/logs
sudo rsync -azv /mnt/PidginWin/logs/ /home/anu/.purple/logs/
anu@sw:/etc/init.d$ sudo chmod +x vboxStartup
anu@sw:/etc/init.d$ sudo update-rc.d vboxStartup defaults

This way my ubuntu pidgin logs are in sync with my windows pidgin logs. Next, I also wanted my Windows pidgin logs to get sync with the Ubuntu pidgin logs whenever I log off. So I create yet another script

anu@sw:/etc/init.d$ sudo vi vboxShutdown
#!/bin/bash

echo "Anuvrat Shutdown Script -- vbox"

# Resync files from purple/logs/ to PidginWin/logs/
sudo rsync -azv /home/anu/.purple/logs/ /mnt/PidginWin/logs/
anu@sw:/etc/init.d$ sudo chmod +x vboxShutdown
anu@sw:/etc/init.d$ sudo update-rc.d vboxShutdown start 80 0 6 .

The funny thing though is that I haven’t yet tested it. I hope it works.

Oh crap! It did not run at startup. Need to check.

Popularity: 44% [?]

Courage And Failure: Read Data Protector

Ever since I joined FICO and received my laptop, I had been wondering if I could install Ubuntu on it. Now officially I might not be allowed to, but what if I take a chance and do it myself. However, there was a small glitch.

The laptop that we use comes with Data Protector installed. The Data Armor software keeps the hard disk encrypted at all times. Even while windows is running, the Data Armor is operational silently in the background. This was a variable in the equation of which I had no clue.

Specifically, my questions were whether Wubi would be allowed to modify the boot entry to make the presence of Ubuntu known? If yes, would it be able to boot into Ubuntu? And if true, would the usage of Ubuntu without Data Armor break my system in any way?

I tried googling around without any success. I was left alone, with no explanations or warnings. And messing around with your official computer’s boot record was too big a risk to take in the starting months of job,

So I waited. I thought of later buying a personal laptop and installing Ubuntu in it. But the impatient man that I am, I was at times tempted to just experiment.

Today was one of those days when the urge to experiment and know overcame my restrictive self. I decided to try it out. In the worse case I would go to the technical department and say sorry for the damage.

I downloaded Wubi. It said it would need to download 700 mb’s of Ubuntu unless a Desktop CD was provided. Yay ! I remembered I had the Desktop CD. A few of franctic search, and I found my CD hiding under a pile of rubbish junk. Excitedly I removed the CD from its cover, slid it into the drive and started the Wubi application.

It recognized my CD and started unpacking the contents. After copying all the files onto the disk, it asked for a reboot. I did so immediately.

Now was the time I would find out if the attempt was successful or was it thwarted by my foe named Data Armor. I kept my fingers crossed.

The boot screen came on for a few seconds and went off. This was normal. The screen was blank for a few seconds which stretched into infinity. I was worried now. If the Data Armor screen failed to show up, I would know that I have messed up things big time and would have to be prepared for some serious lectures the following Monday.

But while all these thoughts were engulfing me, the Data Armor screen came up. Yipee. I was too glad. I entered my account id and password. Upon checking the credentials, the computer restarted as it always does. Now the question was whether the dual boot option would show up. If the installer was successful in modifying the Master Boot Record, I should be getting an option to select either of the operating systems. And get, I did those.

My happiness was beyond measure. I selected Ubuntu and pressed the Enter button. I was jumping up and down, congratulating myself for having the courage to try out Wubi and get rewarded. The Ubuntu boot screen flashed on before me. Woohoo !!

But a few joys are short lived. Immediately a blank screen replaced the bar. Now this was wrong. Then a line mentioning BusyBox came up. The next line was a prompt saying

initramfs >

Wait there, this is not done. At this point of time, my Ubuntu should have been booting up and starting the automatic install procedure. I hard booted my computer and logged into Windows.

Cursed ! I was this close to escaping the fuss of Windows and enjoy the pleasures of Ubuntu. I googled up immediately to know if my setup had gone wrong or was it plain impossible to install Ubuntu in the encrypted hard disk. And this is when I stumbled across the FAQ of Wubi which has mentioned that Wubi fails if the hard disk is encrypted. Damn you Data Protector.

But at least I am happy that I tried. I can now rest peacefully knowing that its not possible as long as the disk is encrypted. The suffering of not knowing is cured.

So long as I do not find another way to get Ubuntu running on my machine, I shall tolerate Windows.

Popularity: 5% [?]

The Agony Of Having Data Protector

The company that I work for has provided me a Dell D630 Latitude. And since this is the only laptop I have, I use it for my personal purpose too. The laptop came with windows XP installed. Since I have never been a fan of Windows, I wanted to get back to ubuntu on my laptop.

This is where the problems started. Now the company is paranoid when it comes to security. They have installed an application called the Data Armor. This application keeps the hard disk encrypted at all times. In fact, when the laptop starts, the whole disk is encrypted and a password dialog box appears from Data Armor. At this point, if you do not know the password, you cannot decrypt the hard drive, thus ensuring the safety of the contents inside it.

When you log into Data Armor, the drive is decrypted, and the laptop boots again. This time though, the normal windows welcome screen appears. All the time that windows is operating, the Data Armor works in the background.

Now, I would like to install ubuntu using wubi on this laptop, but am not sure about a few issues. The biggest concern is that will installing ubuntu break or conflict with the Data Armor? I suppose the initial boot phase should be fine. After all, the hard disk is encrypted and Data Armor has no information whether the computer runs a windows or a linux. But then, once the reboot is done, will logging into ubuntu disrupt the functioning of Data Armor? Will it fail to maintain the encrypted state of the disk, thereby leading to a failure in the next boot up? Or will it gracefully allow the operation of ubuntu go on, and yet start up when logging into windows the next time?

So many questions to which I have no answers. I have tried googling around a bit but to no avail. I have only met with failure every time. In fact, I also tried querying the technical support guys at the company regarding this, but never got a satisfactory reply. A few question my need for ubuntu, reiterating that since my work does not require me to work on ubuntu, my official laptop should not contain the linux. A few nod to my possible explanations, but always remarking that even they are not sure of it. And then there are the few who don’t even know what wubi is!

So as it stands, I am yet to install ubuntu in my laptop. I will have to refine my search to get better results. Hopefully I succeed.

Popularity: 5% [?]

Shoutcast Scrobbling In Last.fm

I was love with the last.fm radio until they made it a subscription only feature. And since I refuse to pay for something that I can get for free, I decided to give up the last fm radio and look for alternatives. I found the shoutcast radio and also the aol radio inbuilt in winamp. They were good and did the job for me.

But what I missed was the scrobbling of tracks. I wanted a plugin which could enable scrobbling of streaming music into my lastfm account.  I found none. This was until I stumbled across Streamripper.

Streamripper is a plugin for winamp which intercepts incoming stream and downloads them to the hard disk. It has a few options using which you can configure the location where the files are to be saved, the maximum size of the said folder, whether t he downloaded tracks are to be added to the current winamp playlist and so on.

So now to scrobble the tracks, all that needs to be done is this – start streaming some radio station. Streamripper detects an incoming stream and offers to save it. Start the interception. Now two streams get created – one for your winamp and the other for the ripper. You could continue listening to the station. But what I generally do is to switch to a different station for time being. Once the ripper has downloaded a few tracks and appended them to the playlist, I switch to the playlist created by the ripper. And obviously the lastfm scrobbler scrobbles the tracks played.

Occasionally, you might have to empty the contents of the folder where your tracks were being downloaded.

Happy streaming and scrobbling.

Popularity: 28% [?]

Hard Disk Manipulation

Hmm … for some reason, I would like to have a tool which allows me to configure my hard disk in a way such that :

  • A portion of my HDD is reserved for some purpose, say movies
  • Only an external web admin can add files to this reserved portion
  • The user can only delete files, copy from/to the reserved portion not allowed

Basically, the idea is to create a reerved spacxe where the web admin can push certain confidential files knowing that the user can only go through them, or delete them if they are not of his use. The user should not be allowed to modify the files or redistribute them.

Well, one solution would be to actually have an online managed space with the permissions appropriately set. But in the event of file size going beyond GB’s, it’ll be more convenient to have a local unmodifiable/non-distributable copy of the file itself, instead of having to stream the whole thing every time.

But I suppose such a thing might not exist. Obviously, its illogical to assert that a computer administrator has limited modification permission to a piece of his own hard disk.

I’ll google around to see if any tool comes near to my requirements – non-redistributable local copy of files on the web server.

Popularity: 2% [?]

Installing Amarok 1.4 in Ubuntu Jaunty

Don’t like Amarok 2? Despair not, because there is a way to get back your Amarok 1.4 in Ubuntu Jaunty. Basically, you need to find a mirror of Amarok 1.4 and just install from there. One of those is mentioned below. This is all you have to do :

Add the repositories for Amarok 1.4 to your sources.list file:

deb http://ppa.launchpad.net/bogdanb/ppa/ubuntu jaunty main
deb-src http://ppa.launchpad.net/bogdanb/ppa/ubuntu jaunty main

Add the key:

sudo apt-key adv –recv-keys –keyserver keyserver.ubuntu.com \
0x1d7e9dd033e89ba781e32a24b9f1c432ae74ae63

And then finally, update your sources, remove Amarok 2 and install 1.4

sudo apt-get update
sudo apt-get remove amarok
sudo apt-get install amarok14

That’s it. You are done. You have successfully installed Amarok 1.4 in Jaunty. Have fun.

Popularity: 8% [?]