-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
StructureNodeImpl.cpp
465 lines (401 loc) · 14.6 KB
/
StructureNodeImpl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
* Original work Copyright 2009 - 2010 Kevin Ackley (kackley@gwi.net)
* Modified work Copyright 2018 - 2020 Andy Maloney <asmaloney@gmail.com>
*
* Permission is hereby granted, free of charge, to any person or organization
* obtaining a copy of the software and accompanying documentation covered by
* this license (the "Software") to use, reproduce, display, distribute,
* execute, and transmit the Software, and to prepare derivative works of the
* Software, and to permit third-parties to whom the Software is furnished to
* do so, all subject to the following:
*
* The copyright notices in the Software and this entire statement, including
* the above license grant, this restriction and the following disclaimer,
* must be included in all copies of the Software, in whole or in part, and
* all derivative works of the Software, unless such copies or derivative
* works are solely in the form of machine-executable object code generated by
* a source language processor.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
* SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
* FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <climits>
#include "CheckedFile.h"
#include "ImageFileImpl.h"
#include "StringFunctions.h"
#include "StructureNodeImpl.h"
using namespace e57;
StructureNodeImpl::StructureNodeImpl( ImageFileImplWeakPtr destImageFile ) :
NodeImpl( destImageFile )
{
checkImageFileOpen( __FILE__, __LINE__, static_cast<const char *>( __FUNCTION__ ) );
}
NodeType StructureNodeImpl::type() const
{
// don't checkImageFileOpen
return TypeStructure;
}
//??? use visitor?
bool StructureNodeImpl::isTypeEquivalent( NodeImplSharedPtr ni )
{
// don't checkImageFileOpen
// Same node type?
if ( ni->type() != TypeStructure )
{
return ( false );
}
// Downcast to shared_ptr<StructureNodeImpl>
std::shared_ptr<StructureNodeImpl> si( std::static_pointer_cast<StructureNodeImpl>( ni ) );
// Same number of children?
if ( childCount() != si->childCount() )
{
return ( false );
}
// Check each child is equivalent
for ( unsigned i = 0; i < childCount(); i++ )
{ //??? vector iterator?
ustring myChildsFieldName = children_.at( i )->elementName();
// Check if matching field name is in same position (to speed things up)
if ( myChildsFieldName == si->children_.at( i )->elementName() )
{
if ( !children_.at( i )->isTypeEquivalent( si->children_.at( i ) ) )
{
return ( false );
}
}
else
{
// Children in different order, so lookup by name and check if equal to our child
if ( !si->isDefined( myChildsFieldName ) )
{
return ( false );
}
if ( !children_.at( i )->isTypeEquivalent( si->lookup( myChildsFieldName ) ) )
{
return ( false );
}
}
}
// Types match
return ( true );
}
bool StructureNodeImpl::isDefined( const ustring &pathName )
{
checkImageFileOpen( __FILE__, __LINE__, static_cast<const char *>( __FUNCTION__ ) );
NodeImplSharedPtr ni( lookup( pathName ) );
return ( ni != nullptr );
}
void StructureNodeImpl::setAttachedRecursive()
{
// Mark this node as attached to an ImageFile
isAttached_ = true;
// Not a leaf node, so mark all our children
for ( auto &child : children_ )
{
child->setAttachedRecursive();
}
}
int64_t StructureNodeImpl::childCount() const
{
checkImageFileOpen( __FILE__, __LINE__, static_cast<const char *>( __FUNCTION__ ) );
return children_.size();
}
NodeImplSharedPtr StructureNodeImpl::get( int64_t index )
{
checkImageFileOpen( __FILE__, __LINE__, static_cast<const char *>( __FUNCTION__ ) );
if ( index < 0 || index >= static_cast<int64_t>( children_.size() ) )
{ // %%% Possible truncation on platforms where size_t = uint64
throw E57_EXCEPTION2( ErrorChildIndexOutOfBounds,
"this->pathName=" + this->pathName() + " index=" + toString( index ) +
" size=" + toString( children_.size() ) );
}
return ( children_.at( static_cast<unsigned>( index ) ) );
}
NodeImplSharedPtr StructureNodeImpl::get( const ustring &pathName )
{
checkImageFileOpen( __FILE__, __LINE__, static_cast<const char *>( __FUNCTION__ ) );
NodeImplSharedPtr ni( lookup( pathName ) );
if ( !ni )
{
throw E57_EXCEPTION2( ErrorPathUndefined,
"this->pathName=" + this->pathName() + " pathName=" + pathName );
}
return ( ni );
}
NodeImplSharedPtr StructureNodeImpl::lookup( const ustring &pathName )
{
// don't checkImageFileOpen
//??? use lookup(fields, level) instead, for speed.
bool isRelative;
std::vector<ustring> fields;
ImageFileImplSharedPtr imf( destImageFile_ );
imf->pathNameParse( pathName, isRelative, fields ); // throws if bad pathName
if ( isRelative || isRoot() )
{
if ( fields.empty() )
{
if ( isRelative )
{
return {}; // empty pointer
}
NodeImplSharedPtr root( getRoot() );
return ( root );
}
// Find child with elementName that matches first field in path
unsigned i;
for ( i = 0; i < children_.size(); i++ )
{
if ( fields.at( 0 ) == children_.at( i )->elementName() )
{
break;
}
}
if ( i == children_.size() )
{
return {}; // empty pointer
}
if ( fields.size() == 1 )
{
return ( children_.at( i ) );
}
//??? use level here rather than unparse
// Remove first field in path
fields.erase( fields.begin() );
// Call lookup on child object with remaining fields in path name
return children_.at( i )->lookup( imf->pathNameUnparse( true, fields ) );
}
// Absolute pathname and we aren't at the root
// Find root of the tree
NodeImplSharedPtr root( getRoot() );
// Call lookup on root
return ( root->lookup( pathName ) );
}
void StructureNodeImpl::set( int64_t index64, NodeImplSharedPtr ni )
{
checkImageFileOpen( __FILE__, __LINE__, static_cast<const char *>( __FUNCTION__ ) );
auto index = static_cast<unsigned>( index64 );
// Allow index == current number of elements, interpret as append
if ( index64 < 0 || index64 > UINT_MAX || index > children_.size() )
{
throw E57_EXCEPTION2( ErrorChildIndexOutOfBounds,
"this->pathName=" + this->pathName() + " index=" + toString( index64 ) +
" size=" + toString( children_.size() ) );
}
// Enforce "set once" policy, only allow append
if ( index != children_.size() )
{
throw E57_EXCEPTION2( ErrorSetTwice, "this->pathName=" + this->pathName() +
" index=" + toString( index64 ) );
}
// Verify that child is destined for same ImageFile as this is
ImageFileImplSharedPtr thisDest( destImageFile() );
ImageFileImplSharedPtr niDest( ni->destImageFile() );
if ( thisDest != niDest )
{
throw E57_EXCEPTION2( ErrorDifferentDestImageFile,
"this->destImageFile" + thisDest->fileName() + " ni->destImageFile" +
niDest->fileName() );
}
// Field name is string version of index value, e.g. "14"
std::stringstream elementName;
elementName << index;
// If this struct is type constrained, can't add new child
if ( isTypeConstrained() )
{
throw E57_EXCEPTION2( ErrorHomogeneousViolation, "this->pathName=" + this->pathName() );
}
ni->setParent( shared_from_this(), elementName.str() );
children_.push_back( ni );
}
void StructureNodeImpl::set( const ustring &pathName, NodeImplSharedPtr ni, bool autoPathCreate )
{
checkImageFileOpen( __FILE__, __LINE__, static_cast<const char *>( __FUNCTION__ ) );
//??? parse pathName! throw if impossible, absolute and multi-level paths...
//??? enforce type constraints on path (non-zero index types match zero index
// types for VECTOR,
// COMPRESSED_VECTOR
#ifdef E57_VERBOSE
std::cout << "StructureNodeImpl::set(pathName=" << pathName
<< ", ni, autoPathCreate=" << autoPathCreate << std::endl;
#endif
bool isRelative;
std::vector<ustring> fields;
// Path may be absolute or relative with several levels. Break string into individual levels.
ImageFileImplSharedPtr imf( destImageFile_ );
imf->pathNameParse( pathName, isRelative, fields ); // throws if bad pathName
if ( isRelative )
{
// Relative path, starting from current object, e.g. "foo/17/bar"
set( fields, 0, ni, autoPathCreate );
}
else
{
// Absolute path (starting from root), e.g. "/foo/17/bar"
getRoot()->set( fields, 0, ni, autoPathCreate );
}
}
void StructureNodeImpl::set( const std::vector<ustring> &fields, unsigned level,
NodeImplSharedPtr ni, bool autoPathCreate )
{
#ifdef E57_VERBOSE
std::cout << "StructureNodeImpl::set: level=" << level << std::endl;
for ( unsigned i = 0; i < fields.size(); i++ )
{
std::cout << " field[" << i << "]: " << fields.at( i ) << std::endl;
}
#endif
checkImageFileOpen( __FILE__, __LINE__, static_cast<const char *>( __FUNCTION__ ) );
//??? check if field is numeric string (e.g. "17"), verify number is same as
// index, else throw
// bad_path
// Check if trying to set the root node "/", which is illegal
if ( level == 0 && fields.empty() )
{
throw E57_EXCEPTION2( ErrorSetTwice, "this->pathName=" + this->pathName() + " element=/" );
}
// Serial search for matching field name, if find match, have error since can't set twice
for ( auto &child : children_ )
{
if ( fields.at( level ) == child->elementName() )
{
if ( level == fields.size() - 1 )
{
// Enforce "set once" policy, don't allow reset
throw E57_EXCEPTION2( ErrorSetTwice, "this->pathName=" + this->pathName() +
" element=" + fields[level] );
}
// Recurse on child
child->set( fields, level + 1, ni );
return;
}
}
// Didn't find matching field name, so have a new child.
// If this struct is type constrained, can't add new child
if ( isTypeConstrained() )
{
throw E57_EXCEPTION2( ErrorHomogeneousViolation, "this->pathName=" + this->pathName() );
}
// Check if we are at bottom level
if ( level == fields.size() - 1 )
{
// At bottom, so append node at end of children
ni->setParent( shared_from_this(), fields.at( level ) );
children_.push_back( ni );
}
else
{
// Not at bottom level, if not autoPathCreate have an error
if ( !autoPathCreate )
{
throw E57_EXCEPTION2( ErrorPathUndefined, "this->pathName=" + this->pathName() +
" field=" + fields.at( level ) );
}
//??? what if extra fields are numbers?
// Do autoPathCreate: Create nested Struct objects for extra field names in path
NodeImplSharedPtr parent( shared_from_this() );
for ( ; level != fields.size() - 1; level++ )
{
std::shared_ptr<StructureNodeImpl> child( new StructureNodeImpl( destImageFile_ ) );
parent->set( fields.at( level ), child );
parent = child;
}
parent->set( fields.at( level ), ni );
}
}
void StructureNodeImpl::append( NodeImplSharedPtr ni )
{
// don't checkImageFileOpen, set() will do it
// Create new node at end of list with integer field name
set( childCount(), ni );
}
//??? use visitor?
void StructureNodeImpl::checkLeavesInSet( const StringSet &pathNames, NodeImplSharedPtr origin )
{
// don't checkImageFileOpen
// Not a leaf node, so check all our children
for ( auto &child : children_ )
{
child->checkLeavesInSet( pathNames, origin );
}
}
//??? use visitor?
void StructureNodeImpl::writeXml( ImageFileImplSharedPtr imf, CheckedFile &cf, int indent,
const char *forcedFieldName )
{
// don't checkImageFileOpen
ustring fieldName;
if ( forcedFieldName != nullptr )
{
fieldName = forcedFieldName;
}
else
{
fieldName = elementName_;
}
cf << space( indent ) << "<" << fieldName << " type=\"Structure\"";
const int numSpaces = indent + static_cast<int>( fieldName.length() ) + 2;
// If this struct is the root for the E57 file, add name space declarations. Note the prototype
// of a CompressedVector is a separate tree, so don't want to write out namespaces if not the
// ImageFile root
if ( isRoot() && shared_from_this() == imf->root() )
{
bool gotDefaultNamespace = false;
for ( size_t i = 0; i < imf->extensionsCount(); i++ )
{
const char *xmlnsExtension;
if ( imf->extensionsPrefix( i ).empty() )
{
gotDefaultNamespace = true;
xmlnsExtension = "xmlns";
}
else
{
xmlnsExtension = "xmlns:";
}
const int index = static_cast<int>( i );
cf << "\n"
<< space( numSpaces ) << xmlnsExtension << imf->extensionsPrefix( index ) << "=\""
<< imf->extensionsUri( index ) << "\"";
}
// If user didn't explicitly declare a default namespace, use the current E57 standard one.
if ( !gotDefaultNamespace )
{
cf << "\n" << space( numSpaces ) << "xmlns=\"" << VERSION_1_0_URI << "\"";
}
}
if ( !children_.empty() )
{
cf << ">\n";
// Write all children nested inside Structure element
for ( auto &child : children_ )
{
child->writeXml( imf, cf, indent + 2 );
}
// Write closing tag
cf << space( indent ) << "</" << fieldName << ">\n";
}
else
{
// XML element has no child elements
cf << "/>\n";
}
}
//??? use visitor?
#ifdef E57_ENABLE_DIAGNOSTIC_OUTPUT
void StructureNodeImpl::dump( int indent, std::ostream &os ) const
{
// don't checkImageFileOpen
os << space( indent ) << "type: Structure" << " (" << type() << ")" << std::endl;
NodeImpl::dump( indent, os );
for ( unsigned i = 0; i < children_.size(); i++ )
{
os << space( indent ) << "child[" << i << "]:" << std::endl;
children_.at( i )->dump( indent + 2, os );
}
}
#endif