Open API

Jindent provides an easy to use Open Java API for developers. This API can be very helpful to beautify generated source code or to write your own Jindent plugins.

To be able to use Jindent's Open API you have to add all jar files from Jindent's /lib directory to your CLASSPATH. That's all.

Example Source Code

Explicit invocation of the Java formatter module. Jindent settings will be read from a user defined settings file.

   import jindent.JindentException;
   import jindent.JindentSettings;
   import jindent.JindentSystem;
   import jindent.java.JavaFormatter;
   
   public class Example {
   
      public static void main(String args[]) {
         
         // read JindentSettings from file  
         JindentSettings settings = null;
         
         try {
            settings = (JindentSettings) JindentSettings.createFromFile("MySettings.xml");
         } catch (FileNotFoundException e1) {
            System.err.println(e1);
         } catch (JindentException e2) {      // catch settings format exceptions
            System.err.println(e2);
         }
         
         // create Java formatter
         JavaFormatter formatter = new JavaFormatter();
         
         // set settings
         formatter.setSettings(settings);
         
         // create sample input
         String input = "class Test{int x,y,z;public int method(int a){return a*2;}}";

         try {
            // run formatter
            String output = formatter.format(input);
            
            // write output
            System.out.println(output);
         } catch(JindentException e) {
            System.err.println(e);
         }
      }
   }