Line data Source code
1 : /* File: io_importer.c; Copyright and License: see below */
2 :
3 : #include "io_importer.h"
4 : #include "json/json_element_reader.h"
5 : #include "u8stream/universal_file_input_stream.h"
6 : #include "u8stream/universal_memory_input_stream.h"
7 : #include "u8stream/universal_null_output_stream.h"
8 : #include "u8/u8_error.h"
9 : #include "utf8stringbuf/utf8string.h"
10 : #include "u8/u8_trace.h"
11 : #include <assert.h>
12 : #include "io_gtk.h"
13 : #include <stdbool.h>
14 :
15 12 : void io_importer_init ( io_importer_t *this_,
16 : data_database_reader_t *db_reader,
17 : ctrl_controller_t *controller )
18 : {
19 12 : U8_TRACE_BEGIN();
20 12 : assert( NULL != db_reader );
21 12 : assert( NULL != controller );
22 :
23 12 : (*this_).db_reader = db_reader;
24 12 : (*this_).controller = controller;
25 :
26 12 : U8_TRACE_END();
27 12 : }
28 :
29 12 : void io_importer_destroy ( io_importer_t *this_ )
30 : {
31 12 : U8_TRACE_BEGIN();
32 12 : assert( NULL != (*this_).db_reader );
33 12 : assert( NULL != (*this_).controller );
34 :
35 12 : (*this_).db_reader = NULL;
36 12 : (*this_).controller = NULL;
37 :
38 12 : U8_TRACE_END();
39 12 : }
40 :
41 15 : u8_error_t io_importer_import_clipboard( io_importer_t *this_,
42 : const char *json_text,
43 : data_row_t diagram_id,
44 : data_stat_t *io_stat,
45 : u8_error_info_t *out_err_info )
46 : {
47 15 : U8_TRACE_BEGIN();
48 15 : assert( NULL != json_text );
49 15 : assert( out_err_info != NULL );
50 15 : u8_error_info_init_void( out_err_info );
51 15 : u8_error_t result = U8_ERROR_NONE;
52 :
53 : universal_null_output_stream_t null_out;
54 15 : universal_null_output_stream_init( &null_out );
55 : utf8stream_writer_t out_writer;
56 15 : utf8stream_writer_init( &out_writer, universal_null_output_stream_get_output_stream( &null_out ) );
57 :
58 15 : io_import_elements_init_for_paste( &((*this_).temp_elements_importer),
59 : diagram_id,
60 : (*this_).db_reader,
61 : (*this_).controller,
62 : io_stat,
63 : &out_writer
64 : );
65 15 : json_importer_init( &((*this_).temp_json_importer), &((*this_).temp_elements_importer) );
66 :
67 : universal_memory_input_stream_t in_mem_stream;
68 15 : universal_memory_input_stream_init( &in_mem_stream, json_text, strlen(json_text) );
69 15 : universal_input_stream_t* in_stream = universal_memory_input_stream_get_input_stream( &in_mem_stream );
70 :
71 15 : result = json_importer_import_stream( &((*this_).temp_json_importer),
72 : in_stream,
73 : out_err_info
74 : );
75 :
76 15 : universal_memory_input_stream_destroy( &in_mem_stream );
77 :
78 15 : json_importer_destroy( &((*this_).temp_json_importer) );
79 15 : io_import_elements_destroy( &((*this_).temp_elements_importer) );
80 :
81 15 : utf8stream_writer_destroy( &out_writer );
82 15 : universal_null_output_stream_destroy( &null_out );
83 :
84 15 : U8_TRACE_END_ERR( result );
85 15 : return result;
86 : }
87 :
88 2 : u8_error_t io_importer_import_file( io_importer_t *this_,
89 : io_import_mode_t import_mode,
90 : const char *import_file_path,
91 : data_stat_t *io_stat,
92 : u8_error_info_t *out_err_info,
93 : utf8stream_writer_t *out_english_report )
94 : {
95 2 : U8_TRACE_BEGIN();
96 2 : assert( import_file_path != NULL );
97 2 : assert( io_stat != NULL );
98 2 : assert( out_english_report != NULL );
99 2 : assert( out_err_info != NULL );
100 2 : u8_error_info_init_void( out_err_info );
101 2 : u8_error_t parse_error = U8_ERROR_NONE;
102 :
103 : /* open file */
104 : universal_file_input_stream_t in_file;
105 2 : universal_file_input_stream_init( &in_file );
106 2 : parse_error |= universal_file_input_stream_open( &in_file, import_file_path );
107 :
108 : /* import from stream */
109 2 : if ( parse_error == U8_ERROR_NONE )
110 : {
111 2 : universal_input_stream_t *const in_stream = universal_file_input_stream_get_input_stream( &in_file );
112 2 : parse_error = io_importer_import_stream( this_, import_mode, in_stream, io_stat, out_err_info, out_english_report );
113 : }
114 :
115 : /* close file */
116 2 : parse_error |= universal_file_input_stream_close( &in_file );
117 2 : parse_error |= universal_file_input_stream_destroy( &in_file );
118 :
119 : /* after importing a file, reset the undo redo list */
120 2 : ctrl_controller_reset_undo_redo_list( (*this_).controller );
121 :
122 2 : U8_TRACE_END_ERR( parse_error );
123 2 : return parse_error;
124 : }
125 :
126 3 : u8_error_t io_importer_import_stream( io_importer_t *this_,
127 : io_import_mode_t import_mode,
128 : universal_input_stream_t *in_stream,
129 : data_stat_t *io_stat,
130 : u8_error_info_t *out_err_info,
131 : utf8stream_writer_t *out_english_report )
132 : {
133 3 : U8_TRACE_BEGIN();
134 3 : assert( in_stream != NULL );
135 3 : assert( io_stat != NULL );
136 3 : assert( out_english_report != NULL );
137 3 : assert( out_err_info != NULL );
138 3 : u8_error_info_init_void( out_err_info );
139 3 : u8_error_t parse_error = U8_ERROR_NONE;
140 :
141 3 : io_import_elements_init( &((*this_).temp_elements_importer),
142 : (*this_).db_reader,
143 : (*this_).controller,
144 : io_stat,
145 : out_english_report
146 : );
147 3 : json_importer_init( &((*this_).temp_json_importer), &((*this_).temp_elements_importer) );
148 :
149 : /* check json structure */
150 3 : if ( parse_error == U8_ERROR_NONE )
151 : {
152 3 : U8_TRACE_INFO("scanning file... (IO_IMPORT_STEP_CHECK)");
153 : static const char *const PASS_CHECK_TITLE
154 : = "PASS: Check that the file structure is valid\n ";
155 3 : utf8stream_writer_write_str( out_english_report, PASS_CHECK_TITLE );
156 :
157 3 : io_import_elements_set_mode( &((*this_).temp_elements_importer), IO_IMPORT_MODE_CHECK, IO_IMPORT_STEP_CHECK );
158 3 : parse_error = json_importer_import_stream( &((*this_).temp_json_importer),
159 : in_stream,
160 : out_err_info
161 : );
162 :
163 3 : utf8stream_writer_write_str( out_english_report, "\n" );
164 : }
165 :
166 : /* reset file */
167 3 : if ( parse_error == U8_ERROR_NONE )
168 : {
169 2 : parse_error |= universal_input_stream_reset( in_stream );
170 : }
171 :
172 : /* start an outer transaction to speed up the creation of objects */
173 3 : parse_error |= ctrl_controller_transaction_begin( (*this_).controller );
174 :
175 : /* import: create elements */
176 3 : if (( import_mode == IO_IMPORT_MODE_PASTE )&&( parse_error == U8_ERROR_NONE ))
177 0 : {
178 0 : U8_TRACE_INFO("importing file... (IO_IMPORT_MODE_PASTE/IO_IMPORT_STEP_CREATE_D_C_F_R)");
179 : static const char *const PASS_CREATE_ALL_TITLE
180 : = "PASS: Create diagrams, classifiers, features and relationships\n ";
181 0 : utf8stream_writer_write_str( out_english_report, PASS_CREATE_ALL_TITLE );
182 :
183 0 : io_import_elements_set_mode( &((*this_).temp_elements_importer), IO_IMPORT_MODE_PASTE, IO_IMPORT_STEP_CREATE_D_C_F_R );
184 0 : parse_error = json_importer_import_stream( &((*this_).temp_json_importer),
185 : in_stream,
186 : out_err_info
187 : );
188 :
189 0 : utf8stream_writer_write_str( out_english_report, "\n" );
190 : }
191 3 : else if (( import_mode == IO_IMPORT_MODE_IMPORT )&&( parse_error == U8_ERROR_NONE ))
192 : {
193 2 : U8_TRACE_INFO("importing file... (IO_IMPORT_MODE_IMPORT/IO_IMPORT_STEP_CREATE_D_C_L)");
194 : static const char *const PASS_CREATE_TITLE
195 : = "PASS: Create diagrams, classifiers and lifelines\n ";
196 2 : utf8stream_writer_write_str( out_english_report, PASS_CREATE_TITLE );
197 :
198 2 : io_import_elements_set_mode( &((*this_).temp_elements_importer), IO_IMPORT_MODE_IMPORT, IO_IMPORT_STEP_CREATE_D_C_L );
199 2 : parse_error = json_importer_import_stream( &((*this_).temp_json_importer),
200 : in_stream,
201 : out_err_info
202 : );
203 :
204 2 : utf8stream_writer_write_str( out_english_report, "\n" );
205 : }
206 :
207 : /* reset file once more */
208 3 : if ( parse_error == U8_ERROR_NONE )
209 : {
210 2 : parse_error |= universal_input_stream_reset( in_stream );
211 : }
212 :
213 : /* import: create elements */
214 3 : if (( import_mode == IO_IMPORT_MODE_IMPORT )&&( parse_error == U8_ERROR_NONE ))
215 : {
216 2 : U8_TRACE_INFO("importing file... (IO_IMPORT_MODE_IMPORT/IO_IMPORT_STEP_ADD_E_DP_F_R)");
217 : static const char *const PASS_LINK_TITLE
218 : = "PASS: Link diagrams to parents, classifiers to diagrams, create features and relationships\n ";
219 2 : utf8stream_writer_write_str( out_english_report, PASS_LINK_TITLE );
220 :
221 2 : io_import_elements_set_mode( &((*this_).temp_elements_importer), IO_IMPORT_MODE_IMPORT, IO_IMPORT_STEP_ADD_E_DP_F_R );
222 2 : parse_error = json_importer_import_stream( &((*this_).temp_json_importer),
223 : in_stream,
224 : out_err_info
225 : );
226 :
227 2 : utf8stream_writer_write_str( out_english_report, "\n" );
228 : }
229 :
230 : /* commit the outer transaction */
231 3 : parse_error |= ctrl_controller_transaction_commit( (*this_).controller );
232 :
233 3 : json_importer_destroy( &((*this_).temp_json_importer) );
234 3 : io_import_elements_destroy( &((*this_).temp_elements_importer) );
235 :
236 3 : U8_TRACE_END_ERR( parse_error );
237 3 : return parse_error;
238 : }
239 :
240 :
241 : /*
242 : Copyright 2021-2026 Andreas Warnke
243 :
244 : Licensed under the Apache License, Version 2.0 (the "License");
245 : you may not use this file except in compliance with the License.
246 : You may obtain a copy of the License at
247 :
248 : http://www.apache.org/licenses/LICENSE-2.0
249 :
250 : Unless required by applicable law or agreed to in writing, software
251 : distributed under the License is distributed on an "AS IS" BASIS,
252 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
253 : See the License for the specific language governing permissions and
254 : limitations under the License.
255 : */
|