// HtmlGenerator.java
// $Id: HtmlGenerator.java,v 1.8 1996/12/16 10:45:27 abaird Exp $
// (c) COPYRIGHT MIT and INRIA, 1996.
// Please first read the full copyright statement in file COPYRIGHT.html

package w3c.jigsaw.html ;

import java.io.* ;
import java.util.*;
import w3c.www.mime.*;

class HtmlHead {
    
    String       title   = null;
    String       base    = null;
    Vector       links   = null;
    HtmlStyle    style   = null;
    StringBuffer meta    = null ; // FIXME
    
    /**
     * @return the current HtmlStyle (if any)
     */

    public HtmlStyle getStyle()
    {
	return style;
    }

    /**
     * Create and add/replace a new style in the head
     * @param type, the MimeType of the style
     * @param style, the full text of the style sheet
     */

    public void addStyle(MimeType type, String style)
    {
	this.style = new HtmlStyle(type, style);
    }

    /**
     * set the HREF part of the BASE element
     */

    public void addBase(String base)
    {
	this.base = base;
    }

    /**
     * Add or append style to the current Style
     * @param style, the full text of the style sheet to append
     */
   
    public void addStyle(String style)
    {
	if(this.style == null)
	    this.style = new HtmlStyle(style);
	else
	    this.style.append(style);
    }

    /**
     * Add a link to the head
     * @param link an HtmlLink
     */

    public void addLink(HtmlLink link)
    {
	if(links == null)
	    links = new Vector(4);
	links.addElement(link);
    }

    /** Add some meta-http-equiv information 
     * @param name, the name of the meta-http 
     * @param value, the name of the meta-http
     */
    
    public void addMeta (String name, String value) {
	if ( meta == null ) {
	     meta = new StringBuffer ("<meta http-equiv=\""
                                      + name
				      + "\" content=\""
                                      + value +"\">") ;
	} else {
	    meta.append ("<meta http-equiv=\""
                         + name
			 + "\" content=\""
                         + value
			 + "\">") ;
	}
    }

    /**
     * generate a String format of the HEAD element that can
     * be inserted in a HTML document
     */

    public String toString()
    {
	StringBuffer strlink = new StringBuffer("");
	if(links != null)
	{
	    for(int i=0; i<links.size(); i++)
		strlink.append(links.elementAt(i).toString());
	}
	return "<HEAD>\n<TITLE>" + title + "</TITLE>\n"
	       + ((meta != null) ? meta.toString() : "")
	       + ((base != null) ? "<BASE HREF=\"" + base + "\">\n":"")
               + strlink.toString() 
               + ((style != null) ? style.toString() : "")
               + "</HEAD>" ;
    }

    public HtmlHead(String title)
    {
	this.title = title;
    }
}
	
/**
 * A simple HTML generator.
 * This class implements an HTML generator that allows to output dynamic
 * HTML content out.
 */

public class HtmlGenerator {
    private static MimeType type = MimeType.TEXT_HTML;

    HtmlHead     head    = null ;
    StringBuffer body    = null ;
    String       content = null ;	// content once closed.
    boolean      bodytag = true ;       // frameset hack

    /**
     * Get this stream MIME type.
     * This defaults to <strong>text/html</strong>.
     */

    public MimeType getMimeType () {
	return type ;
    }

    /**
     * Don't emit body tag. This is usefull in conjunction with the FRAMESET
     * tag, that requires that no BODY tag be emited.
     * @param value If <strong>true</strong>, a BODY tag will be emited.
     */

    public void emitBODYTag (boolean value) {
	this.bodytag = value ;
    }

    /**
     * Append the given string, escaping all special characters. This can be 
     * used only if you know that the string you are inserting doesn't contain
     * HTML tags
     */

    public void appendAndEscape (String content) {
	for (int i = 0 ; i < content.length() ; i++) {
	    char ch = content.charAt(i) ;
	    switch (ch) {
	      case '<': body.append ("&lt;") ; break ;
	      case '>': body.append ("&gt;") ; break ;
	      default:  body.append (ch) ; break;
	    }
        }
    }

    /**
     * Add a Base element to the head
     * @param the href part of the BASE element
     */
    
    public void addBase(String base) {
	head.addBase(base);
    }

    /**
     * Add style to this html page
     * @see w3c.jigsaw.html.HtmlStyle
     */

    public void addStyle(String style) {
	head.addStyle(style);
    }

    /**
     * Add style to this html page
     * @see w3c.jigsaw.html.HtmlStyle
     */

     public void addStyle(MimeType type, String style) {
	head.addStyle(type, style);
    }

    /**
     * Add a link to the head of this html document
     * @param link the link
     */
    
    public void addLink(HtmlLink link)
    {
	head.addLink(link);
    }

    /**
     * Append the given string to the document body.
     * @param The HTML string to append.
     */

    public void append (String content) {
	body.append (content) ;
    }

    /**
     * Append the two strings to the document body.
     * <code>append("x"+"y");</code> is equivalent but slower than
     * <code>append("x", "y");</code>.
     * @param str1 The first string.
     * @param str2 The second string.
     */

    public void append(String str1, String str2) {
	body.append(str1) ;
	body.append(str2) ;
    }

    public void append(String s1, String s2, String s3) {
	body.append(s1);
	body.append(s2);
	body.append(s3);
    }

    /**
     * Get the length of this html document
     * @return the length in bytes of the document
     */
    
    public int length () {
	if ( content == null )
	    close () ;
	return content.length() ;
    }

    /**
     * Close the given document: its composition is now finished.
     * @return	The content length for this document.
     */

    public void close () {
	if ( content != null )
	    return ;
	content = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Draft//EN\">"
	    + "\n<HTML>" 
            + head.toString() + "\n"
            + (bodytag 
	       ? ("<BODY>" + body.toString() + "</BODY>\n")
	       : body.toString())
	    + "</HTML>" ;
	return ;
    }

    public void meta (String name, String value)
    {
	head.addMeta(name, value);
    }

    /**
     * Get the input string for reading the document.
     * @return An input stream to get the generated document from.
     */
    
    public InputStream getInputStream () {
	close() ;
	return new StringBufferInputStream (content) ;
    }

    public HtmlGenerator (String title) {
	this.head = new HtmlHead(title); 
	this.body = new StringBuffer() ;
    }
}
