How to Install Firefox5 on ubuntu

This quick guide is made for home user who are not familiar with official installation of firefox5.

Open terminal and run the following commands

sudo add-apt-repository ppa:mozillateam/firefox-stable

This will install the repository of firefox5

sudo apt-get update

This will update your package manager with the new firefox5

sudo apt-get install firefox

Tadaaaa….. now close your firefox and open it.

To check the version of firefox click on “help” menu and click the “about firefox”

Permanent link to this article: https://blog.openshell.in/2011/08/how-to-install-firefox5-on-ubuntu/

How to destroy a Confidential File completely in linux (Shred)

Some times you may worry if you accidentally delete a file. then after a Google you would be satisfied with the tools like getdataback, recovermyfile, foremost etc. Consider a situation that you have borrowed a pendrive from your friend or someone else and you copied some important or confidential files from your office and moved it to your PC or notebook and going to return it to your friend or to the one who gave the pendriver to you. He can easily recover that file and can steal your information to avoid these things you have to destroy that file immediately after you copied to your pc or notebook.

But how to destroy confidential file?

There come the answer Shred. shred – deletes the content of the file securely by overwriting invalid data on its original contents.

Eg:

# shred confidential.doc

Once you shred that file test it with the following command

# file confidential.doc

It will return that confidential.doc is a data or binary file. This command alters the file content with junk letters. To view the file content in terminal you can use the following command:

# cat confidential.doc

The man page of shred command will help you if you need more on shred.

Permanent link to this article: https://blog.openshell.in/2011/08/how-to-destroy-a-file-completely-in-linux-shred/

Scaling Image Using Java

Target Audience: Java Developers

What should you know already? Java, Java Advanced Imaging

As a Java developer I could say that image representation or image processing using Java is cumbersome work when you work with specific image processing algorithm. Sometimes some of the existing software allows the developers to develop user-defined modules, often using the same API used in the software. The developer may be used his/her own modules, but often there is a restriction of cost or licensing.

A royalty-free, portable and flexible solution is the Java Advanced Imaging API (JAI). JAI is a platform-independent extensible image processing framework. JAI provides a set of object-oriented interfaces that supports a simple, high-level programming model which allows images to be manipulated easily in Java applications and applets.

I am assuming that you have access to jdk 1.4 or later with JAI API installed. Use the following methods to scale an image.

Using “scale” Operator

[java]
public PlanarImage loadScaledImage(File image){
float scale=2.0f;
ParameterBlock pb=new ParameterBlock();
pb.addSource(image);
pb.add(scale);
pb.add(scale);
pb.add(0.0f);
pb.add(0.0f);
pb.add(new InterpolationNearest());
PlanarImage scaledImage=JAI.create("scale", pb);
}
[/java]

Without “scale” Operator

[java]
/**
*
* @param file The image file to scale or resize
* @param destWidth Desired scale value of width for preview
* @param destHeight Desired scale value of height for preview
* @return BufferedImage
*/
public BufferedImage loadScaledImage(String file, int destWidth, int destHeight) {
BufferedImage bim = null;
if (file != null) {
PlanarImage pim = JAI.create("fileload", file);
float xScale = (float) destWidth / pim.getWidth();
float yScale = (float) destHeight / pim.getHeight();
RenderedOp op = ScaleDescriptor.create(pim, new Float(xScale), new Float(yScale), new Float(0.0f), new Float(0.0f), null, null);
bim = op.getAsBufferedImage();
}
return bim;
}
[/java]

Permanent link to this article: https://blog.openshell.in/2011/07/scaling-image-using-java/

How to read XML document using JSTL

Target Audience: Java Web Developers, Web Component Developers, JSP Developers

What you should know already? Java Server Pages (JSP), JSTL

Java Server Pages (JSP) is the standard presentation layer in the J2EE platform. As you know, JSP provides scripting elements and action tags like <jsp:include /> and <jsp:useBean /> those can be used to generate content dynamically. Starting with JSP1.1 developers have been able to create their own actions in the form of custom tag libraries.

The JSP Standard Tag Library (JSTL) is part of JSP1.2 custom tag libraries. JSTL allows the JSP developers to use simple logic and iterative code in the form of tags rather than typical Java code in JSP.

In this article, you will know about how to parse XML document using JSTL XML tags.

The XML

The usage of XML on web is highly increasing since it is well suited for exchanging data among loosely coupled systems. By it’s design XML provides a flexible way of representing data using meaningful tags. For this reason, enterprise companies prefers XML to store semi-important data rather than databases.

Example XML

The following XML (spots_en_US.xml) represents the tourism places of a city.

[xml]
<tourism-spots>
<spot id="tour_home">
<name>The Pondicherry</name>
<img-path>/images/pondicherry-beach.jpeg</img-path>
<about-spot>
<about>Welcome to the peaceful land that is Puducherry.</about>
<about>Pondicherry, now known as Puducherry</about>
</about-spot>
</spot>
<spot id="matrimandir">
<name>Auroville, Matrimandir</name>
<img-path>/images/matrimandir.jpg</img-path>
<about-spot>
<about>Auroville is a universal township</about>
<about>It is made up of 50 communities</about>
</about-spot>
</spot>
</tourism-spots>
[/xml]

Parsing XML using JSTL XML Tag <x:parse>

In JSP file, the XML documents are retrieved using JSTL Core tag <c:import> and stored into a variable which is then use by <x:parse> tag as shown below:

[xml]
<c:import url="/xml/hotspots/spots_en_US.xml" var="spot_doc" />
<x:parse var="docu" xml="${spot_doc}"></x:parse>
[/xml]

The parsed XML document is stored in the variable named docu in the page scope. Now using XPath expressions you can select a particular element from the parsed XML document. See the following code, here the select attribute of <x:set> tag accepts XPath expression which returns zero or more matched elements from the referred XML document. In this example it will return exactly one spot element whose id is matrimandir The matched elements are stored in the variable aSpot.

[xml]
<x:set var="aSpot" select="$docu/tourism-spots/spot[@id=’matrimandir’]"/>
[/xml]

From our example document, now, we can say that the variable aSpot represents the following XML

[xml]
<spot id="matrimandir">
<name>Auroville, Matrimandir</name>
<img-path>/images/matrimandir.jpg</img-path>
<about-spot>
<about>Auroville is a universal township</about>
<about>It is made up of 50 communities</about>
</about-spot>
</spot>
[/xml]

Storing XML document into a variable does not present the data to the user. For that, we have to use typical HTML tags combined with JSTL XML tags, as shown below:

[xml]
<div>
<x:if select="$aSpot/img-path">
<img src="<x:out select="$aSpot/img-path"/>"
alt="<x:out select="$aSpot/name"/>, Pondicherry"
/>
</x:if>
<x:forEach select="$aSpot/about-spot/about" var="about_para">
<p id="about-para">
<x:out select="$about_para" escapeXml="false"/>
</p>
</x:forEach>
</div>
[/xml]
The following JSP shows you the whole code with small change. The change is here it reads the tourism spot id from request scope variable place2see (see line # 6)
[xml]
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
<c:import url="xml/hotspots/spots_en_US.xml" var="spot_doc" />
<x:parse var="docu" xml="${spot_doc}"></x:parse>
<x:set var="aSpot" select="$docu/tourism-spots/spot[@id=$requestScope:place2see]"/>
<html>
<head>
<title>Pondicherry Tourism <x:out select="$aSpot/name"/></title>
</head>
<body>
<h1 style="border-bottom: solid 1px #ddd;">
<x:out select="$aSpot/name"/>
</h1>
<div>
<x:if select="$aSpot/img-path">
<img src="<x:out select="$aSpot/img-path"/>"
alt="<x:out select="$aSpot/name"/>, Pondicherry"
/>
</x:if>
<x:forEach select="$aSpot/about-spot/about" var="about_para">
<p id="about-para">
<x:out select="$about_para" escapeXml="false"/>
</p>
</x:forEach>
</div>
</body>
</html>
[/xml]

Feel free to comment this blog. About Author

Permanent link to this article: https://blog.openshell.in/2011/07/how-to-read-xml-document-using-jstl/

Read and Write an XML File using PHP

How to read and write xml document with php’s DOMDocument. The DOM extension allows you to operate on XML documents through the DOM API with PHP 5. For better understanding, I have written comment for almost every line of code.

XML Code: file name – employee.xml

[xml]
<?xml version="1.0"?>
<employees>
<employee id="1" name="Albert"><age>34</age><![CDATA[
Employee details
]]><?salary $10000?></employee>
<employee id="2" name="Claud"><age>20</age><![CDATA[
Employee details
]]><?salary $2000?></employee>
</employees>
[/xml]

Reading an XML File using PHP

PHP Code:

[php]
<?php
// Create a new DOMDocument object
$doc = new DOMDocument();

// Load XML from a file
$doc->load( ’employee.xml’ );
//echo $doc->childNodes->length;

// Search for all elements with tag name ‘brand’
$employees = $doc->getElementsByTagName(’employee’);

//echo $employees->item(0)->getAttribute(‘id’);

// loop through brands and print brand name and price
foreach ($employees as $key => $employee) {

// print attribute value
echo $employee->getAttribute(‘id’). "<br />";
echo $employee->getAttribute(‘name’). "<br />";

// search for element ‘age’
$age = $doc->getElementsByTagName(‘age’);
// print node value
echo $age->item($key)->nodeValue. "<br />";

}
?>
[/php]

With the above XML file the PHP code will read the XML file and retrieve the information from it and output that in the screen.

Writing XML using PHP

PHP Code:

[php]
<?php
// Initial Array values
$employees = array();
$employees [] = array(
‘id’ => 1,
‘name’ => ‘Albert’,
‘age’ => ’34’,
‘salary’ => "$10000"
);
$employees [] = array(
‘id’ => 2,
‘name’ => ‘Claud’,
‘age’ => ’20’,
‘salary’ => "$2000"
);

// create doctype
$doc = new DOMDocument();
$doc->formatOutput = true;

// create root element
$r = $doc->createElement( "employees" );
$doc->appendChild( $r );

foreach( $employees as $employee )
{
// create child element
$b = $doc->createElement( "employee" );

// create attribute node
$id = $doc->createAttribute("id");
$b->appendChild($id);

// create attribute value node
$idValue = $doc->createTextNode( $employee[‘id’] );
$id->appendChild($idValue);

// add new attribute
$b->setAttribute(‘name’, $employee[‘name’] );

// create sub-child element
$age = $doc->createElement( "age" );
$b->appendChild( $age );

// create text node
$age->appendChild( $doc->createTextNode( $employee[‘age’] ) );

// create CDATA section
$cdata = $doc->createCDATASection("nEmployee detailsn");
$b->appendChild($cdata);

// create PI
$pi = $doc->createProcessingInstruction("salary", $employee[‘salary’]);
$b->appendChild($pi);

$r->appendChild( $b );

}

// save tree to string
echo $employeeDetails = $doc->saveXML();

// save tree to file
$doc->save("employee.xml")
?>
[/php]

This code will output the XML file in console as well as it will create a XML file in the name of employee.xml in the same directory of the PHP script.

Permanent link to this article: https://blog.openshell.in/2011/07/read-and-write-an-xml-file-using-php/

Display first sentence with PHP

To display the first sentence of the paragraph with PHP. Use this code

Code
[php]
function first_sentence($content) {
$pos = strpos($content, ‘.’);
if($pos === false) {
return $content;
}
else {
return substr($content, 0, $pos+1);
}
}
[/php]

Invoke the function like

[php]
echo first_sentence($content);
[/php]

Permanent link to this article: https://blog.openshell.in/2011/07/display-first-sentence-with-php/

uninitialized constant Rake::DSL Ruby Windows Solution

rake aborted!
uninitialized constant Rake::DSL
C:/Ruby192/lib/ruby/1.9.1/rake.rb:2482:in `const_missing’
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-0.9.1/lib/rake/tasklib.rb:8:in `’

Now, the first step I took towards solving this problem is to issue “bundle update rake” command to update my rake. This will update rake to the latest version. An example log for this update is provided below;

bundle update rake
Fetching source index for http://rubygems.org/
Using rake (0.9.1)
Using abstract (1.0.0)
….
….
Using sqlite3 (1.3.3)
Your bundle is updated! Use `bundle show [gemname]` to see where a bundled gem is installed

Now,Once this step is done you can check the version of Rake by using the command “bundle show rake”. The version of Rake came out to be 0.9.1. Now, inside your application you need to edit your Rakefile as shown below;

require File.expand_path(‘../config/application’, __FILE__)
require ‘rake’
#Use correct application name that matches for you
module ::Blog
class Application
include Rake::DSL
end
end

module ::RakeFileUtils
extend Rake::FileUtilsExt
end

Blog::Application.load_tasks

Also had to add require ‘rake/dsl_definition’ above require ‘rake’ in your Rakefile

if the rake version is less then 0.9.2 these problem will occure,
just put gem install rake -v=0.9.2
and bundle update

then
add this module in your rake file

module ::YourApplicationName
class Application
include Rake::DSL
end
end

or

try this first

gem update rake

Permanent link to this article: https://blog.openshell.in/2011/07/uninitialized-constant-rakedsl-ruby-windows-solution/

Window.open Not Working In IE

If you use window.open to open custom windows with JavaScript, you might encounter problems in Internet Explorer. Even though you can open new windows in other browsers, it doesn’t work in IE.

The reason to this is it depends on how you name your JavaScript windows. IE doesn’t allow space here.

window.open takes three parameters – window url, window name and window properties. Make sure the window name doesn’t contain other characters than a-z, A-Z and 0-9. No spaces, no hyphens, no aposthropes.

Note: Its Window name not window title.

[code lang=”js”]
window.open(‘https://blog.openshell.in/’,’blog’, ‘left=0,top=0,width=1024,height=800,channelmode=no, titlebar=yes,scrollbars=yes,toolbar=no,menubar=no, resizable=yes,copyhistory=no,directories=no,status=yes’);
[/code]

Permanent link to this article: https://blog.openshell.in/2011/07/window-open-not-working-in-ie/

Loading Javascript dynamically using jquery

If you are creating a web application, then your web pages may have all possibilities of being overwhelmed with a number of JavaScript files. Including large number of JavaScript files may slow down your web page. So its a good idea to load JavaScript dynamically to your web page, i.e load them only when these are needed. This strategy can help you reducing the load time of your web pages. Fortunately jQuery comes with a built in utility for this feature. The $.getScript function of jQuery provides us this power. The function works like simple AJAX call that includes the remote JavaScript files dynamically in our web page.

Lets see the syntax of this function:

[code lang=”js”]$.getScript(url,callback)
[/code]

Fetches the script specified by the url parameter using a GET request to the specified server, optionally invoking a callback upon success.
Parameters
url (String) The URL of the script file to fetch.
callback (Function) An optional function invoked after the script resource has been loaded and evaluated.
The following parameters are passed:
¦ The text loaded from the resource
¦ The string success
Returns
The XHR instance used to fetch the script.

Usage:
We will load ‘new.js’ dynamically to our web page.

‘new.js’ looks as:
[code lang=”js”]
var testVar = ‘New JS loaded!’;

alert(testVar);

function newFun(dynParam)
{
alert(‘You just passed ‘+dynParam+ ‘ as parameter.’);
}
[/code]

HTML Markup

[code lang=”js”]
<script src="../jquery.js" type="text/javascript"></script>
<script type="text/javascript">// <![CDATA[

$(function()
{
$(‘#loadButton’).click(function(){
$.getScript(‘new.js’,function(){
newFun(‘"Checking new script"’); //This is the external function present in new.js
});
});
});
// ]]></script>
[/code]

[html]
<button id="loadButton">Load</button>
[/html]

Permanent link to this article: https://blog.openshell.in/2011/06/loading-javascript-dynamically-using-jquery/

Create an RSS feed with PHP

Having an RSS feed on your website is a great way of sharing your content with the rest of the Internet. It’s not a new technology and it’s probably something that you use on a daily basis. If you have a blog or use any form of CMS, that software will most likely handle the creation of your RSS feed for you.

Sometimes, however, it might be necessary for you to create a RSS feed yourself. Perhaps you have just won a new client who’s current site has a old bespoke CMS which they like and want to keep, but you want to be able to publish their updated content via RSS. Hopefully this tutorial will help you to achieve this.

What is RSS?

RSS, in its current form, stands for Really Simple Syndication and is a family of web formats to publish frequently updated content. The RSS Feed (as it is commonly known) can then be read by the users feed reading software or by another website which wishes to ‘syndicate’ the content of that feed.

The RSS format is based on XML that is built using standardised tags. Here is an example of a basic RSS document, we will be generating something similar using PHP later in this tutorial:

[xml]
<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">
<channel>
<title>My RSS feed</title>
<link>http://www.openshell.in/</link>
<description>This is an example RSS feed</description>
<language>en-us</language>
<copyright>Copyright (C) 2011 openshell.in</copyright>
<item>
<title>Topic 3</title>
<description>This is an example of topic desc</description>
<link>http://www.openshell.in/topic3.html</link>
<pubDate>Mon, 14 Feb 2011 09:27:16 +0000</pubDate>
</item>
<item>
<title>Topic 2</title>
<description>This is an example of topic desc</description>
<link>http://www.openshell.in/topic2.html</link>
<pubDate>Wed, 12 Jan 2011 12:00:00 +0000</pubDate>
</item>
<item>
<title>Topic 1</title>
<description>This is an example of topic desc</description>
<link>http://www.openshell.in/topic1.html</link>
<pubDate>Wed, 05 Jan 2011 15:57:20 +0000</pubDate>
</item>
</channel>
</rss>
[/xml]

Creating the feed

Step 1: Create the .htaccess file

As we know, RSS is made up of standardised XML tags which you would normally find in a flat XML file. What we are going to do is create this standard XML data dynamically using PHP. We’re going to tell our server to treat files with an .xml extension as PHP files.

To do that, simply create a file named .htaccess. Add AddType application/x-httpd-php .xml and save it to your server. If you have access to your server’s configuration (httpd.conf) file, you can put the AddType declaration there instead.

Keep in mind that this will cause all files with an .xml extension in that directory (or on your server) to be parsed as PHP.

Step 2: Add the RSS ‘Header’ Info[Set the header]

By default, PHP sends text with an HTML content type. As a result, your aggregator may not recognize it as a valid RSS / XML file.

Enter the header() function. We’ll put the following header near the top of our script, before any text gets sent to the client.

[html]
<? header(‘Content-type: application/rss+xml; charset=ISO-8859-1’); ?>
[/html]

Step 3: A basic-begin the feed

A basic RSS document consists of a channel. Within each channel are items. At minimum, each item requires a title, description and link. In this example, we’re also going to add a publication date.

Now we are going to define our database connection details and create the header XML tags for our RSS feed.

RSS feed should start off like this:

[php]
DEFINE (‘DB_USER’, ‘my_username’);
DEFINE (‘DB_PASSWORD’, ‘my_password’);
DEFINE (‘DB_HOST’, ‘localhost’);
DEFINE (‘DB_NAME’, ‘my_database’);

$rssfeed = ‘<?xml version="1.0" encoding="ISO-8859-1"?>’;
$rssfeed .= ‘<rss version="2.0">’;
$rssfeed .= ‘<channel>’;
$rssfeed .= ‘<title>My RSS feed</title>’;
$rssfeed .= ‘<link>http://www.openshell.in</link>’;
$rssfeed .= ‘<description>This is an example RSS feed</description>’;
$rssfeed .= ‘<language>en-us</language>’;
$rssfeed .= ‘<copyright>Copyright (C) 2011 openshell.in</copyright>’;
[/php]

Step 4: Retrieve articles from the database

We need to extract our data by looping through our MySQL database. We’ll need a title, description and publication date for each item.

[php]
$connection = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
or die(‘Could not connect to database’);
mysql_select_db(DB_NAME)
or die (‘Could not select database’);

$query = "SELECT * FROM mytable ORDER BY date DESC";
$result = mysql_query($query) or die ("Could not execute query");

while($row = mysql_fetch_array($result)) {
extract($row);

$rssfeed .= ‘<item>’;
$rssfeed .= ‘<title>’ . htmlentities(strip_tags($title)) . ‘</title>’;
$rssfeed .= ‘<description>’ . htmlentities(strip_tags($description)) . ‘</description>’;
$rssfeed .= ‘<link>’ . $link . ‘</link>’;
$rssfeed .= ‘<pubDate>’ . date("D, d M Y H:i:s O", strtotime($date)) . ‘</pubDate>’;
$rssfeed .= ‘</item>’;
}

$rssfeed .= ‘</channel>’;
$rssfeed .= ‘</rss>’;

echo $rssfeed;
?>
[/php]

Using a while loop, we’re making the script output an RSS item for each row returned from the database. We’re also using strip_tags() and htmlentities() to strip HTML from the title and body, and to escape ampersands and quotes.

From beginning-to-end, your script should look a bit like this:

[php]
<?php
header("Content-Type: application/rss+xml; charset=ISO-8859-1");

DEFINE (‘DB_USER’, ‘my_username’);
DEFINE (‘DB_PASSWORD’, ‘my_password’);
DEFINE (‘DB_HOST’, ‘localhost’);
DEFINE (‘DB_NAME’, ‘my_database’);

$rssfeed = ‘<?xml version="1.0" encoding="ISO-8859-1"?>’;
$rssfeed .= ‘<rss version="2.0">’;
$rssfeed .= ‘<channel>’;
$rssfeed .= ‘<title>My RSS feed</title>’;
$rssfeed .= ‘<link>http://www.openshell.in</link>’;
$rssfeed .= ‘<description>This is an example RSS feed</description>’;
$rssfeed .= ‘<language>en-us</language>’;
$rssfeed .= ‘<copyright>Copyright (C) 2009 openshell.in</copyright>’;

$connection = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
or die(‘Could not connect to database’);
mysql_select_db(DB_NAME)
or die (‘Could not select database’);

$query = "SELECT * FROM mytable ORDER BY date DESC";
$result = mysql_query($query) or die ("Could not execute query");

while($row = mysql_fetch_array($result)) {
extract($row);

$rssfeed .= ‘<item>’;
$rssfeed .= ‘<title>’ . htmlentities(strip_tags($title)) . ‘</title>’;
$rssfeed .= ‘<description>’ . htmlentities(strip_tags($description)) . ‘</description>’;
$rssfeed .= ‘<link>’ . $link . ‘</link>’;
$rssfeed .= ‘<pubDate>’ . date("D, d M Y H:i:s O", strtotime($date)) . ‘</pubDate>’;
$rssfeed .= ‘</item>’;
}

$rssfeed .= ‘</channel>’;
$rssfeed .= ‘</rss>’;

echo $rssfeed;
?>
[/php]

Step 5: Include RSS feed on your site

Now that you have created your feed, you can link to it from your site or publish it. But there is an extra step which will allow your visitor’s browsers to automatically detect the RSS feed on your site. In the section of you pages, include the following tag:

[html]<link rel="alternate" href="rssfeed.xml" title="My RSS feed" type="application/rss+xml" />[/html]

Permanent link to this article: https://blog.openshell.in/2011/06/create-an-rss-feed-with-php/