001 /*
002 [Adapted from BSD licence]
003 Copyright (c) 2002 Terence Parr
004 All rights reserved.
005
006 Redistribution and use in source and binary forms, with or without
007 modification, are permitted provided that the following conditions
008 are met:
009 1. Redistributions of source code must retain the above copyright
010 notice, this list of conditions and the following disclaimer.
011 2. Redistributions in binary form must reproduce the above copyright
012 notice, this list of conditions and the following disclaimer in the
013 documentation and/or other materials provided with the distribution.
014 3. The name of the author may not be used to endorse or promote products
015 derived from this software without specific prior written permission.
016
017 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027 */
028 package gate.wiki.antlr;
029
030 import java.io.Writer;
031 import java.io.IOException;
032 import java.io.FileReader;
033 import java.util.Vector;
034 import java.util.Date;
035
036 /**
037 * This class represents the NOP translation and can be used to check syntax of
038 * your YAM input. More importantly, it is a useful superclass for translation
039 * targets because when I add features later the targets by default ignore these
040 * enhancements--avoiding the cost of modifying every target. The cost is a loss
041 * of compile-error when you don't implement a feature.
042 *
043 * Some useful support routines here. Using write/writeln is good because it
044 * buffers the last output string. For example, If you need to avoid generating
045 * a paragraph symbol after a list because it makes too much space, you can look
046 * back to see what you just generated.
047 */
048 public class NOPTarget implements YAMTarget{
049 protected Writer out = null;
050 protected String lastCommand = null;
051
052 public String getTargetLanguage(){
053 return "NOP";
054 }
055 protected void write(String s){
056 try{
057 out.write(s);
058 String c = extractLastCommand(s);
059 // System.out.println("extracted "+c);
060 if(c != null){
061 lastCommand = c;
062 }
063 }catch(IOException ioe){
064 System.err.println("Problem writing output");
065 ioe.printStackTrace(System.err);
066 }
067 }
068 protected void writeln(String s){
069 write(s);
070 write("\n");
071 }
072 protected String getLastOutputCommand(){
073 return lastCommand;
074 }
075 /**
076 * Subclasses should implement this; i.e., pull out <...>or
077 *
078 * ID. Groovy because you get a list like this when you from say paragraph():
079 *
080 * last cmd=[@Heading] last cmd=[@LP] last cmd=[@EndList] last cmd=[@Begin]
081 */
082 protected String extractLastCommand(String text){
083 return null;
084 }
085 /** insert head.html if found */
086 public void begin(){
087 }
088 /** insert tail.html if found */
089 public void end(){
090 }
091 public void text(String t){
092 }
093 public void bold(String t){
094 }
095 public void italic(String t){
096 }
097 public void tt(String t){
098 }
099 public void begin_ul(int level){
100 }
101 public void begin_ol(int level){
102 }
103 public void end_ul(int level){
104 }
105 public void end_ol(int level){
106 }
107 public void beginListItem(int level){
108 }
109 public void endListItem(int level){
110 }
111 public void paragraph(){
112 }
113 public void linebreak(){
114 }
115 public void blankline(){
116 }
117 public void verbatim(String rawOutput){
118 }
119 public void code(String c){
120 }
121 public void blockquote(String q){
122 }
123 public void link(String url, String title){
124 }
125 public void anchor(String name){
126 }
127 public void title(String title){
128 }
129 public void beginSection(String title, int level){
130 }
131 public void endSection(int level){
132 }
133 public void beginSectionList(int level){
134 }
135 public void endSectionList(int level){
136 }
137 public void col(){
138 }
139 public void row(){
140 }
141 public void hr(){
142 }
143 public void begin_table(){
144 }
145 public void end_table(){
146 }
147 public String beginEM() {
148 return "";
149 }
150 public String endEM(){
151 return "";
152 }
153 public String beginTT() {
154 return "";
155 }
156 public String endTT(){
157 return "";
158 }
159 public String beginBF() {
160 return "";
161 }
162 public String endBF(){
163 return "";
164 }
165 public void longdash() {
166 }
167 public void contents() {
168 }
169 public void wikilink(String url, String title){
170 link(url, title);
171 }
172
173 /**
174 * Former plugins
175 */
176 public void image(YAMContext context, Vector args) {
177 if ( args.size()<1 ) {
178 System.err.println("Missing image name/args; line "+context.getLine());
179 return ;
180 }
181 }
182
183 public void cite(YAMContext context, Vector args) {
184 if ( args.size() <1 ) {
185 System.err.println("Missing cite keys; line "+context.getLine());
186 return;
187 }
188 }
189
190 public void box(YAMContext context, Vector args) {
191 if ( args.size()<1 ) {
192 System.err.println("Missing YAM input; line "+context.getLine());
193 return;
194 }
195 }
196
197 public void c(YAMContext context, Vector args) {
198 if ( args.size()==0 ) {
199 System.err.println("Missing character spec string; line "+context.getLine());
200 return;
201 }
202 }
203
204 public void date(YAMContext context, Vector args) {
205 write(new Date().toString());
206 }
207
208 public void delete(YAMContext context, Vector args) {
209 write("");
210 }
211
212 public void eps(YAMContext context, Vector args) {
213 if ( args.size()<1 ) {
214 System.err.println("Missing EPS arg(s); line "+context.getLine());
215 return;
216 }
217
218 }// End of eps()
219
220 public void footnote(YAMContext context, Vector args) {
221 if ( args.size()<1 ) {
222 System.err.println("Missing YAM input; line "+context.getLine());
223 return;
224 }
225 }
226
227
228 public void include(YAMContext context, Vector args) {
229 // get a big string buffer of the contents of the file
230 if ( args.size()==0 ) {
231 System.err.println("Missing include file name; line "+context.getLine());
232 return;
233 }
234 String fileName = (String)args.elementAt(0);
235 String inputType = "text";
236 if ( args.size()==2 ) {
237 inputType = (String)args.elementAt(1); // "code" probably
238 }
239 FileReader fr = null;
240 try {
241 fr = new FileReader(fileName);
242 }
243 catch (IOException ioe) {
244 System.err.println("Invalid include file name '"+fileName+
245 "'; line "+context.getLine());
246 return;
247 }
248 StringBuffer sbuf = new StringBuffer(4000);
249
250 try {
251 char[] buffer = new char[1024];
252 int n;
253
254 while( (n = fr.read(buffer)) > 0) {
255 sbuf.append(buffer, 0, n);
256 }
257 fr.close();
258 }
259 catch (IOException ioe2) {
260 System.err.println("Error reading include file name '"+fileName+
261 "'; line "+context.getLine());
262 return;
263 }
264 if ( inputType!=null && inputType.equals("code") ) {
265 write( "<<\n"+sbuf.toString()+">>\n");
266 }
267 write(sbuf.toString());
268
269 }// End of include()
270
271
272 public void notes(YAMContext context, Vector args) {
273 if ( args.size()==0 ) {
274 System.err.println("Missing notes string "+context.getLine());
275 return;
276 }
277
278 }// End of notes()
279
280 public void syndiag(YAMContext context, Vector args) {
281 if ( args.size()==0 ) {
282 System.err.println("Missing grammar spec string; line "+context.getLine());
283 return;
284 }
285 }
286
287 public void tree(YAMContext context, Vector args) {
288 if ( args.size()==0 ) {
289 System.err.println("Missing tree spec string; line "+context.getLine());
290 return;
291 }
292 }
293
294 }//End of NOPTarget
|