/* shakespearedoc.h
 An example class for Jon Bosak's Shakespeare play documents, specifically the
  subset we're interested in for our example application.
  Demonstrates the use of the expatParser class and docObjectIface interface.
  http://projects.zillabit.com/xml.html

Copyright (c) 2002, Earl Levine 
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted 
provided that the following conditions are met:

-Redistributions of source code must retain the above copyright notice, this list of conditions 
and the following disclaimer.

-The name of Earl Levine may not be used to endorse or promote products derived from this 
software without specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS 
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNERS OR 
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE. 
*/

#ifndef _SHAKESPEAREDOC_H_
#define _SHAKESPEAREDOC_H_

#include "docObjectIface.h"
#include <stdio.h>

class ShakespeareDocElement; // forward reference needed for following class
class ShakespeareDocElementRoot; // forward reference needed for following class
class ShakespeareDocElementPlay; // forward reference needed for following class

class ShakespeareDoc : public docObjectIface {
public:
  ShakespeareDoc(FILE *warnlog);
  ~ShakespeareDoc();
  const ShakespeareDocElementPlay* getPlay() const;
	
  // Callbacks that will happen during the parsing.
  // It would be nice not to make these public, however they need to
  //  be callable from the extern "C" callback interfaces that expat requires
  //  so no dice.
  virtual void StartElementHandler(const char *name, const char **atts);
  virtual void EndElementHandler(const char *name);
  virtual void CharacterDataHandler(const XML_Char *s, int len);

protected:
  ShakespeareDocElementRoot *pRootElem;
  ShakespeareDocElement *pParsingElem;
	
};

// A base class that holds the common structure for each kind of known element
class ShakespeareDocElement {
 public:
  ShakespeareDocElement(ShakespeareDocElement *_parentElement, FILE *_warnlog);
  virtual ~ShakespeareDocElement();

  // Parsing functions
  ShakespeareDocElement* ParseStart(const char *name);
  ShakespeareDocElement* ParseEnd();
  void ParseCharData(const char *s, int len);

  // Accessors for those who have const objects
  const char* getCharacterData() const; //*Might* be NULLs before the end, caller should check len
  int getCharacterDataLen() const; 
  const ShakespeareDocElement *getUniqueSubelement() const;
  int getNumListSubelements() const;
  const ShakespeareDocElement *getListSubelement (int num) const;

  virtual const char* getElementName() const = 0;
  const char* getUniqueSubelementName();
  const char* getListSubelementName();

 protected:
  void warnNonUnique();
  void warnUniqueNeverFound();
  void warnLocation();
  bool matchesUniqueSubelemName(const char* name);
  bool matchesListSubelemName(const char* name);
  virtual ShakespeareDocElement* constructUniqueSubelem();
  virtual ShakespeareDocElement* constructListSubelem();

  ShakespeareDocElement *parentElement;
  ShakespeareDocElement *uniqueSubelement; // used for TITLE, SPEAKER, etc.
  ShakespeareDocElement **listSubelements; // used for ACT, SCENE, SPEECH, etc.
  int numListSubelements;
  char *characterData;
  int characterDataLen;
  bool parseState_ignoringSubelement;
  int parseState_ignoringSubelementDepth;
  FILE *warnlog;
};

// Thanks to the ShakespeareDocElement base class it's trivial to implement all
//  the element types that define the document structure

class ShakespeareDocElementTitle : public ShakespeareDocElement {
 public:
  ShakespeareDocElementTitle(ShakespeareDocElement *parent, FILE *warnlog)
    : ShakespeareDocElement(parent, warnlog) {};
  virtual const char* getElementName() const;
};

class ShakespeareDocElementLine : public ShakespeareDocElement {
 public:
  ShakespeareDocElementLine(ShakespeareDocElement *parent, FILE *warnlog)
    : ShakespeareDocElement(parent, warnlog) {};
  virtual const char* getElementName() const;
};

class ShakespeareDocElementSpeaker : public ShakespeareDocElement {
 public:
  ShakespeareDocElementSpeaker(ShakespeareDocElement *parent, FILE *warnlog)
    : ShakespeareDocElement(parent, warnlog) {};
  virtual const char* getElementName() const;
};

class ShakespeareDocElementSpeech : public ShakespeareDocElement {
 public:
  ShakespeareDocElementSpeech(ShakespeareDocElement *parent, FILE *warnlog)
    : ShakespeareDocElement(parent, warnlog) {};
  virtual const char* getElementName() const;
  const ShakespeareDocElementSpeaker* getSpeaker() const;
  int getNumLines() const;
  const ShakespeareDocElementLine* getLine (int num) const;
 protected:
  ShakespeareDocElement* constructUniqueSubelem();
  ShakespeareDocElement* constructListSubelem();
};

class ShakespeareDocElementWithTitle : public ShakespeareDocElement {
 public:
  ShakespeareDocElementWithTitle(ShakespeareDocElement *parent, FILE *warnlog)
    : ShakespeareDocElement(parent, warnlog) {};
  const ShakespeareDocElementTitle* getTitle() const;
 protected:
  ShakespeareDocElement* constructUniqueSubelem();
};

class ShakespeareDocElementScene : public ShakespeareDocElementWithTitle {
 public:
  ShakespeareDocElementScene(ShakespeareDocElement *parent, FILE *warnlog)
    : ShakespeareDocElementWithTitle(parent, warnlog) {};
  virtual const char* getElementName() const;
  int getNumSpeeches() const;
  const ShakespeareDocElementSpeech* getSpeech (int num) const;
 protected:
  ShakespeareDocElement* constructListSubelem();
};

class ShakespeareDocElementAct : public ShakespeareDocElementWithTitle {
 public:
  ShakespeareDocElementAct(ShakespeareDocElement *parent, FILE *warnlog)
    : ShakespeareDocElementWithTitle(parent, warnlog) {};
  virtual const char* getElementName() const;
  int getNumScenes() const;
  const ShakespeareDocElementScene* getScene (int num) const;
 protected:
  ShakespeareDocElement* constructListSubelem();
};

class ShakespeareDocElementPlay : public ShakespeareDocElementWithTitle {
 public:
  ShakespeareDocElementPlay(ShakespeareDocElement *parent, FILE *warnlog)
    : ShakespeareDocElementWithTitle(parent, warnlog) {};
  virtual const char* getElementName() const;
  int getNumActs() const;
  const ShakespeareDocElementAct* getAct (int num) const;
 protected:
  ShakespeareDocElement* constructListSubelem();
};

class ShakespeareDocElementRoot : public ShakespeareDocElement {
 public:
  ShakespeareDocElementRoot(FILE *warnlog)
    : ShakespeareDocElement(NULL, warnlog) {};
  virtual const char* getElementName() const;
  const ShakespeareDocElementPlay* getPlay() const;
 protected:
  ShakespeareDocElement* constructUniqueSubelem();
};


#endif /* _SHAKESPEAREDOC_H_ */
