Virtualize template parameters.
authorScott Cantor <cantor.2@osu.edu>
Wed, 20 Dec 2006 03:01:05 +0000 (03:01 +0000)
committerScott Cantor <cantor.2@osu.edu>
Wed, 20 Dec 2006 03:01:05 +0000 (03:01 +0000)
xmltooling/util/TemplateEngine.cpp
xmltooling/util/TemplateEngine.h

index 4a66018..d4deeb2 100644 (file)
@@ -82,7 +82,7 @@ void TemplateEngine::process(
     const string& buf,
     const char*& lastpos,
     ostream& os,
-    const map<string,string>& parameters,
+    const TemplateParameters& parameters,
     const XMLToolingException* e
     ) const
 {
@@ -118,15 +118,11 @@ void TemplateEngine::process(
                 string key = buf.substr(lastpos-line, thispos-lastpos);
                 trimspace(key);
         
-                map<string,string>::const_iterator i=parameters.find(key);
-                if (i != parameters.end()) {
-                    html_encode(os,i->second.c_str());
-                }
-                else if (e) {
-                    const char* ep = e->getProperty(key.c_str());
-                    if (ep)
-                        html_encode(os,ep);
-                }
+                const char* p = parameters.getParameter(key.c_str());
+                if (!p && e)
+                    p = e->getProperty(key.c_str());
+                if (p)
+                    html_encode(os,p);
                 lastpos = thispos + 2; // strlen("/>")
             }
         }
@@ -143,14 +139,9 @@ void TemplateEngine::process(
             if ((thispos = strchr(lastpos, '>')) != NULL) {
                 string key = buf.substr(lastpos-line, thispos-lastpos);
                 trimspace(key);
-                map<string,string>::const_iterator i=parameters.find(key);
                 bool cond=false;
-                if (visible) {
-                    if (i != parameters.end())
-                        cond=true;
-                    else if (e && e->getProperty(key.c_str()))
-                        cond=true;
-                }
+                if (visible)
+                    cond = parameters.getParameter(key.c_str()) || (e && e->getProperty(key.c_str()));
                 lastpos = thispos + 1; // strlen(">")
                 process(cond, buf, lastpos, os, parameters, e);
             }
@@ -178,14 +169,9 @@ void TemplateEngine::process(
             if ((thispos = strchr(lastpos, '>')) != NULL) {
                 string key = buf.substr(lastpos-line, thispos-lastpos);
                 trimspace(key);
-                map<string,string>::const_iterator i=parameters.find(key);
                 bool cond=visible;
-                if (visible) {
-                    if (i != parameters.end())
-                        cond=false;
-                    else if (e && e->getProperty(key.c_str()))
-                        cond=false;
-                }
+                if (visible)
+                    cond = !(parameters.getParameter(key.c_str()) || (e && e->getProperty(key.c_str())));
                 lastpos = thispos + 1; // strlen(">")
                 process(cond, buf, lastpos, os, parameters, e);
             }
@@ -211,7 +197,7 @@ void TemplateEngine::process(
         os << buf.substr(lastpos-line);
 }
 
-void TemplateEngine::run(istream& is, ostream& os, const map<string,string>& parameters, const XMLToolingException* e) const
+void TemplateEngine::run(istream& is, ostream& os, const TemplateParameters& parameters, const XMLToolingException* e) const
 {
     string buf,line;
     while (getline(is, line))
index 5183ff7..9bfec24 100644 (file)
@@ -61,18 +61,43 @@ namespace xmltooling {
         void setTagPrefix(const char* tagPrefix);
         
         /**
+         * Interface to parameters to plug into templates.
+         * Allows callers to supply a more dynamic lookup mechanism to supplement a basic map.
+         */
+        class XMLTOOL_API TemplateParameters {
+            MAKE_NONCOPYABLE(TemplateParameters);
+        public:
+            TemplateParameters() {}
+            virtual ~TemplateParameters() {}
+            
+            /** Map of known parameters to supply to template. */
+            std::map<std::string,std::string> m_map;
+            
+            /**
+             * Returns the value of a parameter to plug into the template.
+             * 
+             * @param name  name of parameter
+             * @return value of parameter, or NULL
+             */
+            virtual const char* getParameter(const char* name) const {
+                std::map<std::string,std::string>::const_iterator i=m_map.find(name);
+                return (i!=m_map.end() ? i->second.c_str() : NULL); 
+            }
+        };
+        
+        /**
          * Processes template from an input stream and executes replacements and
          * conditional logic based on parameters. 
          * 
          * @param is            input stream providing template
          * @param os            output stream to send results of executing template
-         * @param parameters    name/value parameters to plug into template
+         * @param parameters    parameters to plug into template
          * @param e             optional exception to extract parameters from
          */
         virtual void run(
             std::istream& is,
             std::ostream& os,
-            const std::map<std::string,std::string>& parameters,
+            const TemplateParameters& parameters,
             const XMLToolingException* e=NULL
             ) const;
 
@@ -84,7 +109,7 @@ namespace xmltooling {
             const std::string& buf,
             const char*& lastpos,
             std::ostream& os,
-            const std::map<std::string,std::string>& parameters,
+            const TemplateParameters& parameters,
             const XMLToolingException* e
             ) const;