001 package gate.wiki.antlr;
002
003 import antlr.TokenStreamException;
004 import antlr.Token;
005 import java.util.Vector;
006 import java.io.StringReader;
007 import java.io.Reader;
008
009 public class DefaultYAMEngine extends YAMLexer{
010 public DefaultYAMEngine(Reader in, YAMContext context){
011 super(in, context);
012 }
013 public DefaultYAMEngine(Reader in){
014 super(in);
015 }
016 /** Begin on-the-fly syntax-directed translation */
017 public void translate(YAMTarget target) throws TokenStreamException{
018 if(target != null){
019 this.target = target;
020 }
021 boolean done = false;
022 Token t = null;
023 int saveSectionLevel = context.sectionLevel;
024 // added to keep plugins from closing all lists (TP)
025 int saveListLevel = context.listTypeStack.size();
026 if(!context.isNestedTranslator()){
027 target.begin();
028 }
029 while(!done){
030 t = nextToken();
031 if(t.getType() == Token.EOF_TYPE){
032 done = true;
033 }else{
034 // System.out.println("token=="+t);
035 }
036 }
037 captureText();
038 //closeAllLists();
039 // changed to keep plugins from closing all lists (TP)
040 closeAllLists(saveListLevel);
041 closeSectionsToLevel(saveSectionLevel);
042 if(!context.isNestedTranslator()){
043 target.end();
044 }
045 }
046 protected void closeAllLists(){
047 closeAllLists(0);
048 }
049 protected void closeAllLists(int level){
050 while(context.listTypeStack.size() > level){
051 target.endListItem(context.listTypeStack.size());
052 closeList();
053 }
054 }
055 protected void closeList(){
056 String e = (String)context.listTypeStack.pop();
057 if(e.equalsIgnoreCase("UL")){
058 int level = context.listTypeStack.size() + 1;
059 target.end_ul(level);
060 }else if(e.equalsIgnoreCase("OL")){
061 int level = context.listTypeStack.size() + 1;
062 target.end_ol(level);
063 }
064 }
065 protected void captureText(){
066 if(context.textBetweenMarkup.length() > 0){
067 target.text(context.textBetweenMarkup.toString());
068 context.textBetweenMarkup.setLength(0);
069 }
070 }
071 protected void ul(int level){
072 if(context.listTypeStack.size() < level){
073 // starting new list (possibly nested)
074 context.listTypeStack.push("UL"); // altered to keep from closing all lists (TP)
075 target.begin_ul(level);
076 }else if(level < context.listTypeStack.size()){
077 closeAllLists(level);
078 target.endListItem(level);
079 }else if(level == context.listTypeStack.size()){
080 target.endListItem(level);
081 }
082 target.beginListItem(level);
083 }
084 protected void ol(int level){
085 if(context.listTypeStack.size() < level){
086 // starting new list (possibly nested)
087 context.listTypeStack.push("OL");
088 target.begin_ol(level);
089 }else if(level < context.listTypeStack.size()){
090 closeAllLists(level);
091 target.endListItem(level);
092 }else if(level == context.listTypeStack.size()){
093 target.endListItem(level);
094 }
095 target.beginListItem(level);
096 }
097 /** Close levels sectionLevel..level */
098 protected void closeSectionsToLevel(int level){
099 // System.out.println("closeSectionsToLevel("+level+")");
100 // close all sections
101 for(int i = context.sectionLevel; i > level; i--){
102 // System.out.println("i=="+i);
103 target.endSection(i);
104 target.endSectionList(i);
105 }
106 }
107 protected void section(String text, int level){
108 // System.out.println("section("+text+", "+level+")
109 // context.Level="+context.sectionLevel);
110 closeAllLists();
111 if(level > context.sectionLevel){
112 target.beginSectionList(level);
113 }else if(level < context.sectionLevel){
114 closeSectionsToLevel(level);
115 target.endSection(level);
116 }else if(level == context.sectionLevel){
117 target.endSection(level);
118 }
119 context.sectionLevel = level; // enter new level
120 target.beginSection(text, level);
121 }
122 protected void title(String title){
123 target.title(title);
124 }
125 protected void define(String id, String value){
126 // System.out.println(id+"="+value);
127 context.defineVariable(id, value);
128 }
129 protected void plugin(String id, Vector args){
130 // System.out.println("Exec plug in "+id+"("+args+")");
131
132 if ("image".equals(id)){
133 target.image(context, args);
134 return;
135 }
136
137 if ("box".equals(id)){
138 target.box(context, args);
139 return;
140 }
141
142 if ("c".equals(id)){
143 target.c(context, args);
144 return;
145 }
146
147 if ("cite".equals(id)){
148 target.cite(context, args);
149 return;
150 }
151
152 if ("date".equals(id)){
153 target.date(context, args);
154 return;
155 }
156
157 if ("delete".equals(id)){
158 target.delete(context, args);
159 return;
160 }
161
162 if ("eps".equals(id)){
163 target.eps(context, args);
164 return;
165 }
166
167 if ("footnote".equals(id)){
168 target.footnote(context, args);
169 return;
170 }
171
172 if ("include".equals(id)){
173 target.include(context, args);
174 return;
175 }
176
177 if ("notes".equals(id)){
178 target.notes(context, args);
179 return;
180 }
181
182 if ("syndiag".equals(id)){
183 target.syndiag(context, args);
184 return;
185 }
186
187 if ("tree".equals(id)){
188 target.tree(context, args);
189 return;
190 }
191 /*
192 Class c = context.getPlugin(id);
193 if(c != null){
194 try{
195 // get new instance of the plugin
196 YAMPlugin p = (YAMPlugin)c.newInstance();
197 // Get output from plugin
198 String output = p.translate(context, args);
199 if(output != null){
200 // interpret as YAM
201 translate(context, target, id, output);
202 }
203 }catch(Exception e){
204 System.err.println("Problems with plugin " + id + ": ");
205 e.printStackTrace(System.err);
206 }
207 }
208 */
209 }// End of plugin()
210
211
212 /**
213 * Given a target and current YAM context to exec within, translate the given
214 * text to the appropriate output language.
215 */
216 public static void translate(YAMContext context, YAMTarget target,
217 String inputName, String text) throws TokenStreamException // exceptions
218 // go back
219 // to
220 // plugin()
221 {
222 // System.out.println("Translating output of "+inputName+": ["+text+"]");
223 // Create lexer to parse/translate input
224 StringReader sr = new StringReader(text);
225 DefaultYAMEngine sublexer = new DefaultYAMEngine(sr, context);
226 // make sure errors are reported in right file
227 context.pushInputName(inputName);
228 sublexer.setFilename(inputName);
229 // do translation using existing translator for now
230 sublexer.translate(target);
231 // back to old input name
232 context.popInputName();
233 sublexer.setFilename(context.getInputName());
234 // System.out.println("input back to "+context.getInputName());
235 }
236 protected void variable(String id){
237 Object value = context.getVariable(id);
238 context.textBetweenMarkup.append(value);
239 // System.out.println("variable "+id+" (=="+value+")");
240 }
241 /** Remove the *x* and {x} etc.. from front/back */
242 private String trim(String t){
243 if(t != null){ return t.substring(1, t.length() - 1); }
244 return null;
245 }
246 /** Remove the <<x>>from front/back */
247 private String trim2(String t){
248 if(t != null){ return t.substring(2, t.length() - 2); }
249 return null;
250 }
251 /*
252 * public void uponEOF() throws TokenStreamException, CharStreamException {
253 * System.out.println("uponEOF"); }
254 */
255 }
|