KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > IndentFiles


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2006 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava;
35
36 import java.io.*;
37 import java.util.Vector JavaDoc;
38 // TODO: Change the usage of these classes to Collections style.
39
// TODO: Do these need to be synchronized?
40
import edu.rice.cs.plt.io.IOUtil;
41 import edu.rice.cs.drjava.model.definitions.indent.Indenter;
42 import edu.rice.cs.drjava.model.definitions.DefinitionsDocument;
43 import edu.rice.cs.drjava.model.GlobalEventNotifier;
44
45 /** Allows users to pass filenames to a command-line indenter. Unfortunately, this uses the Swing API (high
46  * overhead), but we attempt to run the indentation in "headless AWT" mode to prevent a Java icon from showing
47  * up on the OS X dock.
48  * @version $Id: IndentFiles.java 4075 2007-01-19 21:35:50Z dlsmith $
49  */

50 public class IndentFiles {
51   
52   /**
53    * Command line interface to the indenter.
54    * Usage:
55    * java edu.rice.cs.drjava.IndentFile [-indent N] [filenames]
56    * Where N is the number of spaces in an indentation level
57    *
58    * @param args Command line arguments
59    */

60   public static void main(String JavaDoc[] args) {
61     Vector JavaDoc<String JavaDoc> fileNames = new Vector JavaDoc<String JavaDoc>();
62     int indentLevel = 2;
63     boolean silent = false;
64     if (args.length < 1) _displayUsage();
65     else {
66       for (int i = 0; i < args.length; i++) {
67         String JavaDoc arg = args[i];
68         if (arg.equals("-indent")) {
69           i++;
70           try { indentLevel = Integer.parseInt(args[i]); }
71           catch (Exception JavaDoc e) {
72             _displayUsage();
73             System.exit(-1);
74           }
75         }
76         else if (arg.equals("-silent")) silent = true;
77         else fileNames.add(arg);
78       }
79       indentFiles(fileNames, indentLevel, silent);
80     }
81   }
82
83   /**
84    * Displays a message showing how to use this class.
85    */

86   private static void _displayUsage() {
87     System.out.println(
88       "Usage:" +
89       " java edu.rice.cs.drjava.IndentFile [-indent N] [-silent] [filenames]\n" +
90       " Where N is the number of spaces in an indentation level");
91   }
92   
93   /** Applies the indent logic to each file in the list of file names, saving the new copy of each one.
94    * @param fileNames Vector of filenames of files to be indented
95    * @param indentLevel The number of spaces to use for a level of indentation
96    * @param silent Whether to print any output to System.out
97    */

98   public static void indentFiles(Vector JavaDoc<String JavaDoc> fileNames, int indentLevel, boolean silent) {
99     //System.setProperty("java.awt.headless", "true"); // attempt headless AWT
100
//System.out.println("Using Headless AWT: "+isHeadless());
101
Indenter indenter = new Indenter(indentLevel);
102     
103     if (!silent) System.out.println("DrJava - Indenting files:");
104     for (int i = 0; i < fileNames.size(); i++) {
105       String JavaDoc fname = fileNames.get(i);
106       File file = new File(fname);
107       if (!silent) {
108         System.out.print(" " + fname + " ... ");
109         System.out.flush();
110       }
111       try {
112         String JavaDoc fileContents = IOUtil.toString(file);
113         DefinitionsDocument doc = new DefinitionsDocument(indenter, new GlobalEventNotifier());
114         doc.insertString(0, fileContents, null); // (no attributes)
115
int docLen = doc.getLength();
116         doc.indentLines(0, docLen);
117         fileContents = doc.getText();
118         IOUtil.writeStringToFile(file, fileContents);
119         if (!silent) System.out.println("done.");
120       }
121       catch (Exception JavaDoc e) {
122         if (!silent) {
123           System.out.println("ERROR!");
124           System.out.println(" Exception: " + e.toString());
125           e.printStackTrace(System.out);
126           System.out.println();
127         }
128       }
129       // System.gc();
130
}
131     if (!silent) System.out.println();
132   }
133
134   /**
135    * Java versions 1.4 or above should have this implemented.
136    * Return false, if earlier version.
137    *
138   private static boolean isHeadless() {
139     try {
140       Method isHeadless = java.awt.GraphicsEnvironment.class.getMethod("isHeadless", new Class[0]);
141       return ((Boolean) isHeadless.invoke(null,new Object[0])).booleanValue();
142     }
143     catch(Exception e) {
144       return false;
145     }
146   }*/

147 }
148
Popular Tags