Package anvil.tools

Source Code of anvil.tools.CommandLine

/*
* $Id: CommandLine.java,v 1.16 2002/09/16 08:05:07 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.tools;

import anvil.core.AnyString;
import anvil.core.Array;
import anvil.ErrorEvent;
import anvil.ErrorListener;
import anvil.ForgingException;
import anvil.Location;
import anvil.Product;
import anvil.server.Zone;
import anvil.server.Server;
import anvil.server.ServerControl;
import anvil.server.CompilerPreferences;
import anvil.server.Address;
import anvil.server.ConfigReader;
import anvil.server.LoggingPreferences;
import anvil.script.ScriptException;
import anvil.script.ModuleCache;
import anvil.script.Module;
import java.io.File;
import java.io.FileInputStream;
import java.io.PrintStream;
import java.net.URL;
import java.util.Enumeration;

/**
* class CommandLine
*
* @author: Jani Lehtim�ki
*/
public class CommandLine
{

  static {
    anvil.core.Any.class.getName();
  }


  public static void usage()
  {
    System.out.println("Usage: anvil [OPTIONS] [FILE] [SCRIPTOPTIONS]");
    System.out.println("  -v                          Verbose, do more logging");
    System.out.println("  -h, --help                  Show help");
    System.out.println("  -n, --namespace=NAMESPACE   Namespace for templates (default is none)");
    System.out.println();
    System.exit(1);
  }
 

  public static void main(String[] args)
  {

    String namespace = null;
    String filename = null;
    boolean verbose = false;
    int length = args.length;
    int index = 0;
    int j;
    String s;
    while(index<length) {
      s = args[index++];

      if (s.startsWith("-")) {

        if (s.equals("-h") || s.equals("--help")) {
          usage();
        }
       
        if (s.equals("-v")) {
          verbose = true;
          continue;
        }
       
        if (index<length) {

          if (s.equals("-n")) {
            namespace = args[index++];
            continue;
          }

        }
       
        if (s.startsWith("--namespace")) {
          j = s.indexOf('=');
          if (j>0) {
            namespace = s.substring(j+1);
          } else {
            usage();
          }
        }
       
      } else {
        filename = s;
        break;
      }
     
    }
   
    if (filename == null) {
      usage();
    }
   
    File file = new File(filename);
    filename = file.getAbsolutePath();

    try {
   
      Server server;

      String configFile = System.getProperty("anvil.configFile");
      if (configFile != null) {
        ConfigReader reader = new ConfigReader(null, new File(configFile));
        server = reader.parse();
      } else {
        server = new Server(null);
        CompilerPreferences compiler = new CompilerPreferences(server);
        compiler.setUseTimestamp(false);
        compiler.setStoreImages(false);
        compiler.setClassPath("/var/tmp/");
        server.configure(compiler);
      }
      server.setShouldInvalidate(false);
      server.setContainer("file:/");
      if (namespace != null) {
        server.setNamespace(namespace);
      }
      int severity = verbose ? anvil.Log.DEBUG : anvil.Log.ERROR;
      anvil.Log.log().setSeverity(severity);
      LoggingPreferences logprefs = new LoggingPreferences(server);
      logprefs.setLevel(severity);
      server.configure(logprefs);
      server.start();

      Zone zone = server.resolveZone(filename);
      Address address = zone.resolve(filename);
      Module script = server.getCache().load(address).getModule();
      Product product = new Product(address, System.out, script);
      Array arguments = new Array();
      while(index < length) {
        arguments.append(new AnyString(args[index++]));
      }
      product.forge("main", arguments);
      System.exit(0);

    } catch (Exception e) {
      errors(System.err, e);
      System.exit(1);
    }
  }


  public static void errors(PrintStream out, Exception exception) {
    if (exception instanceof ForgingException) {
      ErrorListener listener = ((ForgingException)exception).getErrorListener();
      Enumeration e = listener.getEvents();
      int count = 0;
      URL last = null;
      URL url;
      while(e.hasMoreElements()) {
        ErrorEvent evt = (ErrorEvent)e.nextElement();
        Location loc = evt.getLocation();
        if (loc != null) {
          url = loc.getURL();
          if (url != null) {
            if (last == null || !last.equals(url)) {
              last = url;
              out.print(url);
              out.println(':');
            }
          }
          out.print("  ");
          if (loc.getLine() > 0) {
            out.print('[');
            out.print(loc.getLine());
            if (loc.getColumn() > 0) {
              out.print(':');
              out.print(loc.getColumn());
            }
            out.print("] ");
          }
        }
        out.println(evt.getMessage());
        count++;
      }
    } else if (exception instanceof ScriptException) {
      out.println("Uncaught exception: ");
      out.println(((ScriptException)exception).getData().toString());
    } else {
      out.print("anvil: ");
      out.println(exception.toString());
      exception.printStackTrace(out);
    }
  }


}
TOP

Related Classes of anvil.tools.CommandLine

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.