001/*******************************************************************************
002 * Copyright (C) 2009-2011 FuseSource Corp.
003 * Copyright (c) 2004, 2008 IBM Corporation and others.
004 *
005 * All rights reserved. This program and the accompanying materials
006 * are made available under the terms of the Eclipse Public License v1.0
007 * which accompanies this distribution, and is available at
008 * http://www.eclipse.org/legal/epl-v10.html
009 *
010 *******************************************************************************/
011package org.fusesource.hawtjni.generator;
012
013import java.io.*;
014import java.util.*;
015
016import org.fusesource.hawtjni.generator.model.JNIClass;
017import org.fusesource.hawtjni.generator.model.JNIMethod;
018
019/**
020 * 
021 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
022 */
023public abstract class CleanupClass extends JNIGenerator {
024
025    String classSourcePath;
026    String[] sourcePath;
027    String classSource;
028    HashMap<File, String> files;
029
030    int usedCount, unusedCount;
031
032    String[] getArgNames(JNIMethod method) {
033        int n_args = method.getParameters().size();
034        if (n_args == 0)
035            return new String[0];
036        String name = method.getName();
037        String params = "";
038        int index = 0;
039        while (true) {
040            index = classSource.indexOf(name, index + 1);
041            if (!Character.isWhitespace(classSource.charAt(index - 1)))
042                continue;
043            if (index == -1)
044                return null;
045            int parantesesStart = classSource.indexOf("(", index);
046            if (classSource.substring(index + name.length(), parantesesStart).trim().length() == 0) {
047                int parantesesEnd = classSource.indexOf(")", parantesesStart);
048                params = classSource.substring(parantesesStart + 1, parantesesEnd);
049                break;
050            }
051        }
052        String[] names = new String[n_args];
053        StringTokenizer tk = new StringTokenizer(params, ",");
054        for (int i = 0; i < names.length; i++) {
055            String s = tk.nextToken().trim();
056            StringTokenizer tk1 = new StringTokenizer(s, " ");
057            String s1 = null;
058            while (tk1.hasMoreTokens()) {
059                s1 = tk1.nextToken();
060            }
061            names[i] = s1.trim();
062        }
063        return names;
064    }
065
066    void loadClassSource() {
067        if (classSourcePath == null)
068            return;
069        File f = new File(classSourcePath);
070        classSource = loadFile(f);
071    }
072
073    void loadFiles() {
074        // BAD - holds on to a lot of memory
075        if (sourcePath == null)
076            return;
077        files = new HashMap<File, String>();
078        for (int i = 0; i < sourcePath.length; i++) {
079            File file = new File(sourcePath[i]);
080            if (file.exists()) {
081                if (!file.isDirectory()) {
082                    if (file.getAbsolutePath().endsWith(".java")) {
083                        files.put(file, loadFile(file));
084                    }
085                } else {
086                    loadDirectory(file);
087                }
088            }
089        }
090    }
091
092    String loadFile(File file) {
093        try {
094            FileReader fr = new FileReader(file);
095            BufferedReader br = new BufferedReader(fr);
096            StringBuffer str = new StringBuffer();
097            char[] buffer = new char[1024];
098            int read;
099            while ((read = br.read(buffer)) != -1) {
100                str.append(buffer, 0, read);
101            }
102            fr.close();
103            return str.toString();
104        } catch (IOException e) {
105            e.printStackTrace(System.out);
106        }
107        return "";
108    }
109
110    void loadDirectory(File file) {
111        String[] entries = file.list();
112        for (int i = 0; i < entries.length; i++) {
113            String entry = entries[i];
114            File f = new File(file, entry);
115            if (!f.isDirectory()) {
116                if (f.getAbsolutePath().endsWith(".java")) {
117                    files.put(f, loadFile(f));
118                }
119            } else {
120                loadDirectory(f);
121            }
122        }
123    }
124
125    public void generate(JNIClass clazz) {
126        loadFiles();
127        loadClassSource();
128    }
129
130    public void setSourcePath(String[] sourcePath) {
131        this.sourcePath = sourcePath;
132        files = null;
133    }
134
135    public void setClassSourcePath(String classSourcePath) {
136        this.classSourcePath = classSourcePath;
137    }
138
139}