001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 /*
019 * $Id: JavaCupRedirect.java 468653 2006-10-28 07:07:05Z minchau $
020 */
021
022 package org.apache.xalan.xsltc.util;
023
024 import java.io.FileInputStream;
025 import java.io.FileNotFoundException;
026 import java.io.InputStream;
027
028 /**
029 * Utility class to redirect input to JavaCup program.
030 *
031 * Usage-command line:
032 * <code>java org.apache.xalan.xsltc.utils.JavaCupRedirect [args] -stdin filename.ext</code>
033 *
034 * @author Morten Jorgensen
035 * @version $Id: JavaCupRedirect.java 468653 2006-10-28 07:07:05Z minchau $
036 */
037 public class JavaCupRedirect {
038
039 private final static String ERRMSG =
040 "You must supply a filename with the -stdin option.";
041
042 public static void main (String args[]) {
043
044 // If we should call System.exit or not
045 //@todo make this settable for use inside other java progs
046 boolean systemExitOK = true;
047
048 // This is the stream we'll set as our System.in
049 InputStream input = null;
050
051 // The number of arguments
052 final int argc = args.length;
053
054 // The arguments we'll pass to the real 'main()'
055 String[] new_args = new String[argc - 2];
056 int new_argc = 0;
057
058 // Parse all parameters passed to this class
059 for (int i = 0; i < argc; i++) {
060 // Parse option '-stdin <filename>'
061 if (args[i].equals("-stdin")) {
062 // This option must have an argument
063 if ((++i >= argc) || (args[i].startsWith("-"))) {
064 System.err.println(ERRMSG);
065 throw new RuntimeException(ERRMSG);
066 }
067 try {
068 input = new FileInputStream(args[i]);
069 }
070 catch (FileNotFoundException e) {
071 System.err.println("Could not open file "+args[i]);
072 throw new RuntimeException(e.getMessage());
073 }
074 catch (SecurityException e) {
075 System.err.println("No permission to file "+args[i]);
076 throw new RuntimeException(e.getMessage());
077 }
078 }
079 else {
080 if (new_argc == new_args.length) {
081 System.err.println("Missing -stdin option!");
082 throw new RuntimeException();
083 }
084 new_args[new_argc++] = args[i];
085 }
086 }
087
088 System.setIn(input);
089 try {
090 java_cup.Main.main(new_args);
091 }
092 catch (Exception e) {
093 System.err.println("Error running JavaCUP:");
094 e.printStackTrace();
095 }
096 }
097 }