File: importxml.c

package info (click to toggle)
denemo 0.5.9-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,500 kB
  • ctags: 2,415
  • sloc: ansic: 23,057; sh: 3,321; yacc: 1,737; makefile: 449; lex: 376
file content (1683 lines) | stat: -rw-r--r-- 42,368 bytes parent folder | download
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
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
/* importxml.c
 * Import Denemo's "native" XML file format into a Denemo score structure
 *
 * for Denemo, a gtk+ frontend to GNU Lilypond
 * (c) 2001 Eric Galluzzo
 */

#include "chordops.h"
#include "graceops.h"
#include "importxml.h"
#include "measureops.h"
#include "objops.h"
#include "scoreops.h"
#include "staffops.h"
#include "tupletops.h"
#include "xmldefs.h"
#include <string.h>
#include <glib.h>

/* libxml includes: for libxml2 this should be <libxml/{parser,tree}.h> */
#include <libxml/parser.h>
#include <libxml/tree.h>


/* Defines for making traversing XML trees easier */

#define FOREACH_CHILD_ELEM(childElem, parentElem) \
for ((childElem) = (parentElem)->xmlChildrenNode; \
     (childElem) != NULL; \
     (childElem) = (childElem)->next)

#define ELEM_NAME_EQ(childElem, childElemName) \
(strcmp ((childElem)->name, (childElemName)) == 0)

#define ILLEGAL_ELEM(parentElemName, childElem) \
do \
  { \
    g_warning ("Illegal element inside <%s>: <%s>", parentElemName, \
               (childElem)->name); \
  } while (0)

#define RETURN_IF_ELEM_NOT_FOUND(parentElemName, childElem, childElemName) \
do \
  { \
    if (childElem == NULL) \
      { \
        g_warning ("Element <%s> not found inside <%s>", childElemName, \
                   parentElemName); \
        return -1; \
      } \
  } while (0)


/* The list of handlers for elements in other XML namespaces */
static GList *sImportHandlers = NULL;

/*
 * The global XML ID to element map
 *
 * FIXME: This won't work for multi-threaded apps.
 */
static GHashTable *sXMLIDToElemMap = NULL;
/*
 * The previous staff element that we came across
 *
 * FIXME: This won't work for multi-threaded apps, but I'm too lazy to pass
 *        this silly thing around everywhere.
 */
static xmlNodePtr sPrevStaffElem = NULL;


/*
 * Free a hash table key.  This function is suitable for use with
 * g_hash_table_foreach.
 */
static void
freeHashTableKey (gpointer key, gpointer value, gpointer userData)
{
  g_free (key);
}


/*
 * Recursively descend the tree starting from node elem and add any element
 * which has an "id" attribute to the map.
 */
static void
buildXMLIDMapForChildren (xmlNodePtr elem)
{
  xmlNodePtr childElem;
  gchar *id;
  if (elem != NULL)
    {
      id = xmlGetProp (elem, "id");
      if (id != NULL)
	{
	  g_hash_table_insert (sXMLIDToElemMap, id, elem);
	}

      FOREACH_CHILD_ELEM (childElem, elem)
      {
	buildXMLIDMapForChildren (childElem);
      }
    }
}


/*
 * Build the global (ick) XML ID to element map from the given document.  We
 * consider any attribute with name "id" to be of type ID.  Not technically
 * correct, but currently true.
 */
static void
buildXMLIDToElemMap (xmlDocPtr doc)
{
  sXMLIDToElemMap = g_hash_table_new (g_str_hash, g_str_equal);

  buildXMLIDMapForChildren (xmlDocGetRootElement (doc));
}


/*
 * Try to find the given XML ID in the ID -> XML element map.  If it's found,
 * return it; if not, return NULL.
 */
static xmlNodePtr
lookupXMLID (gchar * id)
{
  return g_hash_table_lookup (sXMLIDToElemMap, id);
}


/*
 * Get the text from the child node list of elem, convert it to an integer,
 * and return it.  If unsuccessful, return G_MAXINT.
 */
static gint
getXMLIntChild (xmlNodePtr elem)
{
  gchar *text = xmlNodeListGetString (elem->doc, elem->xmlChildrenNode, 1);
  gint num = G_MAXINT;
  if (text == NULL)
    {
      g_warning ("No child text found");
    }
  else
    {
      if (sscanf (text, " %d", &num) != 1)
	{
	  g_warning ("Could not convert child text \"%s\" of <%s> to number",
		     text, elem->name);
	  num = G_MAXINT;
	}
      g_free (text);
    }
  return num;
}


/*
 * Try to find the element with the given name and namespace as an immediate
 * child of the given parent element.  If found, return the child element; if
 * not, return NULL.
 */
static xmlNodePtr
getXMLChild (xmlNodePtr parentElem, gchar * childElemName, xmlNsPtr childNs)
{
  xmlNodePtr childElem;

  FOREACH_CHILD_ELEM (childElem, parentElem)
    if (ELEM_NAME_EQ (childElem, childElemName) && childElem->ns == childNs)
    return childElem;

  return NULL;
}


/*
 * Return the numerator and denominator from the given XML fraction.
 */
static gint
parseFraction (xmlNodePtr parentElem, xmlNsPtr ns,
	       gint * numerator, gint * denominator)
{
  xmlNodePtr childElem;
  gboolean gotNumerator = FALSE, gotDenominator = FALSE;
  gboolean gotCorrectNumerator = FALSE, gotCorrectDenominator = FALSE;

  FOREACH_CHILD_ELEM (childElem, parentElem)
  {
    if (childElem->ns == ns)
      {
	if (ELEM_NAME_EQ (childElem, "numerator"))
	  {
	    if (gotNumerator)
	      {
		g_warning ("Two numerators in the same fraction under <%s>",
			   parentElem->name);
		gotCorrectNumerator = FALSE;
	      }
	    else
	      {
		gotNumerator = TRUE;
		*numerator = getXMLIntChild (childElem);
		if (*numerator == G_MAXINT)
		  *numerator = 1;
		else
		  gotCorrectNumerator = TRUE;
	      }
	  }
	else if (ELEM_NAME_EQ (childElem, "denominator"))
	  {
	    if (gotDenominator)
	      {
		g_warning ("Two numerators in the same fraction under <%s>",
			   parentElem->name);
		gotCorrectDenominator = FALSE;
	      }
	    else
	      {
		gotDenominator = TRUE;
		*denominator = getXMLIntChild (childElem);
		if (*denominator == G_MAXINT)
		  *denominator = 1;
		else
		  gotCorrectDenominator = TRUE;
	      }
	  }
      }
    else
      {
	ILLEGAL_ELEM (parentElem->name, childElem);
      }
  }

  if (!gotNumerator)
    {
      g_warning
	("Fraction's numerator not found inside <%s>; defaulting to 1",
	 parentElem->name);
      *numerator = 1;
    }

  if (!gotDenominator)
    {
      g_warning
	("Fraction's denominator not found inside <%s>; defaulting to " "1",
	 parentElem->name);
      *denominator = 1;
    }

  return (gotCorrectNumerator && gotCorrectDenominator) ? 0 : -1;
}


/*
 * From the given XML accidental name, determine the accidental shift.
 */
static gint
determineAccidentalShift (gchar * accidentalName)
{
  if (strcmp (accidentalName, "natural") == 0)
    return 0;
  else if (strcmp (accidentalName, "sharp") == 0)
    return 1;
  else if (strcmp (accidentalName, "flat") == 0)
    return -1;
  else if (strcmp (accidentalName, "double-sharp") == 0)
    return 2;
  else if (strcmp (accidentalName, "double-flat") == 0)
    return -2;
  else
    {
      g_warning ("Unknown accidental \"%s\"; defaulting to natural",
		 accidentalName);
      return 0;
    }
}

/*
 * Parse the given <dynamic> into a dynamic mudelaobject.
 */
static void
parseDynamic (xmlNodePtr dynamicElem, mudelaobject *curobj)
{
  gchar *dynamicName = xmlGetProp (dynamicElem, "name");
  /*mudelaobject *result = NULL;*/
  GString *dynString = g_string_new(dynamicName);
  if (dynamicName == NULL)
    {
      g_warning ("No \"name\" attribute on <dynamic> element; ignoring");
    }
  else
    {
      curobj->u.chordval.dynamics =
	g_list_append(curobj->u.chordval.dynamics,dynString);
      g_free (dynamicName);
    }

  //return result;
}

/*
 * Parse the given <clef> element into a numeric clef type.
 */
static void
parseClef (xmlNodePtr clefElem, xmlNsPtr ns, gint * clefType)
{
  gchar *clefTypeName = xmlGetProp (clefElem, "name");
  if (clefTypeName == NULL)
    {
      g_warning ("No clef name specified; defaulting to treble");
      *clefType = TREBLE;
    }
  else if (strcmp (clefTypeName, "treble") == 0)
    *clefType = TREBLE;
  else if (strcmp (clefTypeName, "bass") == 0)
    *clefType = BASS;
  else if (strcmp (clefTypeName, "alto") == 0)
    *clefType = ALTO;
  else if (strcmp (clefTypeName, "treble-8vb") == 0)
    *clefType = G_8;
  else if (strcmp (clefTypeName, "tenor") == 0)
    *clefType = TENOR;
  else if (strcmp (clefTypeName, "soprano") == 0)
    *clefType = SOPRANO;
  else
    {
      g_warning ("Unknown clef type \"%s\"; defaulting to treble",
		 clefTypeName);
      *clefType = TREBLE;
    }
}


/*
 * Parse the given <key-signature> element into a key signature (with the
 * given number of sharps plus whether it's minor or not).
 */
static void
parseKeySignature (xmlNodePtr keySigElem, xmlNsPtr ns,
		   gint * keySig, gboolean * isMinor)
{
  xmlNodePtr childElem;
  gint childCount = 0;
  gboolean successful = FALSE;
  gchar *noteName, *accidentalName, *modeName;
  gint note, accidental;
  gboolean noteNameNull = FALSE;
  gboolean accidentalNameNull = FALSE;
  gboolean modeNameNull = FALSE;

  FOREACH_CHILD_ELEM (childElem, keySigElem)
  {
    if (childElem->ns == ns)
      {
	if (++childCount > 1)
	  {
	    /* Only print this warning once (when the child count is 2). */
	    if (childCount == 2)
	      g_warning ("<key-signature> element should only have one "
			 "child (<modal-key-signature>)");
	  }
	else
	  {
	    if (ELEM_NAME_EQ (childElem, "modal-key-signature"))
	      {
		noteName = xmlGetProp (childElem, "note-name");
		accidentalName = xmlGetProp (childElem, "accidental");
		modeName = xmlGetProp (childElem, "mode");

		if (noteName == NULL)
		  {
		    noteNameNull = TRUE;
		    g_warning ("<modal-key-signature> should have a "
			       "note-name attribute; defaulting to C");
		    noteName = "C";
		  }
		if (accidentalName == NULL)
		  {
		    accidentalNameNull = TRUE;
		    accidentalName = "natural";
		  }
		if (modeName == NULL)
		  {
		    modeNameNull = TRUE;
		    g_warning ("<modal-key-signature> should have a "
			       "mode attribute; defaulting to major");
		    modeName = "major";
		  }

		/* Translate note name (A to G) into note number (0 to 6). */

		note = noteName[0] - 'A';
		if (strlen (noteName) != 1 || note < 0 || note > 6)
		  {
		    g_warning ("<modal-key-signature> note name should be A "
			       "through G, received \"%s\"; defaulting to C",
			       noteName);
		    note = 2;
		  }

		accidental = determineAccidentalShift (accidentalName);

		/* Try to determine the base key signature. */

		if (note == 2 && accidental == -1)
		  *keySig = -7;
		else if (note == 6 && accidental == -1)
		  *keySig = -6;
		else if (note == 3 && accidental == -1)
		  *keySig = -5;
		else if (note == 0 && accidental == -1)
		  *keySig = -4;
		else if (note == 4 && accidental == -1)
		  *keySig = -3;
		else if (note == 1 && accidental == -1)
		  *keySig = -2;
		else if (note == 5 && accidental == 0)
		  *keySig = -1;
		else if (note == 2 && accidental == 0)
		  *keySig = 0;
		else if (note == 6 && accidental == 0)
		  *keySig = 1;
		else if (note == 3 && accidental == 0)
		  *keySig = 2;
		else if (note == 0 && accidental == 0)
		  *keySig = 3;
		else if (note == 4 && accidental == 0)
		  *keySig = 4;
		else if (note == 1 && accidental == 0)
		  *keySig = 5;
		else if (note == 5 && accidental == 1)
		  *keySig = 6;
		else if (note == 2 && accidental == 1)
		  *keySig = 7;
		else if (note == 6 && accidental == 1)
		  *keySig = 8;
		else if (note == 3 && accidental == 1)
		  *keySig = 9;
		else if (note == 0 && accidental == 1)
		  *keySig = 10;
		else
		  {
		    g_warning ("Unknown key signature with note name %s and "
			       "accidental %s; defaulting to C", noteName,
			       accidentalName);
		    *keySig = 0;
		  }

		/* Determine whether it's major or minor. */

		if (strcmp (modeName, "major") == 0)
		  *isMinor = FALSE;
		else if (strcmp (modeName, "minor") == 0)
		  *isMinor = TRUE;
		else
		  {
		    g_warning ("Unknown mode %s; defaulting to major",
			       modeName);
		    *isMinor = FALSE;
		  }

		successful = TRUE;

		if (!noteNameNull) g_free (noteName);
		if (!accidentalNameNull) g_free (accidentalName);
		if (!modeNameNull) g_free (modeName);
	      }
	  }
      }

    /*
     * Note: We can ignore other namespaces because the generic "parse this
     *       mudelaobject" code takes care of them for us.
     */
  }

  if (childCount == 0)
    g_warning ("<key-signature> element should have a child in the Denemo "
	       "namespace (<modal-key-signature>); defaulting to C Major");

  if (!successful)
    {
      *keySig = 0;
      *isMinor = FALSE;
    }

  if (*isMinor)
    *keySig -= 3;
}


/*
 * Parse the given <key-signature> element into a key signature (with the
 * given number of sharps plus whether it's minor or not).
 */
static void
parseTimeSignature (xmlNodePtr timeSigElem, xmlNsPtr ns,
		    gint * numerator, gint * denominator)
{
  xmlNodePtr childElem;
  gint childCount = 0;
  gboolean successful = FALSE;

  FOREACH_CHILD_ELEM (childElem, timeSigElem)
  {
    if (childElem->ns == ns)
      {
	if (++childCount > 1)
	  {
	    /* Only print this warning once (when the child count is 2). */
	    if (childCount == 2)
	      g_warning ("<time-signature> element should only have one "
			 "child (<simple-time-signature>)");
	  }
	else
	  {
	    if (ELEM_NAME_EQ (childElem, "simple-time-signature"))
	      {
		if (parseFraction (childElem, ns, numerator, denominator)
		    != 0)
		  g_warning ("Could not parse <simple-time-signature>; "
			     "defaulting to 4/4");
		else
		  successful = TRUE;
	      }
	  }
      }

    /*
     * Note: We can ignore other namespaces because the generic "parse this
     *       mudelaobject" code takes care of them for us.
     */
  }

  if (childCount == 0)
    g_warning ("<time-signature> element should have a child in the Denemo "
	       "namespace (<simple-time-signature>); defaulting to 4/4");

  if (!successful)
    {
      *numerator = 4;
      *denominator = 4;
    }
}


/*
 * Parse the given <note> element into a note structure and add it to the
 * given chord.
 */
static void
parseNote (xmlNodePtr noteElem, xmlNsPtr ns,
	   struct scoreinfo *si, mudelaobject * chordObj, gint currentClef)
{
  xmlNodePtr childElem;
  gint middleCOffset = 0, accidental = 0, noteHeadType = NORMAL;
  gboolean showAccidental = FALSE;
  gchar *accidentalName, *showAccidentalProp, *noteHeadName;

  FOREACH_CHILD_ELEM (childElem, noteElem)
  {
    if (childElem->ns == ns)
      {
	if (ELEM_NAME_EQ (childElem, "middle-c-offset"))
	  {
	    middleCOffset = getXMLIntChild (childElem);
	    if (middleCOffset == G_MAXINT)
	      {
		g_warning ("Couldn't get middle C offset; defaulting to 0");
		middleCOffset = 0;
	      }
	  }
	else if (ELEM_NAME_EQ (childElem, "accidental"))
	  {
	    accidentalName = xmlGetProp (childElem, "name");
	    if (accidentalName == NULL)
	      {
		g_warning ("<accidental> had no name attribute; defaulting "
			   "to natural");
		accidental = 0;
	      }
	    else if (strcmp (accidentalName, "natural") == 0)
	      accidental = 0;
	    else if (strcmp (accidentalName, "flat") == 0)
	      accidental = -1;
	    else if (strcmp (accidentalName, "sharp") == 0)
	      accidental = 1;
	    else if (strcmp (accidentalName, "double-flat") == 0)
	      accidental = -2;
	    else if (strcmp (accidentalName, "double-sharp") == 0)
	      accidental = 2;
	    else
	      {
		g_warning ("Unknown accidental name \"%s\"; defaulting to "
			   "natural", accidentalName);
		accidental = 0;
	      }
	    g_free (accidentalName);

	    showAccidentalProp = xmlGetProp (childElem, "show");
	    if (showAccidentalProp != NULL)
	      {
		if (strcmp (showAccidentalProp, "true") == 0)
		  showAccidental = TRUE;
		else if (strcmp (showAccidentalProp, "false") == 0)
		  showAccidental = FALSE;
		else
		  {
		    g_warning ("Unknown show accidental attribute value "
			       "\"%s\" (should be true or false); "
			       "defaulting to false", showAccidentalProp);
		  }
		g_free (showAccidentalProp);
	      }
	  }
	else if (ELEM_NAME_EQ (childElem, "note-head"))
	  {
	    noteHeadName = xmlGetProp (childElem, "type");
	    if (noteHeadName == NULL)
	      {
		g_warning ("<note-head> element had no type attribute; "
			   "defaulting to normal");
		noteHeadType = NORMAL;
	      }
	    else if (strcmp (noteHeadName, "normal") == 0)
	      noteHeadType = NORMAL;
	    else if (strcmp (noteHeadName, "cross") == 0)
	      noteHeadType = CROSS;
	    else if (strcmp (noteHeadName, "harmonic") == 0)
	      noteHeadType = HARMONIC;
	    else if (strcmp (noteHeadName, "diamond") == 0)
	      noteHeadType = DIAMOND;
	    else
	      {
		g_warning ("Unknown notehead type \"%s\"; defaulting to "
			   "normal", noteHeadName);
		noteHeadType = NORMAL;
	      }
	    g_free (noteHeadName);
	  }
      }

    /*
     * The code near the bottom of parseMeasures() handles other namespaces
     * inside <note>s, so we won't do anything here.
     */
  }

  /* Now actually construct the note object. */

  addtone (chordObj, middleCOffset, accidental, currentClef);
  if (noteHeadType != NORMAL)
    {
      /* FIXME: Is this the right note in the chord? */
      ((note *) chordObj->u.chordval.tones->data)->noteheadtype = noteHeadType;
    }
}


/*
 * Parse the given <chord> or <rest>'s duration only, not any other
 * subelements, and return a simple mudelaobject corresponding to that chord
 * or rest.
 */
static mudelaobject *
parseBaseChord (xmlNodePtr chordElem, xmlNsPtr ns, struct scoreinfo *si)
{
  xmlNodePtr durationElem, dotsElem;
  gboolean successful = FALSE;
  gchar *durationType;
  gint baseDuration=1, numDots = 0;

  /*
   * First, in order to actually create a chord object, we must figure out the
   * Denemo duration and number of dots from the XML duration.
   */
  durationElem = getXMLChild (chordElem, "duration", ns);
  if (durationElem == NULL)
    {
      g_warning ("No duration found in chord; defaulting to quarter note");
    }
  else
    {
      durationType = xmlGetProp (durationElem, "base");
      if (durationType == NULL)
	{
	  g_warning ("No base attribute found in chord duration; defaulting "
		     "to quarter note");
	}
      else
	{
	  if (strcmp (durationType, "whole") == 0)
	    baseDuration = 0;
	  else if (strcmp (durationType, "half") == 0)
	    baseDuration = 1;
	  else if (strcmp (durationType, "quarter") == 0)
	    baseDuration = 2;
	  else if (strcmp (durationType, "eighth") == 0)
	    baseDuration = 3;
	  else if (strcmp (durationType, "sixteenth") == 0)
	    baseDuration = 4;
	  else if (strcmp (durationType, "thirty-second") == 0)
	    baseDuration = 5;
	  else if (strcmp (durationType, "sixty-fourth") == 0)
	    baseDuration = 6;
	  else
	    {
	      g_warning ("Unknown base duration type \"%s\"; defaulting to "
			 "quarter note", durationType);
	    }
	  g_free (durationType);

	  successful = TRUE;
	  dotsElem = getXMLChild (durationElem, "dots", ns);
	  if (dotsElem != NULL)
	    {
	      numDots = getXMLIntChild (dotsElem);
	      if (numDots == G_MAXINT)
		{
		  g_warning ("Invalid number of dots; defaulting to 0");
		  numDots = 0;
		}
	    }
	}
    }

  if (!successful)
    {
      baseDuration = 2;
      numDots = 0;
    }

  return newchord (baseDuration, numDots);
}


/*
 * Parse the given <rest> element and return a chord-type mudelaobject.
 */
static mudelaobject *
parseRest (xmlNodePtr restElem, xmlNsPtr ns, struct scoreinfo *si)
{
  gchar *showProp = xmlGetProp (restElem, "show");
  gboolean show = TRUE;
  if (showProp != NULL)
    {
      if (strcmp (showProp, "true") == 0)
	show = TRUE;
      else if (strcmp (showProp, "false") == 0)
	show = FALSE;
      else
	{
	  g_warning ("Invalid value for show attribute of <rest>: \"%s\"; "
		     "defaulting to false", showProp);
	  show = FALSE;
	}
    }

  if (show)
    return parseBaseChord (restElem, ns, si);
  else
    return NULL;
}


/*
 * Parse the given <decoration> element into the given chord-type mudelaobject.
 */
static void
parseDecoration (xmlNodePtr decorationElem, mudelaobject * chordObj)
{
  gchar *decorationType = xmlGetProp (decorationElem, "type");

  if (decorationType == NULL)
    g_warning ("No \"type\" attribute present on <decoration> element; "
	       "ignoring");
  else if (strcmp (decorationType, "accent") == 0)
    chordObj->u.chordval.is_accented_p = TRUE;
  else if (strcmp (decorationType, "down-bow") == 0)
    chordObj->u.chordval.has_dbow_p = TRUE;
  else if (strcmp (decorationType, "fermata") == 0)
    chordObj->u.chordval.has_fermata_p = TRUE;
  else if (strcmp (decorationType, "left-heel") == 0)
    chordObj->u.chordval.has_lheel_p = TRUE;
  else if (strcmp (decorationType, "left-toe") == 0)
    chordObj->u.chordval.has_ltoe_p = TRUE;
  else if (strcmp (decorationType, "marcato") == 0)
    chordObj->u.chordval.has_marcato_p = TRUE;
  else if (strcmp (decorationType, "mordent") == 0)
    chordObj->u.chordval.has_mordent_p = TRUE;
  else if (strcmp (decorationType, "right-heel") == 0)
    chordObj->u.chordval.has_rheel_p = TRUE;
  else if (strcmp (decorationType, "right-toe") == 0)
    chordObj->u.chordval.has_rtoe_p = TRUE;
  else if (strcmp (decorationType, "staccato") == 0)
    chordObj->u.chordval.has_stacatto_p = TRUE;
  else if (strcmp (decorationType, "staccatissimo") == 0)
    chordObj->u.chordval.has_staccatissimo_p = TRUE;
  else if (strcmp (decorationType, "tenuto") == 0)
    chordObj->u.chordval.has_tenuto_p = TRUE;
  else if (strcmp (decorationType, "trill") == 0)
    chordObj->u.chordval.has_trill_p = TRUE;
  else if (strcmp (decorationType, "turn") == 0)
    chordObj->u.chordval.has_turn_p = TRUE;
  else if (strcmp (decorationType, "up-bow") == 0)
    chordObj->u.chordval.has_ubow_p = TRUE;
  else
    g_warning ("Unknown decoration type \"%s\"; ignoring", decorationType);

  g_free (decorationType);
}


/*
 * Parse the given <chord> element and return a chord-type mudelaobject.
 */
static mudelaobject *
parseChord (xmlNodePtr chordElem, xmlNsPtr ns,
	    struct scoreinfo *si,
	    gint currentClef, GList ** slurEndChordElems,
	    xmlNodePtr * notesElem)
{
  mudelaobject *chordObj = parseBaseChord (chordElem, ns, si);
  xmlNodePtr childElem, grandchildElem, slurEndElem;
  gchar *slurEndXMLID;
  GList *thisSlurListNode;

  *notesElem = NULL;

  /* Check to see if this is the end of a slur. */

  thisSlurListNode = g_list_find (*slurEndChordElems, chordElem);
  if (thisSlurListNode != NULL)
    {
      chordObj->u.chordval.slur_end_p = TRUE;
      *slurEndChordElems = g_list_remove_link (*slurEndChordElems,
					       thisSlurListNode);
      g_list_free_1 (thisSlurListNode);
    }

  FOREACH_CHILD_ELEM (childElem, chordElem)
  {
    if (childElem->ns == ns)
      {
	if (ELEM_NAME_EQ (childElem, "duration"))
	  {
	    /* This was already parsed during parseBaseChord(). */
	  }
	else if(ELEM_NAME_EQ(childElem, "dynamic"))
	  {
	    parseDynamic(childElem, chordObj);
	  }
	else if (ELEM_NAME_EQ (childElem, "decorations"))
	  {
	    FOREACH_CHILD_ELEM (grandchildElem, childElem)
	    {
	      if (grandchildElem->ns == ns
		  && ELEM_NAME_EQ (grandchildElem, "decoration"))
		{
		  parseDecoration (grandchildElem, chordObj);
		}
	      else
		{
		  ILLEGAL_ELEM ("decorations", grandchildElem);
		}
	    }
	  }
	else if (ELEM_NAME_EQ (childElem, "tie"))
	  {
	    /* For now at least, ignore the "to" attribute. */
	    chordObj->u.chordval.is_tied = TRUE;
	  }
	else if (ELEM_NAME_EQ (childElem, "slurs"))
	  {
	    FOREACH_CHILD_ELEM (grandchildElem, childElem)
	    {
	      if (grandchildElem->ns == ns
		  && ELEM_NAME_EQ (grandchildElem, "slur"))
		{
		  /*
		   * Prepend the slur's end element to the list that we're
		   * keeping.
		   */

		  slurEndXMLID = xmlGetProp (grandchildElem, "to");
		  if (slurEndXMLID == NULL)
		    {
		      g_warning ("No \"to\" attribute on <slur> element; "
				 "ignoring");
		    }
		  else
		    {
		      slurEndElem = lookupXMLID (slurEndXMLID);
		      if (slurEndElem == NULL)
			{
			  g_warning ("Chord is slurred to nonexistent "
				     "chord with ID \"%s\"", slurEndXMLID);
			}
		      else if (!ELEM_NAME_EQ (slurEndElem, "chord"))
			{
			  g_warning ("Chord is slurred to non-chord "
				     "element with tag name \"%s\" and ID "
				     "\"%s\"", slurEndElem->name,
				     slurEndXMLID);
			}
		      else if (slurEndElem == chordElem)
			{
			  g_warning ("Can't slur a chord to itself");
			}
		      else
			{
			  chordObj->u.chordval.slur_begin_p = TRUE;
			  *slurEndChordElems =
			    g_list_prepend (*slurEndChordElems, slurEndElem);
			}
		      g_free (slurEndXMLID);
		    }
		}
	      else
		{
		  ILLEGAL_ELEM ("slurs", grandchildElem);
		}
	    }
	  }
	else if (ELEM_NAME_EQ (childElem, "notes"))
	  {
	    *notesElem = childElem;
	    FOREACH_CHILD_ELEM (grandchildElem, childElem)
	    {
	      if (grandchildElem->ns == ns
		  && ELEM_NAME_EQ (grandchildElem, "note"))
		{
		  parseNote (grandchildElem, ns, si, chordObj, currentClef);
		}
	      else
		{
		  ILLEGAL_ELEM ("notes", grandchildElem);
		}
	    }
	  }
	else
	  {
	    ILLEGAL_ELEM ("chord", childElem);
	  }
      }				/* end if childElem->ns == ns */

    /*
     * No need to handle other namespaces here, the generic mudelaobject
     * code will do it for us.
     */

  }				/* end for each childElem in scoreElem */

  return chordObj;
}


/*
 * Parse the given <stem-directive> element into a mudelaobject.
 */
static mudelaobject *
parseStemDirective (xmlNodePtr stemDirectiveElem,
		    xmlNsPtr ns, struct scoreinfo *si)
{
  gchar *stemDirName = xmlGetProp (stemDirectiveElem, "type");
  gint stemDir = STEMBOTH;

  if (stemDirName == NULL)
    g_warning ("No \"type\" attribute on <stem-directive> element; "
	       "defaulting to auto");
  else if (strcmp (stemDirName, "auto") == 0)
    stemDir = STEMBOTH;
  else if (strcmp (stemDirName, "up") == 0)
    stemDir = STEMUP;
  else if (strcmp (stemDirName, "down") == 0)
    stemDir = STEMDOWN;
  else
    g_warning ("Invalid stem directive type \"%s\"; defaulting to auto",
		stemDirName);

  return stem_directive_new (stemDir);
}


/*
 * Parse the given <tuplet-start> into a tuplet open mudelaobject.
 */
static mudelaobject *
parseTupletStart (xmlNodePtr tupletStartElem,
		  xmlNsPtr ns, struct scoreinfo *si)
{
  xmlNodePtr multiplierElem = getXMLChild (tupletStartElem, "multiplier", ns);
  gboolean successful = FALSE;
  gint numerator, denominator;

  if (multiplierElem == NULL)
    {
      g_warning ("No <multiplier> element found under <tuplet-start>; "
		 "defaulting to 1/1");
    }
  else
    {
      if (parseFraction (multiplierElem, ns, &numerator, &denominator) == 0)
	{
	  successful = TRUE;
	}
      else
	{
	  g_warning ("Bad multiplier fraction found for <tuplet-start>; "
		     "defaulting to 1/1");
	}
    }

  if (!successful)
    {
      numerator = 1;
      denominator = 1;
    }

  return newtupopen (numerator, denominator);
}


/*
 * Parse the given <grace-start> into a grace-start mudelaobject.
 */
static mudelaobject *
parseGraceStart (xmlNodePtr graceStartElem, xmlNsPtr ns, struct scoreinfo *si)
{
  gchar *onBeatStr = xmlGetProp (graceStartElem, "on-beat");
  gboolean onBeat = FALSE;
  mudelaobject *result = newgracestart ();

  if (onBeatStr != NULL)
    {
      if (!strcmp (onBeatStr, "true"))
	onBeat = TRUE;
      else if (!strcmp (onBeatStr, "false"))
	onBeat = FALSE;
      else
	g_warning ("Invalid value \"%s\" for \"on-beat\" attribute on "
		   "<grace-start> element; defaulting to \"false\"",
		   onBeatStr);
    }
  result->u.graceval.on_beat = onBeat;

  return result;
}


/*
 * Parse the given <score-info> element into the given score.
 */
static gint
parseScoreInfo (xmlNodePtr scoreInfoElem, xmlNsPtr ns, struct scoreinfo *si)
{
  xmlNodePtr childElem, grandchildElem;
  gint bpm;
  gchar *title, *subtitle, *composer;

  FOREACH_CHILD_ELEM (childElem, scoreInfoElem)
  {
    if (childElem->ns == ns)
      {
	if (ELEM_NAME_EQ (childElem, "tempo"))
	  {
	    FOREACH_CHILD_ELEM (grandchildElem, childElem)
	    {
	      if (grandchildElem->ns == ns)
		{
		  if (ELEM_NAME_EQ (grandchildElem, "duration"))
		    {
		      /* Do nothing -- it's always going to be 1/4. */
		    }
		  else if (ELEM_NAME_EQ (grandchildElem, "bpm"))
		    {
		      bpm = getXMLIntChild (grandchildElem);
		      if (bpm == G_MAXINT)
			{
			  bpm = 60;
			  g_warning
			    ("Bad value for <bpm>: must be an integer");
			}
		      else
			{
			  si->tempo = bpm;
			}
		    }
		  else
		    {
		      ILLEGAL_ELEM ("tempo", grandchildElem);
		    }
		}
	      else
		{
		  ILLEGAL_ELEM ("tempo", grandchildElem);
		}
	    }
	  }
	else if (ELEM_NAME_EQ (childElem, "title"))
	  {
	    title = xmlNodeListGetString (childElem->doc,
					  childElem->xmlChildrenNode, 1);
	    if (title != NULL)
	      {
		g_string_assign (si->title, title);
		g_free (title);
	      }
	  }
	else if (ELEM_NAME_EQ (childElem, "subtitle"))
	  {
	    subtitle = xmlNodeListGetString (childElem->doc,
					     childElem->xmlChildrenNode, 1);
	    if (subtitle != NULL)
	      {
		g_string_assign (si->subtitle, subtitle);
		g_free (subtitle);
	      }
	  }
	else if (ELEM_NAME_EQ (childElem, "composer"))
	  {
	    composer = xmlNodeListGetString (childElem->doc,
					     childElem->xmlChildrenNode, 1);
	    if (composer != NULL)
	      {
		g_string_assign (si->composer, composer);
		g_free (composer);
	      }
	  }
      }
    else
      {
	/* FIXME: Change this so that other namespaces are handled here. */
	ILLEGAL_ELEM ("score-info", childElem);
      }
  }

  return 0;
}


/*
 * Parse the given <staff> element into the staff in the given score.
 */
static gint
parseStaff (xmlNodePtr staffElem, xmlNsPtr ns, struct scoreinfo *si)
{
  xmlNodePtr staffInfoElem, childElem;
  staff *curStaff = (staff *) si->currentprimarystaff->data;

  staffInfoElem = getXMLChild (staffElem, "staff-info", ns);
  RETURN_IF_ELEM_NOT_FOUND ("staff", staffInfoElem, "staff-info");

  FOREACH_CHILD_ELEM (childElem, staffInfoElem)
  {
    if (childElem->ns == ns)
      {
	if (ELEM_NAME_EQ (childElem, "number-of-lines"))
	  {
	    curStaff->no_of_lines = getXMLIntChild (childElem);
	    if (curStaff->no_of_lines == G_MAXINT)
	      {
		g_warning ("Could not determine number of lines in staff; "
			   "defaulting to 5");
		curStaff->no_of_lines = 5;
	      }
	  }
	else if(ELEM_NAME_EQ (childElem, "transpose"))
	  {
	    curStaff->transposition = getXMLIntChild (childElem);
	  }
	else if(ELEM_NAME_EQ (childElem, "instrument"))
	  {
	    gchar *temp = NULL;
	    temp = (gchar *) xmlNodeListGetString 
	      (childElem->doc, childElem->xmlChildrenNode, 1); 
	    strcpy(curStaff->midi_instrument->str,temp);
	  }
	else
	  {
	    ILLEGAL_ELEM ("staff-info", childElem);
	  }
      }
    else
      {
	/* FIXME: Handle other namespaces here. */
	ILLEGAL_ELEM ("staff-info", childElem);
      }
  }

  return 0;
}


/*
 * Parse the given <voice-info> element into the voice in the given score.
 */
static gint
parseVoiceInfo (xmlNodePtr voiceInfoElem, xmlNsPtr ns, struct scoreinfo *si)
{
  xmlNodePtr childElem;
  gchar *voiceName;

  FOREACH_CHILD_ELEM (childElem, voiceInfoElem)
  {
    if (childElem->ns == ns)
      {
	if (ELEM_NAME_EQ (childElem, "voice-name"))
	  {
	    voiceName = xmlNodeListGetString (childElem->doc,
					      childElem->xmlChildrenNode, 1);
	    if (voiceName != NULL)
	      {
		g_string_assign
		  (((staff *) si->currentstaff->data)->denemo_name,
		   voiceName);
		g_free (voiceName);
	      }
	  }
	else if (ELEM_NAME_EQ (childElem, "first-measure-number"))
	  {
	    /* Do nothing (yet).  All voices start at measure 1. */
	  }
	else
	  {
	    ILLEGAL_ELEM ("voice-info", childElem);
	  }
      }
    else
      {
	/* FIXME: Handle other namespaces here. */
	ILLEGAL_ELEM ("voice-info", childElem);
      }
  }

  return 0;
}


/*
 * Parse the given <initial-voice-params> element into the voice in the given
 * score.
 */
static gint
parseInitVoiceParams (xmlNodePtr initVoiceParamsElem, xmlNsPtr ns,
		      struct scoreinfo *si)
{
  staff *curVoice = (staff *) si->currentstaff->data;
  xmlNodePtr childElem, staffElem;
  gchar *staffXMLID;

  FOREACH_CHILD_ELEM (childElem, initVoiceParamsElem)
  {
    if (childElem->ns == ns)
      {
	if (ELEM_NAME_EQ (childElem, "staff-ref"))
	  {
	    /*
	     * FIXME: This all assumes that the staves and voices appear top
	     *        down in the XML file, which is true when we save it
	     *        but may not be if someone tinkers with the file.
	     */

	    staffXMLID = xmlGetProp (childElem, "staff");
	    if (staffXMLID == NULL)
	      {
		g_warning ("No ID found on <staff-ref> element");
	      }
	    else
	      {
		staffElem = lookupXMLID (staffXMLID);
		g_free (staffXMLID);

		if (staffElem == NULL)
		  {
		    g_warning ("Invalid staff ID specified in <staff-ref>");
		  }
		else if (staffElem == sPrevStaffElem)
		  {
		    /* This is a new voice on the previous staff. */

		    curVoice->voicenumber = 2;
		    curVoice->no_of_lines =
		      ((staff *) si->currentprimarystaff->data)->no_of_lines;
		  }
		else
		  {
		    /* This is a new staff. */

		    if (!ELEM_NAME_EQ (staffElem, "staff"))
		      {
			g_warning ("<staff-ref> points to a <%s>, not a "
				   "<staff>", staffElem->name);
		      }

		    si->currentprimarystaff = si->currentstaff;
		    if (parseStaff (staffElem, ns, si) != 0)
		      return -1;
		  }

		sPrevStaffElem = staffElem;
	      }
	  }
	else if (ELEM_NAME_EQ (childElem, "clef"))
	  {
	    parseClef (childElem, ns, &(curVoice->sclef));
	  }
	else if (ELEM_NAME_EQ (childElem, "key-signature"))
	  {
	    parseKeySignature (childElem, ns, &(curVoice->skey),
			       &(curVoice->skey_isminor));
	    initkeyaccs (curVoice->skeyaccs, curVoice->skey);
	  }
	else if (ELEM_NAME_EQ (childElem, "time-signature"))
	  {
	    parseTimeSignature (childElem, ns, &(curVoice->stime1),
				&(curVoice->stime2));
	  }
	else
	  {
	    ILLEGAL_ELEM ("initial-voice-params", childElem);
	  }
      }
    else
      {
	/* FIXME: Handle other namespaces here. */
	ILLEGAL_ELEM ("initial-voice-params", childElem);
      }
  }

  return 0;
}


/*
 * Parse the given <measures> element into the voice in the given score.
 */
static gint
parseMeasures (xmlNodePtr measuresElem, xmlNsPtr ns, struct scoreinfo *si)
{
  xmlNodePtr childElem, objElem, notesElem = NULL;
  mudelaobject *curObj, *prevChord = NULL;
  gboolean startedBeam = FALSE;
  gint currentClef = ((staff *) si->currentstaff->data)->sclef;
  gint newClef, keySig;
  gboolean isMinor;
  gint numerator, denominator;
  GList *slurEndChordElems = NULL;
  FOREACH_CHILD_ELEM (childElem, measuresElem)
  {
    if (ELEM_NAME_EQ (childElem, "measure") && (childElem->ns == ns))
      {
	if (si->currentmeasure == NULL)
	  {
	    si->currentmeasure =
	      addmeasures (si, si->currentmeasurenum - 1, 1);
	  }

	FOREACH_CHILD_ELEM (objElem, childElem)
	{
	  curObj = NULL;
	  notesElem = NULL;

	  if (objElem->ns == ns)
	    {
	      if (ELEM_NAME_EQ (objElem, "barline"))
		{
		  /* FIXME */
		  g_warning ("Cannot yet handle <barline> elements");
		}
	      else if (ELEM_NAME_EQ (objElem, "beam-end"))
		{
		  /* Ignore beam attribute for now. */
		  if (prevChord == NULL)
		    {
		      g_warning ("Received <beam-end> without chord "
				 "before it");
		    }
		  else
		    {
		      prevChord->isstart_beamgroup = FALSE;
		      prevChord->isend_beamgroup = TRUE;
		    }
		}
	      else if (ELEM_NAME_EQ (objElem, "beam-start"))
		{
		  /* Ignore ID for now. */
		  startedBeam = TRUE;
		}
	      else if (ELEM_NAME_EQ (objElem, "chord"))
		{
		  curObj = parseChord (objElem, ns, si, currentClef,
				       &slurEndChordElems, &notesElem);
		  if (startedBeam)
		    {
		      curObj->isstart_beamgroup = TRUE;
		      curObj->isend_beamgroup = FALSE;
		      startedBeam = FALSE;
		    }
		  prevChord = curObj;
		}
	      else if (ELEM_NAME_EQ (objElem, "clef"))
		{
		  parseClef (objElem, ns, &newClef);
		  curObj = newclefobj (newClef);
		  currentClef = newClef;
		}
	      /*else if (ELEM_NAME_EQ (objElem, "dynamic"))
		{
		  curObj = parseDynamic (objElem, ns, si);
		}*/
	      else if (ELEM_NAME_EQ (objElem, "grace-end"))
		{
		  curObj = newgraceend ();
		}
	      else if (ELEM_NAME_EQ (objElem, "grace-start"))
		{
		  curObj = parseGraceStart (objElem, ns, si);
		}
	      else if (ELEM_NAME_EQ (objElem, "key-signature"))
		{
		  parseKeySignature (objElem, ns, &keySig, &isMinor);
		  curObj = newkeyobj (keySig, isMinor);
		  initkeyaccs (curObj->u.keyval.accs, keySig);
		}
	      else if (ELEM_NAME_EQ (objElem, "measure-break"))
		{
		  /* FIXME */
		  g_warning ("Cannot yet handle <measure-break> elements");
		}
	      else if (ELEM_NAME_EQ (objElem, "rest"))
		{
		  curObj = parseRest (objElem, ns, si);
		  if (curObj)
		    {
		      if (startedBeam)
			{
			  curObj->isstart_beamgroup = TRUE;
			  curObj->isend_beamgroup = FALSE;
			  startedBeam = FALSE;
			}
		      prevChord = curObj;
		    }
		}
	      else if (ELEM_NAME_EQ (objElem, "stem-directive"))
		{
		  curObj = parseStemDirective (objElem, ns, si);
		}
	      else if (ELEM_NAME_EQ (objElem, "time-signature"))
		{
		  parseTimeSignature (objElem, ns, &numerator, &denominator);
		  curObj = newtimesigobj (numerator, denominator);
		}
	      else if (ELEM_NAME_EQ (objElem, "tuplet-end"))
		{
		  curObj = newtupclose ();
		}
	      else if (ELEM_NAME_EQ (objElem, "tuplet-start"))
		{
		  curObj = parseTupletStart (objElem, ns, si);
		}
	      else
		{
		  ILLEGAL_ELEM ("measure", objElem);
		}

	      if (curObj != NULL)
		{
		  /*
		   * FIXME: Appending is very inefficient!  But we can't
		   *        prepend then reverse when we're done because
		   *        the import handler callbacks expect the data
		   *        structures to be in the right order.  Instead,
		   *        we should manually keep track of the end of
		   *        the list (si->currentobject?) and append to
		   *        that.
		   */
		  si->currentmeasure->data =
		    g_list_append (si->currentmeasure->data, curObj);

		  /*
		   * FIXME: Loop through notesElem to search for
		   * subelements of <note> that are in other namespaces.
		   */
		}
	    }			/* end if objElem is in the right namespace */
	  else
	    {
	      /* FIXME: Handle other namespaces here. */
	      ILLEGAL_ELEM ("measure", objElem);
	    }
	}			/* end foreach objElem in childElem */
	si->currentmeasurenum++;
	si->currentmeasure = si->currentmeasure->next;
      }				/* end if childElem is a <measure> */
    else
      {
	ILLEGAL_ELEM ("measures", childElem);
      }
  }

  if (slurEndChordElems != NULL)
    {
      g_warning ("Some unterminated slurs were left at the end of the voice");
      g_list_free (slurEndChordElems);
    }

  return 0;
}


/*
 * Parse the given <voice> element into a voice in the given score.
 */
static gint
parseVoice (xmlNodePtr voiceElem, xmlNsPtr ns, struct scoreinfo *si)
{
  xmlNodePtr childElem;
  /*gchar *id;*/

  /* Create the staff structure. */

  si->currentstaffnum++;
  newstaff (si, ADDFROMLOAD, NULL);
  si->currentstaff = g_list_last (si->thescore);

  /* Parse the child elements. */

  childElem = getXMLChild (voiceElem, "voice-info", ns);
  RETURN_IF_ELEM_NOT_FOUND ("voice", childElem, "voice-info");
  if (parseVoiceInfo (childElem, ns, si) != 0)
    return -1;

  childElem = getXMLChild (voiceElem, "initial-voice-params", ns);
  RETURN_IF_ELEM_NOT_FOUND ("voice", childElem, "initial-voice-params");
  if (parseInitVoiceParams (childElem, ns, si) != 0)
    return -1;

  childElem = getXMLChild (voiceElem, "measures", ns);
  RETURN_IF_ELEM_NOT_FOUND ("voice", childElem, "measures");
  if (parseMeasures (childElem, ns, si) != 0)
    return -1;

  /* FIXME: Handle elements in other namespaces. */

  return 0;
}


/*
 * Parse the given <score> element into the given score.
 */
static gint
parseScore (xmlNodePtr scoreElem, xmlNsPtr ns, struct scoreinfo *si)
{
  xmlNodePtr childElem, voiceElem;

  childElem = getXMLChild (scoreElem, "score-info", ns);
  RETURN_IF_ELEM_NOT_FOUND ("score", childElem, "score-info");
  if (parseScoreInfo (childElem, ns, si) != 0)
    return -1;

  /*
   * Note: We don't currently care about <staves>, but we will need to
   *       eventually, since we should parse out all the elements in other
   *       namespaces that are children of <staff-info>.
   */

  childElem = getXMLChild (scoreElem, "voices", ns);
  RETURN_IF_ELEM_NOT_FOUND ("score", childElem, "voices");
  FOREACH_CHILD_ELEM (voiceElem, childElem)
  {
    if (ELEM_NAME_EQ (voiceElem, "voice") && (voiceElem->ns == ns))
      {
	if (parseVoice (voiceElem, ns, si) != 0)
	  return -1;
      }
    else
      {
	ILLEGAL_ELEM ("voices", voiceElem);
      }
  }

  FOREACH_CHILD_ELEM (childElem, scoreElem)
  {
    if (childElem->ns != ns)
      {
	/* FIXME: Handle other namespaces here! */
	ILLEGAL_ELEM ("score", childElem);
      }
  }

  return 0;
}


/*
 * Import the given (possibly zlib-compressed) Denemo "native" XML file into
 * the given score.
 */
gint
importXML (gchar * filename, struct scoreinfo * si)
{
  gint ret = 0;
  xmlDocPtr doc = NULL;
  xmlNsPtr ns;
  xmlNodePtr rootElem;/*, childElem;*/
  gchar *version = NULL;

  /* Try to parse the file. */

  doc = xmlParseFile (filename);
  if (doc == NULL)
    {
      g_warning ("Could not read XML file %s", filename);
      ret = -1;
      return -1;
    }

  /*
   * Do a couple of sanity checks to make sure we've actually got a Denemo
   * format XML file.
   */

  rootElem = xmlDocGetRootElement (doc);
  ns = rootElem->ns;
  if (strcmp (ns->href, DENEMO_XML_NAMESPACE) != 0)
    {
      g_warning ("Root element is not in Denemo namespace");
      ret = -1;
      goto cleanup;
    }
  if (strcmp (rootElem->name, "score") != 0)
    {
      g_warning ("Root element is not <score>");
      ret = -1;
      goto cleanup;
    }
  version = xmlGetProp (rootElem, "version");
  if (version == NULL)
    {
      g_warning ("No version found on root element");
      ret = -1;
      goto cleanup;
    }
  if (strcmp (version, "1.0") != 0)
    {
      g_warning ("Denemo XML file version %s found, but we can only handle "
		 "version 1.0", version);
      ret = -1;
      goto cleanup;
    }

  /*
   * Okay, we've got a bona fide, 100% genuine Denemo XML file (hopefully).
   * So let's parse it.  The first thing to do is to construct a map from IDs
   * (or rather, attributes with name "id") to their respective elements.
   */

  buildXMLIDToElemMap (doc);

  /* Then, parse the score. */

  free_score (si);
  si->currentstaffnum = 0;
  sPrevStaffElem = NULL;
  ret = parseScore (rootElem, ns, si);
  sPrevStaffElem = NULL;

cleanup:

  if (version != NULL)
    g_free (version);
  if (doc != NULL)
    xmlFreeDoc (doc);
  g_hash_table_foreach (sXMLIDToElemMap, freeHashTableKey, NULL);
  g_hash_table_destroy (sXMLIDToElemMap);
  sXMLIDToElemMap = NULL;

  return ret;
}