Friday, October 28, 2011

Shell Scripting 1 - What is Linux Shell ?

Do you know what Linux 'Kernel' is? Simply, it is the heart of the Linux Operating System. It manages the resources. (ie. Memory management, File management, I/O management, Device management, etc.) Kernel lies close to the hardware. So the language it can understand is more over low level. 

For humans to work with the operating system, there are human readable commands. Shell is the special program which converts those human readable commands in to the format which Kernel can understand. So shell is also a user program, which provides a more convenient environment for humans to interact with the system. 


Shell executes commands that are given from the keyboard or a file. There are several shells.
  • BASH
  • CSH
  • KSH
  • TCSH
You can access the Shell simply by opening a terminal. Or else you can connect through SSH (Secure Shell).

Note :  All the available shells in the system can be found in /etc/shells file.
            Current shell name is assigned to SHELL variable.

Let's see what is Shell Scripting in my next post.

Monday, October 24, 2011

Vaadin - Including HyperLinks Inside Text

Vaadin is meant for creating Java web applications. It has built in "Link" component which is needed to include hyperlinks. It works as follows;
Link link = new Link("Click Here", 
                                 new ExternalResource("http://vaadin.com/")); 
//
But when we are using this Link component, the entire String (ie. "Click Here" in the above example), becomes the hyperlink.

But if we want something like, only the word "Here" to be the hyperlink and the "Click" to be in normal text, Vaadin has no solution for it. Vaadin doesn't have a component to use for that.


Left side hyperlink is created using "Link" component and the right side hyperlink is created using "CustomLyout"

To include that kind of a hyperlink we have to use Vaadin "CustomLayout" component. Here is how to create a hyperlink using CustomLayout.

With the CustomLayout component we can create Layouts as a template in XHTML.
String text = "Click " + "<a href='http://vaadin.com/'>" + "Here" + "</a>";
CustomLayout customLayout = new CustomLayout(new ByteArrayInputStream(text.getBytes()));
//

Further we can add styles to the above created CustomLayout, just as we are doing for other Vaadin UI Components.

Thursday, October 20, 2011

How to add Syntax Highlighting in Blogger

Do you like to post your code snippets in your blog as below ?
public class HelloWorld {
    
    public static void main(String[] args) {
        
        System.out.println("Hello World");
    }
}

It looks really pretty. SyntaxHighlighter is the solution for it.

You can download the SyntaxHighlighter from here.

You can either upload the necessary files to your preferred location or you can access them from google-code's SyntaxHighlighter project files. But when accessing them from google code project files, css styles need to be copied and pasted in to your blog's HTML template. It's bit risky, instead you can upload the files in to your website. (https://sites.google.com/ is a really cool place where you can easily upload these files).

To enable code syntax highlighting in your blog posts, you need to add few lines to the html code of your template.

To do this, go to Design -> Template -> Edit HTML -> Proceed

Then add the following lines between <HEAD></HEAD> tags. (you can simply insert them just before the </HEAD> tag).
<link href='YOUR-UPLOADED-LOCATION/SyntaxHighlighter.css' rel='stylesheet' type='text/css'/>
<script src='YOUR-UPLOADED-LOCATION/shCore.js' type='text/javascript'> 
</script> 
 
<!--According to the programming language you want to post in your blog-->
<!--include the following lines as necessary-->
<!--Below 2 lines are for java codes and xml codes--> 
<script src='YOUR-UPLOADED-LOCATION/shBrushJava.js' type='text/javascript'>
</script> 
<script src='YOUR-UPLOADED-LOCATION/shBrushXml.js' type='text/javascript'> 
</script> 
Of course you can insert the relevant lines for all the available programming languages in  SyntaxHighlighter. But this will take some more time to load your blog. So include only necessary languages.

Then insert the following lines just before </BODY> tag and save the template. Now the changes to the template are done.
<script language='javascript'>
dp.SyntaxHighlighter.BloggerMode();
dp.SyntaxHighlighter.HighlightAll('code');
</script> 
Now, you can compose a blog post with code snippets. When including code snippets, you should write them between <pre></pre> tags. To do this you have to switch the HTML tab in your blog editor.
<pre name="code" class="java">
...Java code goes here...
</pre>
To insert a Java code you should mention "java" for the "class" attribute. Like this, for all the available programming languages, there is a specific attribute value for "class" attribute. You can find the supported language list and the relevant attribute values from here.
(ie. cpp, c, c++, c#, c-sharp, csharp, css, delphi, pascal, java, js, jscript, javascript, php, py, python, rb, ruby, rails, ror, sql, vb, vb.net, xml, html, xhtml, xslt)

But, remember one thing, you can't include XML, HTML code as it is. < and > symbols will cause issues in displaying your blog post. So replace them with their counterpart escape characters. For replace < with &lt; and replace > with &gt;

You can read more on SyntaxHighlighter from here.

A small note on how to upload a file on to https://sites.google.com/

Once you create a site on https://sites.google.com/ go to More -> Manage site -> Attachments. There you can upload your files. :)

Monday, October 17, 2011

Date Validation in Java

Java's DataFormat class may be very familiar to you. We use it very often when we want to format a Date object, for example;


public class DateValidationExample {
   
    public static void main(String[] args) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy");

        String formattedDate = simpleDateFormat.format(new Date()); 

        System.out.println(formattedDate); 

     }
}
The output will be  

      "01-Nov-2011"

Now let's try it the other way around. Let's try to parse a String as a Date.


public class DateValidationExample {
   
    public static void main(String[] args) {

        String dateAsString = "01-Nov-2011";

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy"); 

        Date date = simpleDateFormat.parse(dateAsString); 

        System.out.println(date); 

     }
}

The output will be

      "Tue Nov 01 00:00:00 GMT 2011"

Now let's try "31-Nov-2011". Note that November doesn't have a 31st. But if we try the following code it will give an output without any problem.


public class DateValidationExample {
   
    public static void main(String[] args) {

        String dateAsString = "31-Nov-2011";

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy"); 

        Date date = simpleDateFormat.parse(dateAsString); 
        
        System.out.println(date); 

     }
}


The output will be 

      "Thu Dec 01 00:00:00 GMT 2011" 

How does this happen ?

Although the month November has only 30 days, the parser will assume that the user means,

      "30th November + 1 day"

So it will give the output as 1st of December.

But in some cases we need to validate the dates. For example, when we check for input validation, when a user enters an invalid date like the above 30th of November or 31st of February, our program should be capable of validating the date and showing user the date entered is incorrect. So how can this be achieved in Java ?

Answer is simple. In Java DateFormat class, there is a method called 'setLenient()'. When we set this setLenient(false) in our code it will check whether the date is a correct calendar date, otherwise throws an exception.

Let's try the following code to clearly understand this. 



public class DateValidationExample {
   
    public static void main(String[] args) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy"); 

        simpleDateFormat.setLenient(false);

        Date date = simpleDateFormat.parse("31-Nov-2011"); 

        System.out.println(date); 

     }
}

The output here is

      java.text.ParseException: Unparseable date: "31 Nov 2011"
 
Thus we can validate dates. :)

Thursday, October 13, 2011

CSS Tips

Centering a web page in the browser

Do you feel there's nothing to worry about centering a web page in the browser ? But it really matters when you are testing your website for browser compatibility. That is because browsers have default style sheets that are loaded automatically. These default style sheets work as foundation style sheets for other style sheets.

If you've ever tried to center a web page and tested it for different web browsers, you may have experienced issues with internet explorer.

For Chrome and Firefox 
Let's specify a main div tag for the main layout we are using. Then specify a style for that div tag.


Remember, this div should have a fixed width

But this styles definition will not work for internet explorer.

In order to overcome this issue you have to include the following css styles to your style sheet.

For Internet Explorer
The style should be given to body tag.


Then we have a little more work to do. We should specify text-align : left; for main div tag's style, otherwise it will inherit text-align : center; from the body and will try to center all the elements inside it.

So finally the basic html code and styles look like in the following code. I've used embedded styles for the easiness to  understand.


Below image shows how it  is displayed in the browser.