hotspot/src/share/tools/ProjectCreator/MacroDefinitions.java
author stefank
Tue, 23 Nov 2010 13:22:55 -0800
changeset 7397 5b173b4ca846
parent 5547 hotspot/src/share/tools/MakeDeps/MacroDefinitions.java@f4b087cbb361
permissions -rw-r--r--
6989984: Use standard include model for Hospot Summary: Replaced MakeDeps and the includeDB files with more standardized solutions. Reviewed-by: coleenp, kvn, kamg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
     2
 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
import java.io.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
import java.util.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
public class MacroDefinitions {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
    private Vector macros;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
    public MacroDefinitions() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
        macros = new Vector();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
    public void addMacro(String name, String contents) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
        Macro macro = new Macro();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
        macro.name = name;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
        macro.contents = contents;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
        macros.add(macro);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
    private boolean lineIsEmpty(String s) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
        for (int i = 0; i < s.length(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
            if (!Character.isWhitespace(s.charAt(i))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
                return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
        return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
    public void readFrom(String fileName, boolean missingOk)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
        throws FileNotFoundException, FileFormatException, IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
        BufferedReader reader = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
            reader = new BufferedReader(new FileReader(fileName));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
        } catch (FileNotFoundException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
            if (missingOk) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
                return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
            } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
                throw(e);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
        String line;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
        do {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
            line = reader.readLine();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
            if (line != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
                // This had to be rewritten (compare to Database.java)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
                // because the Solaris platform file has been
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
                // repurposed and now contains "macros" with spaces in
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
                // them.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
                if ((!line.startsWith("//")) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
                    (!lineIsEmpty(line))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
                    int nameBegin = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
                    int nameEnd = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
                    boolean gotEquals = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
                    int contentsBegin = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
                    int contentsEnd = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
                    int i = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
                    // Scan forward for beginning of name
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
                    while (i < line.length()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
                        if (!Character.isWhitespace(line.charAt(i))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
                            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
                        i++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
                    nameBegin = i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
                    // Scan forward for end of name
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
                    while (i < line.length()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
                        if (Character.isWhitespace(line.charAt(i))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
                            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
                        i++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
                    nameEnd = i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
                    // Scan forward for equals sign
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
                    while (i < line.length()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
                        if (line.charAt(i) == '=') {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
                            gotEquals = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
                            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
                        i++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
                    // Scan forward for start of contents
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
                    i++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
                    while (i < line.length()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
                        if (!Character.isWhitespace(line.charAt(i))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
                            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
                        i++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
                    contentsBegin = i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
                    // Scan *backward* for end of contents
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
                    i = line.length() - 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
                    while (i >= 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
                        if (!Character.isWhitespace(line.charAt(i))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
                            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
                    contentsEnd = i+1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
                    // Now do consistency check
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
                    if (!((nameBegin < nameEnd) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
                          (nameEnd < contentsBegin) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
                          (contentsBegin < contentsEnd) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
                          (gotEquals == true))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
                        throw new FileFormatException(
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
                            "Expected \"macroname = value\", " +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
                            "but found: " + line
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
                        );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
                    String name = line.substring(nameBegin, nameEnd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
                    String contents = line.substring(contentsBegin,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
                                                     contentsEnd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
                    addMacro(name, contents);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
        } while (line != null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
        reader.close();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
    /** This returns an Iterator of Macros. You should not mutate the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
        returned Macro objects or use the Iterator to remove
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
        macros. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    public Iterator getMacros() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
        return macros.iterator();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
}