File: AttrTable.cc

package info (click to toggle)
libdap 3.20.11-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 24,568 kB
  • sloc: cpp: 50,809; sh: 41,536; xml: 23,511; ansic: 20,030; yacc: 2,508; exp: 1,544; makefile: 990; lex: 309; perl: 52; fortran: 8
file content (1560 lines) | stat: -rw-r--r-- 53,907 bytes parent folder | download | duplicates (2)
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
// -*- mode: c++; c-basic-offset:4 -*-

// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.

// Copyright (c) 2002,2003 OPeNDAP, Inc.
// Author: James Gallagher <jgallagher@opendap.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.

// (c) COPYRIGHT URI/MIT 1994-1999
// Please read the full copyright statement in the file COPYRIGHT_URI.
//
// Authors:
//      jhrg,jimg       James Gallagher <jgallagher@gso.uri.edu>

// jhrg 7/29/94

#include "config.h"

#include <cassert>
#include <sstream>

#include "AttrTable.h"

#include "util.h"
#include "escaping.h"
#include "DapIndent.h"

#include "debug.h"

// Should the www2id and id2www functions be used to encode attribute names?
// Probably not... jhrg 11/16/11
#define WWW_ENCODING 0
// See the note for del_attr_table(). That method now deletes the contained
// AttrTable.
#define NEW_DEL_ATTR_TABLE_BEHAVIOR 0

using std::cerr;
using std::string;
using std::endl;
using std::vector;

namespace libdap {

/** Remove %20 space encoding */
string remove_space_encoding(const string &s)
{
    string::size_type pos = s.find("%20");
    if (pos != string::npos) {
        string n = s;
        do {
            n.replace(pos, 3, " ");
            pos = n.find("%20");
        } while (pos != string::npos);
        return n;
    }
    else {
        return s;
    }
}

/** Add %20 space encoding. */
string add_space_encoding(const string &s)
{
    string::size_type pos = s.find(" ");
    if (pos != string::npos) {
        string n = s;
        do {
            n.replace(pos, 1, "%20");
            pos = n.find(" ");
        } while (pos != string::npos);
        return n;
    }
    else {
        return s;
    }
}

/** Convert an AttrType to it's string representation.
 @param at The Attribute Type.
 @return The type's string representation */
string AttrType_to_String(const AttrType at)
{
    switch (at) {
    case Attr_container:
        return "Container";
    case Attr_byte:
        return "Byte";
    case Attr_int16:
        return "Int16";
    case Attr_uint16:
        return "UInt16";
    case Attr_int32:
        return "Int32";
    case Attr_uint32:
        return "UInt32";
    case Attr_float32:
        return "Float32";
    case Attr_float64:
        return "Float64";
    case Attr_string:
        return "String";
    case Attr_url:
        return "Url";
    case Attr_other_xml:
        return "OtherXML";
    default:
        return "";
    }
}

AttrType String_to_AttrType(const string &s)
{
    string s2 = s;
    downcase(s2);

    if (s2 == "container")
        return Attr_container;
    else if (s2 == "byte")
        return Attr_byte;
    else if (s2 == "int16")
        return Attr_int16;
    else if (s2 == "uint16")
        return Attr_uint16;
    else if (s2 == "int32")
        return Attr_int32;
    else if (s2 == "uint32")
        return Attr_uint32;
    else if (s2 == "float32")
        return Attr_float32;
    else if (s2 == "float64")
        return Attr_float64;
    else if (s2 == "string")
        return Attr_string;
    else if (s2 == "url")
        return Attr_url;
    else if (s2 == "otherxml")
        return Attr_other_xml;
    else
        return Attr_unknown;
}

/** Clone the given attribute table in <tt>this</tt>.
 Protected. */
void AttrTable::clone(const AttrTable &at)
{
    d_name = at.d_name;
    d_is_global_attribute = at.d_is_global_attribute;

    // Set the parent to null (no parent, not in container)
    // since using at.d_parent is semantically incorrect
    // and potentially dangerous.
    d_parent = 0;

    Attr_citer i = at.attr_map.begin();
    Attr_citer ie = at.attr_map.end();
    for (; i != ie; ++i) {
        // this deep-copies containers recursively
        entry *e = new entry(*(*i));
        attr_map.push_back(e);

        // If the entry being added was a container,
        // set its parent to this to maintain invariant.
        if (e->type == Attr_container) {
            assert(e->attributes);
            e->attributes->d_parent = this;
        }
    }
}

/** @name Instance management functions */

//@{
AttrTable::AttrTable() :
    DapObj(), d_name(""), d_parent(0), attr_map(), d_is_global_attribute(true)
{
}

AttrTable::AttrTable(const AttrTable &rhs) :
    DapObj()
{
    clone(rhs);
}

// Private
void AttrTable::delete_attr_table()
{
    for (Attr_iter i = attr_map.begin(); i != attr_map.end(); ++i) {
        delete *i;
    }
    attr_map.clear();
}

AttrTable::~AttrTable()
{
    delete_attr_table();
}

AttrTable &
AttrTable::operator=(const AttrTable &rhs)
{
    if (this != &rhs) {
        delete_attr_table();
        clone(rhs);
    }

    return *this;
}
//@}

/** Attributes that are containers count as one attribute, as do
 attributes with both scalar and vector values.
 @return The number of entries.
 @brief Get the number of entries in this attribute table.
 */
unsigned int AttrTable::get_size() const
{
    return attr_map.size();
}

/** @brief Get the name of this attribute table.
 @return A string containing the name. */
string AttrTable::get_name() const
{
    return d_name;
}

/** @brief Set the name of this attribute table.
 @param n The new name of the attribute table. */
void AttrTable::set_name(const string &n)
{
#if WWW_ENCODING
    d_name = www2id(n);
#else
    d_name = remove_space_encoding(n);
#endif
}

#if 0
// This was taken from das.y and could be used here to make the 'dods_errors'
// attribute container like the parser used to. Then again, maybe this feature
// was just BS. jhrg (ticket 1469)
static void add_bad_attribute(AttrTable *attr, const string &type, const string &name, const string &value,
        const string &msg) {
    // First, if this bad value is already in a *_dods_errors container,
    // then just add it. This can happen when the server side processes a DAS
    // and then hands it off to a client which does the same.
    // Make a new container. Call it <attr's name>_errors. If that container
    // already exists, use it.
    // Add the attribute.
    // Add the error string to an attribute in the container called
    // `<name_explanation.'.

    if (attr->get_name().find("_dods_errors") != string::npos) {
        attr->append_attr(name, type, value);
    }
    else {
        // I think _dods_errors should be _dap_error. jhrg 11/16/11
        string error_cont_name = attr->get_name() + "_dods_errors";
        AttrTable *error_cont = attr->get_attr_table(error_cont_name);
        if (!error_cont)
        error_cont = attr->append_container(error_cont_name);

        error_cont->append_attr(name, type, value);

#ifndef ATTR_STRING_QUOTE_FIX
        error_cont->append_attr(name + "_dap_explanation", "String", "\"" + msg + "\"");
#else
        error_cont->append_attr(name + "_dap_explanation", "String", msg);
#endif
    }
}
#endif

/** If the given name already refers to an attribute, and the attribute has a
 value, the given value is appended to the attribute vector. Calling this
 function repeatedly is the way to append to an attribute vector.

 The function throws an Error if the attribute is a container,
 or if the type of the input value does not match the existing attribute's
 type. Use <tt>append_container()</tt> to add container attributes.

 This method performs a simple search for <tt>name</tt> in this attribute
 table only; sub-tables are not searched and the dot notation is not
 recognized.

 @brief Add an attribute to the table.
 @return Returns the length of the added attribute value.
 @param name The name of the attribute to add or modify.
 @param type The type of the attribute to add or modify.
 @param value The value to add to the attribute table. */
unsigned int AttrTable::append_attr(const string &name, const string &type, const string &value)
{
    DBG(cerr << "Entering AttrTable::append_attr" << endl);
#if WWW_ENCODING
    string lname = www2id(name);
#else
    string lname = remove_space_encoding(name);
#endif

    Attr_iter iter = simple_find(lname);

    // If the types don't match OR this attribute is a container, calling
    // this mfunc is an error!
    if (iter != attr_map.end() && ((*iter)->type != String_to_AttrType(type)))
        throw Error(string("An attribute called `") + name + string("' already exists but is of a different type"));
    if (iter != attr_map.end() && (get_type(iter) == "Container"))
        throw Error(string("An attribute called `") + name + string("' already exists but is a container."));

    if (iter != attr_map.end()) { // Must be a new attribute value; add it.
        (*iter)->attr->push_back(value);
        return (*iter)->attr->size();
    }
    else { // Must be a completely new attribute; add it
        entry *e = new entry;

        e->name = lname;
        e->is_alias = false;
        e->type = String_to_AttrType(type); // Record type using standard names.
        e->attr = new vector<string> ;
        e->attr->push_back(value);

        attr_map.push_back(e);

        return e->attr->size(); // return the length of the attr vector
    }
}

/** This version of append_attr() takes a vector<string> of values.
 If the given name already refers to an attribute, and the attribute has
 values, append the new values to the existing ones.

 The function throws an Error if the attribute is a container,
 or if the type of the input value does not match the existing attribute's
 type. Use <tt>append_container()</tt> to add container attributes.

 This method performs a simple search for <tt>name</tt> in this attribute
 table only; sub-tables are not searched and the dot notation is not
 recognized.

 @brief Add an attribute to the table.
 @return Returns the length of the added attribute value.
 @param name The name of the attribute to add or modify.
 @param type The type of the attribute to add or modify.
 @param values A vector of values. Note: The vector is COPIED, not stored. */

unsigned int AttrTable::append_attr(const string &name, const string &type, vector<string> *values)
{
    DBG(cerr << "Entering AttrTable::append_attr(..., vector)" << endl);
#if WWW_ENCODING
    string lname = www2id(name);
#else
    string lname = remove_space_encoding(name);
#endif
    Attr_iter iter = simple_find(lname);

    // If the types don't match OR this attribute is a container, calling
    // this mfunc is an error!
    if (iter != attr_map.end() && ((*iter)->type != String_to_AttrType(type)))
        throw Error(string("An attribute called `") + name + string("' already exists but is of a different type"));
    if (iter != attr_map.end() && (get_type(iter) == "Container"))
        throw Error(string("An attribute called `") + name + string("' already exists but is a container."));

    if (iter != attr_map.end()) { // Must be new attribute values; add.
        vector<string>::iterator i = values->begin();
        while (i != values->end())
            (*iter)->attr->push_back(*i++);

        return (*iter)->attr->size();
    }
    else { // Must be a completely new attribute; add it
        entry *e = new entry;

        e->name = lname;
        e->is_alias = false;
        e->type = String_to_AttrType(type); // Record type using standard names.
        e->attr = new vector<string> (*values);

        attr_map.push_back(e);

        return e->attr->size(); // return the length of the attr vector
    }
}

/** Create and append an attribute container to this AttrTable. If this
 attribute table already contains an attribute container called
 <tt>name</tt> an exception is thrown. Return a pointer to the new container.

 @brief Add a container to the attribute table.
 @param name The name of the container to create.
 @return A pointer to the new AttrTable object.
 */

AttrTable *
AttrTable::append_container(const string &name)
{
    AttrTable *new_at = new AttrTable;
    AttrTable *ret = NULL;
    try {
        ret = append_container(new_at, name);
    } catch (Error &e) {
        // an error occurred, attribute with that name already exists
        delete new_at;
        new_at = 0;
        throw;
    }
    return ret;
}

/** Append a new attribute container to this attribute table. The new
 container is <tt>at</tt> and its name is set to
 <tt>name</tt>. If this attribute
 table already contains an attribute container called
 <tt>name</tt> an exception is thrown.

 @note The value of \e name will override the name of \e at set using the
 set_name() method.

 @brief Add a container to the attribute table.
 @param at A pointer to the new attribute table to append.
 @param name The name of the new attribute table.
 @return A pointer to the new AttrTable object.
 */
AttrTable *
AttrTable::append_container(AttrTable *at, const string &name)
{
#if WWW_ENCODING
    string lname = www2id(name);
#else
    string lname = remove_space_encoding(name);
#endif

    if (simple_find(name) != attr_end())
        throw Error("There already exists a container called '" + name + "' in this attribute table (" + at->get_name() + "). (1)");

    DBG(cerr << "Setting appended attribute container name to: " << lname << endl);
    at->set_name(lname);

    entry *e = new entry;
    e->name = lname;
    e->is_alias = false;
    e->type = Attr_container;
    e->attributes = at;

    attr_map.push_back(e);

    at->d_parent = this;

    return e->attributes;
}

/** Look for an attribute or an attribute container. If used to search
 for an attribute container, this method returns the container's \e
 parent using the value-result parameter \c at and a reference to the
 container using the iterator value-result parameter \c iter. If used
 to search for an attribute, the attribute's container is returned using
 \c at; the attribute itself can be accessed using the iterator \c iter.

 @param target The name (using dot notation) of the attribute or
 container to find.
 @param at A value-result used to return the attribute container in
 which \c target was found. Null if \c target was not found.
 @param iter The iterator which will reference the attribute found.
 Can be used to access \c target from within \c at. References
 dim_end() within \c at if the attribute or container does not exist. */
void AttrTable::find(const string &target, AttrTable **at, Attr_iter *iter)
{
    string::size_type dotpos = target.rfind('.');
    if (dotpos != string::npos) {
        string container = target.substr(0, dotpos);
        string field = target.substr(dotpos + 1);

        *at = find_container(container);
        if (*at) {
            *iter = (*at)->simple_find(field);
        }
        else {
            *iter = attr_map.end();
        }
    }
    else {
        *at = recurrsive_find(target, iter);
    }
}

/** This method scans for attributes using recursion to look inside containers
 even when the name of the attribute is not fully qualified. It starts
 looking in itself and descends into its children depth first. It will find
 attributes and attribute containers.

 @param target Look for the attribute with this name.
 @param location A value-result parameter. This returns an iterator to the
 attribute within the returned AttrTable object
 @return Returns a pointer to the AttrTable which holds \e target, or null
 if \e target is not found. In the latter case, the value of \e location is
 attr_end() for this AttrTable. */
AttrTable *
AttrTable::recurrsive_find(const string &target, Attr_iter *location)
{
    Attr_iter i = attr_begin();
    while (i != attr_end()) {
        if (target == (*i)->name) {
            *location = i;
            return this;
        }
        else if ((*i)->type == Attr_container) {
            AttrTable *at = (*i)->attributes->recurrsive_find(target, location);
            if (at)
                return at;
        }

        ++i;
    }

    *location = i;
    return 0;
}

// Made public for callers that want non-recursive find.  [mjohnson 6 oct 09]
/** Look in this AttrTable for the attribute called \c name. If found return
 an Attr_iter which references it, otherwise return the end iterator for
 this AttrTable.

 @param target The name of the attribute.
 @return An Attr_iter which references \c target. */
AttrTable::Attr_iter AttrTable::simple_find(const string &target)
{
    Attr_iter i;
    for (i = attr_map.begin(); i != attr_map.end(); ++i) {
        if (target == (*i)->name) {
            break;
        }
    }
    return i;
}

/** Look in this attribute table for an attribute container named
 <tt>target</tt>. The search starts at this attribute table;
 <tt>target</tt> should
 use the dot notation to name containers held within children of this
 attribute table.

 To search the entire DAS object, make sure to invoke this method from
 that object.

 @brief Find an attribute with a given name.
 @param target The attribute container to find.
 @return A pointer to the attribute table or null if the container
 cannot be found. */
AttrTable *
AttrTable::find_container(const string &target)
{
    string::size_type dotpos = target.find('.');
    if (dotpos != string::npos) {
        string container = target.substr(0, dotpos);
        string field = target.substr(dotpos + 1);

        AttrTable *at = simple_find_container(container);
        return (at) ? at->find_container(field) : 0;
    }
    else {
        return simple_find_container(target);
    }
}

// Made public for callers that want non-recursive find.  [mjohnson 6 oct 09]
AttrTable *
AttrTable::simple_find_container(const string &target)
{
    if (get_name() == target)
        return this;

    for (Attr_iter i = attr_map.begin(); i != attr_map.end(); ++i) {
        if (is_container(i) && target == (*i)->name) {
            return (*i)->attributes;
        }
    }

    return 0;
}

/** Each of the following accessors get information using the name of an
 attribute. They perform a simple search for the name in this
 attribute table only; sub-tables are not searched and the dot
 notation is not recognized.

 @name Accessors using an attribute name */
//@{

/** @brief Get an attribute container. */
AttrTable *
AttrTable::get_attr_table(const string &name)
{
    return find_container(name);
}

/** @brief Get the type name of an attribute within this attribute table. */
string AttrTable::get_type(const string &name)
{
    Attr_iter p = simple_find(name);
    return (p != attr_map.end()) ? get_type(p) : (string) "";
}

/** @brief Get the type of an attribute.
 @return The <tt>AttrType</tt> value describing the attribute. */
AttrType AttrTable::get_attr_type(const string &name)
{
    Attr_iter p = simple_find(name);
    return (p != attr_map.end()) ? get_attr_type(p) : Attr_unknown;
}

/** If the indicated attribute is a container attribute, this function
 returns the number of attributes in <i>its</i> attribute table. If the
 indicated attribute is not a container, the method returns the number
 of values for the attribute (1 for a scalar attribute, N for a vector
 attribute value).
 @brief Get the number of attributes in this container.
 */
unsigned int AttrTable::get_attr_num(const string &name)
{
    Attr_iter iter = simple_find(name);
    return (iter != attr_map.end()) ? get_attr_num(iter) : 0;
}

/** Get a pointer to the vector of values associated with the attribute
 referenced by Pix <tt>p</tt> or named <tt>name</tt>.

 Note that all values in an attribute table are stored as string data.
 They may be converted to a more appropriate internal format by the
 calling program.

 @return If the indicated attribute is a container, this function
 returns the null pointer.  Otherwise returns a pointer to the
 the attribute vector value.
 @brief Get a vector-valued attribute.
 */
vector<string> *
AttrTable::get_attr_vector(const string &name)
{
    Attr_iter p = simple_find(name);
    return (p != attr_map.end()) ? get_attr_vector(p) : 0;
}

/** Delete the attribute named <tt>name</tt>. If <tt>i</tt> is given, and
 the attribute has a vector value, delete the <tt>i</tt>$^th$
 element of the vector.

 You can use this function to delete container attributes, although
 the <tt>i</tt> parameter has no meaning for that operation.

 @brief Deletes an attribute.
 @param name The name of the attribute to delete.  This can be an
 attribute of any type, including containers. However, this method
 looks only in this attribute table and does not recognize the dot
 notation.
 @param i If the named attribute is a vector, and <tt>i</tt> is
 non-negative, the i-th entry in the vector is deleted, and the
 array is repacked.  If <tt>i</tt> equals -1 (the default), the
 entire attribute is deleted. */
void AttrTable::del_attr(const string &name, int i)
{
#if WWW_ENCODING
    string lname = www2id(name);
#else
    string lname = remove_space_encoding(name);
#endif

    Attr_iter iter = simple_find(lname);
    if (iter != attr_map.end()) {
        if (i == -1) { // Delete the whole attribute
            entry *e = *iter;
            attr_map.erase(iter);
            delete e;
            e = 0;
        }
        else { // Delete one element from attribute array
            // Don't try to delete elements from the vector of values if the
            // map is a container!
            if ((*iter)->type == Attr_container)
                return;

            vector<string> *sxp = (*iter)->attr;

            assert(i >= 0 && i < (int) sxp->size());
            sxp->erase(sxp->begin() + i); // rm the element
        }
    }
}

//@} Accessors using an attribute name

/** @name get information using an iterator */
//@{
/** Get an iterator to the first entry in this attribute table.
 @return Attr_iter; references the end of the array if empty list. */
AttrTable::Attr_iter AttrTable::attr_begin()
{
    return attr_map.begin();
}

/** Get an iterator to the end attribute table. Does not point to
 the last attribute in the table
 @return Attr_iter */
AttrTable::Attr_iter AttrTable::attr_end()
{
    return attr_map.end();
}

/** Given an index \c i, return the \c Attr_iter to the corresponding
 element. This method provides a way to use all the methods that take an
 \c Attr_iter using a simple integer index. Use the get_attr_num() or
 get_size() methods to determine how many items the AttrTable contains.

 @param i The index
 @return The corresponding Attr_iter
 @see get_attr_num, get_size */
AttrTable::Attr_iter AttrTable::get_attr_iter(int i)
{
    return attr_map.begin() + i;
}

/** Returns the name of the attribute referenced by \e iter. */
string AttrTable::get_name(Attr_iter iter)
{
    assert(iter != attr_map.end());

    return (*iter)->name;
}

/** Returns true if the attribute referenced by \e i is a container. */
bool AttrTable::is_container(Attr_iter i)
{
    return (*i)->type == Attr_container;
}

/** Get the attribute container referenced by \e iter. If no
 such container exists, then return a reference to the end of the
 table.
 @param iter Reference to a table contained by this object.
 @return The child attribute table. */
AttrTable *
AttrTable::get_attr_table(Attr_iter iter)
{
    assert(iter != attr_map.end());
    return (*iter)->type == Attr_container ? (*iter)->attributes : 0;
}

/** Delete the iterator.  Since AttrTable stores pointers to AttrTable
 objects, the caller should be sure to delete the AttrTable itself.
 The caller will gain control of the AttrTable* located at
 get_attr_table(iter) prior to this call.

 @note The original semantics of this methods were odd. The caller was
 responsible for deleting the AttrTable, but if they did that before calling
 this, then memory corruption would happen (because this code accesses a
 field of the table). If the caller did not delete the table, memory leaked.
 The only correct way to call the method was to grab the pointer, call this
 and then delete the pointer. I added a call to delete the contained
 AttrTable pointer, which changes the behavior of this, but probably in a
 way that will fix leaks in existing code. This change can be reverted by
 setting NEW_DEL_ATTR_TABLE_BEHAVIOR to false. jhrg 4/26/13

 @note calling this method <b>invalidates</b> the iterator \e iter.
 @param iter points to the entry to be deleted.
 @return The Attr_iter for the element following \e iter */
AttrTable::Attr_iter AttrTable::del_attr_table(Attr_iter iter)
{
    if ((*iter)->type != Attr_container)
        return ++iter;

    // the caller intends to delete/reuse the contained AttrTable,
    // so zero it out so it doesn't get deleted before we delete the entry
    // [mjohnson]
    struct entry *e = *iter;
    // container no longer has a parent.
    if (e->attributes) {
        e->attributes->d_parent = 0;

#if NEW_DEL_ATTR_TABLE_BEHAVIOR
        delete e->attributes;
#endif
        e->attributes = 0;
    }

    delete e;

    return attr_map.erase(iter);
}

/** Get the type name of an attribute referenced by \e iter.
 @param iter Reference to the Attribute.
 @return A string with the name of this attribute datatype. */
string AttrTable::get_type(Attr_iter iter)
{
    assert(iter != attr_map.end());
    return AttrType_to_String((*iter)->type);
}

/** Get the type of the attribute referenced by \e iter.
 @param iter
 @return The datatype of this attribute in an instance of AttrType. */
AttrType AttrTable::get_attr_type(Attr_iter iter)
{
    return (*iter)->type;
}

/** If the attribute referenced by \e iter is a container attribute, this
 method returns the number of attributes in its attribute table.
 If the indicated attribute is not a container, the method returns the
 number of values for the attribute (1 for a scalar attribute, N for a
 vector attribute value).
 @param iter Reference to an attribute
 @return The number of elements in the attribute. */
unsigned int AttrTable::get_attr_num(Attr_iter iter)
{
    assert(iter != attr_map.end());
    return ((*iter)->type == Attr_container) ? (*iter)->attributes->get_size() : (*iter)->attr->size();
}

/** Returns the value of an attribute. If the attribute has a vector
 value, you can indicate which is the desired value with the index
 argument, \e i. If the argument is omitted, the first value is
 returned. If the attribute has only a single value, the index
 argument is ignored. If \e i is greater than the number of
 elements in the attribute, an error is produced.

 All values in an attribute table are stored as string data. They may
 be converted to a more appropriate internal format by the calling
 program.

 @param iter Reference to an attribute
 @param i The attribute value index, zero-based. Default value: 0
 @return If the indicated attribute is a container, this function
 returns the string ``None''. If using a name to refer to the attribute
 and the named attribute does not exist, return the empty string. */
string AttrTable::get_attr(Attr_iter iter, unsigned int i)
{
    assert(iter != attr_map.end());

    return (*iter)->type == Attr_container ? (string) "None" : (*(*iter)->attr)[i];
}

string AttrTable::get_attr(const string &name, unsigned int i)
{
    Attr_iter p = simple_find(name);
    return (p != attr_map.end()) ? get_attr(p, i) : (string) "";
}

/** Returns a pointer to the vector of values associated with the
 attribute referenced by iterator \e iter.

 Note that all values in an attribute table are stored as string data.
 They may be converted to a more appropriate internal format by the
 calling program.

 @param iter Reference to the Attribute.
 @return If the indicated attribute is a container, this function
 returns the null pointer.  Otherwise returns a pointer to the
 the attribute vector value. */
vector<string> *
AttrTable::get_attr_vector(Attr_iter iter)
{
    assert(iter != attr_map.end());
    return (*iter)->type != Attr_container ? (*iter)->attr : 0;
}

bool AttrTable::is_global_attribute(Attr_iter iter)
{
    assert(iter != attr_map.end());
    if ((*iter)->type == Attr_container)
        return (*iter)->attributes->is_global_attribute();
    else
        return (*iter)->is_global;
}

void AttrTable::set_is_global_attribute(Attr_iter iter, bool ga)
{
    assert(iter != attr_map.end());
    if ((*iter)->type == Attr_container)
        (*iter)->attributes->set_is_global_attribute(ga);
    else
        (*iter)->is_global = ga;
}

//@} Accessors that use an iterator

// Alias an attribute table. The alias should be added to this object.
/** @brief Add an alias to a container held by this attribute table.
 @param name The name of the alias. May <i>not</i> use dot notation.
 @param src The existing attribute container to alias.
 @exception Error if an attribute, container or alias called
 <tt>name</tt> already exists in this attribute table. */
void AttrTable::add_container_alias(const string &name, AttrTable *src)
{
#if WWW_ENCODING
    string lname = www2id(name);
#else
    string lname = remove_space_encoding(name);
#endif

    if (simple_find(lname) != attr_end())
        throw Error(string("There already exists a container called `") + name + string("in this attribute table. (2)"));

    entry *e = new entry;
    e->name = lname;
    e->is_alias = true;
    e->aliased_to = src->get_name();
    e->type = Attr_container;

    e->attributes = src;

    attr_map.push_back(e);
}

/** Assume \e source names an attribute value in some container. Add an alias
 \e name for that value in this object.

 @brief Add an alias for an attribute.

 @param das
 @param name The name of the alias. May <i>not</i> use dot notation.
 @param source The name of the attribute to alias. May use dot
 notation.
 @exception Error if the attribute table already contains an
 attribute, container or alias called <tt>name</tt> or if an
 attribute called <tt>source</tt> does not exist. */
void AttrTable::add_value_alias(AttrTable *das, const string &name, const string &source)
{
#if WWW_ENCODING
    string lname = www2id(name);
#else
    string lname = remove_space_encoding(name);
#endif

#if WWW_ENCODING
    string lsource = www2id(source);
#else
    string lsource = remove_space_encoding(source);
#endif

    // find the container that holds source and its (sources's) iterator
    // within that container. Search at the uppermost level of the attribute
    // object to find values defined `above' the current container.
    AttrTable *at;
    Attr_iter iter;
    das->find(lsource, &at, &iter);

    // If source is not found by looking at the topmost level, look in the
    // current table (i.e., alias z x where x is in the current container
    // won't be found by looking for `x' at the top level). See test case 26
    // in das-testsuite.
    if (!at || (iter == at->attr_end()) || !*iter) {
        find(lsource, &at, &iter);
        if (!at || (iter == at->attr_end()) || !*iter)
            throw Error(string("Could not find the attribute `") + source + string("' in the attribute object."));
    }

    // If we've got a value to alias and it's being added at the top level of
    // the DAS, that's an error.
    if (at && !at->is_container(iter) && this == das)
        throw Error(
                string(
                        "A value cannot be aliased to the top level of the DAS;\nOnly containers may be present at that level of the DAS."));

    if (simple_find(lname) != attr_end())
        throw Error(string("There already exists a container called `") + name + string("in this attribute table. (3)"));

    entry *e = new entry;
    e->name = lname;
    e->is_alias = true;
    e->aliased_to = lsource;
    e->type = get_attr_type(iter);
    if (at && e->type == Attr_container)
        e->attributes = at->get_attr_table(iter);
    else
        e->attr = (*iter)->attr;

    attr_map.push_back(e);
}

// Deprecated
/** Once an alias is
 inserted into an attribute table, reading the attributes for
 <i>alias</i> will return those stored for <i>name</i>.

 Two forms for this function exist: one searches for <i>name</i>
 in the AttrTable referenced by <i>at</i> while the other uses
 <tt>this</tt>. You can use <tt>DAS::get_attr_table()</tt> to
 get the attribute table for an arbitrary name.

 @brief Adds an alias to the set of attributes.
 @see get_attr_table
 @deprecated The current alias design is flawed. It is impossible to map
 this onto the XML implementation where the DAS and DDS information are
 combined in one object.
 @param alias The alias to insert into the attribute table.
 @param name The name of the already-existing attribute to which
 the alias will refer.
 @param at An attribute table in which to insert the alias. */
bool AttrTable::attr_alias(const string &alias, AttrTable *at, const string &name)
{
    add_value_alias(at, alias, name);
    return true;
}

/** @deprecated The current alias design is flawed. It is impossible to map
 this onto the XML implementation where the DAS and DDS information are
 combined in one object.

 @param alias The alias to insert into the attribute table.
 @param name The name of the already-existing attribute to which
 the alias will refer. */
bool AttrTable::attr_alias(const string &alias, const string &name)
{
    return attr_alias(alias, this, name);
}

/** Erase the entire attribute table. This returns an AttrTable to the empty
 state that's the same as the object generated by the null constructor.
 @brief Erase the attribute table. */
void AttrTable::erase()
{
    for (Attr_iter i = attr_map.begin(); i != attr_map.end(); ++i) {
        delete *i;
        *i = 0;
    }

    attr_map.erase(attr_map.begin(), attr_map.end());

    d_name = "";
}

const string double_quote = "\"";

// This is here as a result of the problem described in ticket #1163 where
// the data handlers are adding quotes to string attributes so the DAS will
// be printed correctly. But that has the affect of adding the quotes to the
// attribute's _value_ not just it's print representation. As part of the fix
// I made the code here add the quotes if the handlers are fixed (but not if
// handlers are still adding them). The other part of 1163 is to fix all of
// the handlers... What this fix means is that attributes whose values really
// do contain bracketing quotes might be misunderstood, since we're assuming
// those quotes were added by the handlers as a hack to get the output
// formatting correct for the DAS. jhrg 7/30/08

static void write_string_attribute_for_das(ostream &out, const string &value, const string &term)
{
    if (is_quoted(value))
        out << value << term;
    else
        out << double_quote << value << double_quote << term;
}

#if 0
static void
write_string_attribute_for_das(FILE *out, const string &value, const string &term)
{
    if (is_quoted(value))
    fprintf(out, "%s%s", value.c_str(), term.c_str());
    else
    fprintf(out, "\"%s\"%s", value.c_str(), term.c_str());
}
#endif

// Special treatment for XML: Make sure to escape double quotes when XML is
// printed in a DAS.
static void write_xml_attribute_for_das(ostream &out, const string &value, const string &term)
{
    if (is_quoted(value))
        out << escape_double_quotes(value) << term;
    else
        out << double_quote << escape_double_quotes(value) << double_quote << term;
}

#if 0
static void
write_xml_attribute_for_das(FILE *out, const string &value, const string &term)
{
    if (is_quoted(value))
    fprintf(out, "%s%s", escape_double_quotes(value).c_str(), term.c_str());
    else
    fprintf(out, "\"%s\"%s", escape_double_quotes(value).c_str(), term.c_str());
}
#endif

/** A simple printer that does nothing fancy with aliases.
 Protected. */
void AttrTable::simple_print(FILE *out, string pad, Attr_iter i, bool dereference)
{
    ostringstream oss;
    simple_print(oss, pad, i, dereference);
    fwrite(oss.str().data(), 1, oss.str().length(), out);

#if 0
    switch ((*i)->type) {
        case Attr_container:
#if WWW_ENCODING
        fprintf(out, "%s%s {\n", pad.c_str(), id2www(get_name(i)).c_str());
#else
        fprintf(out, "%s%s {\n", pad.c_str(), get_name(i).c_str());
#endif
        (*i)->attributes->print(out, pad + "    ", dereference);

        fprintf(out, "%s}\n", pad.c_str());
        break;

        case Attr_string: {
#if WWW_ENCODING
            fprintf(out, "%s%s %s ", pad.c_str(), get_type(i).c_str(), id2www(get_name(i)).c_str());
#else
            fprintf(out, "%s%s %s ", pad.c_str(), get_type(i).c_str(), get_name(i).c_str());
#endif
            vector<string> *sxp = (*i)->attr;
            vector<string>::iterator last = sxp->end() - 1;
            for (vector<string>::iterator i = sxp->begin(); i != last; ++i) {
                write_string_attribute_for_das(out, *i, ", ");
            }
            write_string_attribute_for_das(out, *last, ";\n");
        }
        break;

        case Attr_other_xml: {
#if WWW_ENCODING
            fprintf(out, "%s%s %s ", pad.c_str(), get_type(i).c_str(), id2www(get_name(i)).c_str());
#else
            fprintf(out, "%s%s %s ", pad.c_str(), get_type(i).c_str(), get_name(i).c_str());
#endif
            vector<string> *sxp = (*i)->attr;
            vector<string>::iterator last = sxp->end() - 1;
            for (vector<string>::iterator i = sxp->begin(); i != last; ++i) {
                write_xml_attribute_for_das(out, *i, ", ");
            }
            write_xml_attribute_for_das(out, *last, ";\n");
        }
        break;

        default: {
#if WWW_ENCODING
            fprintf(out, "%s%s %s ", pad.c_str(), get_type(i).c_str(), id2www(get_name(i)).c_str());
#else
            fprintf(out, "%s%s %s ", pad.c_str(), get_type(i).c_str(), get_name(i).c_str());
#endif

            vector<string> *sxp = (*i)->attr;
            vector<string>::iterator last = sxp->end() - 1;
            for (vector<string>::iterator i = sxp->begin(); i != last; ++i) {
                fprintf(out, "%s%s", (*i).c_str(), ", ");
            }
            fprintf(out, "%s%s", (*last).c_str(), ";\n");
        }
        break;
    }
#endif
}

/** A simple printer that does nothing fancy with aliases.
 Protected. */
void AttrTable::simple_print(ostream &out, string pad, Attr_iter i, bool dereference)
{
    switch ((*i)->type) {
    case Attr_container:
#if WWW_ENCODING
        out << pad << id2www(get_name(i)) << " {\n";
#else
        out << pad << add_space_encoding(get_name(i)) << " {\n";
#endif
        (*i)->attributes->print(out, pad + "    ", dereference);
        out << pad << "}\n";
        break;

    case Attr_string: {
#if WWW_ENCODING
        out << pad << get_type(i) << " " << id2www(get_name(i)) << " ";
#else
        out << pad << get_type(i) << " " << add_space_encoding(get_name(i)) << " ";
#endif
        vector<string> *sxp = (*i)->attr;
        vector<string>::iterator last = sxp->end() - 1;
        for (vector<string>::iterator i = sxp->begin(); i != last; ++i) {
            write_string_attribute_for_das(out, *i, ", ");
        }
        write_string_attribute_for_das(out, *last, ";\n");
    }
        break;

    case Attr_other_xml: {
#if WWW_ENCODING
        out << pad << get_type(i) << " " << id2www(get_name(i)) << " ";
#else
        out << pad << get_type(i) << " " << add_space_encoding(get_name(i)) << " ";
#endif
        vector<string> *sxp = (*i)->attr;
        vector<string>::iterator last = sxp->end() - 1;
        for (vector<string>::iterator i = sxp->begin(); i != last; ++i) {
            write_xml_attribute_for_das(out, *i, ", ");
        }
        write_xml_attribute_for_das(out, *last, ";\n");
    }
        break;

    default: {
#if WWW_ENCODING
        out << pad << get_type(i) << " " << id2www(get_name(i)) << " ";
#else
        out << pad << get_type(i) << " " << add_space_encoding(get_name(i)) << " ";
#endif
        vector<string> *sxp = (*i)->attr;
        vector<string>::iterator last = sxp->end() - 1;
        for (vector<string>::iterator i = sxp->begin(); i != last; ++i) {
            out << *i << ", ";
        }
        out << *last << ";\n";
    }
        break;
    }
}

/** Prints an ASCII representation of the attribute table to the
 indicated FILE pointer. The \c pad argument is prefixed to each
 line of the output to provide control of indentation.

 @brief Prints the attribute table.
 @param out Print to the given output FILE.
 @param pad Indent elements of a table using this string of spaces. By
 default this is a string of four spaces
 @param dereference If true, follow aliases. Default is false. */

void AttrTable::print(FILE *out, string pad, bool dereference)
{
    ostringstream oss;
    print(oss, pad, dereference);
    fwrite(oss.str().data(), 1, oss.str().length(), out);

#if 0
    for (Attr_iter i = attr_map.begin(); i != attr_map.end(); ++i) {
        if ((*i)->is_alias) {
            if (dereference) {
                simple_print(out, pad, i, dereference);
            }
            else {
#if WWW_ENCODING
                fprintf(out, "%sAlias %s %s;\n",
                        pad.c_str(),
                        id2www(get_name(i)).c_str(),
                        id2www((*i)->aliased_to).c_str());
#else
                fprintf(out, "%sAlias %s %s;\n",
                        pad.c_str(), add_space_encoding(get_name(i)).c_str(), add_space_encoding((*i)->aliased_to).c_str());

#endif
            }
        }
        else {
            simple_print(out, pad, i, dereference);
        }
    }
#endif
}

/** Prints an ASCII representation of the attribute table to the
 indicated output stream. The \c pad argument is prefixed to each
 line of the output to provide control of indentation.

 @brief Prints the attribute table.
 @param out Print to the given output stream.
 @param pad Indent elements of a table using this string of spaces. By
 default this is a string of four spaces
 @param dereference If true, follow aliases. Default is false. */

void AttrTable::print(ostream &out, string pad, bool dereference)
{
    for (Attr_iter i = attr_map.begin(); i != attr_map.end(); ++i) {
        if ((*i)->is_alias) {
            if (dereference) {
                simple_print(out, pad, i, dereference);
            }
            else {
#if WWW_ENCODING
                out << pad << "Alias " << id2www(get_name(i))
                << " " << id2www((*i)->aliased_to) << ";\n";
#else
                out << pad << "Alias " << add_space_encoding(get_name(i)) << " "
                        << add_space_encoding((*i)->aliased_to) << ";\n";
#endif
            }
        }
        else {
            simple_print(out, pad, i, dereference);
        }
    }
}

/** Print the attribute table in XML.
 @param out Destination
 @param pad Indent lines of text/xml this much. Default is four spaces.
 @param constrained Not used
 @deprecated */
void AttrTable::print_xml(FILE *out, string pad, bool /*constrained*/)
{
    XMLWriter xml(pad);
    print_xml_writer(xml);
    fwrite(xml.get_doc(), sizeof(char), xml.get_doc_size(), out);

#if OLD_XML_MOETHODS
    ostringstream oss;
    print_xml(oss, pad);
    fwrite(oss.str().data(), 1, oss.str().length(), out);
#endif

#if 0
    // Why this works: AttrTable is really a hacked class that used to
    // implement a single-level set of attributes. Containers
    // were added several years later by dropping in the 'entry' structure.
    // It's not a class in its own right; instead accessors from AttrTable
    // are used to access information from entry. So... the loop below
    // actually iterates over the entries of *this* (which is an instance of
    // AttrTable). A container is an entry whose sole value is an AttrTable
    // instance. 05/19/03 jhrg
    for (Attr_iter i = attr_begin(); i != attr_end(); ++i) {
        if ((*i)->is_alias) {
            fprintf(out, "%s<Alias name=\"%s\" Attribute=\"%s\"/>\n",
                    pad.c_str(), id2xml(get_name(i)).c_str(),
                    (*i)->aliased_to.c_str());

        }
        else if (is_container(i)) {
            fprintf(out, "%s<Attribute name=\"%s\" type=\"%s\">\n",
                    pad.c_str(), id2xml(get_name(i)).c_str(),
                    get_type(i).c_str());

            get_attr_table(i)->print_xml(out, pad + "    "/*, constrained*/);

            fprintf(out, "%s</Attribute>\n", pad.c_str());
        }
        else {
            fprintf(out, "%s<Attribute name=\"%s\" type=\"%s\">\n",
                    pad.c_str(), id2xml(get_name(i)).c_str(), get_type(i).c_str());

            string value_pad = pad + "    ";
            // Special handling for the OtherXML attribute type - don't escape
            // the XML and don't include the <value> element. Note that there
            // cannot be an vector of XML things as can be with the other types.
            if (get_attr_type(i) == Attr_other_xml) {
                if (get_attr_num(i) != 1)
                throw Error("OtherXML attributes cannot be vector-valued.");
                fprintf(out, "%s%s\n", value_pad.c_str(), get_attr(i, 0).c_str());
            }
            else {
                for (unsigned j = 0; j < get_attr_num(i); ++j) {
                    fprintf(out, "%s<value>%s</value>\n", value_pad.c_str(),
                            id2xml(get_attr(i, j)).c_str());
                }
            }
            fprintf(out, "%s</Attribute>\n", pad.c_str());
        }
    }
#endif
}

/**
 * @deprecated
 */
void AttrTable::print_xml(ostream &out, string pad, bool /*constrained*/)
{
    XMLWriter xml(pad);
    print_xml_writer(xml);
    out << xml.get_doc();

#if 0
    for (Attr_iter i = attr_begin(); i != attr_end(); ++i) {
        if ((*i)->is_alias) {
            out << pad << "<Alias name=\"" << id2xml(get_name(i))
            << "\" Attribute=\"" << (*i)->aliased_to << "\"/>\n";

        }
        else if (is_container(i)) {
            out << pad << "<Attribute name=\"" << id2xml(get_name(i))
            << "\" type=\"" << get_type(i) << "\">\n";

            get_attr_table(i)->print_xml(out, pad + "    "/*, constrained*/);

            out << pad << "</Attribute>\n";
        }
        else {
            out << pad << "<Attribute name=\"" << id2xml(get_name(i))
            << "\" type=\"" << get_type(i) << "\">\n";

            string value_pad = pad + "    ";
            if (get_attr_type(i) == Attr_other_xml) {
                if (get_attr_num(i) != 1)
                throw Error("OtherXML attributes cannot be vector-valued.");
                out << value_pad << get_attr(i, 0) << "\n";
            }
            else {
                string value_pad = pad + "    ";
                for (unsigned j = 0; j < get_attr_num(i); ++j) {
                    out << value_pad << "<value>" << id2xml(get_attr(i, j)) << "</value>\n";
                }
            }
            out << pad << "</Attribute>\n";
        }
    }
#endif
}

/** Print the attribute table in XML.
 @param out Destination stream
 @param pad Indent lines of text/xml this much. Default is four spaces.
 @param constrained Not used */
void AttrTable::print_xml_writer(XMLWriter &xml)
{
    for (Attr_iter i = attr_begin(); i != attr_end(); ++i) {
        if ((*i)->is_alias) {
            if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*) "Alias") < 0)
                throw InternalErr(__FILE__, __LINE__, "Could not write Alias element");
            if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "name",
                    (const xmlChar*) get_name(i).c_str()) < 0)
                throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
            if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "Attribute",
                    (const xmlChar*) (*i)->aliased_to.c_str()) < 0)
                throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
            if (xmlTextWriterEndElement(xml.get_writer()) < 0)
                throw InternalErr(__FILE__, __LINE__, "Could not end Alias element");
        }
        else if (is_container(i)) {
            if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*) "Attribute") < 0)
                throw InternalErr(__FILE__, __LINE__, "Could not write Attribute element");
            if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "name",
                    (const xmlChar*) get_name(i).c_str()) < 0)
                throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
            if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "type",
                    (const xmlChar*) get_type(i).c_str()) < 0)
                throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");

            get_attr_table(i)->print_xml_writer(xml);

            if (xmlTextWriterEndElement(xml.get_writer()) < 0)
                throw InternalErr(__FILE__, __LINE__, "Could not end Attribute element");
        }
        else {
            if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*) "Attribute") < 0)
                throw InternalErr(__FILE__, __LINE__, "Could not write Attribute element");
            if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "name",
                    (const xmlChar*) get_name(i).c_str()) < 0)
                throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");
            if (xmlTextWriterWriteAttribute(xml.get_writer(), (const xmlChar*) "type",
                    (const xmlChar*) get_type(i).c_str()) < 0)
                throw InternalErr(__FILE__, __LINE__, "Could not write attribute for name");

            if (get_attr_type(i) == Attr_other_xml) {
                if (get_attr_num(i) != 1)
                    throw Error("OtherXML attributes cannot be vector-valued.");
                // Replaced xmltextWriterWriteString with xmlTextWriterWriteRaw to keep the
                // libxml2 code from escaping the xml (which was breaking all of the inferencing
                // code. jhrg
                if (xmlTextWriterWriteRaw(xml.get_writer(), (const xmlChar*) get_attr(i, 0).c_str()) < 0)
                    throw InternalErr(__FILE__, __LINE__, "Could not write OtherXML value");
            }
            else {
                for (unsigned j = 0; j < get_attr_num(i); ++j) {
                    if (xmlTextWriterStartElement(xml.get_writer(), (const xmlChar*) "value") < 0)
                        throw InternalErr(__FILE__, __LINE__, "Could not write value element");

                    if (xmlTextWriterWriteString(xml.get_writer(), (const xmlChar*) get_attr(i, j).c_str()) < 0)
                        throw InternalErr(__FILE__, __LINE__, "Could not write attribute value");

                    if (xmlTextWriterEndElement(xml.get_writer()) < 0)
                        throw InternalErr(__FILE__, __LINE__, "Could not end value element");
                }
            }
            if (xmlTextWriterEndElement(xml.get_writer()) < 0)
                throw InternalErr(__FILE__, __LINE__, "Could not end Attribute element");
        }
    }
}

/** Write the DAP4 XML representation for this attribute table. This
 * method is used to build the DAP4 DMR response object.
 *
 * @param xml An XMLWriter that will do the serialization
 */
void
AttrTable::print_dap4(XMLWriter &xml)
{
    print_xml_writer(xml);
}

/** @brief dumps information about this object
 *
 * Displays the pointer value of this instance and all attributes stored
 *
 * @param strm C++ i/o stream to dump the information to
 * @return void
 */
void AttrTable::dump(ostream &strm) const
{
    strm << DapIndent::LMarg << "AttrTable::dump - (" << (void *) this << ")" << endl;
    DapIndent::Indent();
    strm << DapIndent::LMarg << "table name: " << d_name << endl;
    if (attr_map.size()) {
        strm << DapIndent::LMarg << "attributes: " << endl;
        DapIndent::Indent();
        Attr_citer i = attr_map.begin();
        Attr_citer ie = attr_map.end();
        for (; i != ie; ++i) {
            entry *e = (*i);
            string type = AttrType_to_String(e->type);
            if (e->is_alias) {
                strm << DapIndent::LMarg << "alias: " << e->name << " aliased to: " << e->aliased_to << endl;
            }
            else if (e->type == Attr_container) {
                strm << DapIndent::LMarg << "attr: " << e->name << " of type " << type << endl;
                DapIndent::Indent();
                e->attributes->dump(strm);
                DapIndent::UnIndent();
            }
            else {
                strm << DapIndent::LMarg << "attr: " << e->name << " of type " << type << endl;
                DapIndent::Indent();
                strm << DapIndent::LMarg;
                vector<string>::const_iterator iter = e->attr->begin();
                vector<string>::const_iterator last = e->attr->end() - 1;
                for (; iter != last; ++iter) {
                    strm << (*iter) << ", ";
                }
                strm << (*(e->attr->end() - 1)) << endl;
                DapIndent::UnIndent();
            }
        }
        DapIndent::UnIndent();
    }
    else {
        strm << DapIndent::LMarg << "attributes: empty" << endl;
    }
    if (d_parent) {
        strm << DapIndent::LMarg << "parent table:" << d_name << ":" << (void *) d_parent << endl;
    }
    else {
        strm << DapIndent::LMarg << "parent table: none" << d_name << endl;
    }
    DapIndent::UnIndent();
}

} // namespace libdap