Search Here

Custom Search
Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Saturday, June 7, 2008

Properly display paragraphs in browser

When we write something in several paragraphs in textarea of browser then it shows as it is in teaxtarea. But when we want to show the text in read only mode then the paragraphs loose their identity. I mean you can not separate the paragraphs from each other. So we need to replace the newlines of the text by
.

I have written a simple function named ‘keepWhiteSpace’ to do this conversion and I used it in many of Therap's forms to display the saved text as it is. Usage is very simple.

For example, if you want to show your text in a cell then write as follows:

${therap.keepWhiteSpace(myText)}

The implementation is very simple.

<#function keepWhiteSpace value>
<#return value?replace("\r\n","<br>")?replace("\n\r","<br>")?replace("\r","<br>")?replace("\n","<br>")>
</#function>

Ftl(Free Marker Tag Library) users can use it without rewriting any new one. JSP users need to write a tag using the logic.

Note that I haven’t replaced spaces by

Wednesday, January 16, 2008

A simple method to compare versions

Sometimes we may need to compare two versions to know which one is newer and which one is older or whether both of those are same versions. Let me explain. Suppose you have two versions in hand as String “7.2.1″ and “7.1.1″. Now you wanna know which one is the later version. At the very first sight you may think that simple string comparison will give the correct decision. But unfortunately it won’t work correctly always. Think about this input : “10.0.1″ and “9.5.2″. String comparison will return the second one as latest version. But you expect the first one. So definitely we should change the algorithm.

A very simple algorithm is to split the input string by “.” and compare individual components. It guarantees you the correct output. Here is my implementation of the method. I have applied Unit Testing (www.junit.org , www.nunit.org) on it and it passes all possible test cases. You can use it without any doubt and it works for any length of versions.

Here is the implementation. Same code will run both in Java and C#.NET



/* This method compares two given versions and returns 0,1 or -1
It returns 0 if both versions are equal,
1 if first version is newer than second one,
-1 if first version is older than the second one.
Example:
compareVersion("7.1","7.1") will return 0
compareVersion("7.1.1","7.1.1") will return 0
compareVersion("7.1","7.1.1") will return -1
compareVersion("7.1","7.2") will return -1
compareVersion("7.1","7.2.2") will return -1
compareVersion("7.2","7.1") will return 1
compareVersion("7.2.0","7.1") will return 1
compareVersion("7.2.0","7.2") will return 0
compareVersion("7.abc.0","7.2") will throw exception
*/
public static int compareVersion(String v1, String v2) throws Exception {
String[] v1Tokens = v1.split("\\."); // for C#: String[] v1Tokens = v1.split(".");
String[] v2Tokens = v2.split("\\."); // for C#: String[] v2Tokens = v2.split(".");
int maxLen;

if (v1Tokens.length > v2Tokens.length)
maxLen = v1Tokens.length;
else
maxLen = v2Tokens.length;

for (int i = 0; i < maxLen; i++) {
int n1, n2;

n1 = n2 = 0;

try {
if (i < v1Tokens.length)
n1 = Integer.parseInt(v1Tokens[i]);

if (n1 < 0)
throw new Exception(v1 + " is not a valid version.");
} catch (Exception ex) {
throw new Exception(v1 + " is not a valid version.");
}

try {
if (i < v2Tokens.length)
n2 = Integer.parseInt(v2Tokens[i]);

if (n2 < 0)
throw new Exception(v2 + " is not a valid version.");
} catch (Exception ex) {
throw new Exception(v2 + " is not a valid version.");
}

if (n1 > n2)
return 1;
else if (n1 < n2)
return -1;
}

return 0;
}