Monday, January 12, 2009

Ant Build Script for War File Creation

This build script will create two separate war files out of a single project. Useful when building a project that will requires to create multiple war file for deployment (i.e. maintenance page and front page).

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<project default="main" basedir=".">

<property name="project.name" value="PROJECT_NAME"/>
<property name="base.dir" value="."/>

<property name="web.dir" value="${base.dir}\WebContent"/>
<property name="lib.dir" value="${web.dir}\WEB-INF\lib"/>
<property name="build.dir" value="${base.dir}\build"/>

<property name="dest.dir" value="DESTINATION"/>
<property name="out.internal.war" value="${dest.dir}\internal\${project.name}.war"/>
<property name="out.external.war" value="${dest.dir}\external\${project.name}.war"/>

<!-- Cleanup task -->

<target name="cleanup">
<echo message="Deletion of previously created war files:"></echo>
<delete file="${out.internal.war}" />
<delete file="${out.external.war}" />
</target>

<!-- This task will create the directory for external -->

<target name="external" depends="cleanup">
<echo message="Creating the temp directory for external pages."></echo>
<mkdir dir="${base.dir}\temp_external"/>
<copy todir="${base.dir}\temp_external">
<fileset dir="${web.dir}">
<exclude name="**/web.xml"/>
<include name="**/*.*"/><!-- files that needs to be included in external page -->
</fileset>
</copy>
</target>

<!-- This task will create the directory for internal -->

<target name="internal" depends="external">
<echo message="Creating the temp directory for internal pages."></echo>
<mkdir dir="${base.dir}\temp_internal"/>
<copy todir="${base.dir}\temp_internal">
<fileset dir="${web.dir}">
<exclude name="**/web.xml"/>
<include name="**/*.*"/><!-- files that needs to be included in internal page -->
</fileset>
</copy>
</target>

<target name="generate" depends="internal">
<echo message="Creating the internal war file"></echo>
<war destfile="${out.internal.war}" webxml="${web.dir}\WEB-INF\web.xml">
<fileset dir="${base.dir}\temp_internal"/>
<lib dir="${lib.dir}"/>
<classes dir="${build.dir}\classes"/>
</war>

<echo message="Creating the external war file"></echo>

<war destfile="${out.external.war}" webxml="${web.dir}\WEB-INF\web.xml">
<fileset dir="${base.dir}\temp_external"/>
<lib dir="${lib.dir}"/>
<classes dir="${build.dir}\classes"/>
</war>
</target>

<target name="main" depends="generate">
<echo message="End of ant build task..."></echo>
<delete dir="${base.dir}\temp_internal" />
<delete dir="${base.dir}\temp_external" />
</target>

</project>


Actually this is my first time creating ant build script so feel free to give comments on how can I improve or optimize this script.

Thursday, January 8, 2009

Javascript Auto-Ellipse Technique

This tutorial will help you add ellipsis(...) from your text and make sure that you can maximize the space provided by the DOM element.

Actually, I follow the tutorial from http://blog.paranoidferret.com/?p=15 but with some minor modification to meet my needs.


@param element - DOM Element placeholder of the text
@param text - the text for truncation
@param width - desired width
@return text - the truncated text with ellipsis

function autoEllipseText(element, text, width){

var inSpan = document.createElement('span');

inSpan.innerHTML = text;
inSpan.style.whiteSpace = 'nowrap';

$(document.body).append(inSpan);

if(inSpan.offsetWidth > width)
{
var i = 1;
inSpan.innerHTML = '';
var len = text.length;
do{
inSpan.innerHTML = text.substr(0, i) + '...';
}while(inSpan.offsetWidth < width && ++i < len);

text = inSpan.innerHTML;
}
$(inSpan).remove();
return text;
}


The code snippet from http://blog.paranoidferret.com/?p=15 does not work when the DOM element is not yet existing or written in the DOM itself. So the workaround I did was to temporarily write the DOM element in the document's body, apply ellipsis on the text, remove the DOM element, and finally, return the text.

Hope you find it useful. :)

Monday, December 22, 2008

If Programming Languages Were Religion

Guys, check this out, but don't take it seriously :)

http://www.aegisub.net/2008/12/if-programming-languages-were-religions.html

Creating Object Oriented Database Connection using PHP

This tutorial will help you create a simple object oriented implementation of Database connection using the PHP scripting language.

First, we will create the parent class which contains all the abstract functions for our database transactions.

abstract class Database
{
abstract protected function connect($host, $username, $password, $dbname);
abstract protected function query($query);
abstract protected function fetchRow($rs);
abstract protected function fetchAll($rs);
abstract protected function getNumRows();
abstract protected function disconnect();
}


Then create a child class. This is a sample class using the MySQL Database Engine.

class MySqlDatabase extends Database
{

private static $instance = null;
private $numRows = null;
private $connect = null;

private function __construct()
{}

public static function getInstance()
{
if(null == self::$instance)
self::$instance = new MySqlDatabase();
return self::$instance;
}

public function connect($host, $username, $password, $dbname)
{
if( !$this->connect = mysql_connect($host, $username, $password) )
{
die("cannot connect to the database");
}

if( !$select = mysql_select_db($dbname) )
{
die("$dbname cannot be selected from the database");
}
return $this->connect;
}

public function query($query)
{
if( !$rs = mysql_query($query) )
{
die("$query cannot execute this query");
}

if( eregi("^SELECT",$query) )
{
$this->numRows = mysql_num_rows($rs);
}
return $rs;
}

public function fetchAll($rs)
{
$rows = array();
while( $row = mysql_fetch_array($rs, MYSQL_ASSOC) )
{
$rows[] = $row;
}
return $rows;
}

public function fetchRow($rs)
{
if( !$row = mysql_fetch_array($rs, MYSQL_ASSOC) )
{
die("Error fetching row");
}
return $row;
}

public function getNumRows()
{
return $this->numRows;
}

public function disconnect()
{
if( !$close = mysql_close($this->connect) )
{
die("Error closing connection");
}
return $close;
}
}

Samp
le usage:

$instance = MySqlDatabase::getInstance(); # will return an instance of mysql database connection class
$instance->connect(HOST, USERNAME, PASSWORD, DBNAME);
$instance->query("SELECT...");


You can also try using different database engine implementation like Oracle or SQL Server.

Hope you find it useful :)

Javascript UTF-8 Decode/Encode

Decoding/Encoding UTF-8 in Javascript. Useful in dealing with special characters such as trademark symbols(™), registered trademark symbols (®), copyright symbols(ⓒ), and the likes.

function decode_utf8(s)
{
return decodeURIComponent(escape(s));
}

function encode_utf8(s)
{
return unescape( encodeURIComponent(s) );
}

Perl File Formatting Error

(2)No such file or directory: exec of '/var/www/html/index.pl' failed
Premature end of script headers: index.pl

You can resolve this problem by changing the file's format, use an editor(Notepad++ - Format>Convert to UNIX Format)

Also make sure that the file is executable(755 will do) and the script is using the correct path to the perl binary. You can issue the command "whereis perl" to check on this.