1 /*
   2  * Copyright (c) 2008, 2015, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package build.tools.spp;
  27 
  28 import java.util.*;
  29 import java.util.regex.*;
  30 
  31 /*
  32  * Spp: A simple regex-based stream preprocessor based on Mark Reinhold's
  33  *      sed-based spp.sh
  34  *
  35  * Usage: java build.tools.spp.Spp [-be] [-nel] [-Kkey] -Dvar=value ... <in >out
  36  *
  37  * If -nel is declared then empty lines will not be substituted for lines of
  38  * text in the template that do not appear in the output.
  39  *
  40  *   Meaningful only at beginning of line, works with any number of keys:
  41  *
  42  *    #if[key]              Includes text between #if/#end if -Kkey specified,
  43  *    #else[key]            otherwise changes text to blank lines; key test
  44  *    #end[key]             may be negated by prefixing !, e.g., #if[!key]
  45  *
  46  *    #begin                If -be is specified then lines up to and including
  47  *    #end                  #begin, and from #end to EOF, are deleted
  48  *
  49  *    #warn                 Changed into warning that file is generated
  50  *
  51  *    // ##                 Changed into blank line
  52  *
  53  *  Meaningful anywhere in line
  54  *
  55  *    {#if[key]?yes}        Expands to yes if -Kkey specified
  56  *    {#if[key]?yes:no}     Expands to yes if -Kkey, otherwise no
  57  *    {#if[!key]?yes}       Expands to yes if -Kother
  58  *    {#if[!key]?yes:no}    Expands to yes if -Kother, otherwise no
  59  *    $var$                 Expands to value if -Dvar=value given
  60  *
  61  *    yes, no must not contain whitespace
  62  *
  63  * @author Xueming Shen
  64  */
  65 
  66 public class Spp {
  67     public static void main(String args[]) throws Exception {
  68         Map<String, String> vars = new HashMap<>();
  69         Set<String> keys = new HashSet<>();
  70         boolean be = false;
  71         boolean el = true;
  72 
  73         for (String arg:args) {
  74             if (arg.startsWith("-D")) {
  75                 int i = arg.indexOf('=');
  76                 vars.put(arg.substring(2, i),arg.substring(i+1));
  77             } else if (arg.startsWith("-K")) {
  78                 keys.add(arg.substring(2));
  79             } else if ("-be".equals(arg)) {
  80                 be = true;
  81             } else if ("-nel".equals(arg)) {
  82                 el = false;
  83             } else {
  84                 System.err.println("Usage: java build.tools.spp.Spp [-be] [-nel] [-Kkey] -Dvar=value ... <in >out");
  85                 System.exit(-1);
  86             }
  87         }
  88 
  89         StringBuffer out = new StringBuffer();
  90         new Spp().spp(new Scanner(System.in),
  91                       out, "",
  92                       keys, vars, be, el,
  93                       false);
  94         System.out.print(out.toString());
  95     }
  96 
  97     static final String LNSEP = System.getProperty("line.separator");
  98     static final String KEY = "([a-zA-Z0-9]+)";
  99     static final String VAR = "([a-zA-Z0-9_\\-]+)";
 100     static final String TEXT = "([a-zA-Z0-9&;,.<>/#() \\?\\[\\]\\$]+)"; // $ -- hack embedded $var$
 101 
 102     static final int GN_NOT = 1;
 103     static final int GN_KEY = 2;
 104     static final int GN_YES = 3;
 105     static final int GN_NO  = 5;
 106     static final int GN_VAR = 6;
 107 
 108     final Matcher   ifkey = Pattern.compile("^#if\\[(!)?" + KEY + "\\]").matcher("");
 109     final Matcher elsekey = Pattern.compile("^#else\\[(!)?" + KEY + "\\]").matcher("");
 110     final Matcher  endkey = Pattern.compile("^#end\\[(!)?" + KEY + "\\]").matcher("");
 111     final Matcher  vardef = Pattern.compile("\\{#if\\[(!)?" + KEY + "\\]\\?" + TEXT + "(:"+ TEXT + ")?\\}|\\$" + VAR + "\\$").matcher("");
 112     final Matcher vardef2 = Pattern.compile("\\$" + VAR + "\\$").matcher("");
 113 
 114     void append(StringBuffer buf, String ln,
 115                 Set<String> keys, Map<String, String> vars) {
 116         vardef.reset(ln);
 117         while (vardef.find()) {
 118             String repl = "";
 119             if (vardef.group(GN_VAR) != null)
 120                 repl = vars.get(vardef.group(GN_VAR));
 121             else {
 122                 boolean test = keys.contains(vardef.group(GN_KEY));
 123                 if (vardef.group(GN_NOT) != null)
 124                     test = !test;
 125                 repl = test?vardef.group(GN_YES):vardef.group(GN_NO);
 126                 if (repl == null)
 127                     repl = "";
 128                 else {  // embedded $var$
 129                     while (vardef2.reset(repl).find()) {
 130                         repl = vardef2.replaceFirst(vars.get(vardef2.group(1)));
 131                     }
 132                 }
 133             }
 134             vardef.appendReplacement(buf, repl);
 135         }
 136         vardef.appendTail(buf);
 137     }
 138 
 139     // return true if #end[key], #end or EOF reached
 140     boolean spp(Scanner in, StringBuffer buf, String key,
 141                 Set<String> keys, Map<String, String> vars,
 142                 boolean be, boolean el, boolean skip) {
 143         while (in.hasNextLine()) {
 144             String ln = in.nextLine();
 145             if (be) {
 146                 if (ln.startsWith("#begin")) {
 147                     buf.setLength(0);      //clean up to this line
 148                     continue;
 149                 }
 150                 if (ln.equals("#end")) {
 151                     while (in.hasNextLine())
 152                         in.nextLine();
 153                     return true;           //discard the rest to EOF
 154                 }
 155             }
 156             if (ifkey.reset(ln).find()) {
 157                 String k = ifkey.group(GN_KEY);
 158                 boolean test = keys.contains(k);
 159                 if (ifkey.group(GN_NOT) != null)
 160                     test = !test;
 161                 if (el) buf.append(LNSEP);
 162                 if (!spp(in, buf, k, keys, vars, be, el, skip || !test)) {
 163                     spp(in, buf, k, keys, vars, be, el, skip || test);
 164                 }
 165                 continue;
 166             }
 167             if (elsekey.reset(ln).find()) {
 168                 if (!key.equals(elsekey.group(GN_KEY))) {
 169                     throw new Error("Mis-matched #if-else-end at line <" + ln + ">");
 170                 }
 171                 if (el) buf.append(LNSEP);
 172                 return false;
 173             }
 174             if (endkey.reset(ln).find()) {
 175                 if (!key.equals(endkey.group(GN_KEY))) {
 176                     throw new Error("Mis-matched #if-else-end at line <" + ln + ">");
 177                 }
 178                 if (el) buf.append(LNSEP);
 179                 return true;
 180             }
 181             if (ln.startsWith("#warn")) {
 182                 ln = "// -- This file was mechanically generated: Do not edit! -- //";
 183             } else if (ln.trim().startsWith("// ##")) {
 184                 ln = "";
 185             }
 186             if (!skip) {
 187                 append(buf, ln, keys, vars);
 188                 if (!el) buf.append(LNSEP);
 189             }
 190             if (el) buf.append(LNSEP);
 191         }
 192         return true;
 193     }
 194 }