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