KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > util > JavaCupRedirect


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: JavaCupRedirect.java,v 1.3 2004/02/16 22:58:24 minchau Exp $
18  */

19
20 package org.apache.xalan.xsltc.util;
21
22 import java.io.FileInputStream JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.InputStream JavaDoc;
25
26 /**
27  * Utility class to redirect input to JavaCup program.
28  *
29  * Usage-command line:
30  * <code>java org.apache.xalan.xsltc.utils.JavaCupRedirect [args] -stdin filename.ext</code>
31  *
32  * @author Morten Jorgensen
33  * @version $Id: JavaCupRedirect.java,v 1.3 2004/02/16 22:58:24 minchau Exp $
34  */

35 public class JavaCupRedirect {
36
37     private final static String JavaDoc ERRMSG =
38          "You must supply a filename with the -stdin option.";
39
40     public static void main (String JavaDoc args[]) {
41
42          // If we should call System.exit or not
43
//@todo make this settable for use inside other java progs
44
boolean systemExitOK = true;
45
46          // This is the stream we'll set as our System.in
47
InputStream JavaDoc input = null;
48
49          // The number of arguments
50
final int argc = args.length;
51
52          // The arguments we'll pass to the real 'main()'
53
String JavaDoc[] new_args = new String JavaDoc[argc - 2];
54          int new_argc = 0;
55
56          // Parse all parameters passed to this class
57
for (int i = 0; i < argc; i++) {
58              // Parse option '-stdin <filename>'
59
if (args[i].equals("-stdin")) {
60                  // This option must have an argument
61
if ((++i >= argc) || (args[i].startsWith("-"))) {
62                      System.err.println(ERRMSG);
63                      doSystemExit(systemExitOK);
64                  }
65                  try {
66                      input = new FileInputStream JavaDoc(args[i]);
67                  }
68                  catch (FileNotFoundException JavaDoc e) {
69                      System.err.println("Could not open file "+args[i]);
70                      doSystemExit(systemExitOK);
71                  }
72                  catch (SecurityException JavaDoc e) {
73                      System.err.println("No permission to file "+args[i]);
74                      doSystemExit(systemExitOK);
75                  }
76              }
77              else {
78                  if (new_argc == new_args.length) {
79                      System.err.println("Missing -stdin option!");
80                      doSystemExit(systemExitOK);
81                  }
82                  new_args[new_argc++] = args[i];
83              }
84          }
85
86          System.setIn(input);
87          try {
88              java_cup.Main.main(new_args);
89          }
90          catch (Exception JavaDoc e) {
91              System.err.println("Error running JavaCUP:");
92              e.printStackTrace();
93              doSystemExit(systemExitOK);
94          }
95     }
96     public static void doSystemExit (boolean doExit) {
97         if (doExit)
98             System.exit(-1);
99     }
100 }
101
Popular Tags