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 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, | 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.io.FileInputStream; 29 import java.io.FileOutputStream; 30 import java.util.*; 31 import java.util.regex.*; 32 33 /* 34 * Spp: A simple regex-based stream preprocessor based on Mark Reinhold's 35 * sed-based spp.sh 36 * 37 * Usage: java build.tools.spp.Spp [-be] [-nel] [-Kkey] -Dvar=value ... <in >out 38 * 39 * If -nel is declared then empty lines will not be substituted for lines of 40 * text in the template that do not appear in the output. 41 * 42 * Meaningful only at beginning of line, works with any number of keys: 43 * 44 * #if[key] Includes text between #if/#end if -Kkey specified, 45 * #else[key] otherwise changes text to blank lines; key test 46 * #end[key] may be negated by prefixing !, e.g., #if[!key] 47 * 48 * #begin If -be is specified then lines up to and including 49 * #end #begin, and from #end to EOF, are deleted 54 * 55 * Meaningful anywhere in line 56 * 57 * {#if[key]?yes} Expands to yes if -Kkey specified 58 * {#if[key]?yes:no} Expands to yes if -Kkey, otherwise no 59 * {#if[!key]?yes} Expands to yes if -Kother 60 * {#if[!key]?yes:no} Expands to yes if -Kother, otherwise no 61 * $var$ Expands to value if -Dvar=value given 62 * 63 * yes, no must not contain whitespace 64 * 65 * @author Xueming Shen 66 */ 67 68 public class Spp { 69 public static void main(String args[]) throws Exception { 70 Map<String, String> vars = new HashMap<>(); 71 Set<String> keys = new HashSet<>(); 72 boolean be = false; 73 boolean el = true; 74 String inputFile = null; 75 String outputFile = null; 76 77 for (String arg:args) { 78 if (arg.startsWith("-D")) { 79 int i = arg.indexOf('='); 80 vars.put(arg.substring(2, i),arg.substring(i+1)); 81 } else if (arg.startsWith("-K")) { 82 keys.add(arg.substring(2)); 83 } else if (arg.startsWith("-i")) { 84 inputFile = arg.substring(2); 85 } else if (arg.startsWith("-o")) { 86 outputFile = arg.substring(2); 87 } else if ("-be".equals(arg)) { 88 be = true; 89 } else if ("-nel".equals(arg)) { 90 el = false; 91 } else { 92 System.err.println("Usage: java build.tools.spp.Spp [-be] [-nel] [-Kkey] -Dvar=value ... <in >out"); 93 System.exit(-1); 94 } 95 } 96 97 StringBuffer out = new StringBuffer(); 98 new Spp().spp(new Scanner(new FileInputStream(inputFile)), 99 out, "", 100 keys, vars, be, el, 101 false); 102 new FileOutputStream(outputFile, true).write(out.toString().getBytes()); 103 } 104 105 static final String LNSEP = System.getProperty("line.separator"); 106 static final String KEY = "([a-zA-Z0-9]+)"; 107 static final String VAR = "([a-zA-Z0-9_\\-]+)"; 108 static final String TEXT = "([a-zA-Z0-9&;,.<>/#() \\?\\[\\]\\$]+)"; // $ -- hack embedded $var$ 109 110 static final int GN_NOT = 1; 111 static final int GN_KEY = 2; 112 static final int GN_YES = 3; 113 static final int GN_NO = 5; 114 static final int GN_VAR = 6; 115 116 final Matcher ifkey = Pattern.compile("^#if\\[(!)?" + KEY + "\\]").matcher(""); 117 final Matcher elsekey = Pattern.compile("^#else\\[(!)?" + KEY + "\\]").matcher(""); 118 final Matcher endkey = Pattern.compile("^#end\\[(!)?" + KEY + "\\]").matcher(""); 119 final Matcher vardef = Pattern.compile("\\{#if\\[(!)?" + KEY + "\\]\\?" + TEXT + "(:"+ TEXT + ")?\\}|\\$" + VAR + "\\$").matcher(""); 120 final Matcher vardef2 = Pattern.compile("\\$" + VAR + "\\$").matcher(""); 121 122 void append(StringBuffer buf, String ln, |