The Java™ Pretty Printer Library

This is the (as yet somewhat meagre) project web site for the Java™ Pretty Printer Library (jpplib).

Purpose

jpplib is a small Java™ package to pretty-print information using line breaks and indentation. For instance, it can be used to print
    while (i>0) {
      i--;
      j++;
    }
    
instead of
    while (i>0) { i
    --; j++;}
    
if the maximum line width is 15 characters. Or you could use it to print (nested) java.util.Lists and Maps on one line, if they fit, or on several lines, with indentation, if they don't. No big deal, so it doesn't really need a flashy web site, does it?

The purpose of jpplib is not to pretty-print Java™ programs. It is a library that can be used to write pretty-printers for all manner of structured information: programs, XML files, mathematical expressions, abstract syntax trees, content of data structures for debugging... Apart from a few included examples, it does not contain any actual pretty-printers, any more than java.io contains any code for printing concrete information. I may one day write a pretty-printer for Java™ programs using jpplib and include it, just to satisfy those who download the library in expectation of something different.

The algorithm employed by jpplib is that described by Derek C. Oppen in his paper Prettyprinting, TOPLAS volume 2 number 4, ACM, 1980, pp. 465-483, with some extensions. It has the property that if the input contains enough actual text (and not just administrative chit-chat), then pretty-printing uses constant space, and time linear in the size of the input. In fact, output will begin before the whole input has been given, so this class can be used to pretty-print a stream of data.

An interesting extension is the mark() method of the Layouter class, which allows to attach an arbitrary object to a given position in the text. This object can be retrieved in the formatted output by the back-end. The mark() feature is useful e.g. for interactive applications, where the user may click on a position in the formatted text, and the software needs to figure out which piece of data corresponds to the text that was clicked on.

Documentation

You can have a look at the API documentation here. If you download the package, you'll find the source of two little demo programs to get you started.

Download

To download jpplib, go to the Sourceforge.net download page.

If you like jpplib, and use it in your software, it would be kind if you would let me know!

To report bugs, contribute, etc., visit the Sourceforge.net project pages.

Help Needed

Contributions of more demo programs would be greatly appreciated.

I'm planning to write a how-to that says "If you want the text to look like this when it's on one line, and like this when it's broken, then you have to write the program as follows...". I don't know when I'll get around to that though.

History

The Java™ Pretty Printer Library was originally developed in 2002 as part of the KeY system, see http://www.key-project.org/. It was needed to pretty print proof obligations consisting of lists of formulae, which in turn contain pieces of Java™ programs. One particular requirement was that it should be possible to click on some formula or program element, and get a menu listing possible interactions with that item.

The first version of jpplib was implemented by Martin Giese, adapting Derek C. Oppen's layout algorithm, and rephrasing it in a somewhat more object-oriented way. It has since been used successfully, with hardly any problems or changes to the code base.

Realizing that the services provided by this package might be useful to other projects, we decided in 2007 to release jpplib as a separate product under the GNU Public License, and to host it on Sourceforge.net.

For the Sourceforge release, a design change was introduced, namely the use of generics to capture the type of exceptions passed through from the backend.

In 2009, I used jpplib in a context where it was advisable to have a more liberal license. From Version 0.6, jpplib is distributed with a BSD style license.


Java is a trademark of Sun Microsystems, Inc.

This page is maintained by Martin Giese

jpplib is kindly being hosted by Get Java Pretty Printer Library at SourceForge.net. Fast, secure and Free Open Source software downloads.