00001 #ifndef IONFLUX_TOOLS_TEMPLATE 00002 #define IONFLUX_TOOLS_TEMPLATE 00003 /* ========================================================================== 00004 * Ionflux Tools 00005 * Copyright (c) 2004 Joern P. Meier 00006 * mail@ionflux.org 00007 * -------------------------------------------------------------------------- 00008 * Template.hpp Template processor. 00009 * ========================================================================== 00010 * 00011 * This file is part of Ionflux Tools. 00012 * 00013 * Ionflux Tools is free software; you can redistribute it and/or modify it 00014 * under the terms of the GNU General Public License as published by the Free 00015 * Software Foundation; either version 2 of the License, or (at your option) 00016 * any later version. 00017 * 00018 * Ionflux Tools is distributed in the hope that it will be useful, but 00019 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 00020 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 00021 * for more details. 00022 * 00023 * You should have received a copy of the GNU General Public License 00024 * along with Ionflux Tools; if not, write to the Free Software Foundation, 00025 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00026 * 00027 * ========================================================================== */ 00028 00029 #include <string> 00030 #include <iostream> 00031 #include <fstream> 00032 #include <sstream> 00033 #include <vector> 00034 #include <map> 00035 #include "ionflux/tools.hpp" 00036 #include "ionflux/Reporter.hpp" 00037 #include "ionflux/Tree.hpp" 00038 #include "ionflux/Tokenizer.hpp" 00039 #include "ionflux/StringFilter.hpp" 00040 00041 namespace Ionflux 00042 { 00043 00044 namespace Tools 00045 { 00046 00059 struct TemplateVar 00060 { 00062 DataEntry data; 00064 StringFilter *filter; 00065 }; 00066 00072 struct TemplateElement 00073 { 00075 int type; 00077 std::vector<std::string> params; 00079 std::vector<TemplateVar> vars; 00081 std::vector<int> refs; 00083 static const int TE_INVALID; 00085 static const int TE_CDATA; 00087 static const int TE_VAR; 00089 static const int TE_FOREACH; 00091 static const int TE_FOREACH_ELSE; 00093 static const int TE_FOREACH_END; 00095 static const int TE_IF; 00097 static const int TE_IF_ELSE; 00099 static const int TE_IF_END; 00101 static const int TE_FIRST; 00103 static const int TE_FIRST_END; 00105 static const int TE_MID; 00107 static const int TE_MID_END; 00109 static const int TE_LAST; 00111 static const int TE_LAST_END; 00113 static const int TE_SINGLE; 00115 static const int TE_SINGLE_END; 00117 static const int TE_NOTLAST; 00119 static const int TE_NOTLAST_END; 00121 static const int TE_NOTFIRST; 00123 static const int TE_NOTFIRST_END; 00125 static const int TE_NOTMID; 00127 static const int TE_NOTMID_END; 00129 static const int TE_NOTSINGLE; 00131 static const int TE_NOTSINGLE_END; 00133 static const int TE_INCLUDE; 00135 static const int TE_SWRAP; 00137 static const int TE_SWRAP_END; 00139 static const int TE_SECTION; 00141 static const int TE_SECTION_END; 00143 static const int TE_REF; 00144 }; 00145 00150 struct TagNestingLevel 00151 { 00153 unsigned int tagForeach; 00155 unsigned int tagIf; 00157 unsigned int tagFirst; 00159 unsigned int tagMid; 00161 unsigned int tagLast; 00163 unsigned int tagSingle; 00165 unsigned int tagNotFirst; 00167 unsigned int tagNotMid; 00169 unsigned int tagNotLast; 00171 unsigned int tagNotSingle; 00173 unsigned int tagSWrap; 00175 unsigned int tagSection; 00177 unsigned int tagRef; 00179 unsigned int all; 00180 00182 TagNestingLevel(); 00183 }; 00184 00189 struct TemplateSection 00190 { 00192 unsigned int first; 00194 unsigned int last; 00195 }; 00196 00198 typedef std::map<std::string, TemplateSection> TemplateSectionMap; 00199 00204 class Template 00205 : public Tree 00206 { 00207 protected: 00209 Ionflux::Tools::Reporter log; 00211 std::string templateInput; 00213 Tokenizer tok; 00215 Tokenizer tagTok; 00217 std::vector<TemplateElement *> elements; 00219 TemplateSectionMap sections; 00221 unsigned int maxNestingDepth; 00222 00224 virtual void clearElements(); 00225 00238 virtual bool parse(std::vector<TemplateElement *> &target); 00239 00246 virtual bool parse(); 00247 00256 virtual void addCharacterData(const std::string &characterData, 00257 std::vector<TemplateElement *> &target); 00258 00270 virtual bool addTemplateTag(const std::string &tagData, 00271 std::vector<TemplateElement *> &target); 00272 00281 virtual bool preprocess(std::vector<TemplateElement *> &target); 00282 00294 virtual std::string process(unsigned int from, unsigned int to, 00295 TagNestingLevel depth); 00296 00297 public: 00305 static const std::string ITERATION_COUNTER; 00313 static const std::string NUM_ITERATIONS; 00318 static const std::string CONDITION_NODE; 00320 static const TokenType TT_TEMPLATE_TAG_OPEN; 00322 static const TokenType TT_TEMPLATE_TAG_CLOSE; 00324 static const TokenType TT_VAR_MARKER; 00326 static const TokenType TT_COMMENT; 00328 static const TokenType TT_TREEPATH; 00330 static const TokenType TT_END_TAG_MARKER; 00332 static const TokenType TT_OPERATOR_NSO; 00334 static const std::string TAG_FOREACH; 00336 static const std::string TAG_FOREACH_ELSE; 00338 static const std::string TAG_FOREACH_IN; 00340 static const std::string TAG_IF; 00342 static const std::string TAG_IF_ELSE; 00344 static const std::string TAG_FIRST; 00346 static const std::string TAG_MID; 00348 static const std::string TAG_LAST; 00350 static const std::string TAG_SINGLE; 00352 static const std::string TAG_NOTFIRST; 00354 static const std::string TAG_NOTMID; 00356 static const std::string TAG_NOTLAST; 00358 static const std::string TAG_NOTSINGLE; 00360 static const std::string TAG_INCLUDE; 00362 static const std::string TAG_SWRAP; 00364 static const std::string TAG_SECTION; 00366 static const std::string TAG_REF; 00368 static const std::string SWRAP_DEFAULT_LINE_WIDTH; 00370 static const std::string SWRAP_DEFAULT_LINE_TERM; 00372 static const unsigned int DEFAULT_MAX_NESTING_DEPTH; 00373 00378 Template(); 00379 00384 virtual ~Template(); 00385 00393 virtual void readTemplate(const std::string& templateFileName, 00394 std::vector<TemplateElement *> &target); 00395 00402 virtual void readTemplate(const std::string& templateFileName); 00403 00411 virtual void setTemplate(const std::string& newTemplate, 00412 std::vector<TemplateElement *> &target); 00413 00420 virtual void setTemplate(const std::string& newTemplate); 00421 00428 virtual std::string process(const std::string& newTemplate = "", 00429 Node* newConfig = 0); 00430 00437 virtual void setMaxNestingDepth(unsigned int newMaxNestingDepth); 00438 00445 virtual unsigned int getMaxNestingDepth(); 00446 00448 virtual void printElementDebugInfo(); 00449 00454 virtual Reporter &getLog(); 00455 00456 }; 00457 00459 00460 } 00461 00462 } 00463 00467 #endif