//***************************************************************************
// This source code is copyrighted 2002 by Google Inc.  All rights
// reserved.  You are given a limited license to use this source code for
// purposes of participating in the Google programming contest.  If you
// choose to use or distribute the source code for any other purpose, you
// must either (1) first obtain written approval from Google, or (2)
// prominently display the foregoing copyright notice and the following
// warranty and liability disclaimer on each copy used or distributed.
// 
// The source code and repository (the "Software") is provided "AS IS",
// with no warranty, express or implied, including but not limited to the
// implied warranties of merchantability and fitness for a particular
// use.  In no event shall Google Inc. be liable for any damages, direct
// or indirect, even if advised of the possibility of such damages.
//***************************************************************************


#ifndef _PARSE_HANDLER_H_
#define _PARSE_HANDLER_H_
#include "goo-document.h"

// Allowed pattern of calls on a ParserHandler is:
//    ( ( NewDocument
//        ( AddHeader  |
//          AddBody    |
//          AddTerm    |
//          AddNumber  |
//          AddTitle   |
//          AddBaseURL |
//          AddAnchor  |
//          AnchorDone |
//          ChangeFontColor    |
//          ChangeFontColorEnd |
//          ChangeBGColor      |
//          ChangeBGColorEnd   |
//          AddImage   |
//          AddForm    |
//          AddApplet  |
//          AddArea    |
//          AddFrame   |
//          AddMeta    |
//          AddBodyDone)*
//        EndDocument
//      ) | (Flush Checkpoint?)?
//   )*
// I.e all of the "Add*" calls are bracketed by NewDocument/EndDocument,
// and "Flush" is not bracketed by NewDocument/EndDocument.

class ParseHandler {
 public:
  virtual ~ParseHandler() {}

  // These are called before and after we parse a document.
  virtual void NewDocument(const Document* doc) {}
  virtual void EndDocument(const Document* doc) {}

  // This is called when the Parser is told to Flush().
  // It will never be called within a NewDocument/EndDocument pair.
  virtual void Flush() {}

  // The intended use is to parse the header lines of http pages,
  // but it can be called with any associative data found on a page.
  // If valuelen is 0, key holds the status line ("HTTP/1.0 200 OK")
  virtual void AddHeader(const char* key, int keylen,
                         const char* value, int valuelen) {}

  // This tells you info about the status line (first line of header)
  virtual void AddResponseCode(int response_code) {}

  virtual void AddTerm(const char* term, int termlen, int face, int size) { }

  virtual void AddPunctuation(const char * text, int leng,
                              int face, int size) { }

  // These are pretty HTML-specific.  They're used to handle anchors.
  // NewAnchor is called with the HREF of the anchor, while AnchorDone
  // is called upon seeing the </A>.  You can use this information to
  // have AddTerm() treat intervening words as anchor text.
  virtual void AddBaseURL(const char* baseurl, int baseurllen) {}
  virtual void AddAnchor(const char* href, int hreflen) {} 
  virtual void AddLocalName(const char * name, int namelen) {}
  virtual void AnchorDone() {}
  // the following 4 methods are used to trace color changes
  virtual void ChangeFontColor(const char * color, int colorlen) {}
  virtual void ChangeFontColorEnd() {}
  virtual void ChangeBGColor(const char * color, int colorlen) {}
  virtual void ChangeBGColorEnd() {}

  // Also html-specific: the "entire" field of an img tag or perhaps imgmap.
  virtual void AddImage(const char* tag, int taglen) {}

  virtual void AddImageHeight(const char* src, int srclen) {}
  virtual void AddImageWidth(const char* src, int srclen) {}

  // "Embedded" applications
  virtual void AddApplet(const char* src, int srclen) {}
  virtual void AddAppletDone() {}
  virtual void AddIFrame(const char* src, int srclen) {}
  virtual void AddIFrameDone() {}

  // Hard to imagine how these would be used outside html.  The variable
  // name indicates which field of the given tag we're interested in.
  // Note: we treat frames and areas the same as anchors, by default
  virtual void AddFrame(const char* src, int srclen) { 
    AddAnchor(src, srclen); AnchorDone();       // no anchor text for us
  }
  virtual void AddArea(const char* href, int hreflen) {
    AddAnchor(href, hreflen); AnchorDone();       // no anchor text for us
  } 

  // Similar in some ways to AddHeader, but it doesn't try to separate
  // the key or value. 
  virtual void AddMeta(const char* meta, int metalen) {}

  virtual void AddFrameset(const char* fields, int fieldslen) {}
  virtual void AddFramesetDone() {}

  // use with caution, as only the first call in the one document is valid
  virtual void AddBody(const char* body, int bodylen) {}
  virtual void AddBodyDone() {}

  // These are called when we reach a P, /P respectively.  For most
  // applications, we probably just want to do the same thing.
  virtual void ParagraphStart(const char* fields, int fieldlen) { }
  virtual void ParagraphEnd() { }

  virtual void AddBreak() { }
  virtual void AddHorizontalRule() { }

  virtual void AddListItem() { }
  virtual void AddUnorderedList() { }
  virtual void AddOrderedList() { }
  virtual void AddListDone() { }

  virtual void AddDiv(const char * fields, int fieldlen) { }
  virtual void AddDivDone(const char * fields, int fieldlen) { }
  virtual void AddSpan(const char * fields, int fieldlen) { }
  virtual void AddSpanDone(const char * fields, int fieldlen) { }

  virtual void AddTable() { }
  virtual void AddTableDone() { }
  virtual void AddCaption() { }
  virtual void AddCaptionDone() { }
  virtual void AddTableHCell(const char* fields, int fieldlen) { }
  virtual void AddTableDCell(const char* fields, int fieldlen) { }
  virtual void AddTableCellDone() { }
  virtual void AddTableRow() { }
  virtual void AddTableRowDone() { }

  virtual void AddForm(const char * fields, int fieldlen) { }
  virtual void AddFormDone() { }
  virtual void AddSelect(const char * fields, int fieldlen) { }
  virtual void AddSelectDone() { }
  virtual void AddOption(const char * fields, int fieldlen) { }
  virtual void AddOptionDone() { }
  virtual void AddTextArea(const char * fields, int fieldlen) { }
  virtual void AddTextAreaDone() { }
  virtual void AddInput(const char * fields, int fieldlen) { }
  
  virtual void AddHeading(int hnum) { }
  virtual void AddHeadingDone() { }

  virtual void AddNoframes() { }
  virtual void AddNoframesDone() { }

  virtual void WhitespaceEndedTerm() { }

  virtual void AddObject(const char * fields, int fieldlen) { }
  virtual void AddObjectDone() { }
  virtual void AddParam(const char * fields, int fieldlen) { }
  virtual void AddEmbed(const char * fields, int fieldlen) { }

  virtual void AddHead(const char * fields, int fieldlen) { }
  virtual void AddHeadDone() { }
};

#endif /* #ifndef _PARSE_HANDLER_H_ */

