File: RSDTD.c

package info (click to toggle)
r-cran-xml 3.98-1.5-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 9,464 kB
  • ctags: 636
  • sloc: xml: 79,579; ansic: 6,518; asm: 644; sh: 16; makefile: 1
file content (785 lines) | stat: -rw-r--r-- 23,117 bytes parent folder | download | duplicates (4)
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
/**
  This file defines the top-level entry routine called from R and S to parse and convert
  a DTD into a user-level object.

  Most of the routines are support routines. We leave them as global symbols (as opposed to static) so that others might be
  able to utilize them. Some are called from the other files (DocParse, specifically).


 * See Copyright for the license status of this software.

 */

#include "RSDTD.h"

#ifdef USE_S
extern char *strdup(const char *);
#endif



#include "Utils.h" /* for SET_CLASS_NAME */

#include <sys/stat.h>

/* For reading DTDs directly from text, not files. 
   Copied directly from parser.c in the libxml(-1.7.3) library.
*/

#ifdef FROM_GNOME_XML_DIR
#include <gnome-xml/parserInternals.h>
#else
#include <libxml/parserInternals.h>
#endif


#define INPUT_CHUNK	250
#define CUR (ctxt->token ? ctxt->token : (*ctxt->input->cur))

#ifdef OLD_SKIP_BLANKS

#define SKIP_BLANKS 							\
    do { 								\
	while (IS_BLANK(CUR)) NEXT;					\
	if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt);	\
	if (*ctxt->input->cur == '&') xmlParserHandleReference(ctxt);	\
    } while (IS_BLANK(CUR));

#define NEXT {								\
    if (ctxt->token != 0) ctxt->token = 0;				\
    else {								\
    if ((*ctxt->input->cur == 0) &&					\
        (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) {		\
	    xmlPopInput(ctxt);						\
    } else {								\
        if (*(ctxt->input->cur) == '\n') {				\
	    ctxt->input->line++; ctxt->input->col = 1;			\
	} else ctxt->input->col++;					\
	ctxt->input->cur++;						\
        if (*ctxt->input->cur == 0)					\
	    xmlParserInputGrow(ctxt->input, INPUT_CHUNK);		\
    }									\
    if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt);	\
    if (*ctxt->input->cur == '&') xmlParserHandleReference(ctxt);	\
}}

#else

#define SKIP_BLANKS xmlSkipBlankChars(ctxt)
#define NEXT xmlNextChar(ctxt)

#endif


/* end temporary. */



/* Macro that sets the name of an enumerated value by indexing into an array of
   names based on the value being represented.
 */
#define SET_ENUM_NAME(names, which, obj) RS_XML_SetNames(1, RS_XML_##names+which-1,obj);



enum {DTD_ELEMENTS_SLOT, DTD_ENTITIES_SLOT, DTD_NUM_SLOTS};
const char *RS_XML(DtdNames)[] = {"elements", "entities"};
/**
  Top-level entry point for reading the DTD.
   dtdFileName - name of the DTD.
   externalId  - file identfying the DTD from which its contents are read.
 */
USER_OBJECT_
RS_XML(getDTD)(USER_OBJECT_ dtdFileName, USER_OBJECT_ externalId, 
  	       USER_OBJECT_ asText, USER_OBJECT_ isURL, USER_OBJECT_ errorFun)
{
 USER_OBJECT_ ans;
 const char * dtdName = strdup(CHAR_DEREF(STRING_ELT(dtdFileName, 0)));
 const char * extId = strdup(CHAR_DEREF(STRING_ELT(externalId, 0)));
 int localAsText = LOGICAL_DATA(asText)[0];
 xmlParserCtxtPtr ctxt;
 xmlDtdPtr        dtd;
 

 if(localAsText) {
     ctxt = xmlCreateDocParserCtxt((xmlChar*) extId);
 } else {
     if(LOGICAL_DATA(isURL)[0] == 0) {
	 struct stat tmp_stat;
	 if(extId == NULL || stat(extId, &tmp_stat) < 0) {
             PROBLEM "Can't find file %s", extId
	     ERROR;
	 }
     }

      ctxt = xmlCreateFileParserCtxt(extId);  /* from parser.c xmlSAXParseFile */
 }

 if(ctxt == NULL) {
    PROBLEM "error creating XML parser for `%s'", extId
    ERROR;
 }

  ctxt->validate = 1;

#ifdef RS_XML_SET_STRUCTURED_ERROR  /* Done in R code now. */
  xmlSetStructuredErrorFunc(errorFun == NULL_USER_OBJECT ? NULL : errorFun, R_xmlStructuredErrorHandler);
#endif

  if(ctxt->myDoc == NULL)
    ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");


  if(localAsText) {
    xmlCreateIntSubset(ctxt->myDoc, CHAR_TO_XMLCHAR(dtdName), NULL, NULL);
    while(ctxt->input->cur && ctxt->input->cur[0]) {
      SKIP_BLANKS;
      xmlParseMarkupDecl(ctxt);
    }
    dtd = ctxt->myDoc->intSubset;
  }  else {
       /* Read the file. */
       /* Added for 2.2.12. May need to be conditional for 1.8.9 */
    ctxt->sax->internalSubset(ctxt->userData, CHAR_TO_XMLCHAR(dtdName), CHAR_TO_XMLCHAR(extId), CHAR_TO_XMLCHAR(extId));
       /* Warnings will ensue about not being in internal subset if we don't go to level 2. */
#ifdef USE_EXTERNAL_SUBSET
    ctxt->inSubset = 2;
    ctxt->sax->externalSubset(ctxt->userData, CHAR_TO_XMLCHAR(dtdName), CHAR_TO_XMLCHAR(extId), CHAR_TO_XMLCHAR(extId));
    ctxt->inSubset = 0;
#endif
    dtd = ctxt->myDoc->extSubset;
  }

#ifdef RS_XML_SET_STRUCTURED_ERROR 
  xmlSetStructuredErrorFunc(NULL, NULL);
#endif

  if(dtd == NULL) {
      if(errorFun != NULL_USER_OBJECT) {
        RSXML_structuredStop(errorFun, NULL);
      } else
        return(stop("DTDParseError", "error parsing %s", dtdName));

   PROBLEM "error in DTD %s", extId
   ERROR;
  }

  if(localAsText) {
      /* Don't bother with the internal and external split, just do the internal and return it. */
    ans = RS_XML(createDTDParts)(dtd, ctxt);
  } else 
    ans = RS_XML(ConstructDTDList)(ctxt->myDoc, 0, ctxt);

  return(ans);
}


const char *RS_XML(DtdTypeNames)[] = {"external", "internal"};
/**
  Create the representation of the DTD contained in the Document pointer,
  using both the internal and external descriptions and returning a list
  of the appropriate length. If the external description is empty, then we just
  return the description of the internal description. Otherwise, we return a named
  list of length 2 containing descriptions of both. 
 */
USER_OBJECT_
RS_XML(ConstructDTDList)(xmlDocPtr myDoc, int processInternals, xmlParserCtxtPtr ctxt)
{
 USER_OBJECT_ ans, el, klass;
 int i;
 xmlDtdPtr sets[2];
  
 int num = processInternals ? 2  : 1;
 sets[0] = myDoc->extSubset;

 if(processInternals) {
   sets[1] = myDoc->intSubset;
 }

  PROTECT(ans = NEW_LIST(num));
  for(i = 0; i < num; i++) {
    if(sets[i]) {
      SET_VECTOR_ELT(ans, i, el= RS_XML(createDTDParts)(sets[i], ctxt));
      PROTECT(klass = NEW_CHARACTER(1));
      SET_STRING_ELT(klass, 0, mkChar(i==0 ? "ExternalDTD" : "InternalDTD"));
      SET_CLASS(el, klass);
      UNPROTECT(1);
    }
  }
   RS_XML(SetNames)(num, RS_XML(DtdTypeNames), ans);

   UNPROTECT(1);


 return(processInternals ? ans : VECTOR_ELT(ans, 0));
}

/**
  Process the entities and elements of the DTD, returning a list
  of length 2, irrespective if either is empty.
 */
USER_OBJECT_
RS_XML(createDTDParts)(xmlDtdPtr dtd,  xmlParserCtxtPtr ctxt)
{ 
 xmlEntitiesTablePtr entities;
 xmlElementTable *table;
  USER_OBJECT_ ans;
  PROTECT(ans = NEW_LIST(DTD_NUM_SLOTS));
   table = (xmlElementTable*) dtd->elements;
   if(table)
      SET_VECTOR_ELT(ans, DTD_ELEMENTS_SLOT,  RS_XML(ProcessElements)(table, ctxt));


  entities = (xmlEntitiesTablePtr) dtd->entities;
  if(entities)
     SET_VECTOR_ELT(ans, DTD_ENTITIES_SLOT, RS_XML(ProcessEntities)(entities, ctxt));
   
   RS_XML(SetNames)(DTD_NUM_SLOTS, RS_XML(DtdNames), ans);

   UNPROTECT(1);
 return(ans);
}


#ifdef LIBXML2
struct ElementTableScanner {
  USER_OBJECT_ dtdEls;
  USER_OBJECT_ dtdNames;
  int counter;
};

#ifndef NO_XML_HASH_SCANNER_RETURN
void *RS_xmlElementTableConverter(void *payload, void *data, xmlChar *name);
void* RS_xmlEntityTableConverter(void *payload, void *data, xmlChar *name);
#else
void RS_xmlElementTableConverter(void *payload, void *data, xmlChar *name);
void RS_xmlEntityTableConverter(void *payload, void *data, xmlChar *name);
#endif


#endif

/**
 Convert the elements into a named list of objects with each element
 representing an element.
 */
USER_OBJECT_
RS_XML(ProcessElements)(xmlElementTablePtr table, xmlParserCtxtPtr ctxt)
{
 USER_OBJECT_ dtdEls = NULL_USER_OBJECT;
 int n;
#ifdef LIBXML2
 n = xmlHashSize(table);
#else
 int i;
 xmlElementPtr xmlEl;
 n = table->nb_elements;
#endif

  if(n > 0) {
    USER_OBJECT_ dtdNames = NULL_USER_OBJECT;

    PROTECT(dtdEls = NEW_LIST(n));
    PROTECT(dtdNames = NEW_CHARACTER(n));
#ifdef LIBXML2
 {
   struct ElementTableScanner scanData;
   scanData.dtdEls = dtdEls;
   scanData.dtdNames = dtdNames;
   scanData.counter = 0;

   xmlHashScan(table, RS_xmlElementTableConverter, &scanData);

   SET_LENGTH(dtdEls, scanData.counter);
   SET_LENGTH(dtdNames, scanData.counter);
 }
#else
      for(i = 0; i < n; i++) {
     	xmlEl = table->table[i];
     	SET_VECTOR_ELT(dtdEls, i, RS_XML(createDTDElement)(xmlEl));
     	SET_STRING_ELT(dtdNames , i, COPY_TO_USER_STRING(xmlEl->name));
      }
#endif
    SET_NAMES(dtdEls, dtdNames);
    UNPROTECT(2);
  } 

  return(dtdEls);
}

#ifdef LIBXML2
/* libxml2 2.4.21 (and perhaps earlier) redefines this to have a return type of void,
   rather than void*. Need to figure out if this makes any real difference to the interface
   and also when to 
*/
#ifndef NO_XML_HASH_SCANNER_RETURN
void*
#else
void
#endif
RS_xmlElementTableConverter(void *payload, void *data, xmlChar *name)
{
  struct ElementTableScanner *scanData = (struct ElementTableScanner *)data;

 SET_VECTOR_ELT(scanData->dtdEls, scanData->counter, RS_XML(createDTDElement)( payload));
 SET_STRING_ELT(scanData->dtdNames, scanData->counter, COPY_TO_USER_STRING(XMLCHAR_TO_CHAR(name)));

 scanData->counter++;
#ifndef NO_XML_HASH_SCANNER_RETURN
 return(payload);
#endif
}
#endif

/**
  Process the list of entities and convert them into a named list containing
  entity descriptions.
 */
USER_OBJECT_
RS_XML(ProcessEntities)(xmlEntitiesTablePtr table, xmlParserCtxtPtr ctxt)
{
 USER_OBJECT_ dtdEls = NULL_USER_OBJECT;
 int n;
#ifdef LIBXML2
 n = xmlHashSize(table);
#else
 xmlEntity *xmlEl;
 int i;
 n = table->nb_entities;
#endif
  if(n > 0) {
    USER_OBJECT_ dtdNames;

    PROTECT(dtdEls = NEW_LIST(n));
    PROTECT(dtdNames = NEW_CHARACTER(n));

#ifdef LIBXML2
 {
   struct ElementTableScanner scanData;
   scanData.dtdEls = dtdEls;
   scanData.dtdNames = dtdNames;
   scanData.counter = 0;

   xmlHashScan(table, RS_xmlEntityTableConverter, &scanData);
     /* Reset the length to be the actual number rather than the
        capacity of the table.
        See ProcessElements also.
      */

   SET_LENGTH(dtdEls, scanData.counter);
   SET_LENGTH(dtdNames, scanData.counter);

 }
#else
      for(i = 0; i < n; i++) {
     	xmlEl = table->table +i;
     	SET_VECTOR_ELT(dtdEls, i, RS_XML(createDTDEntity)(xmlEl));
     	SET_STRING_ELT(dtdNames, i, COPY_TO_USER_STRING(xmlEl->name));
      }
#endif
    SET_NAMES(dtdEls, dtdNames);
    UNPROTECT(2);
  } 

  return(dtdEls);
}

#ifdef LIBXML2

#ifndef NO_XML_HASH_SCANNER_RETURN
void*
#else
void
#endif
RS_xmlEntityTableConverter(void *payload, void *data, xmlChar *name)
{
  struct ElementTableScanner *scanData = (struct ElementTableScanner *)data;

 SET_VECTOR_ELT(scanData->dtdEls, scanData->counter, RS_XML(createDTDEntity)( payload));
 SET_STRING_ELT(scanData->dtdNames, scanData->counter, COPY_TO_USER_STRING(XMLCHAR_TO_CHAR(name)));

 scanData->counter++;
#ifndef NO_XML_HASH_SCANNER_RETURN
 return(payload);
#endif
}
#endif /* End of LIBXML2 for definint RS_xmlEntityTableConverter */

/**
  Convert an entity definition into a user-level object, handling both internal and system entities.

  We could have different slots for the two types of entities, but that may make it harder to program.
  S3/R classes aren't exactly good with inheritance of slots.
 */

/**
  Indices for the slots of the user-level list representing the entity.
 */
enum { DTD_ENTITY_NAME_SLOT,  DTD_ENTITY_CONTENT_SLOT, DTD_ENTITY_ORIG_SLOT, DTD_ENTITY_NUM_SLOTS};
/* 
  Names for the slots of the user-level list representing the entity.
*/
const char *RS_XML(EntityNames)[] = {"name", "value", "original"};

USER_OBJECT_
RS_XML(createDTDEntity)(xmlEntityPtr entity)
{
  USER_OBJECT_ ans;
  const xmlChar *value;
  const char *localClassName;

  PROTECT(ans = NEW_LIST(DTD_ENTITY_NUM_SLOTS));

  SET_VECTOR_ELT(ans, DTD_ENTITY_NAME_SLOT, NEW_CHARACTER(1));
  SET_STRING_ELT(VECTOR_ELT(ans, DTD_ENTITY_NAME_SLOT), 0, COPY_TO_USER_STRING(XMLCHAR_TO_CHAR(entity->name)));

   if(entity->content == NULL) {
     value = entity->SystemID;
     localClassName = "XMLSystemEntity";
   } else {
     value = entity->content;
     localClassName = "XMLEntity";
   }

  SET_VECTOR_ELT(ans, DTD_ENTITY_CONTENT_SLOT, NEW_CHARACTER(1));
   SET_STRING_ELT(VECTOR_ELT(ans, DTD_ENTITY_CONTENT_SLOT), 0, COPY_TO_USER_STRING(XMLCHAR_TO_CHAR(value)));

   if(entity->orig) {
     SET_VECTOR_ELT(ans, DTD_ENTITY_ORIG_SLOT, NEW_CHARACTER(1));
      SET_STRING_ELT(VECTOR_ELT(ans, DTD_ENTITY_ORIG_SLOT), 0, COPY_TO_USER_STRING(XMLCHAR_TO_CHAR(entity->orig)));
   }

   RS_XML(SetNames)(DTD_ENTITY_NUM_SLOTS, RS_XML(EntityNames), ans);

   /* Set the class of the specified object based on whether it is a internal or external entity. */
   SET_CLASS_NAME(localClassName, ans);

   UNPROTECT(1);

  return(ans);
}






enum { DTD_ELEMENT_NAME_SLOT, DTD_ELEMENT_TYPE_SLOT, DTD_ELEMENT_CONTENT_SLOT, DTD_ELEMENT_ATTRIBUTES_SLOT, DTD_ELEMENT_NUM_SLOTS};
const char *RS_XML(ElementNames)[] = {"name", "type", "contents","attributes"};
const char *RS_XML(ElementTypeNames)[] = {"empty", "any", "mixed","element"};

/**
  Creates the user-level object representing the definition of an element within a DTD,
  including its attribute definitions, its type, name and finally contents.
  This is an object of class XMLElementDef.
 */
USER_OBJECT_
RS_XML(createDTDElement)(xmlElementPtr el)
{
 USER_OBJECT_ rel;
 int type;

#ifdef XML_ELEMENT_ETYPE
 type = el->etype;
#else
 type = el->type;
#endif

   PROTECT(rel =  NEW_LIST(DTD_ELEMENT_NUM_SLOTS));

 SET_VECTOR_ELT(rel, DTD_ELEMENT_NAME_SLOT, NEW_CHARACTER(1));
 SET_STRING_ELT(VECTOR_ELT(rel, DTD_ELEMENT_NAME_SLOT), 0, COPY_TO_USER_STRING( XMLCHAR_TO_CHAR( ( el->name ? el->name : (xmlChar*)""))));

 SET_VECTOR_ELT(rel, DTD_ELEMENT_TYPE_SLOT, NEW_INTEGER(1));
 INTEGER_DATA(VECTOR_ELT(rel, DTD_ELEMENT_TYPE_SLOT))[0] = el->type;
 SET_ENUM_NAME(ElementTypeNames, type, VECTOR_ELT(rel, DTD_ELEMENT_TYPE_SLOT));


 if(el->content != NULL)
   SET_VECTOR_ELT(rel, DTD_ELEMENT_CONTENT_SLOT, RS_XML(createDTDElementContents)(el->content, el, 1));

 SET_VECTOR_ELT(rel, DTD_ELEMENT_ATTRIBUTES_SLOT, RS_XML(createDTDElementAttributes)(el->attributes, el));

 RS_XML(SetNames)(DTD_ELEMENT_NUM_SLOTS, RS_XML(ElementNames), rel);

 SET_CLASS_NAME("XMLElementDef", rel);

 UNPROTECT(1);
 return(rel);
}


/* Indices for the slots/elements in the list. */
enum {DTD_CONTENT_TYPE_SLOT, DTD_CONTENT_OCCURANCE_SLOT, DTD_CONTENT_ELEMENTS_SLOT, DTD_CONTENT_NUM_SLOTS};
/* names for the elements */
const char *RS_XML(ContentNames)[] = {"type", "ocur", "elements"};
/* Names for the enumerated types of the entries in the data */
const char *RS_XML(ContentTypeNames)[] = {"PCData", "Element", "Sequence","Or"};
const char *RS_XML(OccuranceNames)[] = {"Once", "Zero or One", "Mult","One or More"};

/**

  Create an object representing the DTD element. The returned value is a list
  with 3 elements. The names are given by the array ContentNames above.  The
  type and ocur elements are simple named integers identifying that the element
  is simple parsed character data, an element or a composite element which is
  either an one of several possible types (that is an OR or |) or an ordered
  sequence of types. The ocur field indicates whether this element is to be
  expected in this position exactly once (default qualifier), zero or one
  (i.e. optional) (?) , any number of times (including omitted) (*) and finally
  , at least once, but possible more(+)


  The recursive argument allows the RS_XML(SequenceContent) routine  to use part of this
  routine.  

*/

USER_OBJECT_
RS_XML(createDTDElementContents)(xmlElementContentPtr vals, xmlElementPtr el, int recursive)
{
  char *localClassName = NULL;
  int num = 0;
  USER_OBJECT_ ans = NULL_USER_OBJECT;

  PROTECT(ans = NEW_LIST(DTD_CONTENT_NUM_SLOTS));

  SET_VECTOR_ELT(ans, DTD_CONTENT_TYPE_SLOT, NEW_INTEGER(1));
  INTEGER_DATA(VECTOR_ELT(ans, DTD_CONTENT_TYPE_SLOT))[0] = vals->type;
  SET_ENUM_NAME(ContentTypeNames, vals->type, VECTOR_ELT(ans, DTD_CONTENT_TYPE_SLOT));

  SET_VECTOR_ELT(ans, DTD_CONTENT_OCCURANCE_SLOT, NEW_INTEGER(1));
  INTEGER_DATA(VECTOR_ELT(ans, DTD_CONTENT_OCCURANCE_SLOT))[0] = vals->ocur;
  SET_ENUM_NAME(OccuranceNames, vals->ocur, VECTOR_ELT(ans, DTD_CONTENT_OCCURANCE_SLOT));


  if(vals->type == XML_ELEMENT_CONTENT_SEQ && recursive) {
       SET_VECTOR_ELT(ans, DTD_CONTENT_ELEMENTS_SLOT, RS_XML(SequenceContent)(vals, el));
  } else {
    num += (vals->c1 != NULL);
    if(recursive || 1)
      num += (vals->c2 != NULL);

    if(num > 0) {   
      SET_VECTOR_ELT(ans, DTD_CONTENT_ELEMENTS_SLOT, NEW_LIST(num));
      num = 0;
      if(vals->c1) {
        SET_VECTOR_ELT(VECTOR_ELT(ans, DTD_CONTENT_ELEMENTS_SLOT), num++, RS_XML(createDTDElementContents)(vals->c1, el, 1));
      }

      if(recursive || 1) {
        if(vals->c2) {
         SET_VECTOR_ELT(VECTOR_ELT(ans, DTD_CONTENT_ELEMENTS_SLOT), num++, RS_XML(createDTDElementContents)(vals->c2, el, 1));
        }
       }
    } else {
     if(vals->name) {
        SET_VECTOR_ELT(ans, DTD_CONTENT_ELEMENTS_SLOT,  NEW_CHARACTER(1));
        SET_STRING_ELT(VECTOR_ELT(ans, DTD_CONTENT_ELEMENTS_SLOT), 0, COPY_TO_USER_STRING(XMLCHAR_TO_CHAR(vals->name)));
      }
   }
  }
  switch(vals->type) {
    case XML_ELEMENT_CONTENT_SEQ:
      localClassName = "XMLSequenceContent";
      break;
    case XML_ELEMENT_CONTENT_OR:
      localClassName = "XMLOrContent";
      break;
    default:
      localClassName = "XMLElementContent";
  }

      if(localClassName) {
	SET_CLASS_NAME(localClassName, ans);
      }


  RS_XML(SetNames)(DTD_CONTENT_NUM_SLOTS, RS_XML(ContentNames), ans);

  UNPROTECT(1);
  return(ans);
}


/**
  Process the DTD element, knowing that it is a sequence definition.
  Compute the number of elements in the sequence by flattening  out the 
  lob-sided tree and then convert the each element and append it to the list. 
 */
USER_OBJECT_
RS_XML(SequenceContent)(xmlElementContentPtr vals, xmlElementPtr el)
{
  xmlElementContentPtr ptr = vals->c2;
  int ok = 1, n=1, deep = 0;
  USER_OBJECT_ ans = NULL_USER_OBJECT;
  USER_OBJECT_ tmp;

     /* Count the number of elements in this sequence. 
        Descend all the c2's below this one.
      */
  while(ptr && ok) {
    ok = (ptr->type == XML_ELEMENT_CONTENT_SEQ); 
    ptr = ptr->c2;
    n++;
  }

  /* Now build the list and the elements within it.*/
  PROTECT(ans = NEW_LIST(n));
  SET_VECTOR_ELT(ans, 0, RS_XML(createDTDElementContents)(vals->c1, el, 1));

  ptr = vals->c2;
  n = 1;
  do {
      /* Some jumping around here beacuse of the recursion and split types. Should be cleaner. */
    deep = (ptr->c1  != NULL && ptr->type == XML_ELEMENT_CONTENT_SEQ );
    tmp = RS_XML(createDTDElementContents)( deep ? ptr->c1 : ptr, el, deep);
    SET_VECTOR_ELT(ans, n, tmp); 
    ok = (ptr->type == XML_ELEMENT_CONTENT_SEQ); 
    ptr = ptr->c2;
    n++;
  } while(ptr && ok);

  UNPROTECT(1);
  return(ans);
}





/**
   Routine that creates a named list of XMLAttributeDef objects from a collection of 
   attribute definitions associated with the specified XML element definition.
 */
USER_OBJECT_
RS_XML(createDTDElementAttributes)(xmlAttributePtr vals, xmlElementPtr el)
{
  USER_OBJECT_ ans = NULL_USER_OBJECT;
  USER_OBJECT_ names;
  xmlAttributePtr tmp = vals;
  int n = 0, i;

  while(tmp) {
#ifdef LIBXML2
    tmp = tmp->nexth;
#else
    tmp = tmp->next;
#endif
    n++;
  }

  if(n > 0) {
    tmp = vals;
    PROTECT(ans = NEW_LIST(n));
    PROTECT(names = NEW_CHARACTER(n));
    for(i=0; i < n; i++) {
       SET_VECTOR_ELT(ans, i,  RS_XML(createDTDAttribute)(tmp, el));
       SET_STRING_ELT(names, i, COPY_TO_USER_STRING(XMLCHAR_TO_CHAR(tmp->name)));
#ifdef LIBXML2
    tmp = tmp->nexth;
#else
    tmp = tmp->next;
#endif
    }
    SET_NAMES(ans, names);
    UNPROTECT(2);
  }

  return(ans);
}


enum {DTD_ATTRIBUTE_NAME_SLOT, DTD_ATTRIBUTE_TYPE_SLOT, DTD_ATTRIBUTE_DEFAULT_SLOT, DTD_ATTRIBUTE_DEFAULT_VALUE_SLOT, DTD_ATTRIBUTE_NUM_SLOTS};

/* Names for the possible types of an attribute. */
const char *RS_XML(AttributeTypeNames)   [] = {"CDATA","Id", "IDRef", "IDRefs", "Entity","Entities", "NMToken", "NMTokens", "Enumeration", "Notation"};
/* Names for the possible modes or default types of an attribute. */
const char *RS_XML(AttributeDefaultNames)[] = {"None", "Required", "Implied", "Fixed"};

/* Names of the elements within the returned list */
const char *RS_XML(AttributeSlotNames)[] = {"name", "type", "defaultType", "defaultValue"};


/**
  Create a user-level version of a DTD attribute within an Attribute list within the DTD.
  Return a vector of length 4 with elements named
       Name, Type, Default Type and Default Value.
   The first is a simple string (character vector of length 1). The next two are enumerated
   types describing the type of the attribute value and whether it is required, fixed, implied, etc.
   The final value is the default value 
 */
USER_OBJECT_
RS_XML(createDTDAttribute)(xmlAttributePtr val, xmlElementPtr el)
{
  USER_OBJECT_ ans;
  int attrType;

#ifdef XML_ATTRIBUTE_ATYPE
  attrType = val->atype;
#else
  attrType = val->type;
#endif

  PROTECT(ans = NEW_LIST(DTD_ATTRIBUTE_NUM_SLOTS));

  SET_VECTOR_ELT(ans, DTD_ATTRIBUTE_NAME_SLOT, NEW_CHARACTER(1));
  SET_STRING_ELT(VECTOR_ELT(ans, DTD_ATTRIBUTE_NAME_SLOT), 0, COPY_TO_USER_STRING(XMLCHAR_TO_CHAR(val->name)));

 SET_VECTOR_ELT(ans, DTD_ATTRIBUTE_TYPE_SLOT, NEW_INTEGER(1));
 INTEGER_DATA(VECTOR_ELT(ans, DTD_ATTRIBUTE_TYPE_SLOT))[0] =  val->type;
 SET_ENUM_NAME(AttributeTypeNames, attrType, VECTOR_ELT(ans, DTD_ATTRIBUTE_TYPE_SLOT));

 SET_VECTOR_ELT(ans, DTD_ATTRIBUTE_DEFAULT_SLOT, NEW_INTEGER(1));
 INTEGER_DATA(VECTOR_ELT(ans, DTD_ATTRIBUTE_DEFAULT_SLOT))[0] =  val->def;
 SET_ENUM_NAME(AttributeDefaultNames, val->def, VECTOR_ELT(ans, DTD_ATTRIBUTE_DEFAULT_SLOT));

 if(val->type == XML_ATTRIBUTE_ENUMERATION) {
   SET_VECTOR_ELT(ans, DTD_ATTRIBUTE_DEFAULT_VALUE_SLOT, RS_XML(AttributeEnumerationList)(val->tree, val, el));
 } else {
    SET_VECTOR_ELT(ans, DTD_ATTRIBUTE_DEFAULT_VALUE_SLOT, NEW_CHARACTER(1));
    SET_STRING_ELT(VECTOR_ELT(ans, DTD_ATTRIBUTE_DEFAULT_VALUE_SLOT), 0, COPY_TO_USER_STRING( XMLCHAR_TO_CHAR( (val->defaultValue ? val->defaultValue : (xmlChar*)""))));
 }
  RS_XML(SetNames)(DTD_ATTRIBUTE_NUM_SLOTS, RS_XML(AttributeSlotNames),  ans);


  SET_CLASS_NAME("XMLAttributeDef", ans);

  UNPROTECT(1);
  return(ans);
}    


/**
  Return a character vector containing the elements listed in the enumeration of possible
  values in the attribute. These arise in DTD entries such as
    <ATTLIST el
               foo   (true | false)
    >
 */
USER_OBJECT_
RS_XML(AttributeEnumerationList)(xmlEnumerationPtr list, xmlAttributePtr attr, xmlElementPtr element)
{
  USER_OBJECT_ ans = NULL_USER_OBJECT;
  xmlEnumerationPtr tmp = list;
  int n = 0;

    /* Count the number of entries in the list/table. */
  while(tmp) {
    n++;
    tmp = tmp->next;
  }

       /* Now convert each entry and add it to a list. */
  if(n > 0) {
    int i;
    PROTECT(ans  = NEW_CHARACTER(n));
    tmp = list;
    for(i = 0; i < n; i++) {
      SET_STRING_ELT(ans, i, COPY_TO_USER_STRING(XMLCHAR_TO_CHAR(tmp->name)));
      tmp = tmp->next;
    }
    UNPROTECT(1);
  }
  return(ans);
}