Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

Christopher Dale

Ubuntu and writing Java servlets

Kind of a strange, and I'm sure this wouldn't have happened on any other distribution of Linux, but I spent forever looking through blogs, message boards, and all manner of other places looking for an answer to this question.

The problem was with this little itty bitty smidge of code:
/**
 * hello.java
 */
import javax.servlet.*;

class HelloWorld
{
        public static void main(String[] args)
        {
                System.out.println("Hello World!");
        }
}


Everytime I wanted to compile this, I got something like "Could not find javax.servlet. blah blah" So, eventually, I just pulled an apt-cache search j2 (for j2ee) and saw an entry for libservlet2.4-java. Hmmmmm, what the heck is that I wondered. I installed it and went to the Ubuntu site to look at what files were installed and sure enough:

/usr/share/java/jsp-api-2.0.jar
/usr/share/java/jsp-api.jar
/usr/share/java/servlet-api-2.4.jar
/usr/share/java/servlet-api.jar


Perfect! I added these paths to my CLASSPATH variable in my .bashrc and whamo, I'm in business and flaunting my little HelloWorld.class file all around town.

I hope this helps someone else!

Removing <?xml version="1.0"?> from the output of XSLTProcessor->transformToXml()

Just add this as a top level element in your XSL document.

<xsl:output method="html" />


Thanks to this post on php.net for the answer:
http://us3.php.net/manual/en/xsltprocessor.transformtoxml.php#80887

Super simple XML and PHP

This is a super simple implementation of a concept found here:
http://xtech06.usefulinc.com/schedule/paper/19

This xml class provides a very simple way to open an XML file, get information from the XML file, modify information from the XML file and save the XML file. It also supports automatic creation of new XML files based on an automatically incrementing ID senerio (similar to auto incrementing primary keys in databases).

The creation of new XML files is done by looking for a "template" xml file named 0.xml in the directory passed to the create function. If the template file is found, then it is loaded into the xml class and saved to a new file whose name is one more than the highest filename (of course the xml extension is added on). Eventually, I will try and add some validation code and whatever else, but for now I'm trying to keep things simple.

This requires the DOM module to be built into PHP.
I am using PHP 5.

If anyone has any ideas on how to improve this, feel free to post comments.

<?php

class xml
{
	var $dom;
	var $uri;

	function xml($uri)
	{
		$this->dom = new DOMDocument();

		if(preg_match('/\.xml$/', $uri))
		{
			$this->uri = $uri;
		}
		else
		{
			$this->uri = $this->create($uri);
		}
		$this->dom->load($this->uri);
	}

	function set($query, $value)
	{
		$path = new DOMXPath($this->dom);
		$nodes = $path->query($query);
		$nodes->item(0)->nodeValue = $value;
	}

	function get($query)
	{
		$path = new DOMXPath($this->dom);
		$nodes = $path->query($query);
		return $nodes->item(0)->nodeValue;
	}

	function save()
	{
		$this->dom->save($this->uri);
	}

	function create($uri)
	{
		// Build the URI of the template file.
		$template = sprintf('%s/0.xml', $uri);

		// If the directory doesn't exist, we can't really
		// do anything.
		if(!is_dir($uri))
		{
			exit('No directory');
		}

		// If the template file doesn't exist, we cannot
		// create a new file automatically.
		if(!file_exists($template))
		{
			exit('No template');
		}

		// Load the template XML into our DOMDocument.
		$this->dom->load($template);

		// Scan the directory into an array.
		$dir = scandir($uri);

		// Pull out the highest ID
		$id = str_replace('.xml', '', array_pop($dir));

		// Add one to it
		$id++;

		// Construct the new path with the new ID
		$uri = sprintf('%s/%s.xml', $uri, $id);

		// Save the new file
		$this->dom->save($uri);

		// Return the URI of the new XML file.
		return $uri;
	}
}

// The following code will create a new XML file
// under the directory /var/www/data/users.
$x = new xml('/var/www/data/users');
$x->set('//user/name', 'John Doe');
$x->save();

?>

How to set the background image using xv.

I was fed up with using fbsetroot... xsetroot... etc to set my background image, so I decided to see if xv would do it for me since it's a program that I actually use for more than just background setting like fbsetroot, xsetroot, etc.

xv -root -quit -maxpect ~/images/blah.jpg

Deleting a line from a file matching criteria

I was messing around trying to figure out an easier way to remove entries from my /etc/portage/package.keywords file without having to drop into VI all the time and came up with this. You can put this into a bash script to make things alot easier on yourself if you are constantly altering your mask/use/keywords files in Gentoo.

sed -i '/x11-wm\/compiz/d' /etc/portage/package.keywords


The above example will remove any line that matches the regular expression x11-wm/compiz and edit the file in place "-i".

Find solved forum threads with Google

A method of using google which allows for fast and accurate searching of forums and other websites for solved threads matching your criteria.

Just so everyone is aware, I just put the tag "hack" in the tag list so that those looking for google hacks might stumble on this. We'll see how that works out.
http://www.google.com/search?q=intitle:solved+intext:nvidia+5200
// This will find tons of solved issues regarding the NVidia 5200 graphics card

http://www.google.com/search?q=intitle:solved+intext:nvidia+5200+"no+screens+found"
// This should help you fix a problem with "No screens found" on an NVidia 5200 card

Java Program + DWM/WMII/Awesome/... = Grey Rectangle

To get past the problem of java programs showing up as grey squares on your screen when you are using a non-standard window manager, simply prefix the program name or "java -jar ..." with "AWT_TOOLKIT=MToolkit"

AWT_TOOLKIT=MToolkit webscarab

Using notify-send with IRSSI

Requires libnotify, have fun!

use strict;
use vars qw($VERSION %IRSSI);

use Irssi;
$VERSION = '0.0.3';
%IRSSI = (
        authors     => 'Chrelad',
        contact     => 'blah@blah.blah',
        name        => 'notify',
        description => 'Display a pop-up alert for different events.',
        url         => 'http://google.com',
        license     => 'GNU General Public License',
        changed     => '$Date: 2007-02-07 12:00:00 +0100 (Thu, 7 Feb 2008) $'
);

#--------------------------------------------------------------------
# Created by Chrelad
# Feb 7, 2008
#--------------------------------------------------------------------

#--------------------------------------------------------------------
# The notify function for public message
#--------------------------------------------------------------------

sub pub_msg {
        my ($server,$msg,$nick,$address,$target) = @_;
        `notify-send -t 8000 "${target} : ${nick}" "${msg}"`;
}

#--------------------------------------------------------------------
# Irssi::signal_add_last / Irssi::command_bind
#--------------------------------------------------------------------

Irssi::signal_add_last("message public", "pub_msg");
#- end

Play sound for certain events in IRSSI

Perl script to allow sounds to be played for different events in IRSSI

Just keep adding handlers to IRSSI's pools pointing to different functions for different events or whatever you want.

use strict;
use vars qw($VERSION %IRSSI);

use Irssi;
$VERSION = '0.0.3';
%IRSSI = (
        authors     => 'Chrelad',
        contact     => 'blah@blah.blah',
        name        => 'alert',
        description => 'Play sounds for different events in IRSSI.',
        url         => 'http://google.com',
        license     => 'GNU General Public License',
        changed     => '$Date: 2007-02-07 12:00:00 +0100 (Thu, 7 Feb 2008) $'
);

#--------------------------------------------------------------------
# Created by Chrelad
# Feb 7, 2008
#--------------------------------------------------------------------

#--------------------------------------------------------------------
# The sound playing function for public message
#--------------------------------------------------------------------

sub pub_msg {
        my ($server,$msg,$nick,$address,$target) = @_;
        `mplayer -quiet ~/file.mp3 > /dev/null`;
}

#--------------------------------------------------------------------
# Irssi::signal_add_last / Irssi::command_bind
#--------------------------------------------------------------------

Irssi::signal_add_last("message public", "pub_msg");
#- end

Compressing a directory with rar on Linux

I've been struggling to get this to work for so long that when I finally got it going I had to throw it up here so I wouldn't lose it.
rar a -m5 -R output.rar /etc/

This will create a max compression (not taking into account dictionary sizes and the like) archive of the entire etc directory.