swbasicfilter.h

00001 /******************************************************************************
00002  *  swbasicfilter.h     - definition of class SWBasicFilter.  An SWFilter
00003  *                              impl that provides some basic methods that
00004  *                              many filter will need and can use as a starting
00005  *                              point. 
00006  *
00007  * $Id: swbasicfilter.h 1984 2006-10-08 05:06:52Z scribe $
00008  *
00009  * Copyright 1998 CrossWire Bible Society (http://www.crosswire.org)
00010  *      CrossWire Bible Society
00011  *      P. O. Box 2528
00012  *      Tempe, AZ  85280-2528
00013  *
00014  * This program is free software; you can redistribute it and/or modify it
00015  * under the terms of the GNU General Public License as published by the
00016  * Free Software Foundation version 2.
00017  *
00018  * This program is distributed in the hope that it will be useful, but
00019  * WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021  * General Public License for more details.
00022  *
00023  */
00024 
00025 #ifndef SWBASICFILTER_H
00026 #define SWBASICFILTER_H
00027 
00028 #include <swfilter.h>
00029 #include <swbuf.h>
00030 
00031 SWORD_NAMESPACE_START
00032 
00033 
00034 // not a protected inner class because MSVC++ sucks and can't handle it
00035 class SWDLLEXPORT BasicFilterUserData {
00036 public:
00037         BasicFilterUserData(const SWModule *module, const SWKey *key) { this->module = module; this->key = key; suspendTextPassThru = false; supressAdjacentWhitespace = false; }
00038         virtual ~BasicFilterUserData() {}
00039         const SWModule *module;
00040         const SWKey *key;
00041         SWBuf lastTextNode;
00042         SWBuf lastSuspendSegment;
00043         bool suspendTextPassThru;
00044         bool supressAdjacentWhitespace;
00045 };
00046 
00058 class SWDLLEXPORT SWBasicFilter : public SWFilter {
00059 
00060 class Private;
00061 
00062         char *tokenStart;
00063         char *tokenEnd;
00064         char *escStart;
00065         char *escEnd;
00066         char escStartLen;
00067         char escEndLen;
00068         char tokenStartLen;
00069         char tokenEndLen;
00070         bool escStringCaseSensitive;
00071         bool tokenCaseSensitive;
00072         bool passThruUnknownToken;
00073         bool passThruUnknownEsc;
00074         bool passThruNumericEsc;
00075         char processStages;
00076 
00077 
00078         Private *p;
00079 public:
00080 
00081         SWBasicFilter();
00082         virtual char processText(SWBuf &text, const SWKey *key = 0, const SWModule *module = 0);
00083         virtual ~SWBasicFilter();
00084 
00085 protected:
00086 
00087         virtual BasicFilterUserData *createUserData(const SWModule *module, const SWKey *key) {
00088                 return new BasicFilterUserData(module, key);
00089         }
00090 
00091         // STAGEs
00092         static const char INITIALIZE;   // flag for indicating processing before char loop
00093         static const char PRECHAR;      // flag for indicating processing at top in char loop
00094         static const char POSTCHAR;     // flag for indicating processing at bottom in char loop
00095         static const char FINALIZE;     // flag for indicating processing after char loop
00096 
00097 
00099         void setEscapeStart(const char *escStart);
00100 
00102         void setEscapeEnd(const char *escEnd);
00103 
00105         void setTokenStart(const char *tokenStart);
00106 
00108         void setTokenEnd(const char *tokenEnd);
00109 
00113         void setPassThruUnknownToken(bool val);
00114 
00119         void setPassThruUnknownEscapeString(bool val);
00120 
00124         void setPassThruNumericEscapeString(bool val);
00125 
00129         void setEscapeStringCaseSensitive(bool val);
00130 
00133         void addAllowedEscapeString(const char *findString);
00134 
00137         void removeAllowedEscapeString(const char *findString);
00138 
00141         void addEscapeStringSubstitute(const char *findString, const char *replaceString);
00142 
00145         void removeEscapeStringSubstitute(const char *findString);
00146 
00148         bool substituteEscapeString(SWBuf &buf, const char *escString);
00149 
00151         bool passAllowedEscapeString(SWBuf &buf, const char *escString);
00152 
00154         void appendEscapeString(SWBuf &buf, const char *escString);
00155 
00159         void setTokenCaseSensitive(bool val);
00160 
00164         void addTokenSubstitute(const char *findString, const char *replaceString);
00165 
00168         void removeTokenSubstitute(const char *findString);
00169 
00171         bool substituteToken(SWBuf &buf, const char *token);
00172 
00179         virtual bool handleToken(SWBuf &buf, const char *token, BasicFilterUserData *userData);
00180 
00181         virtual bool processStage(char /*stage*/, SWBuf &/*text*/, char *&/*from*/, BasicFilterUserData * /*userData*/) { return false; }
00182         virtual void setStageProcessing(char stages) { processStages = stages; }        // see STATICs up above
00183 
00191         virtual bool handleEscapeString(SWBuf &buf, const char *escString, BasicFilterUserData *userData);
00192 
00198         virtual bool handleNumericEscapeString(SWBuf &buf, const char *escString);
00199 
00200 
00201 };
00202 
00203 SWORD_NAMESPACE_END
00204 #endif