001: /*
002: * Janino - An embedded Java[TM] compiler
003: *
004: * Copyright (c) 2006, 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
040: * {@link org.codehaus.janino.Scanner}. This class declares numerous <code>cook*()</code> methods
041: * that use a {@link java.lang.String}, a {@link java.io.File}, an {@link java.io.InputStream} or
042: * a {@link java.io.Reader} as the source of characters for scanning.
043: * <p>
044: * The <code>cook*()</code> methods eventually invoke the abstract {@link #internalCook(Scanner)}
045: * method with a correctly configured {@link org.codehaus.janino.Scanner}.
046: */
047: public abstract class Cookable {
048:
049: /**
050: * To be implemented by the derived classes.
051: */
052: protected abstract void internalCook(Scanner scanner)
053: throws CompileException, Parser.ParseException,
054: Scanner.ScanException, IOException;
055:
056: // The "cook()" method family.
057:
058: public final void cook(Scanner scanner) throws CompileException,
059: Parser.ParseException, Scanner.ScanException, IOException {
060: this .internalCook(scanner);
061: }
062:
063: public final void cook(Reader r) throws CompileException,
064: Parser.ParseException, Scanner.ScanException, IOException {
065: this .cook(null, r);
066: }
067:
068: /**
069: * @param optionalFileName Used when reporting errors and warnings.
070: */
071: public final void cook(String optionalFileName, Reader r)
072: throws CompileException, Parser.ParseException,
073: Scanner.ScanException, IOException {
074: this .cook(new Scanner(optionalFileName, r));
075: }
076:
077: /**
078: * Cook tokens from an {@link InputStream}, encoded in the "platform default encoding".
079: */
080: public final void cook(InputStream is) throws CompileException,
081: Parser.ParseException, Scanner.ScanException, IOException {
082: this .cook(null, is);
083: }
084:
085: /**
086: * Cook tokens from an {@link InputStream}, encoded in the "platform default encoding".
087: *
088: * @param optionalFileName Used when reporting errors and warnings.
089: */
090: public final void cook(String optionalFileName, InputStream is)
091: throws CompileException, Parser.ParseException,
092: Scanner.ScanException, IOException {
093: this .cook(optionalFileName, is, null);
094: }
095:
096: public final void cook(InputStream is, String optionalEncoding)
097: throws CompileException, Parser.ParseException,
098: Scanner.ScanException, IOException {
099: this .cook(new Scanner(null, is, optionalEncoding));
100: }
101:
102: /**
103: * @param optionalFileName Used when reporting errors and warnings.
104: */
105: public final void cook(String optionalFileName, InputStream is,
106: String optionalEncoding) throws CompileException,
107: Parser.ParseException, Scanner.ScanException, IOException {
108: this .cook(new Scanner(optionalFileName, is, optionalEncoding));
109: }
110:
111: /**
112: * Cook tokens from the given {@link File}, encoded in the "platform default encoding".
113: */
114: public final void cookFile(File file) throws CompileException,
115: Parser.ParseException, Scanner.ScanException, IOException {
116: this .cookFile(file, null);
117: }
118:
119: public final void cookFile(File file, String optionalEncoding)
120: throws CompileException, Parser.ParseException,
121: Scanner.ScanException, IOException {
122: InputStream is = new FileInputStream(file);
123: try {
124: this .internalCook(new Scanner(file.getAbsolutePath(), is,
125: optionalEncoding));
126: is.close();
127: is = null;
128: } finally {
129: if (is != null)
130: try {
131: is.close();
132: } catch (IOException ex) {
133: }
134: }
135: }
136:
137: /**
138: * Cook tokens from the named file, encoded in the "platform default encoding".
139: */
140: public final void cookFile(String fileName)
141: throws CompileException, Parser.ParseException,
142: Scanner.ScanException, IOException {
143: this .cookFile(fileName, null);
144: }
145:
146: public final void cookFile(String fileName, String optionalEncoding)
147: throws CompileException, Parser.ParseException,
148: Scanner.ScanException, IOException {
149: this .cookFile(new File(fileName), optionalEncoding);
150: }
151:
152: /**
153: * Cook tokens from a {@link java.lang.String}.
154: */
155: public final void cook(String s) throws CompileException,
156: Parser.ParseException, Scanner.ScanException {
157: try {
158: this .cook(new StringReader(s));
159: } catch (IOException ex) {
160: throw new RuntimeException(
161: "SNO: IOException despite StringReader");
162: }
163: }
164: }
|