001: package com.quadcap.http.servlets.jsp;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.OutputStream;
042: import java.util.ArrayList;
043:
044: import sun.tools.javac.Main;
045:
046: import com.quadcap.util.Debug;
047:
048: /**
049: * This compiler class uses javac to to the dirty work, in the sad
050: * situation where Jikes can't be found.
051: *
052: * @author Stan Bailes
053: */
054: public class Javac extends JavaCompiler {
055: public Javac() {
056: }
057:
058: final String chopLastQ(String s) {
059: if (s.length() > 0 && s.charAt(s.length() - 1) == '"') {
060: s = s.substring(0, s.length() - 1);
061: }
062: return s;
063: }
064:
065: private final String[] parseArgs(String str) {
066: ArrayList r = new ArrayList();
067: int state = 0; // skipws;
068: StringBuffer sb = new StringBuffer();
069: for (int i = 0; i < str.length(); i++) {
070: final char c = str.charAt(i);
071: switch (state) {
072: case 0:
073: if (!Character.isSpace(c)) {
074: if (c != '"')
075: sb.append(c);
076: state = 1;
077: }
078: break;
079: case 1:
080: if (Character.isSpace(c)) {
081: r.add(chopLastQ(sb.toString()));
082: sb.setLength(0);
083: state = 1;
084: break;
085: } else {
086: sb.append(c);
087: }
088: break;
089: }
090: }
091: if (sb.length() > 0) {
092: r.add(chopLastQ(sb.toString()));
093: }
094: if (r.size() == 0)
095: return null;
096: int i = 0;
097: if (r.get(0).toString().equalsIgnoreCase("javac"))
098: i++;
099: String[] ret = new String[r.size() - i];
100: for (int ri = 0; ri < ret.length; ri++) {
101: ret[ri] = r.get(i++).toString();
102: }
103: return ret;
104: }
105:
106: public boolean doCompile(String cmd, OutputStream compileOutput) {
107: Main compiler = new Main(compileOutput, "javac");
108: return compiler.compile(parseArgs(cmd));
109: }
110: }
|