Home | About | Partners | Contact Us
SourceForge Logo

Quick Links

Home
News
Thoughts and Rants
License
SourceForge Info
Download
Browse CVS
Mailing Lists

Understanding...

Concept programming
Moka
XL
Thin Tools
Active Libraries
Refactoring
Optimizations
Coda
Notes

More Info

Contributors
Related Ideas
Some history
Applications
Other links
The GNU Project

Pretty Printing with Moka

The simplest operation Moka can perform for you is reformatting Java source code. Used that way, Moka simply acts as a pretty printer. In the following examples, we will assume that your code originally looks like:

class Point {
  // numPoints is a class variable
    static int numPoints;
 // x and y are instance variables
              int x,                    y;
 // w[0] is an array component
      int[] w = new int[10];

 // x is a method parameter
           int setX(int x) {
                        // oldx is a local variable
         int oldx = this.x; 
 this.x = x;
 return oldx;
        }
}

Generating source code

Moka can be used as a simple pretty-printer, as follows:

% moka source.java -out > source-new.java

This command line asks Moka to parse the file source.java and to re-emit it on the standard output, which is redirected to a file named source-new.java. Moka reconstructs a new source code that includes the original comments and a lot of the original formatting.
/* Generated by Moka using moka.stylesheet */
class Point
{  
   // numPoints is a class variable 
   static int numPoints;
   // x and y are instance variables 
   int x, y;
   // w[0] is an array component 
   int[] w = new int[10];
   
   // x is a method parameter 
   int setX(int x)
   {  
      // oldx is a local variable 
      int oldx = this .x;
      this .x = x;
      return oldx;
   }
}
/* Thank you for using Moka. */

Generating colorized HTML

As the comments in the generated source code indicate, the formatting of your source is guided by a style sheet. The default style sheet is called moka.stylesheet. You can use another style sheet by using the -style option. For instance, you can format your source code as HTML using the html.stylesheet HTML stylesheet:

% moka source.java -style html -out > source.html
class Point
{  
   // numPoints is a class variable
   
static int numPoints
;
   // x and y are instance variables
   
int xy
;
   // w[0] is an array component
   
int[] w = new int[10]
;
   
   // x is a method parameter
   
int setX(int x)
   {  
      // oldx is a local variable
      
int oldx = this .x;
      this .x = x;
      return  oldx;
   }

}

Copyright Christophe de Dinechin
First published Feb 17, 2000
Version 1.3 (updated 2002/10/09 06:16:44)