1 55 56 package org.apache.bsf.util.cf; 57 58 import java.io.*; 59 import org.apache.bsf.debug.util.DebugLog; 60 61 76 public class CFDriver 77 { 78 81 public CFDriver() 82 { 83 } 84 101 public static void main(String [] argv) 102 { 103 if (argv.length % 2 == 0) 104 { 105 String inFile = null, 106 outFile = null, 107 maxLine = null, 108 indStep = null, 109 delim = null, 110 sDelim = null; 111 Reader in = null; 112 Writer out = null; 113 CodeFormatter cf = new CodeFormatter(); 114 115 for (int i = 0; i < argv.length; i += 2) 116 { 117 if (argv[i].startsWith("-i")) 118 inFile = argv[i + 1]; 119 else if (argv[i].startsWith("-o")) 120 outFile = argv[i + 1]; 121 else if (argv[i].startsWith("-m")) 122 maxLine = argv[i + 1]; 123 else if (argv[i].startsWith("-st")) 124 indStep = argv[i + 1]; 125 else if (argv[i].startsWith("-d")) 126 delim = argv[i + 1]; 127 else if (argv[i].startsWith("-sd")) 128 sDelim = argv[i + 1]; 129 } 130 131 if (inFile != null) 132 { 133 try 134 { 135 in = new FileReader(inFile); 136 } 137 catch (FileNotFoundException e) 138 { 139 printError("Cannot open input file: " + inFile); 140 141 return; 142 } 143 } 144 else 145 { 146 in = new InputStreamReader(System.in); 147 } 148 149 if (outFile != null) 150 { 151 try 152 { 153 out = new FileWriter(outFile); 154 } 155 catch (IOException e) 156 { 157 printError("Cannot open output file: " + outFile); 158 159 return; 160 } 161 } 162 else 163 { 164 out = new OutputStreamWriter(System.out); 165 } 166 167 if (maxLine != null) 168 { 169 try 170 { 171 cf.setMaxLineLength(Integer.parseInt(maxLine)); 172 } 173 catch (NumberFormatException nfe) 174 { 175 printError("Not a valid integer: " + maxLine); 176 177 return; 178 } 179 } 180 181 if (indStep != null) 182 { 183 try 184 { 185 cf.setIndentationStep(Integer.parseInt(indStep)); 186 } 187 catch (NumberFormatException nfe) 188 { 189 printError("Not a valid integer: " + indStep); 190 191 return; 192 } 193 } 194 195 if (delim != null) 196 cf.setDelimiters(delim); 197 198 if (sDelim != null) 199 cf.setStickyDelimiters(sDelim); 200 201 cf.formatCode(in, out); 202 } 203 else 204 printHelp(); 205 } 206 private static void printError(String errMsg) 207 { 208 DebugLog.stderrPrintln("ERROR: " + errMsg, DebugLog.BSF_LOG_L2); 209 } 210 private static void printHelp() 211 { 212 System.out.println("Usage:"); 213 System.out.println(); 214 System.out.println(" java " + CFDriver.class.getName() + " [args]"); 215 System.out.println(); 216 System.out.println(" args:"); 217 System.out.println(); 218 System.out.println(" [-in fileName] default: <STDIN>"); 219 System.out.println(" [-out fileName] default: <STDOUT>"); 220 System.out.println(" [-maxLine length] default: " + 221 CodeFormatter.DEFAULT_MAX); 222 System.out.println(" [-step size] default: " + 223 CodeFormatter.DEFAULT_STEP); 224 System.out.println(" [-delim group] default: " + 225 CodeFormatter.DEFAULT_DELIM); 226 System.out.println(" [-sdelim group] default: " + 227 CodeFormatter.DEFAULT_S_DELIM); 228 } 229 } 230 | Popular Tags |