Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

June 07, 2009

Converting Double to String without E notation

Last week, I got a task of creating a data loader for one of application. The huge data was presented to me in the form of excel files and for sure there is no better way than using program to read the excel files and load the data to database. I jump started to build the loader application using the Apache POI library. The data types of table schema and data read from the excel didn't match!

There is the trouble: A numeric field in excel files was represented as string in database. Even though this number was not used for calculation, it seems for some reason, the database designer has kept it that way! Trouble started when the POI library's getNumericCellValue() method returned the numbers in the form of scientific notation. The number 12,345,600 would be returned as 123.456E5. But I need it in plain text 1245600 without scientific notation and grouping of numbers!

Solution to this problem lies in configuring NumberFormat or DecimalFormat class found in the package java.text. Here is solution code:


Double comSysNumber = cell.getNumericCellValue();
NumberFormat f = NumberFormat.getInstance();
f.setGroupingUsed(false);
String refinedNumber = f.format(comSysNumber);
Now the string variable refinedNumber is all set to be stored into my database :-). Hope this becomes useful to others.

March 11, 2009

Identifying MIME using mime-util library


Mime-util is a very small, easy-to-use MIME detection library for Java. It can be used for any type of Java application and can detect MIME types from different sources like File, InputStream, URLConnection, byte array etc. Recently 1.3 version of the library was released and had a look at it.


The new release brings in some changes in packages by deprecating eu.medsea.util and bringing in eu.medsea.mimeutil package. The library still has the old package for backward compatibility. The MIME detection is based on the Unix magic mime files which is used by Unix file command. When using the library, it actually tries to access this file for detecting the MIME type. For other platforms, the library uses an internal copy of magic.mime file. This is available in eu.medsea.mimeutil

Working with the library is very simple. I tried out a simple swing application where the user will select a file using JFileChooser and the MIME type is detected and displayed. Assuming that you have got the absolute path of the file, here is my code:



FileInputStream fis = new FileInputStream(dataDisplayLabel.getText());
BufferedInputStream bis = new BufferedInputStream(fis);
MimeUtil mimeUtilObject = new MimeUtil();
log.warn("Support for Mark and Reset: " + fis.markSupported());
log.warn("Support for Mark and Reset: " + bis.markSupported());
log.info("Stream size: " + fis.available());
Collection coll = mimeUtilObject.getMimeTypes(bis);
log.info(coll.size());
Iterator itr = coll.iterator();
while(itr.hasNext()) {
MimeType mt = itr.next();
log.info("Media type: " + mt.getMediaType());
log.info("Sub Type: " + mt.getSubType());
}
One important point to note is that FileInputStream does not support mark and reset methods. For MIME detection, you will have to provide a InputStream that supports mark and reset methods. In this case, I have used BufferedInputStream and it is fed into getMimeTypes method. After detection of MIME type all the methods they return a collection. You will have to iterate this collection and get media type and sub type using separate APIs.

Even though this library will not be frequently used, it can be used for validation of files during upload or transfer. It is not always safe to check the file extension and proceed with your program logic. You can download mime-util library from sourceforge.

June 05, 2008

Using XSL-FO to create PDF files

Libraries like iText,PDFBox are some of the best libraries available for PDF creation and manipulation. But these libraries have its disadvantages. One such disadvantage is templating the PDFs you create. In a enterprise where reports (PDF files) are generated automatically and routed to work-flows or stored in content management repositories. These reports follow an enterprise level style and pattern which can be reused, can change and will require proper management. Hardcoding styles and formats of reports into your application can cause lots of issues in future. That's when XSL-FO and Java (or other language) come into picture. In this article I will introduce XSL-FO and Apache FOP.

Let me intoduce XSL-FO first. XSL-FO stand for Extensible Stylesheet Language Formatting Objects and is a language for formatting XML data. It was initially part of the XSL W3C recommendation. So W3C has come up with XSLT for transforming XML documents and XSL-FO for formatting. XSL-FO documents are XML files that contain information about the output layout and output contents. But they do not specify the content type of the output. This output can be any document type; all you need is a transformation library that transform the XSL-FO to the appropriate content. In this article I will make use of Apache FOP, which is part of Apache XML Graphics Project.

First look at XSL-FO:
Here is a sample XSL-FO file. Like any other XML file, its starts with XML declaration:
<?xml version="1.0" encoding="utf-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

<fo:layout-master-set>
<fo:simple-page-master master-name="A4">
<fo:region-body margin="2cm" />
<fo:region-before/>
</fo:simple-page-master>
</fo:layout-master-set>

<fo:page-sequence master-reference="A4">
<fo:flow flow-name="xsl-region-body">
<fo:block>Hello World!</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
The element is the root element of XSL-FO document. contains one or more template pages and master-name attribute specify the name for the template. elements describe the page contents. contents are not directly placed, they are placed inside a flow which in turn hold block. Each block can hold the data that is displayed. For further details please go through the tutorial.

Apache FOP:
Apache FOP (Formatting Objects Processor) claims to be the first processor that reads a formatting object (FO) tree and renders the resulting pages to a specified output. The primary output format is PDF but the following formats are supported:
  • PDF (Portable Document Format)
  • PS (Adobe Postscript)
  • PCL (Printer Control Language)
  • AFP (MO:DCA)
  • SVG (Scalable Vector Graphics)
  • XML (area tree representation)
  • Print
  • AWT/Java2D
  • MIF
  • RTF (Rich Text Format)
  • TXT (text)
Using the FOP library, you will be able to process any XSL-FO and render it to PDF. The library is very simple and straight forward. Since all the details of formatting is in the XSL file, all we need is to make use of the transformation API to transform and generate the output. This content is stored as file on your file system. FOP makes use of JAXP. Have a look at the Java application to transform a simple XSL-FO document into PDF.
OutputStream outputPDF = new BufferedOutputStream(new FileOutputStream("hello.pdf"));

FopFactory ff = FopFactory.newInstance();
Fop fop = ff.newFop(MimeConstants.MIME_PDF, outputPDF);
TransformerFactory factory = TransformerFactory.newInstance();

Transformer transformer = factory.newTransformer();
Source src = new StreamSource(new File("hello.fo"));
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
For your reference, you may download the sample application. Like I mentioned, the main advantage will be to use XSL for formatting and designing the templates of different and simple use the API to generate the complete report. This way, all reports will have consistency and it will be easy to modify the reports style if necessary.

April 12, 2008

Introducing Java Kernel...

Sun recently released Java SE 6 Update 10 Beta, and this release has some interesting updates to Java SE 6! With the stiff competition on the web application and RIA arena, all players are "in" to provide the best of their platform at the earliest. With this release, Sun is all set to provide some major improvements on Java Applets which has been ignored for quite long.

So, what's so great with this release? The new beta focuses on three major improvements:
  • Enhanced Java deployment
  • Improved performance and look & feel
  • Next-Generation Java Plug-In

Java, for quite long has been one of the most preferred programming language even though the size of JRE has been a issue. Once a Java application is up and running, the sailing is smooth. New JRE has been stable, fast and more reliable. But, the bottle neck was getting the application up and running. Some of the major challenges faced presently are:
  • Difficult to detect JREs, especially from a web browser
  • Difficult to automatically install new JREs
  • Large download size of JREs
  • Poor cold start performance
  • Little overlap between applets and Web Start programs
And the new beta release is aimed in solving these challenges sooner than later.

Java Kernel
The JRE provides many different APIs: Swing, AWT, ImageIO, SQL, CORBA, RMI, XML and much more. The new distribution is aimed at getting Java software up and running faster. So we have,Java Kernel which divides JRE libraries into small bundles. The kernel will include only the most commonly needed JRE components. Additional components are downloaded as needed, and the JRE will download remaining components in the background and then reassemble itself.


Java Delopyment Toolkit
Client using Java have different variety or flavor of JRE installed. Also, there JREs run on different operating systems. Detecting the JRE version is one of the major challenge and deployment toolkit will help in a consistent Java content to all these clients. The deployment toolkit will provide a powerful JavaScripts library that will help in automatically installing Java Platform for Java Plug-in applets and Java Web Start applications.

New Look and Feel
With this new release comes a new cross-platform Swing look & feel called Nimbus. It provides a polished look to applications which choose to use it. And because Nimbus is drawn entirely using Java 2D vector graphics, rather than static bitmaps, it's tiny (only 56KB!) and can be rendered at arbitrary resolutions.

Performance
This release includes improvements to patch downloading and installation. The new download engine can resume interrupted downloads automatically. It also uses a new patching algorithm which will significantly reduce the size of future updates.

Well that's all about the new release. If you want to try out these features, I suggest you quick get the early access download and have a look!

October 07, 2007

Improved modularity in Java 7

Java 7 has been evolving @ at jdk7.dev.java.net for some time now. I decided to peep in and have a look at the features of what the next generation JDK has to offer. One of the eye catching features is the enhancement of modularity feature in JDK7. Technically we can call it: "JSR 294", but for now let’s have a look at it.

The JSR 294 calls for language extensions in support of information hiding and separate compilation. Along with this we also have JSR 277 which calls for a new form of distribution format or say new "jarring" technology. According to D.L. Parnas information hiding is :

Each module is characterized by its knowledge of a design decision which hides from all others. Its interface is chosen to reveal as little as possible about its inner workings.


So, Information hiding is powerful and should be enabled by the language itself. Java already has packages for information hiding. What is wrong with this? What are its limitations? For this we have the new "super package" or JSR 294!

Java packages supports information hiding, prevents name conflicts and they can be hierarchical. But projects are not confined to just one simple package. Thus public in a package become too "public"! Well what does that mean? Let’s have a look... In the current scenario, we can have a package P and another P.Q. Now when we import P.* to a client, we are able to access the entire P package’s hierarchy. What if we want to restrict P.Q package? So the goal of new language construct is to have an entity bigger than the package, provide a runtime access control, interfaces for packages and a new basis for deployment modules.



Thus, superpackage is defined as: A superpackage is a named collection of one or more packages or superpackages and their types. For example:

superpackage example.sample.lib {

// member packages
member package example.sample.lib;

// member superpackages
member superpackage example.sample.lib.network, example.sample.lib.parser;

// list of exported types
export example.sample.lib.SampleClass;

export superpackage example.sample.lib.network;

}

By using the superpackage feature we can limit the access to certain package part. We can depict it like this:



So, how are we going to work? What changes and do we implement this superpackage? To implement we will have another java file say, super-package.java. This file will declare which type belongs to the superpackage. The normal java class file will not have information on which superpackage it belongs to. On compiling the superpackage declaration file, we get a with .spkg extension which will contain declarations about the superpackage. At the run-time, accessibility inside the superpackage remains the same as today. Outside the superpackage, the JVM consults the .spkg file to determine accessibility. Other features of superpackage are nested superpackage and interfaces.

Conclusion:

Superpackages are an effective mechanism for information hiding in the Java programming language. It will work fine with the existing deployment mechanisms like JAR, WAR and EAR. But watch out for the JSR 277 (Java Module System).

September 15, 2007

Should Java developers look at ColdFusion?

Adobe has been on heights with their products for the last one year. Adobe ColdFusion 8 was released this july.It's an application server and software development framework used for the development of computer software in general, and dynamic web sites. Question is, should java web developers look into these technologies? Lets see why and why not choose ColdFusion.

ColdFusion is a similar product to Microsoft's ASP.NET or Java Enterprise Edition. The first version of ColdFusion was released in 1995 by Allaire. In 2001 Allaire was acquired by Macromedia, which was in turn acquired by Adobe Systems in 2005.Interesting part of history is, Microsoft was once interested in acquiring Allaire but,Allaire showed a lack of interest! Initially ColdFusion was completly written in C++ until 2002. The entire application server was rewritten in Java that has resulted in recent Java-based versions being labelled with the "MX" suffix.But in the latest version, this "MX" has been dropped.

Enough of history.. Lets see its features and get into our topics! ColdFusion is available for on Linux, UNIX, Windows, and Mac OS X. But the application server is not a freeware from Abode. You can download a free developer edition and use it. The primary feature of ColdFusion is its associated scripting language, ColdFusion Markup Language (CFML), which resembles HTML in syntax. Other than CFML, ColdFusion supports programming languages such as server-side Actionscript and embedded scripts that can be written in a JavaScript-like language, known as CFScript.

Another great feature is increased productivity using ColdFusion. Like many dynamically typed scripting languages, there is no need to build, deploy, or restart your application server.You simply modify the code and refresh your browser to view your latest changes.What happends behind the scene is that server compiles .cfm pages into Java servlets.However, compilation is done in memory and not to disk.

Next major feature is something which java developers really will like. Developers can make seamless calls to all the built-in Java libraries, as well as those bundled with ColdFusion and any custom APIs of your own.Using CFScript developers to wrap java code and feel home! Integration with other technologies such as CORBA, web services, and COM is quite simple. Developers can also make calls to native methods on Linu,Unix or Windows using CFXes (ColdFusion Extension Tags). Due to these facilities there is no need to import another third-party library. So at the same time, ColdFusion is a big library!

So with these advantages, should Java developers turn and have a look at this technology? According to me, Java and JSP will still stay more powerful and flexible. Even though ColdFusion has productivity, does it give the high performance and reliability? For those who have never (Including me) tried ColdFusion before, its time we try it!

August 16, 2007

Creating PDF files through Java

PDF has become a very popular interchange format for text and graphical documents and are used in electronic publishing. Abobe's PDF format has penetrated into the market extensively and this format has a big market share. Initially, it was designed as a "final" document format, but the dynamic requirements of document exchange over the Internet led to dynamic PDFs and also other formats. You will find many other formats like excel sheets, word documents dynamically created on net to deliver reports, results, product comparisons etc. So, lets have a look at creating PDF files dynamically.
As a java programmer you should be able to create PDF files in web applications and standalone applications once you finish this article. In the end I will also explain how to convert other documents to PDF online and also how to make options for creating PDFs of your blog or webpage.

PDF libraries:

You can find many libraries both commercial and open-source that will help you create PDF files.I will consider only open-source libraries here and particularly one library: iText. Apart from this library you have gnujpdf, JFreeReport, PJX etc...

As I said iText is a open-source pure Java class library for creating and manipulating PDF documents. It provides a rich set of features like support for both PDF and FDF documents, different layouts, images, document encryption, watermarks, document templates etc. Its published in two different licenses: MPL and LGPL.

On to Coding...

Like I said, I will be looking into iText library for now.. Let's get started. Upon downloading and extracting the compressed file, you will have a single jar file which holds all the necessary classes. There are two major classes in creating a PDF file, they are Document and PdfWriter. Document represents your programmatic PDF file and the writer helps you to associate the document object with any output stream. So you can directly write to a file system or transfer the PDF through a servlet's output stream. Now lets look into a simple method that will create a PDF file on file system:

public void createTestPdf() throws FileNotFoundException {
Document pdfDocument = new Document();
String filePath = "c:\\" + fileName;
FileOutputStream fos = new FileOutputStream( filePath );

PdfWriter writer = null;
try {
writer = PdfWriter.getInstance(pdfDocument, fos);
writer.open();
pdfDocument.addTitle("A sample PDF Title");
pdfDocument.addAuthor("Abdel");
pdfDocument.open();
pdfDocument.add(new Paragraph("This is a sample PDF document created using iText PDF library!"));
pdfDocument.add(Image.getInstance("C:\\Documents and Settings\\My Documents\\My Pictures\\puzzle.jpg"));
pdfDocument.add(new Paragraph("This is second Paragraph"));
pdfDocument.close();
writer.close();
}catch (DocumentException e) {
System.out.println("Document Exception!");
e.printStackTrace();
}
}

The above method createTestPdf will create a simple PDF file with some text and image. The API is quite simple.

Online PDF tools:

There are many online PDF tools available to create and convert files. Adobe provides PDF conversion for a fee, but lets look into some other alternatives. PDF Online provides two services: Doc2PDF Online and Web2PDF Online. Web2PDF Online is still in BETA and I will talk about it too.. but, first Doc2PDF. It gives you the facility to convert different documents like word document, excel sheets, power point slides, HTML etc to be converted online in three steps. Upload the documents, give the output filename and email address to which the converted document is to be sent.

Now onto web2PDF Online service. This helps you to convert your blog or website into PDF files. Your visitors can quickly save their useful information with a click of a button. To avail this service you have to create a account with PDF Online. They also track your site's PDF conversions. I tried it on my testing blogs and found some small glitches, like the quality and page size. But overall the service is good.

May 11, 2007

Gosling talks about JavaFX

James Gosling, the co- founder of java talked with CNET News.com's Stephen Shankland about the new application platform, JavaFX. Gosling talks a bit about how JavaFX will compare to Adobe's Flash (Flex and Apollo) and Microsoft's Silverlight. Here is the video!

So, as the heat increasing in three camps (Adobe,Sun and Microsoft), what will happen to other small projects that have made their venture into this hot field. Two notable camps are Dekoh and Dojo toolkits offline web applications. Dekoh is making of the existing technologies including Adobe's Flash and is going to make use of JVM,HTML,Java script and XML for RIA development. Dojo on the other hand has come up with a caching proxy to save the web application and will use the existing scripting languages. Flex makes use of the MXML and Action script which is poplar among developer but JavaFX script is not! Developers will take time to adopt, learn and start using the new scripting language. For a developer, he will have to wait and see what the industry will accept and only time will tell! And for now... simply go about your daily work :)

January 25, 2007

Multiple file upload using Apache Struts

Uploading of file using struts with the FormFile is easy! But the requirement that I got was not as sweet as I thought. I had to upload multiple files and the user had the facility to add files before doing the upload. Thus I had to have a “Add File” button that will present the user with one more column to select a file using the browse button.

Having the facility of FormFile and the apache upload package, I thought the task would be easy but I had tough time due the dynamic part of this task. In the end I solved the problem. And here is the solution for you people as you should not reinvent the wheel. :)

Add button problem was solved by a simple java script that created an INPUT tag and inserted to the existing form. The code is given below:

function addFileElement() {
fCount++;
var fObject = document.getElementById('fileSection');
var text = 'File:';
var tag='<input type="file" name="testFile['+fCount+']" value="">';
var brk='<br>';
var o1 = document.createTextNode(text);
var o2 = document.createElement(tag);
var o3 = document.createElement(brk);

fObject.appendChild(o3);
fObject.appendChild(o1);
fObject.appendChild(o2);
fObject.appendChild(o3);

}

As usual the form bean of struts had a FormFile and an ArrayList variable that would hold the entire set of files. Now, I the form bean we tune the setter method of FormFile so that, each time the function is called the new file that is being set is added to the ArrayList. The code is given below:

public void setTestFile(int in,FormFile t) {

try {

this.testFile = t; // testFile is FormFile’s obj
setFormFiles(t); // add the file to array
index++;
}catch(Exception e) {
System.out.println("Exception in setTestFile!" + e);
}

}

And now the jsp code! It’s fairly simple and sweet. All you have to do is provide a strut form that has "browse" buttons, a "submit" button and a “add” button. The add button will run the above java script and you will get more options to upload file!
The complete source code can be downloaded from here. Enjoy Programming! ;)

December 28, 2006

Scripting in Java

The latest version of java brings a number of new features and enhancements that will improve the development of software in java platform. One new feature is the Scripting support that the new JDK will provide and this article is just the basics of it.

Most developers know scripting languages like Perl,PHP,Python,Ruby and some are crazy about it because they are simple,flexible and highly productive. Java now comes with built-in support for scripting languages. This has been encoprated through Java Specification Request (JSR) 223, which defines a framework and provide the necessary API required to access and control the scripting environment, create web content with scripting and embed scripting technology in java based applications.

So the start with, we have a script engine that helps us in executing the script and our first step would be to import some packages. The classes and interfaces required are defined in the package javax.script. The main class is ScriptEngineManager which is the script engine that executes the scripts. So we have it instantiate it as our second step. So the simple steps for incoprating scripting will be:

1) Create an instance of ScriptEngineManager.
2) Get the required scripting engine.
3) Execute the script using the scripting engine.

The first two steps are as follows:

ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine engine = sem.getEngineByName("JavaScript");

So now you have the engine to run java scripts. To execute a script we have to use the eval API which is in ScriptEngine.
To execute (evaluate) a script we have:

engine.eval("script to run");

One thing to remeber is eval method return an Object and we have many overloaded versions of eval. These are not the only features, we can invoke procedures using the Invocable interface. To invoke a procedure we use invokeFunction or invokeMethod methods.

So that's all about scripting for now. For reference check the tutorials provided in java site and also check the javadocs.

December 13, 2006

Why Java was GPLed?

Sun's open sourcing of java is not a one night task. It has been on news for quite long now. java.net even had polls to see what developers community thought about it and also under which license it should be given out. Now it's finally open under GPLv2! But what surprised me was the license which they have used.With the conterversy of GPL versions arround I really didn't like the choice of license. But that's not all!! What made Sun to open source java?
A little investigation what is happening arround will give you a picture. Through my hunt I feel the main reason for Sun to open source java was the pressure from the open source community to open it up. The pressure was in the form of open source implementations of the J2SE. One such project is Harmony of Apache Software Foundation. The Harmony project site says "Apache Harmony is a top-level project of the Apache Software Foundation! See below for more information". And the big news is they plan to complete the implementation of Java SE 5 JDK by mid 2007. It can run servers like Tomcat,Maven etc. The license under which its distributed is Apache License.
Now its not just the Apache group that's under hard work! GNU is also comming up with its own implementaion of Java SE! Its called GNU Classpath. but the problem is its behind as we have implementation of 1.4 only. But they warmly welcomes the Sun's open sourcing of java.
So Sun Micorsystem is under pressure by these two open source java implementation because they will become more popular and may even become better than Sun's own implementation. Another reason is, at present no Linux distribution have sun's J2SE.The user have to install a copy by downloading from Sun's site. By making Java open source, you will now get to see the Sun's JDK with many major distributions like Novell,Red Hat,Open Suse, Fedora etc...
So what's the effect of it all on developers? Developers are going to be more involved in Java's development and tune it. One good example is the new Java JDK ie JDK6.So as a developer : KEEP PROGRAMMING ! :)

June 25, 2006

What Mustang offers...

Sun has released J2SE version 6 Beta 2. It’s code named Mustang. The sun web site states 10 things programmers need to know about Mustang.. Here they are:


1. Web Services : Developers are given good support for writing XML web service client applications. Mustang has new XML parser and XML to Java object-mapping APIs.

2. Scripting: Developers can now mix JavaScript technology with Java technology.

3. Database: Developers will have the JDBC 4.0 to work with! It will also co-bundle the All-Java JDBC database, Java DB.

4. Desktop APIs: Swing gets more features!

5. Monitoring and Management: Mustang has more diagnostic information and co-bundles Jhat tool.

6. Compiler Access: Now developers have access to the compiler API so that they can access javac for in-process compilation to generate dynamic java code.!

7. Pluggable Annotations : Developrs can now define their own annotations!

8. Desktop deployment : some of the features added are new looks and feel, LCD text rendering, and snappier GUI performance.

9. Security : Its one of the core feature that Java has really looked into. This time it is releasing better security features in Digital signatures, Public Key Infrastructure (PKI) and cryptography.

10. The -ilities: Quality, Compatibility, Stability : All the ilities followed to ensure that Java is the best programming platform!!!