Coverage Report - edu.rice.cs.drjava.IndentFiles
 
Classes in this File Line Coverage Branch Coverage Complexity
IndentFiles
0%
0/44
0%
0/20
5
 
 1  
 /*BEGIN_COPYRIGHT_BLOCK
 2  
  *
 3  
  * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu)
 4  
  * All rights reserved.
 5  
  * 
 6  
  * Redistribution and use in source and binary forms, with or without
 7  
  * modification, are permitted provided that the following conditions are met:
 8  
  *    * Redistributions of source code must retain the above copyright
 9  
  *      notice, this list of conditions and the following disclaimer.
 10  
  *    * Redistributions in binary form must reproduce the above copyright
 11  
  *      notice, this list of conditions and the following disclaimer in the
 12  
  *      documentation and/or other materials provided with the distribution.
 13  
  *    * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
 14  
  *      names of its contributors may be used to endorse or promote products
 15  
  *      derived from this software without specific prior written permission.
 16  
  * 
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19  
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20  
  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 21  
  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 22  
  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 23  
  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 24  
  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 25  
  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 26  
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27  
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28  
  *
 29  
  * This software is Open Source Initiative approved Open Source Software.
 30  
  * Open Source Initative Approved is a trademark of the Open Source Initiative.
 31  
  * 
 32  
  * This file is part of DrJava.  Download the current version of this project
 33  
  * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
 34  
  * 
 35  
  * END_COPYRIGHT_BLOCK*/
 36  
 
 37  
 package edu.rice.cs.drjava;
 38  
 
 39  
 import java.io.*;
 40  
 import java.util.Vector;
 41  
 // TODO: Change the usage of these classes to Collections style.
 42  
 // TODO: Do these need to be synchronized?
 43  
 import edu.rice.cs.plt.io.IOUtil;
 44  
 import edu.rice.cs.drjava.model.definitions.indent.Indenter;
 45  
 import edu.rice.cs.drjava.model.definitions.DefinitionsDocument;
 46  
 import edu.rice.cs.drjava.model.GlobalEventNotifier;
 47  
 
 48  
 /** Allows users to pass filenames to a command-line indenter.  Unfortunately, this uses the Swing API (high 
 49  
   * overhead), but we attempt to run the indentation in "headless AWT" mode to prevent a Java icon from showing 
 50  
   * up on the OS X dock.
 51  
   * @version $Id: IndentFiles.java 5175 2010-01-20 08:46:32Z mgricken $
 52  
   */
 53  0
 public class IndentFiles {
 54  
   
 55  
   /** Command line interface to the indenter.
 56  
     * Usage:
 57  
     *   java edu.rice.cs.drjava.IndentFile [-indent N] [filenames]
 58  
     *   Where N is the number of spaces in an indentation level
 59  
     * @param args Command line arguments
 60  
     */
 61  
   public static void main(String[] args) {
 62  0
     Vector<String> fileNames = new Vector<String>();
 63  0
     int indentLevel = 2;
 64  0
     boolean silent = false;
 65  0
     if (args.length < 1) _displayUsage();
 66  
     else {
 67  0
       for (int i = 0; i < args.length; i++) {
 68  0
         String arg = args[i];
 69  0
         if (arg.equals("-indent")) {
 70  0
           i++;
 71  0
           try { indentLevel = Integer.parseInt(args[i]); }
 72  0
           catch (Exception e) {
 73  0
             _displayUsage();
 74  0
             System.exit(-1);
 75  
           }
 76  
         }
 77  0
         else if (arg.equals("-silent")) silent = true;
 78  0
         else fileNames.add(arg);
 79  
       }
 80  0
       indentFiles(fileNames, indentLevel, silent);
 81  
     }
 82  0
   }
 83  
 
 84  
   /** Displays a message showing how to use this class. */
 85  
   private static void _displayUsage() {
 86  0
     System.out.println(
 87  0
       "Usage:" +
 88  
       "  java edu.rice.cs.drjava.IndentFile [-indent N] [-silent] [filenames]\n" +
 89  
       "  Where N is the number of spaces in an indentation level");
 90  0
   }
 91  
   
 92  
   /** Applies the indent logic to each file in the list of file names, saving the new copy of each one.
 93  
     * @param fileNames Vector of filenames of files to be indented
 94  
     * @param indentLevel The number of spaces to use for a level of indentation
 95  
     * @param silent Whether to print any output to System.out
 96  
     */
 97  
   public static void indentFiles(Vector<String> fileNames, int indentLevel, boolean silent) {
 98  
     //System.setProperty("java.awt.headless", "true"); // attempt headless AWT
 99  
     //System.out.println("Using Headless AWT: " + isHeadless());
 100  0
     Indenter indenter = new Indenter(indentLevel);
 101  
     
 102  0
     if (! silent) System.out.println("DrJava - Indenting files:");
 103  0
     for (int i = 0; i < fileNames.size(); i++) {
 104  0
       String fname = fileNames.get(i);
 105  0
       File file = new File(fname);
 106  0
       if (!silent) { 
 107  0
         System.out.print("  " + fname + " ... ");
 108  0
         System.out.flush();
 109  
       }
 110  
       try {
 111  0
         String fileContents = IOUtil.toString(file);
 112  0
         DefinitionsDocument doc = new DefinitionsDocument(indenter, new GlobalEventNotifier());
 113  0
         doc.insertString(0, fileContents, null); // (no attributes)
 114  0
         int docLen = doc.getLength();
 115  0
         doc.indentLines(0, docLen);
 116  0
         fileContents = doc.getText();
 117  0
         IOUtil.writeStringToFile(file, fileContents);
 118  0
         if (!silent) System.out.println("done.");
 119  
       }
 120  0
       catch (Exception e) {
 121  0
         if (!silent) {
 122  0
           System.out.println("ERROR!");
 123  0
           System.out.println("  Exception: " + e.toString());
 124  0
           e.printStackTrace(System.out);
 125  0
           System.out.println();
 126  
         }
 127  
       }
 128  
       // System.gc();
 129  
     }
 130  0
     if (!silent) System.out.println();
 131  0
   }
 132  
 
 133  
 //  /** Java versions 1.4 or above should have this implemented.  
 134  
 //    * Return false, if earlier version.
 135  
 //    */
 136  
 //  private static boolean isHeadless() {
 137  
 //    try {
 138  
 //      Method isHeadless = java.awt.GraphicsEnvironment.class.getMethod("isHeadless", new Class[0]);
 139  
 //      return ((Boolean) isHeadless.invoke(null,new Object[0])).booleanValue();
 140  
 //    }
 141  
 //    catch(Exception e) {
 142  
 //      return false;
 143  
 //    }
 144  
 //  }
 145  
 }