Tuesday, June 22, 2010

Top ten footballers in Fifa Worldcup

10 Best Footballers in the FIFA World Cup 2010

















Thursday, June 10, 2010

Parsing Excel files in Java

Hi Friends,

Recently i worked on parsing the Microsoft Excel Files in java and want to share some useful info with you.

I found two good API's for this purpose.

1. Apache POI(free)
2. Smart XLS(free for non commercial use)

Apache POI:
Its a free API made by apache. You can parse the following things
No of Physical rows, Sheets Count, Cell Values, Cell formula,Styles(not full colors),But No Chart Support. It can read both xls and xlsx formats.

Smart XLS:
It is a good API as compared to POI.It parses almost all the values and have additional chart support as well. We can get Chart basic attributes like Series Count, Series Formula, Series Names, Series Category Formula etc etc. It also support the styling of each cell.


Regards,
Vajahat Ali Niazi
Lahore, Pakistan

Match Differences Between Two Strings

Hi friends,

Once i was asked to write an algorithm which matches the two strings and tell the difference between them.

In other words we have two sentences
1. Its raining cats and dogs since morning
2. Its raining monkey and dogs since morning.

than the algorithm should efficient enough to detect the difference between the two strings.
In the above example it should tell that there are two difference in the string.

i-e monkeys found at the place of cat.

In order to implement the algorithm i would recommend to use


Diff takes two texts and finds the differences. This implementation works on a character by character basis. The result of any diff may contain 'chaff', irrelevant small commonalities which complicate the output. A post-diff cleanup algorithm factors out these trivial commonalities.

Google Difference Match patch
It is an open source algorithm written in the Java, JavaScript, C++, C#, Lua and Python.
You can find it here
http://code.google.com/p/google-diff-match-patch/

The Diff Match and Patch libraries offer robust algorithms to perform the operations required for synchronizing plain text.

  1. Diff:
    • Compare two blocks of plain text and efficiently return a list of differences.
    • Diff Demo
  2. Match:
    • Given a search string, find its best fuzzy match in a block of plain text. Weighted for both accuracy and location.
    • Match Demo
  3. Patch:
    • Apply a list of patches onto plain text. Use best-effort to apply patch even when the underlying text doesn't match.
    • Patch Demo

Currently available in Java, JavaScript, C++, C#, Lua and Python. Regardless of language, each library features the same API and the same functionality. All versions also have comprehensive test harnesses.


Thanks
Vajahat Ali Niazi
Lahore Pakistan

Wednesday, May 19, 2010

Software Engineering Joke : Difference Between Defect and a Bug

Once Asked in an Interview, What Is the Difference Between Defect and a Bug?

Candidate: Can I Explain this with an example.

Employer: Yes

Candidate: Once a guy went to a tea shop and ordered one cup of tea and when the tea came he saw a fly in his cup.
Now this is a BUG.



Employer: Laughed and said my dear now you tell me what is a defect?


Candidate: Once a Guy went to a tea shop and ordered a tea and when a tea came he tasted and said there is less sugar in the tea.
Now this is a defect!



Employer: Laughed Hahahaha Haaa and said Can You Elaborate what you want to say?

Candidate: Yes Sir!
You can manage to drink a cup of tea with less or more sugar but you can’t drink a cup of tea with a bug.

I.e.:

Product can’t be delivered with a Live Bug.
Quality of the Product can be compromised with a defect.

Thursday, April 29, 2010

How to extract comments from pdf using Itext

Hi Friends,

Here is the code of extracting comments from the PDF file using the itext.This extracts the comments of each page one by one and print them on the console.


/******************************************************************************/
package pdftest;
import com.itextpdf.text.pdf.PdfArray;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfObject;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfString;
import java.util.ListIterator;

public class CommentsTest {

public static void main(String args[]) {
try {
System.out.println("Hi in the Comments Test");
PdfReader reader = new PdfReader("c:\\temp\\testattachments.pdf");

for (int i = 1; i <= reader.getNumberOfPages(); i++)
{

PdfDictionary page = reader.getPageN(i);
PdfArray annotsArray = null;

if(page.getAsArray(PdfName.ANNOTS)==null)
continue;

annotsArray = page.getAsArray(PdfName.ANNOTS);
for (ListIterator iter = annotsArray.listIterator(); iter.hasNext();)
{
PdfDictionary annot = (PdfDictionary) PdfReader.getPdfObject(iter.next());
PdfString content = (PdfString) PdfReader.getPdfObject(annot.get(PdfName.CONTENTS));
if (content != null) {
System.out.println(content);
}
}
}


} catch (Exception e) {
e.printStackTrace();
}

}
}

/******************************************************************************/


Regards,
Vajahat Ali

Wednesday, March 31, 2010

Remove Atatchments from PDF file using itext

Hi Friends:
Here is the code that will be used to unpack(remove) the attachments from the PDF using itext.


PdfReader reader = new PdfReader(filename);
PdfDictionary root = reader.getCatalog();
PdfDictionary documentnames = root.getAsDict(PdfName.NAMES);
PdfDictionary embeddedfiles = documentnames.getAsDict(PdfName.EMBEDDEDFILES);
PdfArray filespecs = embeddedfiles.getAsArray(PdfName.NAMES);
PdfDictionary filespec;
PdfDictionary refs;
FileOutputStream fos;
PRStream stream;
for (int i = 0; i < filespecs.size(); ) {
filespecs.getAsName(i++);
filespec = filespecs.getAsDict(i++);
refs = filespec.getAsDict(PdfName.EF);
for (PdfName key : refs.getKeys()) {
fos = new FileOutputStream(filespec.getAsString(key).toString());
stream = (PRStream) PdfReader.getPdfObject(refs.getAsIndirectObject(key));
fos.write(PdfReader.getStreamBytes(stream));
fos.flush();
fos.close();
}
}