PageCompiler: support <%@ include file="<path>" %> syntax for includes.

This commit is contained in:
Günter Obiltschnig 2017-09-08 20:51:05 +02:00
parent 3e5cf21cf3
commit b7aaaabdcb
2 changed files with 30 additions and 25 deletions

View File

@ -4,8 +4,8 @@ POCO PageCompiler
!!!Introduction !!!Introduction
PageCompiler is a command line tool that translates HTML files (and other kinds of files) into PageCompiler is a command line tool that translates HTML files (and other kinds of files) into
C++ code, more precisely, subclasses of Poco::Net::HTTPRequestHandler. C++ code, more precisely, subclasses of Poco::Net::HTTPRequestHandler.
The source files can contain special directives that allow embedding of C++ code. The source files can contain special directives that allow embedding of C++ code.
The syntax of these directives is based on the syntax used for The syntax of these directives is based on the syntax used for
Java Server Pages (JSP) and Active Server Pages (ASP). Java Server Pages (JSP) and Active Server Pages (ASP).
The following introductory sample shows the code for a simple page that displays the The following introductory sample shows the code for a simple page that displays the
@ -16,13 +16,13 @@ current date and time.
#include "Poco/DateTime.h" #include "Poco/DateTime.h"
#include "Poco/DateTimeFormatter.h" #include "Poco/DateTimeFormatter.h"
#include "Poco/DateTimeFormat.h" #include "Poco/DateTimeFormat.h"
using Poco::DateTime; using Poco::DateTime;
using Poco::DateTimeFormatter; using Poco::DateTimeFormatter;
using Poco::DateTimeFormat; using Poco::DateTimeFormat;
%> %>
<% <%
DateTime now; DateTime now;
std::string dt(DateTimeFormatter::format(now, DateTimeFormat::SORTABLE_FORMAT)); std::string dt(DateTimeFormatter::format(now, DateTimeFormat::SORTABLE_FORMAT));
@ -36,14 +36,14 @@ current date and time.
<p><%= dt %></p> <p><%= dt %></p>
</body> </body>
</html> </html>
---- ----
Sending the above code to the page compiler will generate two files, a header file Sending the above code to the page compiler will generate two files, a header file
(<[TimeHandler.h]>) and an implementation file (<[TimeHandler.cpp]>). (<[TimeHandler.h]>) and an implementation file (<[TimeHandler.cpp]>).
The files define a subclass of Poco::Net::HTTPRequestHandler named <[TimeHandler]>. The files define a subclass of Poco::Net::HTTPRequestHandler named <[TimeHandler]>.
The generated <[handleRequest]> member function contains code to send the HTML The generated <[handleRequest]> member function contains code to send the HTML
code contained in the source file to the client, as well as the C++ code fragments found code contained in the source file to the client, as well as the C++ code fragments found
in between the Scriptlet tags. in between the Scriptlet tags.
!!!C++ Server Page Syntax !!!C++ Server Page Syntax
@ -124,12 +124,17 @@ Example:
return response.redirect("/"); return response.redirect("/");
} }
%> %>
!!Include Directive !!Include Directive
Another CPSP file can be included into the current file using the Include Another CPSP file can be included into the current file using the Include
Directive. Directive.
<%@ include file="<path>" %>
----
Alternatively, this can also be written as:
<%@ include page="<path>" %> <%@ include page="<path>" %>
---- ----
@ -140,7 +145,7 @@ Include a C++ header file in the generated header file.
This corresponds to: This corresponds to:
<%!! #include "<path>" %> <%!! #include "<path>" %>
A variant of this directive is: A variant of this directive is:
<%@ header sinclude="<path>" %> <%@ header sinclude="<path>" %>
@ -155,7 +160,7 @@ Include a C++ header file in the generated implementation file.
This corresponds to: This corresponds to:
<%! #include "<path>" %> <%! #include "<path>" %>
A variant of this directive is: A variant of this directive is:
<%@ impl sinclude="<path>" %> <%@ impl sinclude="<path>" %>
@ -185,7 +190,7 @@ No namespace will be used if omitted.
!baseClass !baseClass
Specifies the name of the class used as the base class for the generated Specifies the name of the class used as the base class for the generated
request handler class. request handler class.
Defaults to Poco::Net::HTTPRequestHandler. Do not forget to add a Header Declaration Defaults to Poco::Net::HTTPRequestHandler. Do not forget to add a Header Declaration
containing an <[#include]> directive for the header file containing the definition containing an <[#include]> directive for the header file containing the definition
@ -204,7 +209,7 @@ Cannot be used together with <[ctorArg]>.
Allows to specify the type of a single argument being passed to the constructor Allows to specify the type of a single argument being passed to the constructor
of the generated request handler class. Can only be used together with <[baseClass]>. of the generated request handler class. Can only be used together with <[baseClass]>.
The argument is passed on to the constructor of the base class, therefore, one of the The argument is passed on to the constructor of the base class, therefore, one of the
constructors of the base class must also accept a single argument of the specified type. constructors of the base class must also accept a single argument of the specified type.
Cannot be used together with <[context]>. Cannot be used together with <[context]>.
@ -224,9 +229,9 @@ Set the value to <[false]> to disable form handling.
!formPartHandler !formPartHandler
Allows you to pass a Poco::Net::PartHandler object to the form object for Allows you to pass a Poco::Net::PartHandler object to the form object for
processing file uploads. A subclass of Poco::Net::PartHandler must be processing file uploads. A subclass of Poco::Net::PartHandler must be
defined (using an Implementation Declaration), and the constructor of the part defined (using an Implementation Declaration), and the constructor of the part
handler must take a (const) reference to the request handler instance as argument. handler must take a (const) reference to the request handler instance as argument.
!contentType !contentType
@ -236,7 +241,7 @@ Allows you to specify the MIME content type for the page. Defaults to text/html.
!contentLanguage !contentLanguage
Allows to specify a language tag (e.g., "en") that will be sent in the Allows to specify a language tag (e.g., "en") that will be sent in the
response Content-Language header if the client sends an Accept-Language response Content-Language header if the client sends an Accept-Language
header in the request. header in the request.
!chunked !chunked
@ -250,7 +255,7 @@ Set the value to <[false]> to disable chunked transfer encoding.
Enables or disables response body compression. If set to <[true]>, and the client supports Enables or disables response body compression. If set to <[true]>, and the client supports
the "gzip" content encoding (indicated by the "Accept-Encoding" header), the "gzip" content encoding (indicated by the "Accept-Encoding" header),
the response body will be compressed using the "gzip" format and the the response body will be compressed using the "gzip" format and the
"Content-Encoding" header will be set accordingly. "Content-Encoding" header will be set accordingly.
Defaults to <[false]>. Cannot be enabled together with response buffering. Defaults to <[false]>. Cannot be enabled together with response buffering.
!compressionLevel !compressionLevel
@ -264,7 +269,7 @@ Enables or disables response buffering. Response buffering is disabled by defaul
Set to <[true]> to enable buffering, or to <[false]> to disable it. Set to <[true]> to enable buffering, or to <[false]> to disable it.
If response buffering is enabled, everything written to the response stream If response buffering is enabled, everything written to the response stream
is actually written to a string stream (<[std::ostringstream]>). is actually written to a string stream (<[std::ostringstream]>).
Sending of the HTTP response back to the client is deferred to Sending of the HTTP response back to the client is deferred to
when the page is complete. when the page is complete.
!session (OSP only) !session (OSP only)
@ -290,10 +295,10 @@ and the timeout value is read from the respective bundle property.
For use with the POCO Open Service Platform only. For use with the POCO Open Service Platform only.
If set to <[true]>, which is the default if the attribute is not specified, If set to <[true]>, which is the default if the attribute is not specified,
a new session will be created if the request does not contain a (valid) session cookie. a new session will be created if the request does not contain a (valid) session cookie.
If set to <[false]> and there is no existing session that matches the session If set to <[false]> and there is no existing session that matches the session
cookie. the <[session]> variable will be null. cookie. the <[session]> variable will be null.
!precondition !precondition
@ -368,7 +373,7 @@ system (e.g., <[/help]> on Windows, <[--help]> or <[-h]> on Unix).
!!Configuration Properties !!Configuration Properties
The Page Compiler supports one configuration property, named The Page Compiler supports one configuration property, named
<[PageCompiler.fileHeader]>, to optionally specify a header that is <[PageCompiler.fileHeader]>, to optionally specify a header that is
included in every generated file. included in every generated file.
The file header can contain references to other configuration properties, The file header can contain references to other configuration properties,

View File

@ -309,7 +309,7 @@ void PageReader::nextToken(std::istream& istr, std::string& token)
void PageReader::handleAttribute(const std::string& name, const std::string& value) void PageReader::handleAttribute(const std::string& name, const std::string& value)
{ {
if (name == "include.page") if (name == "include.page" || name == "include.file")
{ {
include(value); include(value);
} }
@ -341,14 +341,14 @@ void PageReader::include(const std::string& path)
Poco::Path currentPath(_path); Poco::Path currentPath(_path);
Poco::Path includePath(path); Poco::Path includePath(path);
currentPath.resolve(includePath); currentPath.resolve(includePath);
_page.handler() << "\t// begin include " << currentPath.toString() << "\n"; _page.handler() << "\t// begin include " << currentPath.toString() << "\n";
Poco::FileInputStream includeStream(currentPath.toString()); Poco::FileInputStream includeStream(currentPath.toString());
PageReader includeReader(*this, currentPath.toString()); PageReader includeReader(*this, currentPath.toString());
includeReader.emitLineDirectives(_emitLineDirectives); includeReader.emitLineDirectives(_emitLineDirectives);
includeReader.parse(includeStream); includeReader.parse(includeStream);
_page.handler() << "\t// end include " << currentPath.toString() << "\n"; _page.handler() << "\t// end include " << currentPath.toString() << "\n";
} }