001: /*
002: * Janino - An embedded Java[TM] compiler
003: *
004: * Copyright (c) 2001-2007, Arno Unkrig
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above
014: * copyright notice, this list of conditions and the following
015: * disclaimer in the documentation and/or other materials
016: * provided with the distribution.
017: * 3. The name of the author may not be used to endorse or promote
018: * products derived from this software without specific prior
019: * written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
022: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
023: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
024: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
025: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
026: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
027: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
028: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
029: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
030: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
031: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
032: */
033:
034: package org.codehaus.janino;
035:
036: import java.io.*;
037:
038: /**
039: * "Cooking" means scanning a sequence of Java<sup>TM</sup> tokens with a {@link
040: * org.codehaus.janino.Scanner} and turning them into some artifact. For example, if you cook a
041: * {@link ClassBodyEvaluator}, then the tokens are interpreted as a class body and compiled into
042: * a {@link Class} which is accessible through {@link ClassBodyEvaluator#getClazz()}.
043: * <p>
044: * This class declares numerous <code>cook*()</code> methods
045: * that use a {@link java.lang.String}, a {@link java.io.File}, an {@link java.io.InputStream} or
046: * a {@link java.io.Reader} as the source of characters for scanning.
047: * <p>
048: * The <code>cook*()</code> methods eventually invoke the abstract {@link #cook(Scanner)}
049: * method with a correctly configured {@link org.codehaus.janino.Scanner}.
050: */
051: public abstract class Cookable {
052:
053: /**
054: * To be implemented by the derived classes.
055: */
056: public abstract void cook(Scanner scanner) throws CompileException,
057: Parser.ParseException, Scanner.ScanException, IOException;
058:
059: // The "cook()" method family.
060:
061: public final void cook(Reader r) throws CompileException,
062: Parser.ParseException, Scanner.ScanException, IOException {
063: this .cook(null, r);
064: }
065:
066: /**
067: * @param optionalFileName Used when reporting errors and warnings.
068: */
069: public final void cook(String optionalFileName, Reader r)
070: throws CompileException, Parser.ParseException,
071: Scanner.ScanException, IOException {
072: this .cook(new Scanner(optionalFileName, r));
073: }
074:
075: /**
076: * Cook tokens from an {@link InputStream}, encoded in the "platform default encoding".
077: */
078: public final void cook(InputStream is) throws CompileException,
079: Parser.ParseException, Scanner.ScanException, IOException {
080: this .cook(null, is);
081: }
082:
083: /**
084: * Cook tokens from an {@link InputStream}, encoded in the "platform default encoding".
085: *
086: * @param optionalFileName Used when reporting errors and warnings.
087: */
088: public final void cook(String optionalFileName, InputStream is)
089: throws CompileException, Parser.ParseException,
090: Scanner.ScanException, IOException {
091: this .cook(optionalFileName, is, null);
092: }
093:
094: public final void cook(InputStream is, String optionalEncoding)
095: throws CompileException, Parser.ParseException,
096: Scanner.ScanException, IOException {
097: this .cook(new Scanner(null, is, optionalEncoding));
098: }
099:
100: /**
101: * @param optionalFileName Used when reporting errors and warnings.
102: */
103: public final void cook(String optionalFileName, InputStream is,
104: String optionalEncoding) throws CompileException,
105: Parser.ParseException, Scanner.ScanException, IOException {
106: this .cook(new Scanner(optionalFileName, is, optionalEncoding));
107: }
108:
109: /**
110: * Cook tokens from a {@link java.lang.String}.
111: * <p>
112: * <b>Notice:</b> If you pass a string literal, be sure to escape all Java<sup>TM</sup> special
113: * characters, especially backslashes.
114: */
115: public final void cook(String s) throws CompileException,
116: Parser.ParseException, Scanner.ScanException {
117: try {
118: this .cook(new StringReader(s));
119: } catch (IOException ex) {
120: throw new RuntimeException(
121: "SNO: IOException despite StringReader");
122: }
123: }
124:
125: /**
126: * Cook tokens from the given {@link File}, encoded in the "platform default encoding".
127: */
128: public final void cookFile(File file) throws CompileException,
129: Parser.ParseException, Scanner.ScanException, IOException {
130: this .cookFile(file, null);
131: }
132:
133: public final void cookFile(File file, String optionalEncoding)
134: throws CompileException, Parser.ParseException,
135: Scanner.ScanException, IOException {
136: InputStream is = new FileInputStream(file);
137: try {
138: this .cook(new Scanner(file.getAbsolutePath(), is,
139: optionalEncoding));
140: is.close();
141: is = null;
142: } finally {
143: if (is != null)
144: try {
145: is.close();
146: } catch (IOException ex) {
147: }
148: }
149: }
150:
151: /**
152: * Cook tokens from the named file, encoded in the "platform default encoding".
153: */
154: public final void cookFile(String fileName)
155: throws CompileException, Parser.ParseException,
156: Scanner.ScanException, IOException {
157: this .cookFile(fileName, null);
158: }
159:
160: public final void cookFile(String fileName, String optionalEncoding)
161: throws CompileException, Parser.ParseException,
162: Scanner.ScanException, IOException {
163: this .cookFile(new File(fileName), optionalEncoding);
164: }
165: }
|