src/share/tools/MakeDeps/MacroDefinitions.java

Thu, 27 May 2010 19:08:38 -0700

author
trims
date
Thu, 27 May 2010 19:08:38 -0700
changeset 1907
c18cbe5936b8
parent 435
a61af66fc99e
permissions
-rw-r--r--

6941466: Oracle rebranding changes for Hotspot repositories
Summary: Change all the Sun copyrights to Oracle copyright
Reviewed-by: ohair

duke@435 1 /*
trims@1907 2 * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 import java.io.*;
duke@435 26 import java.util.*;
duke@435 27
duke@435 28 public class MacroDefinitions {
duke@435 29 private Vector macros;
duke@435 30
duke@435 31 public MacroDefinitions() {
duke@435 32 macros = new Vector();
duke@435 33 }
duke@435 34
duke@435 35 private String lookup(String name) throws NoSuchElementException {
duke@435 36 for (Iterator iter = macros.iterator(); iter.hasNext(); ) {
duke@435 37 Macro macro = (Macro) iter.next();
duke@435 38 if (macro.name.equals(name)) {
duke@435 39 return macro.contents;
duke@435 40 }
duke@435 41 }
duke@435 42 throw new NoSuchElementException(name);
duke@435 43 }
duke@435 44
duke@435 45 public void addMacro(String name, String contents) {
duke@435 46 Macro macro = new Macro();
duke@435 47 macro.name = name;
duke@435 48 macro.contents = contents;
duke@435 49 macros.add(macro);
duke@435 50 }
duke@435 51
duke@435 52 private boolean lineIsEmpty(String s) {
duke@435 53 for (int i = 0; i < s.length(); i++) {
duke@435 54 if (!Character.isWhitespace(s.charAt(i))) {
duke@435 55 return false;
duke@435 56 }
duke@435 57 }
duke@435 58 return true;
duke@435 59 }
duke@435 60
duke@435 61 public void readFrom(String fileName, boolean missingOk)
duke@435 62 throws FileNotFoundException, FileFormatException, IOException {
duke@435 63 BufferedReader reader = null;
duke@435 64 try {
duke@435 65 reader = new BufferedReader(new FileReader(fileName));
duke@435 66 } catch (FileNotFoundException e) {
duke@435 67 if (missingOk) {
duke@435 68 return;
duke@435 69 } else {
duke@435 70 throw(e);
duke@435 71 }
duke@435 72 }
duke@435 73 String line;
duke@435 74 do {
duke@435 75 line = reader.readLine();
duke@435 76 if (line != null) {
duke@435 77 // This had to be rewritten (compare to Database.java)
duke@435 78 // because the Solaris platform file has been
duke@435 79 // repurposed and now contains "macros" with spaces in
duke@435 80 // them.
duke@435 81
duke@435 82 if ((!line.startsWith("//")) &&
duke@435 83 (!lineIsEmpty(line))) {
duke@435 84 int nameBegin = -1;
duke@435 85 int nameEnd = -1;
duke@435 86 boolean gotEquals = false;
duke@435 87 int contentsBegin = -1;
duke@435 88 int contentsEnd = -1;
duke@435 89
duke@435 90 int i = 0;
duke@435 91 // Scan forward for beginning of name
duke@435 92 while (i < line.length()) {
duke@435 93 if (!Character.isWhitespace(line.charAt(i))) {
duke@435 94 break;
duke@435 95 }
duke@435 96 i++;
duke@435 97 }
duke@435 98 nameBegin = i;
duke@435 99
duke@435 100 // Scan forward for end of name
duke@435 101 while (i < line.length()) {
duke@435 102 if (Character.isWhitespace(line.charAt(i))) {
duke@435 103 break;
duke@435 104 }
duke@435 105 i++;
duke@435 106 }
duke@435 107 nameEnd = i;
duke@435 108
duke@435 109 // Scan forward for equals sign
duke@435 110 while (i < line.length()) {
duke@435 111 if (line.charAt(i) == '=') {
duke@435 112 gotEquals = true;
duke@435 113 break;
duke@435 114 }
duke@435 115 i++;
duke@435 116 }
duke@435 117
duke@435 118 // Scan forward for start of contents
duke@435 119 i++;
duke@435 120 while (i < line.length()) {
duke@435 121 if (!Character.isWhitespace(line.charAt(i))) {
duke@435 122 break;
duke@435 123 }
duke@435 124 i++;
duke@435 125 }
duke@435 126 contentsBegin = i;
duke@435 127
duke@435 128 // Scan *backward* for end of contents
duke@435 129 i = line.length() - 1;
duke@435 130 while (i >= 0) {
duke@435 131 if (!Character.isWhitespace(line.charAt(i))) {
duke@435 132 break;
duke@435 133 }
duke@435 134 }
duke@435 135 contentsEnd = i+1;
duke@435 136
duke@435 137 // Now do consistency check
duke@435 138 if (!((nameBegin < nameEnd) &&
duke@435 139 (nameEnd < contentsBegin) &&
duke@435 140 (contentsBegin < contentsEnd) &&
duke@435 141 (gotEquals == true))) {
duke@435 142 throw new FileFormatException(
duke@435 143 "Expected \"macroname = value\", " +
duke@435 144 "but found: " + line
duke@435 145 );
duke@435 146 }
duke@435 147
duke@435 148 String name = line.substring(nameBegin, nameEnd);
duke@435 149 String contents = line.substring(contentsBegin,
duke@435 150 contentsEnd);
duke@435 151 addMacro(name, contents);
duke@435 152 }
duke@435 153 }
duke@435 154 } while (line != null);
duke@435 155 reader.close();
duke@435 156 }
duke@435 157
duke@435 158 /** Throws IllegalArgumentException if passed token is illegally
duke@435 159 formatted */
duke@435 160 public String expand(String token)
duke@435 161 throws IllegalArgumentException {
duke@435 162 // the token may contain one or more <macroName>'s
duke@435 163
duke@435 164 String out = "";
duke@435 165
duke@435 166 // emacs lingo
duke@435 167 int mark = 0;
duke@435 168 int point = 0;
duke@435 169
duke@435 170 int len = token.length();
duke@435 171
duke@435 172 if (len == 0)
duke@435 173 return out;
duke@435 174
duke@435 175 do {
duke@435 176 // Scan "point" forward until hitting either the end of
duke@435 177 // the string or the beginning of a macro
duke@435 178 if (token.charAt(point) == '<') {
duke@435 179 // Append (point - mark) to out
duke@435 180 if ((point - mark) != 0) {
duke@435 181 out += token.substring(mark, point);
duke@435 182 }
duke@435 183 mark = point + 1;
duke@435 184 // Scan forward from point for right bracket
duke@435 185 point++;
duke@435 186 while ((point < len) &&
duke@435 187 (token.charAt(point) != '>')) {
duke@435 188 point++;
duke@435 189 }
duke@435 190 if (point == len) {
duke@435 191 throw new IllegalArgumentException(
duke@435 192 "Could not find right angle-bracket in token " + token
duke@435 193 );
duke@435 194 }
duke@435 195 String name = token.substring(mark, point);
duke@435 196 if (name == null) {
duke@435 197 throw new IllegalArgumentException(
duke@435 198 "Empty macro in token " + token
duke@435 199 );
duke@435 200 }
duke@435 201 try {
duke@435 202 String contents = lookup(name);
duke@435 203 out += contents;
duke@435 204 point++;
duke@435 205 mark = point;
duke@435 206 } catch (NoSuchElementException e) {
duke@435 207 throw new IllegalArgumentException(
duke@435 208 "Unknown macro " + name + " in token " + token
duke@435 209 );
duke@435 210 }
duke@435 211 } else {
duke@435 212 point++;
duke@435 213 }
duke@435 214 } while (point != len);
duke@435 215
duke@435 216 if (mark != point) {
duke@435 217 out += token.substring(mark, point);
duke@435 218 }
duke@435 219
duke@435 220 return out;
duke@435 221 }
duke@435 222
duke@435 223 public MacroDefinitions copy() {
duke@435 224 MacroDefinitions ret = new MacroDefinitions();
duke@435 225 for (Iterator iter = macros.iterator();
duke@435 226 iter.hasNext(); ) {
duke@435 227 Macro orig = (Macro) iter.next();
duke@435 228 Macro macro = new Macro();
duke@435 229 macro.name = orig.name;
duke@435 230 macro.contents = orig.contents;
duke@435 231 ret.macros.add(macro);
duke@435 232 }
duke@435 233 return ret;
duke@435 234 }
duke@435 235
duke@435 236 public void setAllMacroBodiesTo(String s) {
duke@435 237 for (Iterator iter = macros.iterator();
duke@435 238 iter.hasNext(); ) {
duke@435 239 Macro macro = (Macro) iter.next();
duke@435 240 macro.contents = s;
duke@435 241 }
duke@435 242 }
duke@435 243
duke@435 244 /** This returns an Iterator of Macros. You should not mutate the
duke@435 245 returned Macro objects or use the Iterator to remove
duke@435 246 macros. */
duke@435 247 public Iterator getMacros() {
duke@435 248 return macros.iterator();
duke@435 249 }
duke@435 250
duke@435 251 private void error(String text) throws FileFormatException {
duke@435 252 throw new FileFormatException(
duke@435 253 "Expected \"macroname = value\", but found: " + text
duke@435 254 );
duke@435 255 }
duke@435 256 }

mercurial