add basic support for serializing objects to xml

This commit is contained in:
Tomasz Sowa 2022-12-16 02:48:07 +01:00
parent a3c337355d
commit 794051fa15
7 changed files with 492 additions and 1 deletions

View File

@ -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

View File

@ -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();
}

View File

@ -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

65
src/xmlconnector.cpp Normal file
View File

@ -0,0 +1,65 @@
/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* 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);
}
}
}

60
src/xmlconnector.h Normal file
View File

@ -0,0 +1,60 @@
/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* 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

167
src/xmlexpression.cpp Normal file
View File

@ -0,0 +1,167 @@
/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* 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) << "<xml>";
}
}
void XMLExpression::after_generate_from_model()
{
BaseExpression::after_generate_from_model();
if( work_mode == MORM_WORK_MODE_MODEL_FIELDS_VALUES_FIELDS )
{
(*out_stream) << "</xml>";
}
}
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) << "<table>";
}
void XMLExpression::after_field_value_list()
{
(*out_stream) << "</table>";
}
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);
}
}
}

81
src/xmlexpression.h Normal file
View File

@ -0,0 +1,81 @@
/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* 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