ada.platform/src/org/netbeans/modules/ada/platform/compiler/jgnat/JGnatProject.java
author Andrea Lucarelli <raster@netbeans.org>
Sun, 22 Aug 2010 23:37:11 +0200
branchrelease68
changeset 16367 d2820c029d3a
permissions -rw-r--r--
Add JVM compiler support.
raster@16367
     1
/*
raster@16367
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
raster@16367
     3
 *
raster@16367
     4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
raster@16367
     5
 *
raster@16367
     6
 * The contents of this file are subject to the terms of either the GNU
raster@16367
     7
 * General Public License Version 2 only ("GPL") or the Common
raster@16367
     8
 * Development and Distribution License("CDDL") (collectively, the
raster@16367
     9
 * "License"). You may not use this file except in compliance with the
raster@16367
    10
 * License. You can obtain a copy of the License at
raster@16367
    11
 * http://www.netbeans.org/cddl-gplv2.html
raster@16367
    12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
raster@16367
    13
 * specific language governing permissions and limitations under the
raster@16367
    14
 * License.  When distributing the software, include this License Header
raster@16367
    15
 * Notice in each file and include the License file at
raster@16367
    16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
raster@16367
    17
 * particular file as subject to the "Classpath" exception as provided
raster@16367
    18
 * by Sun in the GPL Version 2 section of the License file that
raster@16367
    19
 * accompanied this code. If applicable, add the following below the
raster@16367
    20
 * License Header, with the fields enclosed by brackets [] replaced by
raster@16367
    21
 * your own identifying information:
raster@16367
    22
 * "Portions Copyrighted [year] [name of copyright owner]"
raster@16367
    23
 *
raster@16367
    24
 * Contributor(s):
raster@16367
    25
 *
raster@16367
    26
 * The Original Software is NetBeans. The Initial Developer of the Original
raster@16367
    27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
raster@16367
    28
 * Microsystems, Inc. All Rights Reserved.
raster@16367
    29
 *
raster@16367
    30
 * If you wish your version of this file to be governed by only the CDDL
raster@16367
    31
 * or only the GPL Version 2, indicate your decision by adding
raster@16367
    32
 * "[Contributor] elects to include this software in this distribution
raster@16367
    33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
raster@16367
    34
 * single choice of license, a recipient has the option to distribute
raster@16367
    35
 * your version of this file under either the CDDL, the GPL Version 2 or
raster@16367
    36
 * to extend the choice of license to its licensees as provided above.
raster@16367
    37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
raster@16367
    38
 * Version 2 license, then the option applies only if the new code is
raster@16367
    39
 * made subject to such option by the copyright holder.
raster@16367
    40
 */
raster@16367
    41
package org.netbeans.modules.ada.platform.compiler.jgnat;
raster@16367
    42
raster@16367
    43
import java.io.BufferedReader;
raster@16367
    44
import java.io.BufferedWriter;
raster@16367
    45
import java.io.File;
raster@16367
    46
import java.io.FileOutputStream;
raster@16367
    47
import java.io.IOException;
raster@16367
    48
import java.io.InputStream;
raster@16367
    49
import java.io.InputStreamReader;
raster@16367
    50
import java.io.OutputStreamWriter;
raster@16367
    51
import java.net.URL;
raster@16367
    52
import java.util.ArrayList;
raster@16367
    53
import java.util.StringTokenizer;
raster@16367
    54
import org.netbeans.modules.ada.platform.compiler.jgnat.commands.JGnatCommand;
raster@16367
    55
import org.openide.filesystems.FileUtil;
raster@16367
    56
raster@16367
    57
public class JGnatProject {
raster@16367
    58
raster@16367
    59
    private final JGnatCompiler jgnat;
raster@16367
    60
    private String gprFilePath;
raster@16367
    61
raster@16367
    62
    public JGnatProject(final JGnatCompiler jgnat) {
raster@16367
    63
        this.jgnat = jgnat;
raster@16367
    64
    }
raster@16367
    65
raster@16367
    66
    public void write() {
raster@16367
    67
        cleanup();
raster@16367
    68
        writeGprfileImpl();
raster@16367
    69
    }
raster@16367
    70
raster@16367
    71
    private void cleanup() {
raster@16367
    72
        // Remove all *.gpr files
raster@16367
    73
        File folder = new File(jgnat.getProjectPath() + '/' + "nbproject"); // UNIX path // NOI18N
raster@16367
    74
        File[] children = folder.listFiles();
raster@16367
    75
        if (children != null) {
raster@16367
    76
            for (int i = 0; i < children.length; i++) {
raster@16367
    77
                if (children[i].getName().endsWith("gpr")) { // NOI18N
raster@16367
    78
                    children[i].delete();
raster@16367
    79
                }
raster@16367
    80
            }
raster@16367
    81
        }
raster@16367
    82
    }
raster@16367
    83
raster@16367
    84
    public String removeSpaces(String s) {
raster@16367
    85
        StringTokenizer st = new StringTokenizer(s, " ", false);
raster@16367
    86
        String t = "";
raster@16367
    87
        while (st.hasMoreElements()) {
raster@16367
    88
            t += st.nextElement();
raster@16367
    89
        }
raster@16367
    90
        return t;
raster@16367
    91
    }
raster@16367
    92
raster@16367
    93
    private void writeGprfileImpl() {
raster@16367
    94
        String resource = "/org/netbeans/modules/ada/platform/resources/GprFileTemplate.gpr"; // NOI18N
raster@16367
    95
        InputStream is = null;
raster@16367
    96
        FileOutputStream os = null;
raster@16367
    97
        try {
raster@16367
    98
            URL url = new URL("nbresloc:" + resource); // NOI18N
raster@16367
    99
            is = url.openStream();
raster@16367
   100
        } catch (Exception e) {
raster@16367
   101
            is = JGnatCommand.class.getResourceAsStream(resource);
raster@16367
   102
        }
raster@16367
   103
raster@16367
   104
        String projectName = removeSpaces(jgnat.getProjectName());
raster@16367
   105
        String mainFile = jgnat.getMainFile();
raster@16367
   106
        String execFile = jgnat.getExecutableFile();
raster@16367
   107
        ArrayList<String> sources = jgnat.getSourceFolders();
raster@16367
   108
raster@16367
   109
        gprFilePath = jgnat.getProjectPath() + '/' + "nbproject" + '/' + projectName + ".gpr"; // UNIX path // NOI18N
raster@16367
   110
        try {
raster@16367
   111
            os = new FileOutputStream(gprFilePath);
raster@16367
   112
        } catch (IOException ioe) {
raster@16367
   113
            ioe.printStackTrace();
raster@16367
   114
        }
raster@16367
   115
raster@16367
   116
        if (is == null || os == null) {
raster@16367
   117
            // FIXUP: ERROR
raster@16367
   118
            return;
raster@16367
   119
        }
raster@16367
   120
raster@16367
   121
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
raster@16367
   122
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
raster@16367
   123
raster@16367
   124
        try {
raster@16367
   125
            while (true) {
raster@16367
   126
                String line = br.readLine();
raster@16367
   127
                if (line == null) {
raster@16367
   128
                    break;
raster@16367
   129
                }
raster@16367
   130
                if (line.indexOf("<PN>") >= 0) { // NOI18N
raster@16367
   131
                    line = line.replaceFirst("<PN>", projectName); // NOI18N
raster@16367
   132
                }
raster@16367
   133
                if (line.indexOf("<SRC>") >= 0) { // NOI18N
raster@16367
   134
                    String srcDirs = new String();
raster@16367
   135
                    for (int index = 0; index < sources.size(); index++) {
raster@16367
   136
                        srcDirs = srcDirs + "\"" + FileUtil.toFileObject(new File(sources.get(index))).getPath() + "/**\"";
raster@16367
   137
                        if (index < sources.size() - 1) {
raster@16367
   138
                            srcDirs = srcDirs + ",";
raster@16367
   139
                        }
raster@16367
   140
                    }
raster@16367
   141
                    line = line.replaceFirst("\"<SRC>\"", srcDirs); // NOI18N
raster@16367
   142
                }
raster@16367
   143
                if (line.indexOf("<MAINFILE>") >= 0) { // NOI18N
raster@16367
   144
                    line = line.replaceFirst("<MAINFILE>", mainFile); // NOI18N
raster@16367
   145
                }
raster@16367
   146
                if (line.indexOf("<EXECFILE>") >= 0) { // NOI18N
raster@16367
   147
                    line = line.replaceFirst("<EXECFILE>", execFile); // NOI18N
raster@16367
   148
                }
raster@16367
   149
                if (line.indexOf("<SPC-EXT>") >= 0) { // NOI18N
raster@16367
   150
                    line = line.replaceFirst("<SPC-EXT>", jgnat.getSpcExt()); // NOI18N
raster@16367
   151
                }
raster@16367
   152
                if (line.indexOf("<BDY-EXT>") >= 0) { // NOI18N
raster@16367
   153
                    line = line.replaceFirst("<BDY-EXT>", jgnat.getBdyExt()); // NOI18N
raster@16367
   154
                }
raster@16367
   155
                if (line.indexOf("<EXT-SEP>") >= 0) { // NOI18N
raster@16367
   156
                    line = line.replaceFirst("<EXT-SEP>", jgnat.getSepExt()); // NOI18N
raster@16367
   157
                }
raster@16367
   158
                if (line.indexOf("<SPC-POSTFIX>") >= 0) { // NOI18N
raster@16367
   159
                    line = line.replaceFirst("<SPC-POSTFIX>", jgnat.getSpcPostfix()); // NOI18N
raster@16367
   160
                }
raster@16367
   161
                if (line.indexOf("<BDY-POSTFIX>") >= 0) { // NOI18N
raster@16367
   162
                    line = line.replaceFirst("<BDY-POSTFIX>", jgnat.getBdyPostfix()); // NOI18N
raster@16367
   163
                }
raster@16367
   164
                bw.write(line + "\n"); // NOI18N
raster@16367
   165
            }
raster@16367
   166
            br.close();
raster@16367
   167
            bw.flush();
raster@16367
   168
            bw.close();
raster@16367
   169
        } catch (Exception e) {
raster@16367
   170
        }
raster@16367
   171
raster@16367
   172
    }
raster@16367
   173
raster@16367
   174
    public String getGprFilePath() {
raster@16367
   175
        return this.gprFilePath;
raster@16367
   176
    }
raster@16367
   177
}