01 /*
02 [Adapted from BSD licence]
03 Copyright (c) 2002 Terence Parr
04 All rights reserved.
05
06 Redistribution and use in source and binary forms, with or without
07 modification, are permitted provided that the following conditions
08 are met:
09 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 3. The name of the author may not be used to endorse or promote products
15 derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 package gate.wiki.antlr.plugin;
29
30 import gate.wiki.antlr.*;
31
32 import java.util.Vector;
33 import java.io.FileReader;
34 import java.io.IOException;
35
36 public class include implements YAMPlugin {
37 public String translate(YAMContext context, Vector args) {
38 // get a big string buffer of the contents of the file
39 if ( args.size()==0 ) {
40 System.err.println("Missing include file name; line "+context.getLine());
41 return null;
42 }
43 String fileName = (String)args.elementAt(0);
44 String inputType = "text";
45 if ( args.size()==2 ) {
46 inputType = (String)args.elementAt(1); // "code" probably
47 }
48 FileReader fr = null;
49 try {
50 fr = new FileReader(fileName);
51 }
52 catch (IOException ioe) {
53 System.err.println("Invalid include file name '"+fileName+
54 "'; line "+context.getLine());
55 return null;
56 }
57 StringBuffer sbuf = new StringBuffer(4000);
58
59 try {
60 char[] buffer = new char[1024];
61 int n;
62
63 while( (n = fr.read(buffer)) > 0) {
64 sbuf.append(buffer, 0, n);
65 }
66 fr.close();
67 }
68 catch (IOException ioe2) {
69 System.err.println("Error reading include file name '"+fileName+
70 "'; line "+context.getLine());
71 return null;
72 }
73 if ( inputType!=null && inputType.equals("code") ) {
74 return "<<\n"+sbuf.toString()+">>\n";
75 }
76 return sbuf.toString();
77 }
78 }
|