Quadcap Embeddable Server

com/quadcap/http/servlets/jsp/Javac.java

Go to the documentation of this file.
00001 package com.quadcap.http.servlets.jsp; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.OutputStream; 00042 import java.util.ArrayList; 00043 00044 import sun.tools.javac.Main; 00045 00046 import com.quadcap.util.Debug; 00047 00048 /** 00049 * This compiler class uses javac to to the dirty work, in the sad 00050 * situation where Jikes can't be found. 00051 * 00052 * @author Stan Bailes 00053 */ 00054 public class Javac extends JavaCompiler { 00055 public Javac() {} 00056 00057 final String chopLastQ(String s) { 00058 if (s.length() > 0 && s.charAt(s.length()-1) == '"') { 00059 s = s.substring(0, s.length()-1); 00060 } 00061 return s; 00062 } 00063 00064 private final String[] parseArgs(String str) { 00065 ArrayList r = new ArrayList(); 00066 int state = 0; // skipws; 00067 StringBuffer sb = new StringBuffer(); 00068 for (int i = 0; i < str.length(); i++) { 00069 final char c = str.charAt(i); 00070 switch (state) { 00071 case 0: 00072 if (!Character.isSpace(c)) { 00073 if (c != '"') sb.append(c); 00074 state = 1; 00075 } 00076 break; 00077 case 1: 00078 if (Character.isSpace(c)) { 00079 r.add(chopLastQ(sb.toString())); 00080 sb.setLength(0); 00081 state = 1; 00082 break; 00083 } else { 00084 sb.append(c); 00085 } 00086 break; 00087 } 00088 } 00089 if (sb.length() > 0) { 00090 r.add(chopLastQ(sb.toString())); 00091 } 00092 if (r.size() == 0) return null; 00093 int i = 0; 00094 if (r.get(0).toString().equalsIgnoreCase("javac")) i++; 00095 String[] ret = new String[r.size() - i]; 00096 for (int ri = 0; ri < ret.length; ri++) { 00097 ret[ri] = r.get(i++).toString(); 00098 } 00099 return ret; 00100 } 00101 00102 public boolean doCompile(String cmd, OutputStream compileOutput) { 00103 Main compiler = new Main(compileOutput, "javac"); 00104 return compiler.compile(parseArgs(cmd)); 00105 } 00106 } 00107 00108 00109