001 /*
002 * [Adapted from BSD licence] Copyright (c) 2002-2004 Terence Parr All rights
003 * reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without
006 * modification, are permitted provided that the following conditions are met:
007 * 1. Redistributions of source code must retain the above copyright notice,
008 * this list of conditions and the following disclaimer. 2. Redistributions in
009 * binary form must reproduce the above copyright notice, this list of
010 * conditions and the following disclaimer in the documentation and/or other
011 * materials provided with the distribution. 3. The name of the author may not
012 * be used to endorse or promote products derived from this software without
013 * specific prior written permission.
014 *
015 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
016 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
017 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
018 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
019 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
020 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
021 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
022 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
023 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
024 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
025 */
026 package gate.wiki.antlr;
027
028 import antlr.*;
029 import java.io.*;
030 import java.util.*;
031
032 /**
033 * This class is the top level class for the YAM language. It is used to
034 * translate from YAM to html, latex, and other languages.
035 *
036 * @author tamara
037 */
038 public class Tool{
039 public static String version = "3 (language) / 0.1 (tool)";
040 //init variables
041 /**
042 * The string which can be used to check for the LaTeX target language.
043 */
044 public static String LATEX = "latex";
045 /**
046 * The string which can be used to check for the HTML target language.
047 */
048 public static String HTML = "html";
049
050 /**
051 * The string which can be used to check for the XHTML target language.
052 */
053 public static String XHTML = "xhtml";
054
055 /**
056 * The string which can be used to check for the WikiHTML target language.
057 */
058 public static String WIKI = "wiki";
059 private static HashMap overRide = new HashMap();
060
061
062 // end init variables
063
064 /**
065 * main procedure processes arguments:
066 * <ul>
067 * <li> -i <tt>inputFile</tt> </li>
068 * <li> -o <tt>outputFile</tt> </li>
069 * <li> -l <tt>targetLanguage</tt> where <tt>targetLanguage</tt> can be html or latex</li>
070 * <li> -D <tt>x=y</tt> predefine YAM variable <tt>x</tt> to be <tt>y</tt></li>
071 * </ul>
072 * By default the target language is html, the input is from stdin and output to stdout.
073 * @throws Exception
074 */
075 public static void main(String[] args) throws Exception{
076 boolean error = false;
077 String lang = HTML;
078 String inputFile = null;
079 String outputFile = null;
080 Reader in = null;
081 Writer out= null;
082 if(args != null){
083 int i = 0;
084 while(i < args.length && !error){
085 if(args[i].equals("-i")){
086 i++;
087 if(i >= args.length || args[i].startsWith("-")){
088 System.err.println("Input file not specified");
089 error = true;
090 break;
091 }else if(args[i].endsWith(".yam")){
092 inputFile = args[i];
093 in = new FileReader(inputFile);
094 }else{
095 System.err.println("Input file is not YAM");
096 error = true;
097 break;
098 }
099 }else if(args[i].equals("-o")){
100 i++;
101 if(i >= args.length || args[i].startsWith("-")){
102 System.err.println("Output file not specified");
103 error = true;
104 break;
105 }else{
106 outputFile = args[i];
107 out = new FileWriter(outputFile);
108 }
109 }else if(args[i].equals("-D")){
110 i++;
111 if(i >= args.length){
112 System.err.println("missing assignment for -D");
113 error = true;
114 break;
115 }
116 String a = args[i];
117 int eq = a.indexOf('=');
118 if(eq < 0){
119 System.err.println("missing '=' in assignment for -D");
120 error = true;
121 break;
122 }
123 String id = a.substring(0, eq);
124 String value = a.substring(eq + 1, a.length());
125 overRide.put(id, value);
126 System.out.println("override " + id + "=" + value);
127 }
128 /*
129 * else if ( args[i].equals("-lout") ) { if ( (i+1) <args.length &&
130 * args[(i+1)].charAt(0)!='-' ) { i++; target = new LoutTarget(args[i]); }
131 * else { target = new LoutTarget(); } }
132 */
133 else if(args[i].equals("-l")){
134 i++;
135 if(i >= args.length || args[i].startsWith("-")){
136 System.err.println("Output language not specified");
137 error = true;
138 break;
139 }else{
140 lang = args[i];
141 }
142 }
143 /*
144 * TODO: allow random Java class as target else if (
145 * args[i].equals("-target") ) { i++; if (i >= args.length) {
146 * System.err.println("missing assignment for -target"); return; }
147 * String a = args[i]; }
148 */
149 else{
150 System.err.println("invalid arg: " + args[i]);
151 // usage();
152 error = true;
153 }
154 i++;
155 }
156 }
157
158 // init engine
159 message();
160 if(in == null) {
161 in = new InputStreamReader(System.in);
162 }
163 if(out == null) {
164 out = new OutputStreamWriter(System.out);
165 }
166 if(!error) {
167 run(in, out, lang);
168 }
169 else {
170 usage();
171 }
172 }
173
174 /** Initializes and runs the YAM engine. It reads YAM source from
175 * <tt>in</tt> and outputs the version translated into <tt>lang</tt> to <tt>out</tt>.
176 */
177 public static void run(Reader in, Writer out, String lang) {
178 DefaultYAMEngine engine;
179 YAMTarget target;
180
181 // configure target language and output file
182
183 if (lang.compareToIgnoreCase(LATEX) == 0) {
184 target = new LaTeXTarget(out);
185 }
186 else if(lang.compareToIgnoreCase(WIKI) == 0) {
187 target = new WikiHTMLTarget(out);
188 }
189 else if(lang.compareToIgnoreCase(XHTML) == 0) {
190 target = new XHTMLTarget(out);
191 }
192 else { // default
193 target = new HTMLTarget(out);
194 }
195
196 // configure input file
197
198 engine = new DefaultYAMEngine(in);
199 engine.getContext().pushInputName("input");
200 engine.setFilename("input");
201
202
203
204 // -D option
205 if(overRide.size() > 0){
206 // get the names of all the override variables
207 String[] ids = (String[])(overRide.keySet().toArray());
208 for(int i = 0; i < ids.length; ++i) {
209 engine.getContext().defineVariable(ids[i], overRide.get(ids[i]));
210 }
211 }
212
213
214 try{
215 engine.translate(target);
216 }catch(TokenStreamException atse){
217 System.err.println("YAM lexer error: " + atse);
218 }
219 }
220 /** Prints version message. */
221 public static void message(){
222 System.err.println("YAM Version " + version + " (c) 2005 gate.ac.uk");
223 }
224 /** Prints the help message. */
225 public static void usage(){
226 System.err
227 .println("usage: java gate.wiki.antlr.Tool [args]");
228 //System.err.println(" -lout [slides] generate lout Doc output.");
229 System.err.println(" -i <infile.tml> input file name (default stdin)");
230 System.err.println(" -o <outfile> output file name (default stdout)");
231 System.err.println(" -l html generate html output (default).");
232 System.err.println(" -D x=y predefine YAM variable x to be y.");
233
234 }
235 }
|