Rework support for libcurl-based input to parser.
[shibboleth/cpp-xmltooling.git] / xmltooling / util / TemplateEngine.h
index 5183ff7..bbbcf18 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2006 Internet2
+ *  Copyright 2001-2007 Internet2
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #ifndef __xmltooling_template_h__
 #define __xmltooling_template_h__
 
-#include <xmltooling/base.h>
+#include <xmltooling/io/GenericRequest.h>
 
 #include <map>
 #include <string>
 #include <iostream>
 
+#if defined (_MSC_VER)
+    #pragma warning( push )
+    #pragma warning( disable : 4251 )
+#endif
+
 namespace xmltooling {
 
     /**
@@ -61,18 +66,46 @@ 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() : m_request(NULL) {}
+            virtual ~TemplateParameters() {}
+            
+            /** Map of known parameters to supply to template. */
+            std::map<std::string,std::string> m_map;
+            
+            /** Request from client that resulted in template being processed. */
+            const GenericRequest* m_request;
+            
+            /**
+             * 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() : (m_request ? m_request->getParameter(name) : 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 +117,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;
             
@@ -92,4 +125,8 @@ namespace xmltooling {
     };
 };
 
+#if defined (_MSC_VER)
+    #pragma warning( pop )
+#endif
+
 #endif /* __xmltooling_template_h__ */