001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava;
038:
039: import java.io.*;
040: import java.util.Vector; // TODO: Change the usage of these classes to Collections style.
041: // TODO: Do these need to be synchronized?
042: import edu.rice.cs.plt.io.IOUtil;
043: import edu.rice.cs.drjava.model.definitions.indent.Indenter;
044: import edu.rice.cs.drjava.model.definitions.DefinitionsDocument;
045: import edu.rice.cs.drjava.model.GlobalEventNotifier;
046:
047: /** Allows users to pass filenames to a command-line indenter. Unfortunately, this uses the Swing API (high
048: * overhead), but we attempt to run the indentation in "headless AWT" mode to prevent a Java icon from showing
049: * up on the OS X dock.
050: * @version $Id: IndentFiles.java 4255 2007-08-28 19:17:37Z mgricken $
051: */
052: public class IndentFiles {
053:
054: /**
055: * Command line interface to the indenter.
056: * Usage:
057: * java edu.rice.cs.drjava.IndentFile [-indent N] [filenames]
058: * Where N is the number of spaces in an indentation level
059: *
060: * @param args Command line arguments
061: */
062: public static void main(String[] args) {
063: Vector<String> fileNames = new Vector<String>();
064: int indentLevel = 2;
065: boolean silent = false;
066: if (args.length < 1)
067: _displayUsage();
068: else {
069: for (int i = 0; i < args.length; i++) {
070: String arg = args[i];
071: if (arg.equals("-indent")) {
072: i++;
073: try {
074: indentLevel = Integer.parseInt(args[i]);
075: } catch (Exception e) {
076: _displayUsage();
077: System.exit(-1);
078: }
079: } else if (arg.equals("-silent"))
080: silent = true;
081: else
082: fileNames.add(arg);
083: }
084: indentFiles(fileNames, indentLevel, silent);
085: }
086: }
087:
088: /**
089: * Displays a message showing how to use this class.
090: */
091: private static void _displayUsage() {
092: System.out
093: .println("Usage:"
094: + " java edu.rice.cs.drjava.IndentFile [-indent N] [-silent] [filenames]\n"
095: + " Where N is the number of spaces in an indentation level");
096: }
097:
098: /** Applies the indent logic to each file in the list of file names, saving the new copy of each one.
099: * @param fileNames Vector of filenames of files to be indented
100: * @param indentLevel The number of spaces to use for a level of indentation
101: * @param silent Whether to print any output to System.out
102: */
103: public static void indentFiles(Vector<String> fileNames,
104: int indentLevel, boolean silent) {
105: //System.setProperty("java.awt.headless", "true"); // attempt headless AWT
106: //System.out.println("Using Headless AWT: "+isHeadless());
107: Indenter indenter = new Indenter(indentLevel);
108:
109: if (!silent)
110: System.out.println("DrJava - Indenting files:");
111: for (int i = 0; i < fileNames.size(); i++) {
112: String fname = fileNames.get(i);
113: File file = new File(fname);
114: if (!silent) {
115: System.out.print(" " + fname + " ... ");
116: System.out.flush();
117: }
118: try {
119: String fileContents = IOUtil.toString(file);
120: DefinitionsDocument doc = new DefinitionsDocument(
121: indenter, new GlobalEventNotifier());
122: doc.insertString(0, fileContents, null); // (no attributes)
123: int docLen = doc.getLength();
124: doc.indentLines(0, docLen);
125: fileContents = doc.getText();
126: IOUtil.writeStringToFile(file, fileContents);
127: if (!silent)
128: System.out.println("done.");
129: } catch (Exception e) {
130: if (!silent) {
131: System.out.println("ERROR!");
132: System.out.println(" Exception: " + e.toString());
133: e.printStackTrace(System.out);
134: System.out.println();
135: }
136: }
137: // System.gc();
138: }
139: if (!silent)
140: System.out.println();
141: }
142:
143: /**
144: * Java versions 1.4 or above should have this implemented.
145: * Return false, if earlier version.
146: *
147: private static boolean isHeadless() {
148: try {
149: Method isHeadless = java.awt.GraphicsEnvironment.class.getMethod("isHeadless", new Class[0]);
150: return ((Boolean) isHeadless.invoke(null,new Object[0])).booleanValue();
151: }
152: catch(Exception e) {
153: return false;
154: }
155: }*/
156: }
|