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 14 : 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 14 : U8_TRACE_BEGIN();
48 14 : assert( NULL != json_text );
49 14 : assert( out_err_info != NULL );
50 14 : u8_error_info_init_void( out_err_info );
51 14 : u8_error_t result = U8_ERROR_NONE;
52 :
53 : universal_null_output_stream_t null_out;
54 14 : universal_null_output_stream_init( &null_out );
55 : utf8stream_writer_t out_writer;
56 14 : utf8stream_writer_init( &out_writer, universal_null_output_stream_get_output_stream( &null_out ) );
57 :
58 14 : 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 14 : json_importer_init( &((*this_).temp_json_importer), &((*this_).temp_elements_importer) );
66 :
67 : universal_memory_input_stream_t in_mem_stream;
68 14 : universal_memory_input_stream_init( &in_mem_stream, json_text, strlen(json_text) );
69 14 : universal_input_stream_t* in_stream = universal_memory_input_stream_get_input_stream( &in_mem_stream );
70 :
71 14 : result = json_importer_import_stream( &((*this_).temp_json_importer),
72 : in_stream,
73 : out_err_info
74 : );
75 :
76 14 : universal_memory_input_stream_destroy( &in_mem_stream );
77 :
78 14 : json_importer_destroy( &((*this_).temp_json_importer) );
79 14 : io_import_elements_destroy( &((*this_).temp_elements_importer) );
80 :
81 14 : utf8stream_writer_destroy( &out_writer );
82 14 : universal_null_output_stream_destroy( &null_out );
83 :
84 14 : U8_TRACE_END_ERR( result );
85 14 : 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...");
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 );
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_CHECK )&&( parse_error == U8_ERROR_NONE ))
177 : {
178 2 : U8_TRACE_INFO("importing file...");
179 : static const char *const PASS_CREATE_TITLE
180 : = "PASS: Create diagrams, classifiers and features\n ";
181 2 : utf8stream_writer_write_str( out_english_report, PASS_CREATE_TITLE );
182 :
183 2 : io_import_elements_set_mode( &((*this_).temp_elements_importer), IO_IMPORT_MODE_CREATE );
184 2 : parse_error = json_importer_import_stream( &((*this_).temp_json_importer),
185 : in_stream,
186 : out_err_info
187 : );
188 :
189 2 : utf8stream_writer_write_str( out_english_report, "\n" );
190 : }
191 :
192 : /* reset file once more */
193 3 : if ( parse_error == U8_ERROR_NONE )
194 : {
195 2 : parse_error |= universal_input_stream_reset( in_stream );
196 : }
197 :
198 : /* import: create elements */
199 3 : if (( import_mode != IO_IMPORT_MODE_CHECK )&&( parse_error == U8_ERROR_NONE ))
200 : {
201 2 : U8_TRACE_INFO("importing file...");
202 : static const char *const PASS_LINK_TITLE
203 : = "PASS: Link diagrams to parents, classifiers to diagrams, create relationships\n ";
204 2 : utf8stream_writer_write_str( out_english_report, PASS_LINK_TITLE );
205 :
206 2 : io_import_elements_set_mode( &((*this_).temp_elements_importer), IO_IMPORT_MODE_LINK );
207 2 : parse_error = json_importer_import_stream( &((*this_).temp_json_importer),
208 : in_stream,
209 : out_err_info
210 : );
211 :
212 2 : utf8stream_writer_write_str( out_english_report, "\n" );
213 : }
214 :
215 : /* commit the outer transaction */
216 3 : parse_error |= ctrl_controller_transaction_commit( (*this_).controller );
217 :
218 3 : json_importer_destroy( &((*this_).temp_json_importer) );
219 3 : io_import_elements_destroy( &((*this_).temp_elements_importer) );
220 :
221 3 : U8_TRACE_END_ERR( parse_error );
222 3 : return parse_error;
223 : }
224 :
225 :
226 : /*
227 : Copyright 2021-2025 Andreas Warnke
228 :
229 : Licensed under the Apache License, Version 2.0 (the "License");
230 : you may not use this file except in compliance with the License.
231 : You may obtain a copy of the License at
232 :
233 : http://www.apache.org/licenses/LICENSE-2.0
234 :
235 : Unless required by applicable law or agreed to in writing, software
236 : distributed under the License is distributed on an "AS IS" BASIS,
237 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
238 : See the License for the specific language governing permissions and
239 : limitations under the License.
240 : */
|