001: /*
002: * Writer.java Copyright (c) 2006,07 Swaroop Belur
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008:
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013:
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
017: *
018: */
019:
020: package net.sf.jdec.io;
021:
022: import net.sf.jdec.config.Configuration;
023: import net.sf.jdec.exceptions.ApplicationException;
024: import net.sf.jdec.main.ConsoleLauncher;
025: import net.sf.jdec.ui.main.UILauncher;
026: import net.sf.jdec.util.AllExceptionHandler;
027:
028: import java.io.*;
029:
030: public abstract class Writer extends java.io.PrintWriter {
031:
032: public Writer() {
033: super (new PrintWriter(new BufferedOutputStream(System.out)));
034: }
035:
036: /**
037: * @param arg0
038: */
039: public Writer(java.io.Writer writer) {
040: super (writer);
041:
042: }
043:
044: protected static java.io.Writer opwriter = null;
045:
046: protected static java.io.Writer logwriter = null;
047:
048: public static java.io.Writer getUIConsoleWriter()
049: throws IOException {
050:
051: return UIConsoleWriter.getInstance();
052:
053: }
054:
055: public static net.sf.jdec.io.Writer getWriter(java.lang.String type)
056: throws IOException {
057: if (type.equalsIgnoreCase("log")) {
058: return getLogWriter();
059: } else if (type.equalsIgnoreCase("output")) {
060: return getOutputWriter();
061: } else
062: throw new ApplicationException(
063: "Invalid type passed to getWriter Method of getWriter in Writer Class...");
064:
065: }
066:
067: private static net.sf.jdec.io.Writer getOutputWriter()
068: throws IOException {
069: if (Configuration.getOutputMode().equalsIgnoreCase("file")) {
070: if (Configuration.getDecompileroption().equals("dc")
071: || Configuration.getDecompileroption()
072: .equals("jar")) {
073: String classpath = Configuration.getClassPath();
074: String className = "";
075: int dot = classpath.lastIndexOf(".");
076: int start = dot - 1;
077: if (dot != -1) {
078: boolean ok = false;
079: while (start >= 0) {
080: char ch = classpath.charAt(start);
081: if (ch == '\\' || ch == '/') {
082: ok = true;
083: break;
084: }
085: start--;
086:
087: }
088: if (ok) {
089:
090: className = classpath.substring(start + 1);
091: dot = className.lastIndexOf(".");
092: if (dot != -1)
093: className = className
094: .substring(0, dot)
095: .concat(
096: "."
097: + Configuration
098: .getFileExtension());
099: }
100:
101: }
102:
103: //ConsoleLauncher.setResultFilePath(Configuration.getOutputFolderPath()+File.separator+className);
104: return net.sf.jdec.io.FileWriter.getFileWriter(
105: Configuration.getOutputFolderPath()
106: + File.separator + className, "output");
107: } else if (Configuration.getDecompileroption()
108: .equals("vcp")) {
109: String classpath = Configuration.getClassPath();
110: String className = "";
111:
112: int dot = classpath.lastIndexOf(".");
113: int start = dot - 1;
114: if (dot != -1) {
115: boolean ok = false;
116: while (start >= 0) {
117: char ch = classpath.charAt(start);
118: if (ch == '\\' || ch == '/') {
119: ok = true;
120: break;
121: }
122: start--;
123:
124: }
125: if (ok) {
126:
127: className = classpath.substring(start + 1);
128: dot = className.lastIndexOf(".");
129: if (dot != -1)
130: className = className
131: .substring(0, dot)
132: .concat(
133: "."
134: + Configuration
135: .getFileExtension());
136: }
137:
138: }
139:
140: String s = Configuration.getOutputFolderPath()
141: + File.separator + className;//File.createTempFile("cpool",".txt").getAbsolutePath();
142:
143: if (UILauncher.getUIutil() != null)
144: UILauncher.getUIutil().setConstantPoolResultFile(s);
145: ConsoleLauncher.setResultFilePath(Configuration
146: .getOutputFolderPath()
147: + File.separator + className);
148: return net.sf.jdec.io.FileWriter.getFileWriter(s,
149: "output");
150: } else if (Configuration.getDecompileroption()
151: .equals("dis")) {
152: String classpath = Configuration.getClassPath();
153: String disClass = "";
154: int dot = classpath.lastIndexOf(".");
155: int start = dot - 1;
156: if (dot != -1) {
157: boolean ok = false;
158: while (start >= 0) {
159: char ch = classpath.charAt(start);
160: if (ch == '\\' || ch == '/') {
161: ok = true;
162: break;
163: }
164: start--;
165:
166: }
167: if (ok) {
168:
169: disClass = classpath.substring(start + 1);
170: dot = disClass.lastIndexOf(".");
171: if (dot != -1)
172: disClass = disClass
173: .substring(0, dot)
174: .concat(
175: "."
176: + Configuration
177: .getFileExtension());
178: }
179:
180: }
181:
182: ConsoleLauncher.setResultFilePath(Configuration
183: .getOutputFolderPath()
184: + File.separator + disClass);
185: return net.sf.jdec.io.FileWriter.getFileWriter(
186: Configuration.getOutputFolderPath()
187: + File.separator + disClass, "output");
188: } else if (Configuration.getDecompileroption().equals(
189: "nocode")) {
190: String classpath = Configuration.getClassPath();
191: String className = "";
192: int dot = classpath.lastIndexOf(".");
193: int start = dot - 1;
194: if (dot != -1) {
195: boolean ok = false;
196: while (start >= 0) {
197: char ch = classpath.charAt(start);
198: if (ch == '\\' || ch == '/') {
199: ok = true;
200: break;
201: }
202: start--;
203:
204: }
205: if (ok) {
206:
207: className = classpath.substring(start + 1);
208: dot = className.lastIndexOf(".");
209: if (dot != -1)
210: className = className
211: .substring(0, dot)
212: .concat(
213: "."
214: + Configuration
215: .getFileExtension());
216: }
217:
218: }
219:
220: ConsoleLauncher.setResultFilePath(Configuration
221: .getOutputFolderPath()
222: + File.separator + className);
223: return net.sf.jdec.io.FileWriter.getFileWriter(
224: Configuration.getOutputFolderPath()
225: + File.separator + className, "output");
226: } else if (Configuration.getDecompileroption()
227: .equals("llv")) {
228: String classpath = Configuration.getClassPath();
229: String className = "";
230: int dot = classpath.lastIndexOf(".");
231: int start = dot - 1;
232: if (dot != -1) {
233: boolean ok = false;
234: while (start >= 0) {
235: char ch = classpath.charAt(start);
236: if (ch == '\\' || ch == '/') {
237: ok = true;
238: break;
239: }
240: start--;
241:
242: }
243: if (ok) {
244:
245: className = classpath.substring(start + 1);
246: dot = className.lastIndexOf(".");
247: if (dot != -1)
248: className = className
249: .substring(0, dot)
250: .concat(
251: "."
252: + Configuration
253: .getFileExtension());
254: }
255:
256: }
257:
258: return net.sf.jdec.io.FileWriter.getFileWriter(
259: Configuration.getOutputFolderPath()
260: + File.separator + className, "output");
261: } else {
262: java.lang.String error = "Invalid Option provided during run....Please check The usage of this jdec....";
263: error += "\nFor usage type help after option to the jdec....";
264: throw new ApplicationException(error);
265: }
266:
267: }
268: //TODO HANDLE ZIP SIMILARLY AS FILEWRITER
269: else if (Configuration.getOutputMode().equalsIgnoreCase("zip")) {
270: return net.sf.jdec.io.ZipWriter.getZipWriter(Configuration
271: .getOutputFolderPath());
272: } else if (Configuration.getOutputMode().equalsIgnoreCase(
273: "memory")) {
274: return net.sf.jdec.io.ZipWriter.getZipWriter(Configuration
275: .getOutputFolderPath());
276: } else {
277: return net.sf.jdec.io.ConsoleWriter
278: .getConsoleWriter("output");
279: }
280:
281: }
282:
283: private static net.sf.jdec.io.Writer getLogWriter()
284: throws IOException {
285:
286: if (Configuration.getLogMode().equalsIgnoreCase("file")) {
287: return FileWriter.getFileWriter(Configuration
288: .getLogFilePath(), "log");
289: }
290: //TODO
291: else {
292: return net.sf.jdec.io.ConsoleWriter.getConsoleWriter("log");
293: }
294:
295: }
296:
297: public abstract void writeOutput(String data) throws IOException;
298:
299: public abstract void writeLog(String data) throws IOException;
300:
301: public abstract void writeLog(String data, String level)
302: throws IOException;
303:
304: /***
305: *
306: * @param mode Can either be log or output
307: * NOTE: In case some error is made in passing parameter no writer object is closed.
308: * @return
309: */
310:
311: public boolean close(java.lang.String mode) {
312: try {
313: if (mode.equalsIgnoreCase("log")) {
314: this .logwriter.flush();
315: this .logwriter = null;
316: } else if (mode.equalsIgnoreCase("output")) {
317: this .opwriter.flush();
318: this .opwriter = null;
319: } else {
320: // Left Blank
321: }
322: return true;
323:
324: } catch (IOException ioe) {
325: java.lang.String error = "Error while Closing writer Object.\nThe writer was a "
326: + mode + "writer Object";
327: AllExceptionHandler handler = new AllExceptionHandler(
328: new IOException(error));
329: handler.reportException();
330: return false;
331:
332: }
333:
334: }
335: }
|