File indexing completed on 2024-04-28 15:35:14

0001 /***************************************************************************
0002  *   Copyright (C) 2004 by Tomas Mecir                                     *
0003  *   kmuddy@kmuddy.org                                                     *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU Library General Public License as       *
0007  *   published by the Free Software Foundation; either version 2 of the    *
0008  *   License, or (at your option) any later version.                       *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU Library General Public License for more details.                  *
0014  ***************************************************************************/
0015 #ifndef CELEMENTMANAGER_H
0016 #define CELEMENTMANAGER_H
0017 
0018 class cEntityManager;
0019 class cMXPParser;
0020 class cMXPState;
0021 class cResultHandler;
0022 
0023 #include <list>
0024 #include <map>
0025 #include <string>
0026 
0027 using namespace std;
0028 
0029 
0030 struct sElementPart {
0031   bool istag;
0032   string text;
0033 };
0034 
0035 /** one external element (defined by <!element>) */
0036 struct sElement {
0037   /** is it an open element? */
0038   bool open;
0039   /** is it an element with no closing tag? */
0040   bool empty;
0041   /** tag associated with this element */
0042   int tag;
0043   /** flag associated with this element */
0044   string flag;
0045   /** list of all element contents */
0046   list<sElementPart *> element;
0047   /** list of element attributes in the right order */
0048   list<string> attlist;
0049   /** default values for attributes */
0050   map<string, string> attdefault;
0051   /** closing sequence */
0052   list<string> closingseq;
0053 };
0054 
0055 /** one internal element */
0056 struct sInternalElement {
0057   /** is it an open element? */
0058   bool open;
0059   /** is it an element with no closing tag? */
0060   bool empty;
0061   /** list of element attributes in the right order */
0062   list<string> attlist;
0063   /** default values for attributes; if there's an empty string (but defined), then it's a flag */
0064   map<string, string> attdefault;
0065 };
0066 
0067 /** one parameter in one tag :-) */
0068 struct sParam {
0069   bool flag;
0070   string name, value;
0071 };
0072 
0073 /**
0074 Element manages handles a list of user-defined elements, it also supports second-level parsing of incoming elements.
0075 
0076 @author Tomas Mecir
0077 */
0078 
0079 class cElementManager {
0080  public:
0081   /** constructor */
0082   cElementManager (cMXPState *st, cResultHandler *res, cEntityManager *enm);
0083   /** destructor */
0084   ~cElementManager ();
0085   
0086   /** set pointer to cMXPState - needed due to circular dependencies */
0087   void assignMXPState (cMXPState *st);
0088 
0089   void reset ();
0090 
0091   /** is this element defined? */
0092   bool elementDefined (const string &name);
0093   /** is it an internal tag? */
0094   bool internalElement (const string &name);
0095   /** is it a custom element? (i.e. defined via <!element>) */
0096   bool customElement (const string &name);
0097   /** open element? */
0098   bool openElement (const string &name);
0099   /** empty element? i.e. does it need a closing tag? */
0100   bool emptyElement (const string &name);
0101 
0102   /** we've got a new tag, parameter contains the tag without <> chars */
0103   void gotTag (const string &tag);
0104 
0105   /** got a line tag - expand it to an associated tag, if any */
0106   void gotLineTag (int number);
0107   /** got newline - expand last line-tag to closing tag, if needed */
0108   void gotNewLine ();
0109 protected:
0110   /** identify flags in an internal tag */
0111   void identifyFlags (const map<string, string> &attdefault, list<sParam> &args);
0112   /** create a list of parameters with their names */
0113   void handleParams (const string &tagname, list<sParam> &args,
0114       const list<string> &attlist, const map<string, string> &attdefault);
0115 
0116   void handleClosingTag (const string &name);
0117 
0118   /** process internal tag, the list consists of pairs name, value */
0119   void processInternalTag (const string &name, const list<sParam> &params,
0120       const list<string> &flags);
0121   /** process <support> tag, which has a special syntax */
0122   void processSupport (const list<sParam> &params);
0123   /** process external tag */
0124   void processCustomTag (const string &name, const list<sParam> &params);
0125 
0126   /** process a list of parameters for !ELEMENT and !ATTLIST */
0127   void processParamList (const string &params, list<string> &attlist,
0128       map<string, string> &attdefault);
0129   /** add a new element */
0130   void addElement (const string &name, list<sElementPart *> contents, list<string> attlist,
0131       map<string, string> attdefault, bool open, bool empty, int tag, string flag);
0132   /** set attribute list for an existing element */
0133   void setAttList (const string &name, list<string> attlist, map<string, string> attdefault);
0134   /** remove an EXTERNAL element */
0135   void removeElement (const string &name);
0136   /** remove all EXTERNAL elements */
0137   void removeAll ();
0138 
0139   void createInternalElements ();
0140 
0141   /** list of all custom elements */
0142   map<string, sElement *> elements;
0143   map<string, sInternalElement *> ielements;
0144 
0145   /** line tags associated with elements */
0146   map<int, string> lineTags;
0147 
0148   /** aliases for internal elements */
0149   map<string, string> aliases;
0150 
0151   /** last line tag */
0152   int lastLineTag;
0153 
0154   /** state class */
0155   cMXPState *state;
0156   /** result handler */
0157   cResultHandler *results;
0158   /** entity manager */
0159   cEntityManager *entities;
0160   /** expander of parameters in custom tags */
0161   cEntityManager *paramexpander;
0162   /** parser of custom element definitions */
0163   cMXPParser *parser;
0164   
0165 };
0166 
0167 #endif