// (c) MIT 2003.  All rights reserved.

// -*-Mode: Java -*- 
// Content.java -- 
// Author: mark a. foltz <mfoltz@ai.mit.edu>
// Maintainer: mark a. foltz <mfoltz@ai.mit.edu>
// Copyright: Copyright (C) 2000 by mark a. foltz
// Created: <Thu Jan  6 08:43:50 2000> 
// Keywords: 

/**
 * 
 * @author mark a. foltz <mfoltz@ai.mit.edu>
 * @version $Id: Content.java,v 1.2 2003/03/06 00:40:41 moltmans Exp $
 * */

package rec.tinybrowser;

import java.awt.*;
import java.util.*;

public class Content {

  public static final Integer PARAGRAPH_OBJ = new Integer(3000);
  public static final Integer BREAK_OBJ = new Integer(3001);
  public static final Integer LIST_ITEM_OBJ = new Integer(3002);
  public static final Integer START_UL_OBJ = new Integer(3003);
  public static final Integer END_UL_OBJ = new Integer(3004);
  public static final Integer START_OL_OBJ = new Integer(3005);
  public static final Integer END_OL_OBJ = new Integer(3006);
  public static final Integer INDENT_OBJ = new Integer(3007);
  public static final Integer UNINDENT_OBJ = new Integer(3008);
  public static final Integer ALIGN_LEFT_OBJ = new Integer(3009);
  public static final Integer ALIGN_RIGHT_OBJ = new Integer(30010);
  public static final Integer ALIGN_CENTER_OBJ = new Integer(3011);

  public static final Integer RESTORE_FONT_OBJ = new Integer(2000);
  public static final Integer RESTORE_COLOR_OBJ = new Integer(2001);
  public static final Integer START_BOLD_OBJ = new Integer(2002);
  public static final Integer END_BOLD_OBJ = new Integer(2003);
  public static final Integer START_ITALIC_OBJ = new Integer(2004);
  public static final Integer END_ITALIC_OBJ = new Integer(2005);

  public static final int PARAGRAPH = 3000;
  public static final int BREAK = 3001;
  public static final int LIST_ITEM = 3002;
  public static final int START_UL = 3003;
  public static final int END_UL = 3004;
  public static final int START_OL = 3005;
  public static final int END_OL = 3006;
  public static final int INDENT = 3007;
  public static final int UNINDENT = 3008;
  public static final int ALIGN_LEFT = 3009;
  public static final int ALIGN_RIGHT = 30010;
  public static final int ALIGN_CENTER = 3011;

  public static final int RESTORE_FONT = 2000;
  public static final int RESTORE_COLOR = 2001;
  public static final int START_BOLD = 2002;
  public static final int END_BOLD = 2003;
  public static final int START_ITALIC = 2004;
  public static final int END_ITALIC = 2005;

  private Vector elements = new Vector();

  public Content() { }

  public void setColor(Color c) {
    elements.addElement(c);
  }

  public void setFont(Font f) {
    elements.addElement(f);
  }

  public void startItalic() {
     elements.addElement(START_ITALIC_OBJ);
  }

  public void endItalic() {
    elements.addElement(END_ITALIC_OBJ);
  }

  public void startBold() {
    elements.addElement(START_BOLD_OBJ);
  }

  public void endBold() {
    elements.addElement(END_BOLD_OBJ);
  }

  public void restoreColor() {
    elements.addElement(RESTORE_COLOR_OBJ);
  }

  public void restoreFont() {
    elements.addElement(RESTORE_FONT_OBJ);
  }

  public void indent() {
    elements.addElement(INDENT_OBJ);
  }

  public void unIndent() {
    elements.addElement(UNINDENT_OBJ);
  }
    
  public void setAlignment(Integer alignment) {
    elements.addElement(alignment);
  }

  public void addText(String s) {
    elements.addElement(s);
  }

  public void addLink(Hyperlink link) {
    elements.addElement(link);
  }

  public void addParagraph() {
    elements.addElement(PARAGRAPH_OBJ);
  }

  public void addBreak() {
    elements.addElement(BREAK_OBJ);
  }

  public void startUnorderedList() {
    elements.addElement(START_UL_OBJ);
  }

  public void nextListItem() {
    elements.addElement(LIST_ITEM_OBJ);
  }

  public void endUnorderedList() {
    elements.addElement(END_UL_OBJ);
  }

  public void startOrderedList() {
    elements.addElement(START_OL_OBJ);
  }

  public void endOrderedList() {
    elements.addElement(END_OL_OBJ);
  }

  public Enumeration elements() {
    return elements.elements();
  }
  
  public void clear() {
    elements.removeAllElements();
  }

  public int size() {
    return elements.size();
  }

  public static final Content parse(String html) {
    Content content = new Content();
    StringTokenizer st = new StringTokenizer(html, "<>", true);
    String token;
    // 0 = expect string, 1 = expect less-than, 
    // 2 = expect tag, 3 = expect greater-than
    int parse_state = 0;

    while (st.hasMoreTokens()) {
      token = st.nextToken();
      if (parse_state == 0) {
	if ("<".equals(token)) parse_state = 2;
	else {
	  content.addText(token);
	  parse_state = 1;
	}
      } else if (parse_state == 1) {
	if ("<".equals(token)) parse_state = 2;
	else {
	  content.addText(token);
	  parse_state = 0;
	}
      } else if (parse_state == 3) {
	if ("><".equals(token)) parse_state = 2;
	else parse_state = 0;
      } else {
	parse_state = 3;
	if ("p".equalsIgnoreCase(token)) content.addParagraph();
	else if ("br".equalsIgnoreCase(token)) content.addBreak();
	else if ("b".equalsIgnoreCase(token)) content.startBold();
 	else if ("/b".equalsIgnoreCase(token)) content.endBold();
	else if ("i".equalsIgnoreCase(token)) content.startItalic();
	else if ("/i".equalsIgnoreCase(token)) content.endItalic();
	else if ("li".equalsIgnoreCase(token)) content.nextListItem();
 	else if ("center".equalsIgnoreCase(token)) content.setAlignment(ALIGN_CENTER_OBJ);
 	else if ("right".equalsIgnoreCase(token)) content.setAlignment(ALIGN_RIGHT_OBJ);
 	else if ("/right".equalsIgnoreCase(token) || "/center".equalsIgnoreCase(token)) 
	  content.setAlignment(ALIGN_LEFT_OBJ);
	else if ("ul".equalsIgnoreCase(token)) content.startUnorderedList();
	else if ("/ul".equalsIgnoreCase(token)) content.endUnorderedList();
	else if ("ol".equalsIgnoreCase(token)) content.startOrderedList();
	else if ("/ol".equalsIgnoreCase(token)) content.endOrderedList();
	else if ("/font".equalsIgnoreCase(token)) content.restoreFont();
	else if ("/color".equalsIgnoreCase(token)) content.restoreColor();
	else if (token.startsWith("font") || token.startsWith("FONT")) 
	  parseFontTag(content, token);
	else if (token.startsWith("color") || token.startsWith("COLOR")) 
	  parseColorTag(content, token);
	else if (token.startsWith("a ") || token.startsWith("A ")) 
	  parseHyperlink(content, token, st);
	else {
	  content.addText(token);
	  parse_state = 0;
	}
      }
    }

    return content;
    
  }

  private static final void parseFontTag(Content content, String token) {
  }

  private static final void parseColorTag(Content content, String token) {
    StringTokenizer st = new StringTokenizer(token," \"");
    String color = st.nextToken(); // color
    color = st.nextToken(); // color value
    try {
      Integer color_value = Integer.decode(color);
      content.setColor(new Color(color_value.intValue()));
    } catch (NumberFormatException ex) { 
      System.err.println("Could not decode color "+color);
      ex.printStackTrace();
    }
  }

  private static final void parseHyperlink(Content content, String token, StringTokenizer st) {
  }

  
}
