Package anvil.tools

Source Code of anvil.tools.Compiler

/*
* $Id: Compiler.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.ErrorEvent;
import anvil.ErrorListener;
import anvil.ForgingException;
import anvil.Location;
import anvil.Log;
import anvil.script.ModuleEnvelope;
import anvil.script.Module;
import anvil.script.ModuleCache;
import anvil.server.Address;
import anvil.server.Zone;
import anvil.server.Server;
import anvil.server.ServerControl;
import anvil.server.Configurable;
import anvil.server.ConfigReader;
import anvil.server.ConfigurationError;
import anvil.server.CompilerPreferences;
import anvil.server.LoggingPreferences;
import java.io.File;
import java.io.PrintStream;
import java.io.LineNumberReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

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

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


  private int _errors = 0;
  private Server _server;


  public Compiler()
  {
  }

  public void usage()
  {
    System.out.println("Usage: anvilc [OPTIONS] [FILES...]");
    System.out.println("  -h, --help             Show help");
    System.out.println("  -d, --timestamp        Use timestamp [ false ]");
    System.out.println("  -c, --config=CONFIG    Path to configuration file");
    System.out.println("  -t, --target=PATH      Compilation target directory [ ./ ]");
    System.out.println("  -v, --verbose          Debug logging level");
    System.out.println();
    System.exit(1);
  }
 
 
  public void reconfigure(Zone zone)
  {
    Configurable[] configs = zone.getConfigurations();
    Configurable c;
    int n = configs.length;
    for(int i=0; i<n; i++) {
      c = configs[i];
      switch (c.getType()) {
      case Configurable.ZONE:
      case Configurable.DOMAIN:
      case Configurable.SERVER:
        reconfigure((Zone)c);
        break;
       
      case Configurable.COMPILER:
      case Configurable.ACCESS:
      case Configurable.SESSION_CONTAINER:
      case Configurable.SESSION:
        zone.deleteConfiguration(c);
        break;
       
      default:
        break;
      }
    }
  }
 
 

  public void compile(String[] args)
  {

    String config = null;
    String target = "./";
    boolean timestamp = false;
    boolean verbose = false;
    boolean use_root = false;
   
    int length = args.length;
    int index = 0;
    int j;
    String s;
    while(index<length) {
      s = args[index];

      if (s.startsWith("-")) {
        index++;

        if (s.equals("-h") || s.equals("--help")) {
          usage();
        }
       
        if (index<length) {

          if (s.equals("-c")) {
            config = args[index++];
            continue;
          } else if (s.equals("-t")) {
            target = args[index++];
            continue;
          } else if (s.equals("-d")) {
            timestamp = true;
            continue;
          } else if (s.equals("-v")) {
            verbose = true;
            continue;
          }

        }
       
        if (s.startsWith("--config")) {
          j = s.indexOf('=');
          if (j>0) {
            config = s.substring(j+1);
          } else {
            usage();
          }
        } else if (s.startsWith("--target")) {
          j = s.indexOf('=');
          if (j>0) {
            target = s.substring(j+1);
          } else {
            usage();
          }
        } else if (s.startsWith("--timestamp")) {
          timestamp = true;
        } else if (s.startsWith("--verbose")) {
          verbose = true;
        }       
       
      } else {
        break;
      }
     
    }
   
    if (config == null) {
      usage();
    }
   
    try {
   
      _server = new ConfigReader(null, new File(config)).parse()
      reconfigure(_server);
     
      Log.log().setSeverity(verbose ? Log.DEBUG : Log.ERROR);
     
      CompilerPreferences prefs = new CompilerPreferences(_server);
      prefs.setUseTimestamp(timestamp);
      prefs.setClassPath(target);
      _server.configure(prefs);
      _server.start();
     
      Vector envelopes = new Vector();

      if (index < length) {
        while(index < length) {
          compile(args[index++]);
        }
       
      } else {
     
        LineNumberReader reader = new LineNumberReader(new InputStreamReader(System.in));
        for(;;) {
          String pathinfo = reader.readLine();
          if (pathinfo == null) {
            break;
          }
          compile(pathinfo);
        }
       
      }

      if (_errors>0) {
        System.out.println(_errors + " error(s).");
        System.exit(1);
      } else {
        System.exit(0);
      }     
    
    } catch (Throwable t) {
      errors(System.err, t);
      System.exit(1);
    }
  }


  public void compile(String pathinfo)
  {
    if (pathinfo.startsWith("./")) {
      pathinfo = pathinfo.substring(1);
    } else if (!pathinfo.startsWith("/")) {
      pathinfo = "/" + pathinfo;
    }
    try {
      Zone zone = _server.resolveZone(pathinfo);
      Address address = zone.resolve(pathinfo);
      ModuleEnvelope envelope = _server.getCache().load(address);
      System.out.println("generated: "+envelope.getClassname());
    } catch (ForgingException e) {
      errors(System.err, e);
    } catch (Exception e) {
      errors(System.err, e);
    }
  }


  public void errors(PrintStream out, Throwable throwable)
  {
    if (throwable instanceof ForgingException) {
      ErrorListener listener = ((ForgingException)throwable).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();
        _errors++;
      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 {
      out.println(throwable.toString());
      throwable.printStackTrace();
      _errors++;
    }
  }


  public static final void main(String[] args)
  {
    Compiler compiler = new Compiler();
    compiler.compile(args);
  }

}
TOP

Related Classes of anvil.tools.Compiler

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.