added File2Page

This commit is contained in:
Guenter Obiltschnig
2009-05-19 18:36:53 +00:00
parent 697639134b
commit 593c44292f
2 changed files with 250 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
#
# Makefile
#
# $Id: //poco/1.3/PageCompiler/File2Page/Makefile#1 $
#
# Makefile for Poco C++ Server Page Compiler File2Page Utility
#
include $(POCO_BASE)/build/rules/global
objects = File2Page
target = f2cpsp
target_version = 1
target_libs = PocoUtil PocoXML PocoFoundation
include $(POCO_BASE)/build/rules/exec

View File

@@ -0,0 +1,233 @@
//
// File2Page.cpp
//
// $Id: //poco/1.3/PageCompiler/File2Page/src/File2Page.cpp#1 $
//
// An application that creates a Page Compiler source file from an
// ordinary file.
//
// Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/Util/Application.h"
#include "Poco/Util/Option.h"
#include "Poco/Util/OptionSet.h"
#include "Poco/Util/HelpFormatter.h"
#include "Poco/Util/AbstractConfiguration.h"
#include "Poco/AutoPtr.h"
#include "Poco/File.h"
#include "Poco/Path.h"
#include "Poco/FileStream.h"
#include "Poco/NumberFormatter.h"
#include <iostream>
using Poco::Util::Application;
using Poco::Util::Option;
using Poco::Util::OptionSet;
using Poco::Util::HelpFormatter;
using Poco::Util::AbstractConfiguration;
using Poco::Util::OptionCallback;
using Poco::NumberFormatter;
class File2PageApp: public Application
{
public:
File2PageApp(): _helpRequested(false)
{
}
protected:
void initialize(Application& self)
{
loadConfiguration(); // load default configuration files, if present
Application::initialize(self);
// add your own initialization code here
}
void uninitialize()
{
// add your own uninitialization code here
Application::uninitialize();
}
void reinitialize(Application& self)
{
Application::reinitialize(self);
// add your own reinitialization code here
}
void defineOptions(OptionSet& options)
{
Application::defineOptions(options);
options.addOption(
Option("help", "h", "display help information on command line arguments")
.required(false)
.repeatable(false)
.callback(OptionCallback<File2PageApp>(this, &File2PageApp::handleHelp)));
options.addOption(
Option("contentType", "t", "specify a content type")
.required(false)
.repeatable(false)
.argument("MIME-Type")
.callback(OptionCallback<File2PageApp>(this, &File2PageApp::handleContentType)));
options.addOption(
Option("class", "c", "specify the handler class name")
.required(false)
.repeatable(false)
.argument("class-name")
.callback(OptionCallback<File2PageApp>(this, &File2PageApp::handleClassName)));
options.addOption(
Option("output", "o", "specify the output file name")
.required(false)
.repeatable(false)
.argument("class-name")
.callback(OptionCallback<File2PageApp>(this, &File2PageApp::handleOutput)));
}
void handleHelp(const std::string& name, const std::string& value)
{
_helpRequested = true;
displayHelp();
stopOptionsProcessing();
}
void handleContentType(const std::string& name, const std::string& value)
{
_contentType = value;
}
void handleClassName(const std::string& name, const std::string& value)
{
_clazz = value;
}
void handleOutput(const std::string& name, const std::string& value)
{
_output = value;
}
void displayHelp()
{
HelpFormatter helpFormatter(options());
helpFormatter.setCommand(commandName());
helpFormatter.setUsage("OPTIONS");
helpFormatter.setHeader("Create a PageCompiler source file from a binary file.");
helpFormatter.format(std::cout);
}
void convert(const std::string& path)
{
Poco::Path p(path);
Poco::Path op(path);
if (_output.empty())
{
op.setExtension("cpsp");
}
else
{
op = _output;
}
if (_contentType.empty())
{
_contentType = extToContentType(p.getExtension());
}
if (_clazz.empty())
{
_clazz = p.getBaseName();
}
Poco::FileInputStream istr(path);
Poco::FileOutputStream ostr(op.toString());
ostr << "<%@ page\n"
<< " contentType=\"" << _contentType << "\"\n"
<< " form=\"false\"\n"
<< " class=\"" << _clazz << "\"\n%><%!\n";
ostr << "// " << path << "\n";
ostr << "static const char data[] = {\n\t";
int ch = istr.get();
int pos = 0;
while (ch != -1)
{
ostr << "0x" << NumberFormatter::formatHex(ch, 2) << ", ";
if (pos++ == 16)
{
ostr << "\n\t";
pos = 0;
}
ch = istr.get();
}
ostr << "\n};\n%><%\n";
ostr << "\tostr.write(data, sizeof(data));\n";
ostr << "%>";
}
std::string extToContentType(const std::string& ext)
{
if (ext == "jpg")
return "image/jpeg";
else if (ext == "png")
return "image/png";
else if (ext == "gif")
return "image/gif";
else if (ext == "htm")
return "text/html";
else if (ext == "html")
return "text/html";
else if (ext == "css")
return "text/css";
else if (ext == "js")
return "text/javascript";
else
return "application/binary";
}
int main(const std::vector<std::string>& args)
{
if (!_helpRequested)
{
for (std::vector<std::string>::const_iterator it = args.begin(); it != args.end(); ++it)
{
convert(*it);
}
}
return Application::EXIT_OK;
}
private:
bool _helpRequested;
std::string _contentType;
std::string _clazz;
std::string _output;
};
POCO_APP_MAIN(File2PageApp)