Archive for January, 2006

Deauthorize iTunes

Just a friendly reminder. Developers are constantly reformatting their hard drives or upgrading to new bigger giga drives. If you are like me, you have a ton of tunes. If you are like me, you might forget to deauthorize you iTunes music from your computer before you reformat your drive. If you don’t deauthorize your music you will use up one of the five computers you can play your music on. To deauthorize your iTunes songs go to Advanced > Deauthorize Computer and click Ok to ‘Deauthorize Computer for Music Store Account.’

Java Five-Oh #2: Static Import

Perhaps one of my favorite Java 1.5 features is the static imports. The way I have explained static import is that it makes available other class members and method as if they were defined locally. If that didn’t make much sense, let me demonstrate it with an example. The java.lang.Math class provides a ton of static helper methods. Each time you want to use the abs() method for example you have to do so as such:

int absolute = Math.abs(-7);

But with the new static import you can make available all of the methods in the Math class in your own class so you don’t have to write out Math every time you want to use the abs() method. So if you static import the Math class you can do this:

int absolute = abs(7);

To declare a static import for the Math class use the following construct:

import static java.lang.Math.*;

jQuery Library

Yet another effects JavaScript library has been unleashed and this time it is named jQuery. Some code is based if not entirely inspired by moo.fx which in turn is base on prototype which in turn is base on work based on other work etc. JQuery is a light framework that you can get started with in less than 10 minutes. This is the sub-10 minutes tutorial. Lets get started.

The jQuery examples below I will be referring the the following HTML:

jQuery supports XPath expressions and CSS 1-3, this means that you can get a jQuery object that represents one or more elements by a tag name, an element’s id or class, or by an XPath expression. To get a jQuery reference of an element use the dollar sign function as in the following code:

As the comments state, the mypara variable will hold one paragraph, the one whose id is mypara. The allpara variable will refer to all paragraphs in the document. Once you have jQuery object for one or more elements you can update its style. The following example will change the text color of all paragraph tags in my document.

If you want to update more than one style attribute at a time you can use the following code:

Now, if you are using some Ajax library you will most likely want to update more than just an element’s style, you may want to update an element’s inner HTML. To replace the inner HTML use the following code:

Other useful methods that do just what there function name states are append(), prepend(), before(), and after(). jQuery also provides a hide(), show(), and toggle() method for special effects. jQuery is a new effects JavaScript library similar in spirit to moo.fx and Prototype. jQuery should be easy to pick up and start using in your next web 2.0 project as it simplifies what you already do.

I have already mentioned moo.fx in JavaScript FX.

Technorati Tags: , , , , , , ,

Java Five-Oh #1: For/In

Java 1.5, sometimes referred to as Java 5 or as I like to call it Java five-oooh, has been out for a while now and yet I recently got an email from a old college mate who emailed me immediately after he saw the new printf method in action in some sample code. So for my benefit, as well as his and maybe even yours, I am going to go break down over several entries some sample code detailing the new features in Java 1.5.

One Java 1.5 feature that I use most is probably the new foreach, for/in, statement. The new for/in construct is basically a new for loop that provides a shortcut for iterating over collections such as lists and arrays. The new for/in statement provides a shortcut for working with lists; you don’t have to declare iterators or integer indices. Here is the new foreach construct in action:

List l = ...
for(Object o : l) {
   System.out.println(o);
}

I’ve already written about generics in Java 1.5 and how to use the new for/in loop in conjunction with generics in Generic Java.

Word To PDF

Why isn’t there are good and simple command line doc2pdf application? I just can’t find any good command line programs that can faithfully produce a PDF document given a Word document. There are a lot of commercial and some open source applications that can create a PDF document but I can’t find a simple command line tool that does this. For example, PDFCreator is an open source application that allows you to create a PDF document from Word by ‘printing’ the document to a virtual PDFCreator printer. Several commercially available Word to PDF solutions do this same thing; installing a ‘printer’ to print a document as a PDF. This solution is really a hack that exploits the fact that documents sent to the printer need to be transformed to what is essentially PostScript. Once you have a document in its PostScript format you can create a PDF using Adobe Acrocat Distiller or GhostScript’s ps2pdf.cmd batch file.

PDFCreator does not provide a nice command line interface but that is easy to get past that limitation with some simple Visual Basic. You can write some simple Visual Basic script code that opens a Word document, sets the default printer to PDFCreator, and ‘prints’ out document allowing PDFCreator to create a PDF for you. You might want to edit the PDFCreator’s auto-save options otherwise you will be prompted where to save the new PDF. Here is some sample Visual Basic code that does just what I described above.

Set word = CreateObject("Word.Application")
Set docs = wdo.Documents

' Remember current active printer
Set sPrevPrinter = wdo.ActivePrinter

' Select the PDFCreator as your printer
word.ActivePrinter = "PDFCreator"

' Open the Word document
Set document = docs.Open(sMyDocumentFile)

' Print the document file to the PDFCreator
word.ActiveDocument.PrintOut

document.Close WdDoNotSaveChanges
word.ActivePrinter = sPrevPrinter
word.Quit WdDoNotSaveChanges

For completeness sakes let me mention how to create a PDF document using the Apache POI project. You can of course convert a Word document to PDF using the Apache POI API. Using POI you can create a XSL-FO version of your document which can be transformed into a PDF using Apache FOP. It has been my experience that the results generated by POI are not perfect but here is some code for you go get started. The POI scratch pad jar contains a WordDocument class that will create a XSL-FO version of the Word document. The WordDocment might have been intended to be just a command line application because it throws a NullPointerException if you try to use it in your code so you will have to modify this class. Once you fix the exception you can code the following two lines to produce an XSL-FO for a given Word document:

WordDocument file = new WordDocument(wordDocumentPath);
file.closeDoc();

Of course once you have the XSL-FO version of your document you can transform it to a PDF using Apache FOP. One word of warning, the WordDocument class is in the scratch pad jar and might not be as stable as you might think.

Decode Java

Sometimes when working on web applications you need to encode GET parameter values that are part of a URL. Recently while working on a custom URL handler in Java I ran into a situation where I thought I had to encode part of the URL. I didn’t have to go that route, to encode/decode the custom URL, but this reminded me how to do that in Java. If you need to encode string value to be included in a URL use the URLEncoder as such:

String encoded = URLEncoder.encode("value: *(&#%");
// encoded would equal to "value%3A+*%28%26%23%25"

If you notice spaces will be converted to plus signs and characters like the pound sign (#) will be converted to their hexadecimal value. To decode a sting value you can use the URLDecoder’s decode(String) method.

Next Page »