001: /*
002: * $Id: FormattingCallbacks.java,v 1.6 2002/09/16 08:05:03 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.core.system;
011:
012: import anvil.core.Any;
013: import anvil.core.runtime.AnyFunction;
014: import anvil.Location;
015: import anvil.parser.Attribute;
016: import anvil.parser.Locator;
017: import anvil.parser.Tag;
018: import anvil.script.Context;
019: import anvil.script.Function;
020: import anvil.script.parser.Token;
021: import anvil.util.Conversions;
022:
023: public class FormattingCallbacks {
024:
025: private static final Any getCallback(Context context, Any handler,
026: String name) {
027: if (handler == null) {
028: return null;
029: }
030: Any callable = handler.checkAttribute(context, name);
031: if (!callable.isNull()) {
032: return callable;
033: } else {
034: return null;
035: }
036: }
037:
038: private Any _on_white = null;
039: private Any _on_comment = null;
040: private Any _on_string = null;
041: private Any _on_number = null;
042: private Any _on_pattern = null;
043: private Any _on_symbol = null;
044: private Any _on_delimiter = null;
045: private Any _on_operator = null;
046: private Any _on_op_keyword = null;
047: private Any _on_keyword = null;
048: private Any _on_default = null;
049:
050: private Any _on_characters = null;
051: private Any _on_html_comment = null;
052: private Any _on_processing = null;
053: private Any _on_tag_start = null;
054: private Any _on_attribute = null;
055: private Any _on_tag_end = null;
056: private Any _on_code_tag_start = null;
057: private Any _on_code_attribute = null;
058: private Any _on_code_tag_end = null;
059:
060: private Context _context;
061: private Locator _locator;
062:
063: public FormattingCallbacks(Context context, Any handler) {
064: _context = context;
065: _on_white = getCallback(context, handler, "onWhitespace");
066: _on_comment = getCallback(context, handler, "onComment");
067: _on_string = getCallback(context, handler, "onString");
068: _on_number = getCallback(context, handler, "onNumber");
069: _on_pattern = getCallback(context, handler, "onPattern");
070: _on_symbol = getCallback(context, handler, "onSymbol");
071: _on_delimiter = getCallback(context, handler, "onDelimiter");
072: _on_operator = getCallback(context, handler, "onOperator");
073: _on_op_keyword = getCallback(context, handler,
074: "onOperatorKeyword");
075: _on_keyword = getCallback(context, handler, "onKeyword");
076: _on_default = getCallback(context, handler, "onDefault");
077:
078: _on_characters = getCallback(context, handler, "onCharacters");
079: _on_html_comment = getCallback(context, handler,
080: "onHtmlComment");
081: _on_processing = getCallback(context, handler,
082: "onProcessingInstruction");
083: _on_tag_start = getCallback(context, handler, "onTagStart");
084: _on_tag_end = getCallback(context, handler, "onTagEnd");
085: _on_attribute = getCallback(context, handler, "onAttribute");
086: _on_code_tag_start = getCallback(context, handler,
087: "onCodeTagStart");
088: _on_code_tag_end = getCallback(context, handler, "onCodeTagEnd");
089: _on_code_attribute = getCallback(context, handler,
090: "onCodeAttribute");
091: }
092:
093: public void setLocator(Locator locator) {
094: _locator = locator;
095: }
096:
097: public void call(Any callable, Token t) {
098: callable.execute(_context, Any.create(t.beginLine), Any
099: .create(t.beginColumn), Any.create(t.image));
100: }
101:
102: public void call(Any callable, String s) {
103: Location loc = _locator.getLocation();
104: callable.execute(_context, Any.create(loc.getLine()), Any
105: .create(loc.getColumn()), Any.create(s));
106: }
107:
108: public void onWhitespace(Token t) {
109: if (_on_white != null) {
110: call(_on_white, t);
111: } else {
112: _context.print(t.image);
113: }
114: }
115:
116: public void onComment(Token t) {
117: if (_on_comment != null) {
118: call(_on_comment, t);
119: } else {
120: _context.print("<font color=\"teal\"><i>");
121: _context.print(anvil.util.Conversions
122: .encodeEntities(t.image));
123: _context.print("</i></font>");
124: }
125: }
126:
127: public void onString(Token t) {
128: if (_on_string != null) {
129: call(_on_string, t);
130: } else {
131: _context.print("<font color=\"#0066bb\">");
132: _context.print(anvil.util.Conversions
133: .encodeEntities(t.image));
134: _context.print("</font>");
135: }
136: }
137:
138: public void onNumber(Token t) {
139: if (_on_number != null) {
140: call(_on_number, t);
141: } else {
142: _context.print("<font color=\"red\">");
143: _context.print(t.image);
144: _context.print("</font>");
145: }
146: }
147:
148: public void onPattern(Token t) {
149: if (_on_pattern != null) {
150: call(_on_pattern, t);
151: } else {
152: _context.print("<font color=\"#500050\">");
153: _context.print(anvil.util.Conversions
154: .encodeEntities(t.image));
155: _context.print("</font>");
156: }
157: }
158:
159: public void onSymbol(Token t) {
160: if (_on_symbol != null) {
161: call(_on_symbol, t);
162: } else {
163: _context.print("<font color=\"#335577\">");
164: _context.print(t.image);
165: _context.print("</font>");
166: }
167: }
168:
169: public void onOperator(Token t) {
170: if (_on_operator != null) {
171: call(_on_operator, t);
172: } else {
173: _context.print("<b>");
174: _context.print(t.image);
175: _context.print("</b>");
176: }
177: }
178:
179: public void onDelimiter(Token t) {
180: if (_on_delimiter != null) {
181: call(_on_delimiter, t);
182: } else {
183: _context.print("<font color=\"brown\"><b>");
184: _context.print(t.image);
185: _context.print("</b></font>");
186: }
187: }
188:
189: public void onOperatorKeyword(Token t) {
190: if (_on_op_keyword != null) {
191: call(_on_op_keyword, t);
192: } else {
193: _context.print("<font color=\"#4400cc\"><b>");
194: _context.print(t.image);
195: _context.print("</b></font>");
196: }
197: }
198:
199: public void onKeyword(Token t) {
200: if (_on_keyword != null) {
201: call(_on_keyword, t);
202: } else {
203: _context.print("<b>");
204: _context.print(t.image);
205: _context.print("</b>");
206: }
207: }
208:
209: public void onDefault(Token t) {
210: if (_on_default != null) {
211: call(_on_default, t);
212: } else {
213: _context.print("<b>");
214: _context.print(anvil.util.Conversions
215: .encodeEntities(t.image));
216: _context.print("</b>");
217: }
218: }
219:
220: public void onCharacters(String characters) {
221: if (_on_characters != null) {
222: call(_on_characters, characters);
223: } else {
224: _context.print(anvil.util.Conversions
225: .encodeEntities(characters));
226: }
227: }
228:
229: public void onHtmlComment(String comment) {
230: if (_on_html_comment != null) {
231: call(_on_html_comment, comment);
232: } else {
233: _context.print("<font color=\"teal\"><i><!");
234: _context.print(anvil.util.Conversions
235: .encodeEntities(comment));
236: _context.print("></i></font>");
237: }
238: }
239:
240: public void onProcessingInstruction(String data) {
241: if (_on_processing != null) {
242: call(_on_processing, data);
243: } else {
244: _context.print("<font color=\"#600000\"><?");
245: _context.print(anvil.util.Conversions.encodeEntities(data));
246: _context.print("></font>");
247: }
248: }
249:
250: public void onTag(Tag tag) {
251: Location loc = _locator.getLocation();
252: Any line = Any.create(loc.getLine());
253: Any column = Any.create(loc.getColumn());
254:
255: if (_on_tag_start != null) {
256: _on_tag_start.execute(_context, line, column, Any
257: .create(tag.getPrefixedName()));
258: } else {
259: _context.print("<font color=\"#335577\"><");
260: _context.print(tag.getPrefixedName());
261: }
262:
263: if (_on_attribute != null) {
264: int n = tag.getLength();
265: for (int i = 0; i < n; i++) {
266: Attribute attr = tag.getAttribute(i);
267: _on_attribute.execute(_context, line, column, Any
268: .create(attr.getName()), Any.create(attr
269: .getValue()));
270: }
271:
272: } else {
273: int n = tag.getLength();
274: for (int i = 0; i < n; i++) {
275: _context.print(" ");
276: Attribute attr = tag.getAttribute(i);
277: _context.print(attr.getName());
278: String s = attr.getValue();
279: if (s != null) {
280: _context.print("="");
281: _context.print(Conversions.encodeEntities(s));
282: _context.print(""");
283: }
284: }
285: }
286:
287: if (_on_tag_end != null) {
288: _on_tag_end.execute(_context, line, column, Any.create(tag
289: .hasEndSlash()));
290: } else {
291: if (tag.hasEndSlash()) {
292: _context.print("/>");
293: } else {
294: _context.print(">");
295: }
296: _context.print("</font>");
297: }
298: }
299:
300: public void onCodeTag(Tag tag) {
301: Location loc = _locator.getLocation();
302: Any line = Any.create(loc.getLine());
303: Any column = Any.create(loc.getColumn());
304:
305: if (_on_code_tag_start != null) {
306: _on_code_tag_start.execute(_context, line, column, Any
307: .create(tag.getPrefixedName()));
308: } else {
309: _context
310: .print("<strong><font color=\"#335577\"><</font>");
311: _context.print("<font color=\"#600060\">");
312: _context.print(tag.getPrefixedName());
313: _context.print("</font><font color=\"#335577\">");
314: }
315:
316: if (_on_code_attribute != null) {
317: int n = tag.getLength();
318: for (int i = 0; i < n; i++) {
319: Attribute attr = tag.getAttribute(i);
320: _on_code_attribute.execute(_context, line, column, Any
321: .create(attr.getName()), Any.create(attr
322: .getValue()));
323: }
324:
325: } else {
326: int n = tag.getLength();
327: for (int i = 0; i < n; i++) {
328: _context.print(" ");
329: Attribute attr = tag.getAttribute(i);
330: _context.print(attr.getName());
331: String s = attr.getValue();
332: if (s != null) {
333: _context.print("="");
334: _context.print(Conversions.encodeEntities(s));
335: _context.print(""");
336: }
337: }
338: }
339:
340: if (_on_code_tag_end != null) {
341: _on_code_tag_end.execute(_context, line, column, Any
342: .create(tag.hasEndSlash()));
343: } else {
344: if (tag.hasEndSlash()) {
345: _context.print("/>");
346: } else {
347: _context.print(">");
348: }
349: _context.print("</font></strong>");
350: }
351: }
352:
353: }
|