001: /**************************************************************************/
002: /* NICE Testsuite */
003: /* A testsuite for the Nice programming language */
004: /* (c) Alex Greif 2002 */
005: /* */
006: /* This program is free software; you can redistribute it and/or modify */
007: /* it under the terms of the GNU General Public License as published by */
008: /* the Free Software Foundation; either version 2 of the License, or */
009: /* (at your option) any later version. */
010: /* */
011: /**************************************************************************/package nice.tools.testsuite;
012:
013: import java.io.*;
014: import java.util.*;
015:
016: /**
017: * Class represents a nice source file.
018: *
019: * @author Alex Greif <a href="mailto:alex.greif@web.de">alex.greif@web.de</a>
020: * @version $Id: NiceSourceFile.java,v 1.21 2005/06/09 07:54:28 bonniot Exp $
021: */
022: public class NiceSourceFile {
023: /**
024: * TODO
025: *
026: */
027: public final static int STATUS_MAIN = 0;
028: /**
029: * TODO
030: *
031: */
032: public final static int STATUS_TOPLEVEL = 1;
033:
034: /**
035: * TODO
036: *
037: */
038: private int _status = STATUS_MAIN;
039:
040: /**
041: * TODO
042: *
043: */
044: public static final String DEFAULT_PACKAGE = "testcase";
045:
046: /**
047: * TODO
048: *
049: */
050: private String _package;
051: /**
052: * TODO
053: *
054: */
055: private StringBuffer _mainMethodContent = new StringBuffer();
056:
057: /**
058: * Number of lines writte for the main section.
059: */
060:
061: private int _topLevelSectionLength = 0;
062:
063: /**
064: * Number of lines in the main method section.
065: */
066:
067: private int _mainSectionLength = 0;
068:
069: /**
070: * TODO
071: *
072: */
073: private StringBuffer _topLevelContent = new StringBuffer();
074: /**
075: * TODO
076: *
077: */
078: private List _imports = new LinkedList();
079:
080: /**
081: * TODO
082: *
083: */
084: //private final String _className = "main";
085: /**
086: * TODO
087: *
088: */
089: //private final String _fileName = _className + ".nice";
090:
091: /**
092: * Sets the status.
093: *
094: * @param status TODO
095: */
096: public void setStatus(int status) {
097: _status = status;
098: }
099:
100: /**
101: * Returns the status.
102: *
103: */
104: public int getStatus() {
105: return _status;
106: }
107:
108: /**
109: * Consumes a read line from the testsuite file.
110: * Sets the status if necessary.
111: *
112: * @param line TODO
113: */
114: public void consumeLine(String line) {
115: switch (_status) {
116: case STATUS_MAIN:
117: addToMainMethod(line);
118: break;
119: case STATUS_TOPLEVEL:
120: addToTopLevel(line);
121: break;
122: }
123: }
124:
125: /**
126: * Adds the global import statement
127: *
128: */
129: public void addImportGlobal() {
130: _imports.add(GlobalSourceFile.PACKAGE_GLOBAL);
131: }
132:
133: /**
134: * Add a line to the main methods body.
135: *
136: * @param line TODO
137: */
138: public void addToMainMethod(String line) {
139: _mainMethodContent.append(line).append('\n');
140: _mainSectionLength++;
141: }
142:
143: /**
144: * Add a line to the main methods body.
145: *
146: * @param line TODO
147: */
148: public void addToTopLevel(String line) {
149: _topLevelContent.append(line).append('\n');
150: _topLevelSectionLength++;
151: }
152:
153: public int getTopLevelSectionLength() {
154: return _topLevelSectionLength;
155: }
156:
157: public int getMainSectionLength() {
158: return _mainSectionLength;
159: }
160:
161: /**
162: * Consumes a keyword statement that starts with the word package.
163: * The dontcompile keyword is handled and package imports too.
164: * Package imports can me separated by : space colon, semicolon and the plus sign
165: *
166: * @param keywordStatement TODO
167: * @exception TestSuiteException TODO
168: */
169: public void consumePackageKeyword(String keywordStatement)
170: throws TestSuiteException {
171: // get rid of the dontcompile keyword
172: int dontCompilePos = keywordStatement
173: .indexOf(TestNice.KEYWORD_DONTCOMPILE);
174: if (dontCompilePos != -1)
175: keywordStatement = keywordStatement.substring(0,
176: dontCompilePos)
177: + keywordStatement.substring(dontCompilePos
178: + TestNice.KEYWORD_DONTCOMPILE.length());
179:
180: keywordStatement = keywordStatement.substring(
181: TestNice.KEYWORD_PACKAGE.length()).trim();
182: // if contains no space then statement has only package name
183: int spacePos = keywordStatement.indexOf(" ");
184: if (spacePos == -1) {
185: setPackage(keywordStatement);
186: return;
187: }
188:
189: setPackage(keywordStatement.substring(0, spacePos));
190:
191: // look for import keyword
192: int importStartPos = keywordStatement.indexOf(
193: TestNice.KEYWORD_IMPORT, spacePos);
194: if (importStartPos == -1)
195: throw new TestSuiteException("unknown keyword statement: "
196: + keywordStatement);
197:
198: keywordStatement = keywordStatement.substring(
199: importStartPos + TestNice.KEYWORD_IMPORT.length())
200: .trim();
201: StringTokenizer tokenizer = new StringTokenizer(
202: keywordStatement, " ,;+");
203: for (; tokenizer.hasMoreTokens();)
204: _imports.add(tokenizer.nextToken());
205: }
206:
207: /**
208: * Sets the package, this file belongs to.
209: *
210: * @param packageName TODO
211: */
212: public void setPackage(String packageName) {
213: _package = packageName;
214: }
215:
216: /**
217: * Returns the package, this file belongs to.
218: *
219: */
220: public String getPackage() {
221: if (_package == null)
222: _package = DEFAULT_PACKAGE + TestNice.getFileCounter();
223:
224: return _package;
225: }
226:
227: /**
228: * Returns the file name.
229: */
230: public String getFileName() {
231: return "main.nice";
232: }
233:
234: /**
235: * Returns the class name.
236: *
237: */
238: /*public String getClassName() {
239: return _className;
240: }
241: */
242:
243: /**
244: * Writes the nice source file to disc if it is not empty.
245: *
246: * @exception TestSuiteException TODO
247: */
248: public void write() throws TestSuiteException {
249: if (isEmpty())
250: return;
251:
252: File packageFolder = new File(TestNice.getTempFolder(),
253: getPackage().replace('.', File.separatorChar));
254: if (!packageFolder.exists() && !packageFolder.mkdirs())
255: throw new TestSuiteException("could not create folder: "
256: + packageFolder);
257:
258: File sourceFile = new File(packageFolder, getFileName());
259: BufferedWriter writer = null;
260: try {
261: StringWriter debugWriter = new StringWriter();
262: writer = new BufferedWriter(debugWriter);
263:
264: write(writer);
265:
266: writer.close();
267: //System.out.println("#####\n" + debugWriter.toString() + "\n###\n");
268: writer = new BufferedWriter(new FileWriter(sourceFile));
269: writer.write(debugWriter.toString());
270: } catch (IOException e) {
271: throw new TestSuiteException(
272: "Exception while writing file: " + sourceFile, e);
273: } finally {
274: if (writer != null)
275: try {
276: writer.close();
277: } catch (IOException e) {
278: throw new TestSuiteException(
279: "Could not close file: " + sourceFile, e);
280: }
281: }
282: }
283:
284: /**
285: * Writes the contenmts to the writer
286: */
287: void write(BufferedWriter writer) throws IOException {
288: writePackage(writer);
289: writeImports(writer);
290: writeTopLevel(writer);
291: writeMainMethod(writer);
292: }
293:
294: /**
295: * Writes the package in the nice source file.
296: *
297: * @param writer TODO
298: * @exception IOException TODO
299: */
300: private void writePackage(BufferedWriter writer) throws IOException {
301: writer.write("package ");
302: writer.write(getPackage());
303: writer.write(";");
304: writer.newLine();
305: writer.newLine();
306: }
307:
308: /**
309: * Writes the import statements in the nice source file.
310: *
311: * @param writer TODO
312: * @exception IOException TODO
313: */
314: private void writeImports(BufferedWriter writer) throws IOException {
315: for (Iterator iter = _imports.iterator(); iter.hasNext();) {
316: writer.write("import ");
317: writer.write((String) iter.next());
318: writer.write(";");
319: writer.newLine();
320: }
321: }
322:
323: /**
324: * Writes the main() method in the nice source file.
325: *
326: * @param writer TODO
327: * @exception IOException TODO
328: */
329: private void writeMainMethod(BufferedWriter writer)
330: throws IOException {
331: if (!hasMainMethod())
332: return;
333:
334: writer.write("// main method");
335: writer.newLine();
336: writer.write("void main(java.lang.String[] _args) {\n");
337: writer.write(_mainMethodContent.toString());
338: writer.write("}\n");
339: }
340:
341: /**
342: * Writes the toplevel source in the nice source file.
343: *
344: * @param writer TODO
345: * @exception IOException TODO
346: */
347: private void writeTopLevel(BufferedWriter writer)
348: throws IOException {
349: writer.write("// Top level code");
350: writer.newLine();
351: writer.write(_topLevelContent.toString());
352: }
353:
354: /**
355: * Returns whether this file has sources for the main() method.
356: *
357: */
358: public boolean hasMainMethod() {
359: return true;
360: }
361:
362: /**
363: * Return whether the source file contains code or not.
364: *
365: */
366: public boolean isEmpty() {
367: return (_mainMethodContent.length() == 0)
368: && (_topLevelContent.length() == 0);
369: }
370:
371: /**
372: Returns the number of import statements
373: */
374: int getCountImports() {
375: return _imports.size();
376: }
377:
378: }
379:
380: // Local Variables:
381: // tab-width: 2
382: // indent-tabs-mode: t
383: // End:
|