From eb8c662c4c8ec56c396e4025972cc8d25ae57c12 Mon Sep 17 00:00:00 2001 From: Guenter Obiltschnig <guenter.obiltschnig@appinf.com> Date: Mon, 15 Oct 2012 12:27:56 +0000 Subject: [PATCH] code cleanups --- JSON/include/Poco/JSON/Array.h | 11 ++- JSON/include/Poco/JSON/DefaultHandler.h | 2 + JSON/include/Poco/JSON/Handler.h | 5 +- JSON/include/Poco/JSON/JSONException.h | 3 + JSON/include/Poco/JSON/Object.h | 14 +-- JSON/include/Poco/JSON/Parser.h | 38 ++++---- JSON/include/Poco/JSON/Stringifier.h | 7 +- JSON/include/Poco/JSON/Template.h | 112 +++++++++++------------- JSON/include/Poco/JSON/TemplateCache.h | 12 ++- JSON/src/Array.cpp | 6 +- JSON/src/DefaultHandler.cpp | 3 +- JSON/src/Handler.cpp | 12 +-- JSON/src/JSONException.cpp | 6 +- JSON/src/Object.cpp | 6 +- JSON/src/Parser.cpp | 18 ++-- JSON/src/Query.cpp | 9 +- JSON/src/Stringifier.cpp | 4 +- JSON/src/Template.cpp | 45 +++------- JSON/src/TemplateCache.cpp | 13 +-- 19 files changed, 151 insertions(+), 175 deletions(-) diff --git a/JSON/include/Poco/JSON/Array.h b/JSON/include/Poco/JSON/Array.h index 98477b0dd..a68e27ead 100644 --- a/JSON/include/Poco/JSON/Array.h +++ b/JSON/include/Poco/JSON/Array.h @@ -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 diff --git a/JSON/include/Poco/JSON/DefaultHandler.h b/JSON/include/Poco/JSON/DefaultHandler.h index 2801dfa8a..84b65e123 100644 --- a/JSON/include/Poco/JSON/DefaultHandler.h +++ b/JSON/include/Poco/JSON/DefaultHandler.h @@ -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 diff --git a/JSON/include/Poco/JSON/Handler.h b/JSON/include/Poco/JSON/Handler.h index d1e31d560..9bbb7d185 100644 --- a/JSON/include/Poco/JSON/Handler.h +++ b/JSON/include/Poco/JSON/Handler.h @@ -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 diff --git a/JSON/include/Poco/JSON/JSONException.h b/JSON/include/Poco/JSON/JSONException.h index d9eefea23..4ba70b671 100644 --- a/JSON/include/Poco/JSON/JSONException.h +++ b/JSON/include/Poco/JSON/JSONException.h @@ -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 diff --git a/JSON/include/Poco/JSON/Object.h b/JSON/include/Poco/JSON/Object.h index 012dcd35b..0b243aa0f 100644 --- a/JSON/include/Poco/JSON/Object.h +++ b/JSON/include/Poco/JSON/Object.h @@ -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 diff --git a/JSON/include/Poco/JSON/Parser.h b/JSON/include/Poco/JSON/Parser.h index 6ef1c7c12..be908f10b 100644 --- a/JSON/include/Poco/JSON/Parser.h +++ b/JSON/include/Poco/JSON/Parser.h @@ -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; diff --git a/JSON/include/Poco/JSON/Stringifier.h b/JSON/include/Poco/JSON/Stringifier.h index d9c9f0fe4..f7ef4ca89 100644 --- a/JSON/include/Poco/JSON/Stringifier.h +++ b/JSON/include/Poco/JSON/Stringifier.h @@ -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 diff --git a/JSON/include/Poco/JSON/Template.h b/JSON/include/Poco/JSON/Template.h index 117f4fae5..26a8480da 100644 --- a/JSON/include/Poco/JSON/Template.h +++ b/JSON/include/Poco/JSON/Template.h @@ -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 diff --git a/JSON/include/Poco/JSON/TemplateCache.h b/JSON/include/Poco/JSON/TemplateCache.h index 72d55e712..c1b23da7f 100644 --- a/JSON/include/Poco/JSON/TemplateCache.h +++ b/JSON/include/Poco/JSON/TemplateCache.h @@ -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; diff --git a/JSON/src/Array.cpp b/JSON/src/Array.cpp index 0782db243..434ab80e5 100644 --- a/JSON/src/Array.cpp +++ b/JSON/src/Array.cpp @@ -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 diff --git a/JSON/src/DefaultHandler.cpp b/JSON/src/DefaultHandler.cpp index 129a5c3f8..de5b216b2 100644 --- a/JSON/src/DefaultHandler.cpp +++ b/JSON/src/DefaultHandler.cpp @@ -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 diff --git a/JSON/src/Handler.cpp b/JSON/src/Handler.cpp index 6d9454f28..47e17fe93 100644 --- a/JSON/src/Handler.cpp +++ b/JSON/src/Handler.cpp @@ -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 diff --git a/JSON/src/JSONException.cpp b/JSON/src/JSONException.cpp index 151df49d5..4612b268c 100644 --- a/JSON/src/JSONException.cpp +++ b/JSON/src/JSONException.cpp @@ -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 diff --git a/JSON/src/Object.cpp b/JSON/src/Object.cpp index 01b750122..0ada7d6dc 100644 --- a/JSON/src/Object.cpp +++ b/JSON/src/Object.cpp @@ -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 diff --git a/JSON/src/Parser.cpp b/JSON/src/Parser.cpp index be16e0dd4..3ef5fd419 100644 --- a/JSON/src/Parser.cpp +++ b/JSON/src/Parser.cpp @@ -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 diff --git a/JSON/src/Query.cpp b/JSON/src/Query.cpp index ab9df0fa6..93cda2248 100644 --- a/JSON/src/Query.cpp +++ b/JSON/src/Query.cpp @@ -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 diff --git a/JSON/src/Stringifier.cpp b/JSON/src/Stringifier.cpp index 6c3834236..e65cd2953 100644 --- a/JSON/src/Stringifier.cpp +++ b/JSON/src/Stringifier.cpp @@ -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 diff --git a/JSON/src/Template.cpp b/JSON/src/Template.cpp index 70d8ef2af..e24abeb3e 100644 --- a/JSON/src/Template.cpp +++ b/JSON/src/Template.cpp @@ -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 diff --git a/JSON/src/TemplateCache.cpp b/JSON/src/TemplateCache.cpp index 409431792..29044c1f5 100644 --- a/JSON/src/TemplateCache.cpp +++ b/JSON/src/TemplateCache.cpp @@ -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