KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > tools > internal > CleanupClass


1 /*******************************************************************************
2  * Copyright (c) 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.tools.internal;
12
13 import java.io.*;
14 import java.util.*;
15
16 public abstract class CleanupClass extends JNIGenerator {
17
18 String JavaDoc classSourcePath;
19 String JavaDoc[] sourcePath;
20 String JavaDoc classSource;
21 Hashtable files;
22 int usedCount, unusedCount;
23
24 void loadClassSource() {
25     if (classSourcePath == null) return;
26     File f = new File(classSourcePath);
27     classSource = loadFile(f);
28 }
29
30 void loadFiles () {
31     // BAD - holds on to a lot of memory
32
if (sourcePath == null) return;
33     files = new Hashtable ();
34     for (int i = 0; i < sourcePath.length; i++) {
35         File file = new File(sourcePath[i]);
36         if (file.exists()) {
37             if (!file.isDirectory()) {
38                 if (file.getAbsolutePath().endsWith(".java")) {
39                     files.put(file, loadFile(file));
40                 }
41             } else {
42                 loadDirectory(file);
43             }
44         }
45     }
46 }
47
48 String JavaDoc loadFile (File file) {
49     try {
50         FileReader fr = new FileReader(file);
51         BufferedReader br = new BufferedReader(fr);
52         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
53         char[] buffer = new char[1024];
54         int read;
55         while ((read = br.read(buffer)) != -1) {
56             str.append(buffer, 0, read);
57         }
58         fr.close();
59         return str.toString();
60     } catch (IOException e) {
61         e.printStackTrace(System.out);
62     }
63     return "";
64 }
65
66 void loadDirectory(File file) {
67     String JavaDoc[] entries = file.list();
68     for (int i = 0; i < entries.length; i++) {
69         String JavaDoc entry = entries[i];
70         File f = new File(file, entry);
71         if (!f.isDirectory()) {
72             if (f.getAbsolutePath().endsWith(".java")) {
73                 files.put(f, loadFile(f));
74             }
75         } else {
76             loadDirectory(f);
77         }
78     }
79 }
80
81 public void generate(Class JavaDoc clazz) {
82     loadFiles ();
83     loadClassSource();
84 }
85
86 public void setSourcePath(String JavaDoc[] sourcePath) {
87     this.sourcePath = sourcePath;
88     files = null;
89 }
90
91 public void setClassSourcePath(String JavaDoc classSourcePath) {
92     this.classSourcePath = classSourcePath;
93 }
94
95 }
96
Popular Tags