Пример #1
0
  static void processFiles(Context cx, String[] files) throws IOException {
    StringBuffer cout = new StringBuffer();
    if (files.length > 0) {
      for (int i = 0; i < files.length; i++) {
        try {
          String source = (String) readFileOrUrl(files[i], true);
          cout.append(Compressor.compressScript(source, 0, 1, escapeUnicode, stripConsole));
        } catch (IOException ex) {
          // continue processing files
        }
      }
    } else {
      byte[] data = Kit.readStream(global.getIn(), 4096);
      // Convert to String using the default encoding
      String source = new String(data);
      if (source != null) {
        cout.append(Compressor.compressScript(source, 0, 1, escapeUnicode, stripConsole));
      }
    }

    global.getOut().println(cout);
  }
Пример #2
0
  public static String[] processOptions(String args[]) {
    List fileList = new ArrayList();
    String usageError = null;
    boolean showUsage = false;

    for (int i = 0; i < args.length; i++) {
      String arg = args[i];
      if (!arg.startsWith("-")) {
        fileList.add(arg);
      } else if (arg.equals("-js-version")) {
        if (++i == args.length) {
          usageError = arg;
        }
        int version = 0;
        try {
          version = Integer.parseInt(args[i]);
        } catch (NumberFormatException ex) {
          usageError = args[i];
        }
        if (!Context.isValidLanguageVersion(version)) {
          usageError = args[i];
        }
        if (usageError != null) shellContextFactory.setLanguageVersion(version);
      }
      /*
      			else if (arg.equals("-opt") || arg.equals("-O")) {
      				if (++i == args.length) {
      					usageError = arg;
      				}
      				int opt = 0;
      				try {
      					opt = Integer.parseInt(args[i]);
      				} catch (NumberFormatException ex) {
      					usageError = args[i];
      				}
      				if (opt == -2) {
      					// Compatibility with Cocoon Rhino fork
      					opt = -1;
      				} else if (!Context.isValidOptimizationLevel(opt)) {
      					usageError = args[i];
      				}
      				if (usageError != null) {
      					shellContextFactory.setOptimizationLevel(opt);
      				}
      			}
      			else if (arg.equals("-debug")) {
      				shellContextFactory.setGeneratingDebug(true);
      			}
      */
      else if (arg.equals("-?") || arg.equals("-help")) {
        showUsage = true;
      } else if (arg.equals("-escape-unicode")) {
        escapeUnicode = true;
      } else if (arg.equals("-stripConsole")) {
        if (i >= (args.length - 1)) {
          usageError = getMessage("msg.shell.stripConsoleMissingArg");
        } else {
          stripConsole = args[++i];
          if (!stripConsole.equals("normal")
              && !stripConsole.equals("warn")
              && !stripConsole.equals("all")) {
            usageError = getMessage("msg.shell.stripConsoleInvalid");
          }
        }
      }
    }

    // print error and usage message
    if (usageError != null) {
      global.getOut().println(getMessage("msg.shell.invalid", usageError));
    }
    if (usageError != null || showUsage) {
      global.getOut().println(getMessage("msg.shell.usage"));
      System.exit(1);
    }

    String[] files = new String[fileList.size()];
    files = (String[]) fileList.toArray(files);
    return files;
  }