/*
* $Id: FormattingCallbacks.java,v 1.6 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core.system;
import anvil.core.Any;
import anvil.core.runtime.AnyFunction;
import anvil.Location;
import anvil.parser.Attribute;
import anvil.parser.Locator;
import anvil.parser.Tag;
import anvil.script.Context;
import anvil.script.Function;
import anvil.script.parser.Token;
import anvil.util.Conversions;
public class FormattingCallbacks {
private static final Any getCallback(Context context, Any handler, String name)
{
if (handler == null) {
return null;
}
Any callable = handler.checkAttribute(context, name);
if (!callable.isNull()) {
return callable;
} else {
return null;
}
}
private Any _on_white = null;
private Any _on_comment = null;
private Any _on_string = null;
private Any _on_number = null;
private Any _on_pattern = null;
private Any _on_symbol = null;
private Any _on_delimiter = null;
private Any _on_operator = null;
private Any _on_op_keyword = null;
private Any _on_keyword = null;
private Any _on_default = null;
private Any _on_characters = null;
private Any _on_html_comment = null;
private Any _on_processing = null;
private Any _on_tag_start = null;
private Any _on_attribute = null;
private Any _on_tag_end = null;
private Any _on_code_tag_start = null;
private Any _on_code_attribute = null;
private Any _on_code_tag_end = null;
private Context _context;
private Locator _locator;
public FormattingCallbacks(Context context, Any handler)
{
_context = context;
_on_white = getCallback(context, handler, "onWhitespace");
_on_comment = getCallback(context, handler, "onComment");
_on_string = getCallback(context, handler, "onString");
_on_number = getCallback(context, handler, "onNumber");
_on_pattern = getCallback(context, handler, "onPattern");
_on_symbol = getCallback(context, handler, "onSymbol");
_on_delimiter = getCallback(context, handler, "onDelimiter");
_on_operator = getCallback(context, handler, "onOperator");
_on_op_keyword = getCallback(context, handler, "onOperatorKeyword");
_on_keyword = getCallback(context, handler, "onKeyword");
_on_default = getCallback(context, handler, "onDefault");
_on_characters = getCallback(context, handler, "onCharacters");
_on_html_comment = getCallback(context, handler, "onHtmlComment");
_on_processing = getCallback(context, handler, "onProcessingInstruction");
_on_tag_start = getCallback(context, handler, "onTagStart");
_on_tag_end = getCallback(context, handler, "onTagEnd");
_on_attribute = getCallback(context, handler, "onAttribute");
_on_code_tag_start = getCallback(context, handler, "onCodeTagStart");
_on_code_tag_end = getCallback(context, handler, "onCodeTagEnd");
_on_code_attribute = getCallback(context, handler, "onCodeAttribute");
}
public void setLocator(Locator locator)
{
_locator = locator;
}
public void call(Any callable, Token t)
{
callable.execute(_context,
Any.create(t.beginLine),
Any.create(t.beginColumn),
Any.create(t.image));
}
public void call(Any callable, String s)
{
Location loc = _locator.getLocation();
callable.execute(_context,
Any.create(loc.getLine()),
Any.create(loc.getColumn()),
Any.create(s));
}
public void onWhitespace(Token t)
{
if (_on_white != null) {
call(_on_white, t);
} else {
_context.print(t.image);
}
}
public void onComment(Token t)
{
if (_on_comment != null) {
call(_on_comment, t);
} else {
_context.print("<font color=\"teal\"><i>");
_context.print(anvil.util.Conversions.encodeEntities(t.image));
_context.print("</i></font>");
}
}
public void onString(Token t)
{
if (_on_string != null) {
call(_on_string, t);
} else {
_context.print("<font color=\"#0066bb\">");
_context.print(anvil.util.Conversions.encodeEntities(t.image));
_context.print("</font>");
}
}
public void onNumber(Token t)
{
if (_on_number != null) {
call(_on_number, t);
} else {
_context.print("<font color=\"red\">");
_context.print(t.image);
_context.print("</font>");
}
}
public void onPattern(Token t)
{
if (_on_pattern != null) {
call(_on_pattern, t);
} else {
_context.print("<font color=\"#500050\">");
_context.print(anvil.util.Conversions.encodeEntities(t.image));
_context.print("</font>");
}
}
public void onSymbol(Token t)
{
if (_on_symbol != null) {
call(_on_symbol, t);
} else {
_context.print("<font color=\"#335577\">");
_context.print(t.image);
_context.print("</font>");
}
}
public void onOperator(Token t)
{
if (_on_operator != null) {
call(_on_operator, t);
} else {
_context.print("<b>");
_context.print(t.image);
_context.print("</b>");
}
}
public void onDelimiter(Token t)
{
if (_on_delimiter != null) {
call(_on_delimiter, t);
} else {
_context.print("<font color=\"brown\"><b>");
_context.print(t.image);
_context.print("</b></font>");
}
}
public void onOperatorKeyword(Token t)
{
if (_on_op_keyword != null) {
call(_on_op_keyword, t);
} else {
_context.print("<font color=\"#4400cc\"><b>");
_context.print(t.image);
_context.print("</b></font>");
}
}
public void onKeyword(Token t)
{
if (_on_keyword != null) {
call(_on_keyword, t);
} else {
_context.print("<b>");
_context.print(t.image);
_context.print("</b>");
}
}
public void onDefault(Token t)
{
if (_on_default != null) {
call(_on_default, t);
} else {
_context.print("<b>");
_context.print(anvil.util.Conversions.encodeEntities(t.image));
_context.print("</b>");
}
}
public void onCharacters(String characters)
{
if (_on_characters != null) {
call(_on_characters, characters);
} else {
_context.print(anvil.util.Conversions.encodeEntities(characters));
}
}
public void onHtmlComment(String comment)
{
if (_on_html_comment != null) {
call(_on_html_comment, comment);
} else {
_context.print("<font color=\"teal\"><i><!");
_context.print(anvil.util.Conversions.encodeEntities(comment));
_context.print("></i></font>");
}
}
public void onProcessingInstruction(String data)
{
if (_on_processing != null) {
call(_on_processing, data);
} else {
_context.print("<font color=\"#600000\"><?");
_context.print(anvil.util.Conversions.encodeEntities(data));
_context.print("></font>");
}
}
public void onTag(Tag tag)
{
Location loc = _locator.getLocation();
Any line = Any.create(loc.getLine());
Any column = Any.create(loc.getColumn());
if (_on_tag_start != null) {
_on_tag_start.execute(_context, line, column, Any.create(tag.getPrefixedName()));
} else {
_context.print("<font color=\"#335577\"><");
_context.print(tag.getPrefixedName());
}
if (_on_attribute != null) {
int n = tag.getLength();
for(int i=0; i<n; i++) {
Attribute attr = tag.getAttribute(i);
_on_attribute.execute(_context,
line, column, Any.create(attr.getName()), Any.create(attr.getValue()));
}
} else {
int n = tag.getLength();
for(int i=0; i<n; i++) {
_context.print(" ");
Attribute attr = tag.getAttribute(i);
_context.print(attr.getName());
String s = attr.getValue();
if (s != null) {
_context.print("="");
_context.print(Conversions.encodeEntities(s));
_context.print(""");
}
}
}
if (_on_tag_end != null) {
_on_tag_end.execute(_context, line, column, Any.create(tag.hasEndSlash()));
} else {
if (tag.hasEndSlash()) {
_context.print("/>");
} else {
_context.print(">");
}
_context.print("</font>");
}
}
public void onCodeTag(Tag tag)
{
Location loc = _locator.getLocation();
Any line = Any.create(loc.getLine());
Any column = Any.create(loc.getColumn());
if (_on_code_tag_start != null) {
_on_code_tag_start.execute(_context, line, column, Any.create(tag.getPrefixedName()));
} else {
_context.print("<strong><font color=\"#335577\"><</font>");
_context.print("<font color=\"#600060\">");
_context.print(tag.getPrefixedName());
_context.print("</font><font color=\"#335577\">");
}
if (_on_code_attribute != null) {
int n = tag.getLength();
for(int i=0; i<n; i++) {
Attribute attr = tag.getAttribute(i);
_on_code_attribute.execute(_context, line, column,
Any.create(attr.getName()), Any.create(attr.getValue()));
}
} else {
int n = tag.getLength();
for(int i=0; i<n; i++) {
_context.print(" ");
Attribute attr = tag.getAttribute(i);
_context.print(attr.getName());
String s = attr.getValue();
if (s != null) {
_context.print("="");
_context.print(Conversions.encodeEntities(s));
_context.print(""");
}
}
}
if (_on_code_tag_end != null) {
_on_code_tag_end.execute(_context, line, column, Any.create(tag.hasEndSlash()));
} else {
if (tag.hasEndSlash()) {
_context.print("/>");
} else {
_context.print(">");
}
_context.print("</font></strong>");
}
}
}