From 794051fa15a77a1455a4de6dd988516b6b5f392b Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Fri, 16 Dec 2022 02:48:07 +0100 Subject: [PATCH] add basic support for serializing objects to xml --- src/Makefile.dep | 41 +++++++++++ src/baseexpression.h | 76 +++++++++++++++++++ src/morm_types.h | 3 +- src/xmlconnector.cpp | 65 ++++++++++++++++ src/xmlconnector.h | 60 +++++++++++++++ src/xmlexpression.cpp | 167 ++++++++++++++++++++++++++++++++++++++++++ src/xmlexpression.h | 81 ++++++++++++++++++++ 7 files changed, 492 insertions(+), 1 deletion(-) create mode 100644 src/xmlconnector.cpp create mode 100644 src/xmlconnector.h create mode 100644 src/xmlexpression.cpp create mode 100644 src/xmlexpression.h diff --git a/src/Makefile.dep b/src/Makefile.dep index 21d5cf4..cfa181b 100644 --- a/src/Makefile.dep +++ b/src/Makefile.dep @@ -293,3 +293,44 @@ ./transaction.o: ../../pikotools/src/textstream/types.h ./transaction.o: ../../pikotools/src/log/filelog.h modelconnector.h clearer.h ./transaction.o: ft.h dbconnector.h queryresult.h flatconnector.h +./xmlconnector.o: xmlconnector.h flatconnector.h +./xmlconnector.o: ../../pikotools/src/textstream/textstream.h +./xmlconnector.o: ../../pikotools/src/textstream/stream.h +./xmlconnector.o: ../../pikotools/src/space/space.h +./xmlconnector.o: ../../pikotools/src/textstream/types.h +./xmlconnector.o: ../../pikotools/src/convert/inttostr.h +./xmlconnector.o: ../../pikotools/src/utf8/utf8.h +./xmlconnector.o: ../../pikotools/src/textstream/stream.h +./xmlconnector.o: ../../pikotools/src/utf8/utf8_templates.h +./xmlconnector.o: ../../pikotools/src/utf8/utf8_private.h +./xmlconnector.o: ../../pikotools/src/date/date.h +./xmlconnector.o: ../../pikotools/src/membuffer/membuffer.h +./xmlconnector.o: ../../pikotools/src/textstream/types.h xmlexpression.h +./xmlconnector.o: flatexpression.h baseexpression.h morm_types.h modelenv.h +./xmlconnector.o: modeldata.h cursorhelper.h queryresult.h +./xmlconnector.o: ../../pikotools/src/log/log.h +./xmlconnector.o: ../../pikotools/src/log/filelog.h finderhelper.h +./xmlconnector.o: fieldvaluehelper.h wrapper.h spacewrapper.h +./xmlconnector.o: baseobjectwrapper.h modelcontainerwrapper.h select.h ft.h +./xmlconnector.o: ../../pikotools/src/convert/text.h +./xmlexpression.o: xmlexpression.h flatexpression.h baseexpression.h +./xmlexpression.o: ../../pikotools/src/textstream/textstream.h +./xmlexpression.o: ../../pikotools/src/textstream/stream.h +./xmlexpression.o: ../../pikotools/src/space/space.h +./xmlexpression.o: ../../pikotools/src/textstream/types.h +./xmlexpression.o: ../../pikotools/src/convert/inttostr.h +./xmlexpression.o: ../../pikotools/src/utf8/utf8.h +./xmlexpression.o: ../../pikotools/src/textstream/stream.h +./xmlexpression.o: ../../pikotools/src/utf8/utf8_templates.h +./xmlexpression.o: ../../pikotools/src/utf8/utf8_private.h +./xmlexpression.o: ../../pikotools/src/date/date.h +./xmlexpression.o: ../../pikotools/src/membuffer/membuffer.h +./xmlexpression.o: ../../pikotools/src/textstream/types.h morm_types.h +./xmlexpression.o: modelenv.h modeldata.h cursorhelper.h queryresult.h +./xmlexpression.o: ../../pikotools/src/log/log.h +./xmlexpression.o: ../../pikotools/src/log/filelog.h finderhelper.h +./xmlexpression.o: fieldvaluehelper.h wrapper.h spacewrapper.h +./xmlexpression.o: baseobjectwrapper.h modelcontainerwrapper.h select.h ft.h +./xmlexpression.o: ../../pikotools/src/convert/text.h +./xmlexpression.o: ../../pikotools/src/convert/misc.h +./xmlexpression.o: ../../pikotools/src/convert/text.h diff --git a/src/baseexpression.h b/src/baseexpression.h index 753c5db..3952dce 100644 --- a/src/baseexpression.h +++ b/src/baseexpression.h @@ -139,6 +139,31 @@ public: put_field_value_or_null(field_value, getter_method, field_type, model_env); } } + else + if( work_mode == MORM_WORK_MODE_MODEL_FIELDS_VALUES_FIELDS ) + { + (*out_stream) << '\n'; + put_field_name_and_table_if_needed(field_name, field_type, model_env); + put_name_value_separator(); + put_field_value_or_null(field_value, getter_method, field_type, model_env); + put_name_value_separator(); + /* + * IMPROVEME currently only used in XML serializer so we put / directly here + * we need some virtual methods for it + */ + before_field_name(); // here should be a different method such as before_closing_field_name() + + if( out_stream ) + { + (*out_stream) << '/'; + } + + esc(field_name, *out_stream, FT::default_type, nullptr); /* do not use provided field_type here - it would use e.g. binary mode if it was set, similar don't use model_env */ + after_field_name(); // here too, e.g. after_closing_field_name() + /* + * + */ + } field_after(); } @@ -209,6 +234,31 @@ public: put_name_value_separator(); put_field_value_list(field_value, model_container_type, model_connector, model_env, foo); } + else + if( work_mode == MORM_WORK_MODE_MODEL_FIELDS_VALUES_FIELDS ) + { + (*out_stream) << '\n'; + put_field_name_and_table_if_needed(field_name, field_type, model_env); + put_name_value_separator(); + put_field_value_list(field_value, model_container_type, model_connector, model_env, foo); + put_name_value_separator(); + /* + * IMPROVEME currently only used in XML serializer so we put / directly here + * we need some virtual methods for it + */ + before_field_name(); // here should be a different method such as before_closing_field_name() + + if( out_stream ) + { + (*out_stream) << '/'; + } + + esc(field_name, *out_stream, FT::default_type, nullptr); /* do not use provided field_type here - it would use e.g. binary mode if it was set, similar don't use model_env */ + after_field_name(); // here too, e.g. after_closing_field_name() + /* + * + */ + } field_after(); } @@ -237,6 +287,32 @@ public: put_name_value_separator(); generate_from_model(field_model); } + else + if( work_mode == MORM_WORK_MODE_MODEL_FIELDS_VALUES_FIELDS ) + { + (*out_stream) << '\n'; + put_field_name_and_table_if_needed(field_name, field_type, model_env); + put_name_value_separator(); + generate_from_model(field_model); + put_name_value_separator(); + /* + * IMPROVEME currently only used in XML serializer so we put / directly here + * we need some virtual methods for it + */ + before_field_name(); // here should be a different method such as before_closing_field_name() + + if( out_stream ) + { + (*out_stream) << '/'; + } + + esc(field_name, *out_stream, FT::default_type, nullptr); /* do not use provided field_type here - it would use e.g. binary mode if it was set, similar don't use model_env */ + after_field_name(); // here too, e.g. after_closing_field_name() + (*out_stream) << '\n'; + /* + * + */ + } field_after(); } diff --git a/src/morm_types.h b/src/morm_types.h index f013ada..c8a9010 100644 --- a/src/morm_types.h +++ b/src/morm_types.h @@ -64,7 +64,8 @@ #define MORM_WORK_MODE_MODEL_FIELDS 1 #define MORM_WORK_MODE_MODEL_VALUES 2 #define MORM_WORK_MODE_MODEL_FIELDS_VALUES 3 -#define MORM_WORK_MODE_MODEL_SAVE_FIELDS 4 +#define MORM_WORK_MODE_MODEL_FIELDS_VALUES_FIELDS 4 +#define MORM_WORK_MODE_MODEL_SAVE_FIELDS 5 diff --git a/src/xmlconnector.cpp b/src/xmlconnector.cpp new file mode 100644 index 0000000..129077e --- /dev/null +++ b/src/xmlconnector.cpp @@ -0,0 +1,65 @@ +/* + * This file is a part of morm + * and is distributed under the 2-Clause BSD licence. + * Author: Tomasz Sowa + */ + +/* + * Copyright (c) 2022, Tomasz Sowa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "xmlconnector.h" +#include "xmlexpression.h" + + +namespace morm +{ + +void XMLConnector::allocate_default_expression() +{ + deallocate_expression(); + flat_expression = new XMLExpression(); + expression_allocated = true; + +} + + +void XMLConnector::to_text(pt::TextStream & stream, Model & model) +{ + allocate_default_expression_if_needed(); + + if( flat_expression ) + { + flat_expression->clear(); + flat_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS_VALUES_FIELDS); + flat_expression->allow_to_use_prefix(false); + flat_expression->generate_from_model(stream, model); + } +} + + +} diff --git a/src/xmlconnector.h b/src/xmlconnector.h new file mode 100644 index 0000000..340163e --- /dev/null +++ b/src/xmlconnector.h @@ -0,0 +1,60 @@ +/* + * This file is a part of morm + * and is distributed under the 2-Clause BSD licence. + * Author: Tomasz Sowa + */ + +/* + * Copyright (c) 2022, Tomasz Sowa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef headerfile_morm_src_xmlconnector +#define headerfile_morm_src_xmlconnector + +#include "flatconnector.h" + + +namespace morm +{ + +class XMLConnector : public FlatConnector +{ + +public: + + void to_text(pt::TextStream & stream, Model & model); + +protected: + + + void allocate_default_expression(); + +}; + +} + +#endif diff --git a/src/xmlexpression.cpp b/src/xmlexpression.cpp new file mode 100644 index 0000000..f95dfcc --- /dev/null +++ b/src/xmlexpression.cpp @@ -0,0 +1,167 @@ +/* + * This file is a part of morm + * and is distributed under the 2-Clause BSD licence. + * Author: Tomasz Sowa + */ + +/* + * Copyright (c) 2022, Tomasz Sowa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "xmlexpression.h" +#include "morm_types.h" +#include "convert/misc.h" + + +namespace morm +{ + + +void XMLExpression::before_generate_from_model() +{ + BaseExpression::before_generate_from_model(); + + if( work_mode == MORM_WORK_MODE_MODEL_FIELDS_VALUES_FIELDS ) + { + (*out_stream) << ""; + } +} + + +void XMLExpression::after_generate_from_model() +{ + BaseExpression::after_generate_from_model(); + + if( work_mode == MORM_WORK_MODE_MODEL_FIELDS_VALUES_FIELDS ) + { + (*out_stream) << ""; + } +} + + + +void XMLExpression::field_before() +{ + BaseExpression::field_before(); + + if( !is_first_field ) + { + //(*out_stream) << ","; + } +} + + + +void XMLExpression::before_field_name() +{ + (*out_stream) << "<"; +} + +void XMLExpression::after_field_name() +{ + (*out_stream) << ">"; +} + + + +void XMLExpression::before_field_value_string(const FT & field_type, ModelEnv * model_env) +{ + //(*out_stream) << "\""; +} + +void XMLExpression::after_field_value_string(const FT & field_type, ModelEnv * model_env) +{ + //(*out_stream) << "\""; +} + + +void XMLExpression::before_field_value(const pt::Space &, const FT & field_type, ModelEnv * model_env) +{ + if( field_type.is_space() ) + { + //before_field_value_string(field_type, model_env); + } +} + +void XMLExpression::after_field_value(const pt::Space &, const FT & field_type, ModelEnv * model_env) +{ + if( field_type.is_space() ) + { + //after_field_value_string(field_type, model_env); + } +} + + + +void XMLExpression::put_name_value_separator() +{ + //(*out_stream) << ':'; +} + + +void XMLExpression::before_field_value_list() +{ + (*out_stream) << ""; +} + + +void XMLExpression::after_field_value_list() +{ + (*out_stream) << "
"; +} + + +bool XMLExpression::esc_char(wchar_t val, pt::TextStream & stream, const FT & field_type, ModelEnv * model_env) +{ + return pt::try_esc_to_xml(val, stream); +} + + + +void XMLExpression::esc(const pt::Space & space, pt::TextStream & stream, const FT & field_type, ModelEnv * model_env) +{ + bool pretty_print = field_type.is_pretty_print(); + + if( field_type.is_space() ) + { + pt::WTextStream tmp_stream; + space.serialize_to_space_stream(tmp_stream, pretty_print); + BaseExpression::esc(tmp_stream, stream, field_type, model_env); + } + else + { + /* + * IMPROVEME it would be better to serialize to xml + * + */ + pt::WTextStream tmp_stream; + space.serialize_to_json_stream(tmp_stream, pretty_print); + BaseExpression::esc(tmp_stream, stream, field_type, model_env); + } +} + +} diff --git a/src/xmlexpression.h b/src/xmlexpression.h new file mode 100644 index 0000000..a713af5 --- /dev/null +++ b/src/xmlexpression.h @@ -0,0 +1,81 @@ +/* + * This file is a part of morm + * and is distributed under the 2-Clause BSD licence. + * Author: Tomasz Sowa + */ + +/* + * Copyright (c) 2022, Tomasz Sowa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef headerfile_morm_src_jsonexpression +#define headerfile_morm_src_jsonexpression + +#include "flatexpression.h" + + + +namespace morm +{ + +class XMLExpression : public FlatExpression +{ + +protected: + + void before_generate_from_model(); + void after_generate_from_model(); + + + + void field_before(); + + void before_field_name(); + void after_field_name(); + + void put_name_value_separator(); + + void before_field_value_list(); + void after_field_value_list(); + + bool esc_char(wchar_t val, pt::TextStream & stream, const FT & field_type, ModelEnv * model_env); + void esc(const pt::Space & space, pt::TextStream & stream, const FT & field_type, ModelEnv * model_env); + + +private: + + void before_field_value_string(const FT & field_type, ModelEnv * model_env); + void after_field_value_string(const FT & field_type, ModelEnv * model_env); + void before_field_value(const pt::Space &, const FT & field_type, ModelEnv * model_env); + void after_field_value(const pt::Space &, const FT & field_type, ModelEnv * model_env); + + +}; + +} + +#endif