1
0
mirror of https://github.com/pocoproject/poco.git synced 2025-03-23 23:49:26 +01:00

code cleanups

This commit is contained in:
Guenter Obiltschnig 2012-10-15 12:27:56 +00:00
parent ac8c9ad1ca
commit eb8c662c4c
19 changed files with 151 additions and 175 deletions

@ -35,27 +35,28 @@
// DEALINGS IN THE SOFTWARE.
//
#ifndef JSON_Array_INCLUDED
#define JSON_Array_INCLUDED
#include <vector>
#include <sstream>
#include "Poco/JSON/JSON.h"
#include "Poco/SharedPtr.h"
#include "Poco/Dynamic/Var.h"
#include <vector>
#include <sstream>
namespace Poco {
namespace JSON {
class Object;
class JSON_API Array
{
public:
typedef std::vector<Dynamic::Var> ValueVector;
typedef SharedPtr<Array> Ptr;
@ -199,6 +200,7 @@ inline void Array::remove(unsigned int index)
namespace Poco {
namespace Dynamic {
template <>
class VarHolderImpl<JSON::Array::Ptr>: public VarHolder
{
@ -337,6 +339,7 @@ private:
JSON::Array::Ptr _val;
};
}} // namespace Poco::JSON

@ -35,6 +35,7 @@
// DEALINGS IN THE SOFTWARE.
//
#ifndef JSON_DefaultHandler_INCLUDED
#define JSON_DefaultHandler_INCLUDED
@ -154,4 +155,5 @@ inline void DefaultHandler::null()
}} // namespace Poco::JSON
#endif // JSON_DefaultHandler_INCLUDED

@ -35,12 +35,14 @@
// DEALINGS IN THE SOFTWARE.
//
#ifndef JSON_Handler_INCLUDED
#define JSON_Handler_INCLUDED
#include "Poco/Dynamic/Var.h"
#include "Poco/JSON/JSON.h"
#include "Poco/Dynamic/Var.h"
namespace Poco {
namespace JSON {
@ -95,4 +97,5 @@ private:
}} // namespace Poco::JSON
#endif // JSON_Handler_INCLUDED

@ -35,6 +35,7 @@
// DEALINGS IN THE SOFTWARE.
//
#ifndef JSON_JSONException_INCLUDED
#define JSON_JSONException_INCLUDED
@ -46,8 +47,10 @@
namespace Poco {
namespace JSON {
POCO_DECLARE_EXCEPTION(JSON_API, JSONException, Poco::Exception)
}} // namespace Poco::JSON

@ -35,30 +35,29 @@
// DEALINGS IN THE SOFTWARE.
//
#ifndef JSON_Object_INCLUDED
#define JSON_Object_INCLUDED
#include "Poco/JSON/JSON.h"
#include "Poco/JSON/Array.h"
#include "Poco/SharedPtr.h"
#include "Poco/Dynamic/Var.h"
#include <map>
#include <vector>
#include <iostream>
#include <sstream>
#include "Poco/SharedPtr.h"
#include "Poco/Dynamic/Var.h"
#include "Poco/JSON/JSON.h"
#include "Poco/JSON/Array.h"
namespace Poco {
namespace JSON {
class JSON_API Object
/// Represents a JSON object
/// Represents a JSON object.
{
public:
typedef SharedPtr<Object> Ptr;
Object();
@ -344,6 +343,7 @@ private:
JSON::Object::Ptr _val;
};
}} // namespace Poco::JSON

@ -35,68 +35,66 @@
// DEALINGS IN THE SOFTWARE.
//
#ifndef JSON_JSONParser_INCLUDED
#define JSON_JSONParser_INCLUDED
#include <istream>
#include <sstream>
#include "Poco/Dynamic/Var.h"
#include "Poco/StreamTokenizer.h"
#include "Poco/JSON/JSON.h"
#include "Poco/JSON/Object.h"
#include "Poco/JSON/Array.h"
#include "Poco/JSON/Handler.h"
#include "Poco/Dynamic/Var.h"
#include "Poco/StreamTokenizer.h"
#include <istream>
#include <sstream>
namespace Poco {
namespace JSON {
class JSON_API Parser
/// A class for passing JSON strings or streams
/// A class for passing JSON strings or streams.
{
public:
Parser();
/// Constructor
/// Constructor.
virtual ~Parser();
/// Destructor
/// Destructor.
void parse(const std::string& source);
/// Parses a string
/// Parses a string.
void parse(std::istream& in);
/// Parses a JSON from the input stream
/// Parses a JSON from the input stream.
void setHandler(Handler* handler);
/// Set the handler
/// Set the handler.
Handler* getHandler();
/// Returns the handler
/// Returns the handler.
private:
const Token* nextToken();
/// Returns the next token
/// Returns the next token.
void readObject();
/// Starts reading an object
/// Starts reading an object.
void readArray();
/// Starts reading an array
/// Starts reading an array.
bool readRow(bool firstCall = false);
/// Reads a property value pair. Returns true when a next row is expected.
void readValue(const Token* token);
/// Read a value from the token
/// Read a value from the token.
bool readElements(bool firstCall = false);
/// Read all elements of an array
/// Read all elements of an array.
StreamTokenizer _tokenizer;
Handler* _handler;

@ -35,14 +35,15 @@
// DEALINGS IN THE SOFTWARE.
//
#ifndef JSON_JSONStringifier_INCLUDED
#define JSON_JSONStringifier_INCLUDED
#include <iostream>
#include "Poco/Dynamic/Var.h"
#include "Poco/JSON/JSON.h"
#include <ostream>
namespace Poco {
namespace JSON {
@ -52,7 +53,6 @@ class JSON_API Stringifier
/// Helper class for creating a String from a JSON object or array
{
public:
static void stringify(const Dynamic::Var& any, std::ostream& out, unsigned int indent = 0);
/// Writes a String representation of the value to the output stream.
/// When indent is 0, the String will be created as small as possible.
@ -61,4 +61,5 @@ public:
}} // namespace Poco::JSON
#endif // JSON_JSONStringifier_INCLUDED

@ -39,14 +39,13 @@
#define JSON_JSONTemplate_INCLUDED
#include <sstream>
#include <stack>
#include "Poco/JSON/JSON.h"
#include "Poco/Dynamic/Var.h"
#include "Poco/SharedPtr.h"
#include "Poco/Path.h"
#include "Poco/Timestamp.h"
#include <sstream>
#include <stack>
namespace Poco {
@ -66,106 +65,91 @@ class JSON_API Template
/// conditional output, ...
///
/// All text is send to the outputstream. A command is placed
/// between <? and ?>.
/// between
/// <?
/// and
/// ?>
/// ----
///
/// These are the available commands:
/// <? echo query ?>
/// The result of the query is send to the output stream
/// This command can also be written as <?= query ?>
/// <? if query ?> <? else ?> <? endif ?>
/// When the result of query is true, all the text between
/// if and else (or endif when there is no else) is send to the
/// output stream. When the result of query is false, all the text
/// between else and endif is send to the output stream. An empty
/// object, an empty array or a null value is considered as a false value.
/// For numbers a zero is false. An empty String is also false.
/// <? ifexist query ?> <? else ?> <? endif ?>
/// This can be used to check the existance of the value.
/// Use this for example when a zero value is ok (which returns false for <? if ?>.
/// <? for variable query ?> <? endfor ?>
/// The result of the query must be an array. For each element
/// in the array the text between for and endfor is send to the
/// output stream. The active element is stored in the variable.
/// <? include "filename" ?>
/// Includes a template. When the filename is relative it will try
/// to resolve the filename against the active template. When this
/// file doesn't exist, it can still be found when the JSONTemplateCache
/// is used.
///
/// <? echo query ?>
/// ----
/// The result of the query is send to the output stream
/// This command can also be written as <?= query ?>
///
/// <? if query ?> <? else ?> <? endif ?>
/// ----
/// When the result of query is true, all the text between
/// if and else (or endif when there is no else) is send to the
/// output stream. When the result of query is false, all the text
/// between else and endif is send to the output stream. An empty
/// object, an empty array or a null value is considered as a false value.
/// For numbers a zero is false. An empty String is also false.
///
/// <? ifexist query ?> <? else ?> <? endif ?>
/// ----
/// This can be used to check the existance of the value.
/// Use this for example when a zero value is ok (which returns false for <? if ?>.
///
/// <? for variable query ?> <? endfor ?>
/// ----
/// The result of the query must be an array. For each element
/// in the array the text between for and endfor is send to the
/// output stream. The active element is stored in the variable.
///
/// <? include "filename" ?>
/// ----
/// Includes a template. When the filename is relative it will try
/// to resolve the filename against the active template. When this
/// file doesn't exist, it can still be found when the JSONTemplateCache
/// is used.
///
/// A query is passed to Poco::JSON::Query to get the value.
{
public:
typedef SharedPtr<Template> Ptr;
Template();
/// Constructor
/// Constructor.
Template(const Path& templatePath);
/// Constructor. Creates a template from a file.
virtual ~Template();
/// Destructor
/// Destructor.
void parse();
/// Parse a template from a file
/// Parse a template from a file.
void parse(const std::string& source);
/// Parse a template from a String
/// Parse a template from a string.
void parse(std::istream& in);
/// Parse a template from a input stream
/// Parse a template from a input stream.
Timestamp parseTime() const;
/// Returns the time when the template was parsed
/// Returns the time when the template was parsed.
void render(const Dynamic::Var& data, std::ostream& out) const;
/// Renders the template and send the output to the stream
/// Renders the template and send the output to the stream.
private:
std::string readText(std::istream& in);
std::string readWord(std::istream& in);
std::string readQuery(std::istream& in);
std::string readTemplateCommand(std::istream& in);
std::string readString(std::istream& in);
void readWhiteSpace(std::istream& in);
MultiPart* _parts;
std::stack<MultiPart*> _partStack;
MultiPart* _currentPart;
Path _templatePath;
Timestamp _parseTime;
};
inline void Template::parse(const std::string& source)
{
std::istringstream is(source);
@ -178,6 +162,8 @@ inline Timestamp Template::parseTime() const
return _parseTime;
}
}} // namespace Poco::JSON
#endif // JSON_JSONTemplate_INCLUDED

@ -39,13 +39,13 @@
#define JSON_JSONTemplateCache_INCLUDED
#include <vector>
#include <map>
#include "Poco/JSON/JSON.h"
#include "Poco/JSON/Template.h"
#include "Poco/Path.h"
#include "Poco/SharedPtr.h"
#include "Poco/Logger.h"
#include "Poco/JSON/Template.h"
#include <vector>
#include <map>
namespace Poco {
@ -60,13 +60,12 @@ class JSON_API TemplateCache
/// and load a new one.
{
public:
TemplateCache();
/// Constructor. The cache must be created
/// and not destroyed as long as it is used.
virtual ~TemplateCache();
/// Destructor
/// Destructor.
void addPath(const Path& path);
/// Add a path for resolving template paths.
@ -88,7 +87,6 @@ public:
/// Sets the logger for the cache.
private:
static TemplateCache* _instance;
std::vector<Path> _includePaths;
std::map<std::string, Template::Ptr> _cache;

@ -33,6 +33,7 @@
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/JSON/Array.h"
#include "Poco/JSON/Object.h"
#include "Poco/JSON/Stringifier.h"
@ -47,19 +48,16 @@ namespace JSON {
Array::Array()
{
}
Array::Array(const Array& copy) : _values(copy._values)
{
}
Array::~Array()
{
}
@ -153,4 +151,4 @@ void Array::stringify(std::ostream& out, unsigned int indent) const
}
}} // Namespace Poco::JSON
} } // namespace Poco::JSON

@ -33,6 +33,7 @@
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/JSON/DefaultHandler.h"
#include "Poco/JSON/Object.h"
@ -151,4 +152,4 @@ void DefaultHandler::setValue(const Var& value)
}
}} // Namespace Poco::JSON
} } // namespace Poco::JSON

@ -33,16 +33,18 @@
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/JSON/Handler.h"
#include "Poco/JSON/Object.h"
namespace Poco
{
namespace JSON
{
namespace Poco {
namespace JSON {
Handler::~Handler()
{
}
}} // Namespace Poco::JSON
} } // namespace Poco::JSON

@ -33,12 +33,16 @@
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/JSON/JSONException.h"
#include <typeinfo>
namespace Poco {
namespace JSON {
POCO_IMPLEMENT_EXCEPTION(JSONException, Exception, "JSON Exception")
}} // Namespace Poco::JSON
} } // Namespace Poco::JSON

@ -33,10 +33,10 @@
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/JSON/Object.h"
#include "Poco/JSON/Array.h"
#include "Poco/JSON/Stringifier.h"
#include <iostream>
#include <sstream>
@ -153,7 +153,7 @@ void Object::stringify(std::ostream& out, unsigned int indent) const
}
out << '}';
}
}} // Namespace Poco::JSON
} } // namespace Poco::JSON

@ -33,19 +33,18 @@
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/Ascii.h"
#include "Poco/Token.h"
#include "Poco/JSON/Parser.h"
#include "Poco/JSON/JSONException.h"
#include "Poco/Ascii.h"
#include "Poco/Token.h"
#undef min
#undef max
#include <limits>
namespace Poco
{
namespace JSON
{
namespace Poco {
namespace JSON {
class SeparatorToken: public Token
@ -90,6 +89,7 @@ public:
}
};
class StringToken: public Token
{
public:
@ -243,6 +243,7 @@ public:
}
};
class KeywordToken : public Token
{
public:
@ -281,6 +282,7 @@ public:
}
};
class NumberToken: public Token
{
public:
@ -418,9 +420,7 @@ public:
}
private:
Class _activeClass;
};
@ -697,4 +697,4 @@ bool Parser::readElements(bool firstCall)
throw JSONException(format("Invalid token '%s' found.", token->asString()));
}
}}
} } // namespace Poco::JSON

@ -33,13 +33,12 @@
// DEALINGS IN THE SOFTWARE.
//
#include <sstream>
#include "Poco/JSON/Query.h"
#include "Poco/StringTokenizer.h"
#include "Poco/RegularExpression.h"
#include "Poco/NumberParser.h"
#include "Poco/JSON/Query.h"
#include <sstream>
using Poco::Dynamic::Var;
@ -49,7 +48,7 @@ namespace Poco {
namespace JSON {
Query::Query(const Var& source) : _source(source)
Query::Query(const Var& source): _source(source)
{
}
@ -145,4 +144,4 @@ Var Query::find(const std::string& path) const
}
}} // Namespace Poco::JSON
} } // Namespace Poco::JSON

@ -33,11 +33,11 @@
// DEALINGS IN THE SOFTWARE.
//
#include <iomanip>
#include "Poco/JSON/Stringifier.h"
#include "Poco/JSON/Array.h"
#include "Poco/JSON/Object.h"
#include <iomanip>
using Poco::Dynamic::Var;
@ -115,4 +115,4 @@ void Stringifier::stringify(const Var& any, std::ostream& out, unsigned int inde
}
}} // Namespace Poco::JSON
} } // Namespace Poco::JSON

@ -32,11 +32,13 @@
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/File.h"
#include "Poco/FileStream.h"
#include "Poco/JSON/Template.h"
#include "Poco/JSON/TemplateCache.h"
#include "Poco/JSON/Query.h"
#include "Poco/File.h"
#include "Poco/FileStream.h"
using Poco::Dynamic::Var;
@ -52,12 +54,10 @@ POCO_IMPLEMENT_EXCEPTION(JSONTemplateException, Exception, "Template Exception")
class Part
{
public:
Part()
{
}
virtual ~Part()
{
}
@ -68,10 +68,9 @@ public:
};
class StringPart : public Part
class StringPart: public Part
{
public:
StringPart() : Part()
{
}
@ -80,57 +79,46 @@ public:
{
}
virtual ~StringPart()
{
}
void render(const Var& data, std::ostream& out) const
{
out << _content;
}
void setContent(const std::string& content)
{
_content = content;
}
inline std::string getContent() const
{
return _content;
}
private:
std::string _content;
};
class MultiPart : public Part
class MultiPart: public Part
{
public:
MultiPart()
{
}
virtual ~MultiPart()
{
}
virtual void addPart(Part* part)
{
_parts.push_back(part);
}
void render(const Var& data, std::ostream& out) const
{
for(VectorParts::const_iterator it = _parts.begin(); it != _parts.end(); ++it)
@ -140,15 +128,13 @@ public:
}
protected:
VectorParts _parts;
};
class EchoPart : public Part
class EchoPart: public Part
{
public:
EchoPart(const std::string& query) : Part(), _query(query)
{
}
@ -157,7 +143,6 @@ public:
{
}
void render(const Var& data, std::ostream& out) const
{
Query query(data);
@ -170,7 +155,6 @@ public:
}
private:
std::string _query;
};
@ -261,7 +245,6 @@ public:
class LogicPart : public MultiPart
{
public:
LogicPart() : MultiPart()
{
}
@ -296,22 +279,19 @@ public:
}
private:
std::vector<SharedPtr<LogicQuery> > _queries;
};
class LoopPart : public MultiPart
class LoopPart: public MultiPart
{
public:
LoopPart(const std::string& name, const std::string& query) : MultiPart(), _name(name), _query(query)
{
}
virtual ~LoopPart()
{
}
void render(const Var& data, std::ostream& out) const
@ -336,14 +316,12 @@ public:
}
private:
std::string _name;
std::string _query;
};
class IncludePart : public Part
class IncludePart: public Part
{
public:
@ -370,7 +348,6 @@ public:
{
}
void render(const Var& data, std::ostream& out) const
{
TemplateCache* cache = TemplateCache::instance();
@ -388,9 +365,7 @@ public:
}
private:
Path _path;
};
@ -748,4 +723,4 @@ void Template::render(const Var& data, std::ostream& out) const
}
}} // Namespace Poco::JSON
} } // Namespace Poco::JSON

@ -33,16 +33,18 @@
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/File.h"
#include "Poco/JSON/TemplateCache.h"
namespace Poco
{
namespace JSON
{
namespace Poco {
namespace JSON {
TemplateCache* TemplateCache::_instance = NULL;
TemplateCache::TemplateCache() : _logger(NULL)
{
setup();
@ -169,4 +171,5 @@ Path TemplateCache::resolvePath(const Path& path) const
throw FileNotFoundException(path.toString());
}
}} // Poco::JSON
} } // Poco::JSON