Coverage Report

Created: 2024-09-06 07:53

/src/libxml2/xmlreader.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * xmlreader.c: implements the xmlTextReader streaming node API
3
 *
4
 * NOTE:
5
 *   XmlTextReader.Normalization Property won't be supported, since
6
 *     it makes the parser non compliant to the XML recommendation
7
 *
8
 * See Copyright for the status of this software.
9
 *
10
 * daniel@veillard.com
11
 */
12
13
/*
14
 * TODOs:
15
 *   - XML Schemas validation
16
 */
17
#define IN_LIBXML
18
#include "libxml.h"
19
20
#ifdef LIBXML_READER_ENABLED
21
#include <string.h> /* for memset() only ! */
22
#include <stdarg.h>
23
#include <ctype.h>
24
#include <stdlib.h>
25
26
#include <libxml/xmlmemory.h>
27
#include <libxml/xmlIO.h>
28
#include <libxml/xmlreader.h>
29
#include <libxml/parserInternals.h>
30
#ifdef LIBXML_SCHEMAS_ENABLED
31
#include <libxml/relaxng.h>
32
#include <libxml/xmlschemas.h>
33
#endif
34
#include <libxml/uri.h>
35
#ifdef LIBXML_XINCLUDE_ENABLED
36
#include <libxml/xinclude.h>
37
#endif
38
#ifdef LIBXML_PATTERN_ENABLED
39
#include <libxml/pattern.h>
40
#endif
41
42
#include "private/buf.h"
43
#include "private/error.h"
44
#include "private/tree.h"
45
#include "private/parser.h"
46
#ifdef LIBXML_XINCLUDE_ENABLED
47
#include "private/xinclude.h"
48
#endif
49
50
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
51
/* Keeping free objects can hide memory errors. */
52
0
#define MAX_FREE_NODES 1
53
#else
54
#define MAX_FREE_NODES 100
55
#endif
56
57
#ifndef va_copy
58
  #ifdef __va_copy
59
    #define va_copy(dest, src) __va_copy(dest, src)
60
  #else
61
    #define va_copy(dest, src) memcpy(dest, src, sizeof(va_list))
62
  #endif
63
#endif
64
65
0
#define CHUNK_SIZE 512
66
/************************************************************************
67
 *                  *
68
 *  The parser: maps the Text Reader API on top of the existing *
69
 *    parsing routines building a tree      *
70
 *                  *
71
 ************************************************************************/
72
73
0
#define XML_TEXTREADER_INPUT  1
74
0
#define XML_TEXTREADER_CTXT 2
75
76
typedef enum {
77
    XML_TEXTREADER_NONE = -1,
78
    XML_TEXTREADER_START= 0,
79
    XML_TEXTREADER_ELEMENT= 1,
80
    XML_TEXTREADER_END= 2,
81
    XML_TEXTREADER_EMPTY= 3,
82
    XML_TEXTREADER_BACKTRACK= 4,
83
    XML_TEXTREADER_DONE= 5,
84
    XML_TEXTREADER_ERROR= 6
85
} xmlTextReaderState;
86
87
typedef enum {
88
    XML_TEXTREADER_NOT_VALIDATE = 0,
89
    XML_TEXTREADER_VALIDATE_DTD = 1,
90
    XML_TEXTREADER_VALIDATE_RNG = 2,
91
    XML_TEXTREADER_VALIDATE_XSD = 4
92
} xmlTextReaderValidate;
93
94
struct _xmlTextReader {
95
    int       mode; /* the parsing mode */
96
    xmlDocPtr     doc;    /* when walking an existing doc */
97
    xmlTextReaderValidate       validate;/* is there any validation */
98
    int       allocs; /* what structure were deallocated */
99
    xmlTextReaderState    state;
100
    xmlParserCtxtPtr    ctxt; /* the parser context */
101
    xmlSAXHandlerPtr    sax;  /* the parser SAX callbacks */
102
    xmlParserInputBufferPtr input;  /* the input */
103
    startElementSAXFunc   startElement;/* initial SAX callbacks */
104
    endElementSAXFunc   endElement;  /* idem */
105
    startElementNsSAX2Func  startElementNs;/* idem */
106
    endElementNsSAX2Func  endElementNs;  /* idem */
107
    charactersSAXFunc   characters;
108
    cdataBlockSAXFunc   cdataBlock;
109
    unsigned int    base; /* base of the segment in the input */
110
    unsigned int    cur;  /* current position in the input */
111
    xmlNodePtr      node; /* current node */
112
    xmlNodePtr      curnode;/* current attribute node */
113
    int       depth;  /* depth of the current node */
114
    xmlNodePtr      faketext;/* fake xmlNs chld */
115
    int       preserve;/* preserve the resulting document */
116
    xmlBufPtr           buffer; /* used to return const xmlChar * */
117
    xmlDictPtr      dict; /* the context dictionary */
118
119
    /* entity stack when traversing entities content */
120
    xmlNodePtr         ent;          /* Current Entity Ref Node */
121
    int                entNr;        /* Depth of the entities stack */
122
    int                entMax;       /* Max depth of the entities stack */
123
    xmlNodePtr        *entTab;       /* array of entities */
124
125
    /* error handling */
126
    xmlTextReaderErrorFunc errorFunc;    /* callback function */
127
    void                  *errorFuncArg; /* callback function user argument */
128
129
#ifdef LIBXML_SCHEMAS_ENABLED
130
    /* Handling of RelaxNG validation */
131
    xmlRelaxNGPtr          rngSchemas;  /* The Relax NG schemas */
132
    xmlRelaxNGValidCtxtPtr rngValidCtxt;/* The Relax NG validation context */
133
    int                    rngPreserveCtxt; /* 1 if the context was provided by the user */
134
    int                    rngValidErrors;/* The number of errors detected */
135
    xmlNodePtr             rngFullNode; /* the node if RNG not progressive */
136
    /* Handling of Schemas validation */
137
    xmlSchemaPtr          xsdSchemas; /* The Schemas schemas */
138
    xmlSchemaValidCtxtPtr xsdValidCtxt;/* The Schemas validation context */
139
    int                   xsdPreserveCtxt; /* 1 if the context was provided by the user */
140
    int                   xsdValidErrors;/* The number of errors detected */
141
    xmlSchemaSAXPlugPtr   xsdPlug;  /* the schemas plug in SAX pipeline */
142
#endif
143
#ifdef LIBXML_XINCLUDE_ENABLED
144
    /* Handling of XInclude processing */
145
    int                xinclude;  /* is xinclude asked for */
146
    const xmlChar *    xinclude_name; /* the xinclude name from dict */
147
    xmlXIncludeCtxtPtr xincctxt;  /* the xinclude context */
148
    int                in_xinclude; /* counts for xinclude */
149
#endif
150
#ifdef LIBXML_PATTERN_ENABLED
151
    int                patternNr;       /* number of preserve patterns */
152
    int                patternMax;      /* max preserve patterns */
153
    xmlPatternPtr     *patternTab;      /* array of preserve patterns */
154
#endif
155
    int                preserves; /* level of preserves */
156
    int                parserFlags; /* the set of options set */
157
    /* Structured error handling */
158
    xmlStructuredErrorFunc sErrorFunc;  /* callback function */
159
160
    xmlResourceLoader resourceLoader;
161
    void *resourceCtxt;
162
};
163
164
0
#define NODE_IS_EMPTY   0x1
165
0
#define NODE_IS_PRESERVED 0x2
166
0
#define NODE_IS_SPRESERVED  0x4
167
168
static int xmlTextReaderReadTree(xmlTextReaderPtr reader);
169
static int xmlTextReaderNextTree(xmlTextReaderPtr reader);
170
171
/**
172
 * DICT_FREE:
173
 * @str:  a string
174
 *
175
 * Free a string if it is not owned by the "dict" dictionary in the
176
 * current scope
177
 */
178
#define DICT_FREE(str)            \
179
0
  if ((str) && ((!dict) ||       \
180
0
      (xmlDictOwns(dict, (const xmlChar *)(str)) == 0)))  \
181
0
      xmlFree((char *)(str));
182
183
static void xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur);
184
static void xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur);
185
186
static void
187
0
xmlTextReaderErrMemory(xmlTextReaderPtr reader) {
188
0
    if (reader->ctxt != NULL)
189
0
        xmlCtxtErrMemory(reader->ctxt);
190
0
    else
191
0
        xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_PARSER, NULL);
192
0
    reader->mode = XML_TEXTREADER_MODE_ERROR;
193
0
    reader->state = XML_TEXTREADER_ERROR;
194
0
}
195
196
static xmlChar *
197
0
readerStrdup(xmlTextReaderPtr reader, const xmlChar *string) {
198
0
    xmlChar *copy;
199
200
0
    if (string == NULL)
201
0
        return(NULL);
202
203
0
    copy = xmlStrdup(string);
204
0
    if (copy == NULL)
205
0
        xmlTextReaderErrMemory(reader);
206
207
0
    return(copy);
208
0
}
209
210
static const xmlChar *
211
0
constString(xmlTextReaderPtr reader, const xmlChar *string) {
212
0
    const xmlChar *dictString;
213
214
0
    if (string == NULL)
215
0
        return(NULL);
216
217
0
    dictString = xmlDictLookup(reader->dict, string, -1);
218
0
    if (dictString == NULL)
219
0
        xmlTextReaderErrMemory(reader);
220
221
0
    return(dictString);
222
0
}
223
224
static const xmlChar *
225
constQString(xmlTextReaderPtr reader, const xmlChar *prefix,
226
0
             const xmlChar *name) {
227
0
    const xmlChar *dictString;
228
229
0
    if (name == NULL)
230
0
        return(NULL);
231
232
0
    dictString = xmlDictQLookup(reader->dict, prefix, name);
233
0
    if (dictString == NULL)
234
0
        xmlTextReaderErrMemory(reader);
235
236
0
    return(dictString);
237
0
}
238
239
/************************************************************************
240
 *                  *
241
 *  Our own version of the freeing routines as we recycle nodes *
242
 *                  *
243
 ************************************************************************/
244
245
/**
246
 * xmlTextReaderFreeProp:
247
 * @reader:  the xmlTextReaderPtr used
248
 * @cur:  the node
249
 *
250
 * Free a node.
251
 */
252
static void
253
0
xmlTextReaderFreeProp(xmlTextReaderPtr reader, xmlAttrPtr cur) {
254
0
    xmlDictPtr dict;
255
256
0
    if ((reader != NULL) && (reader->ctxt != NULL))
257
0
  dict = reader->ctxt->dict;
258
0
    else
259
0
        dict = NULL;
260
0
    if (cur == NULL) return;
261
262
0
    if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
263
0
  xmlDeregisterNodeDefaultValue((xmlNodePtr) cur);
264
265
0
    if (cur->children != NULL)
266
0
        xmlTextReaderFreeNodeList(reader, cur->children);
267
268
0
    if (cur->id != NULL) {
269
        /*
270
         * Operating in streaming mode, attr is gonna disappear
271
         */
272
0
        cur->id->attr = NULL;
273
0
        if (cur->id->name != NULL)
274
0
            DICT_FREE(cur->id->name);
275
0
        cur->id->name = cur->name;
276
0
        cur->name = NULL;
277
0
    } else {
278
0
        DICT_FREE(cur->name);
279
0
    }
280
281
0
    if ((reader != NULL) && (reader->ctxt != NULL) &&
282
0
        (reader->ctxt->freeAttrsNr < MAX_FREE_NODES)) {
283
0
        cur->next = reader->ctxt->freeAttrs;
284
0
  reader->ctxt->freeAttrs = cur;
285
0
  reader->ctxt->freeAttrsNr++;
286
0
    } else {
287
0
  xmlFree(cur);
288
0
    }
289
0
}
290
291
/**
292
 * xmlTextReaderFreePropList:
293
 * @reader:  the xmlTextReaderPtr used
294
 * @cur:  the first property in the list
295
 *
296
 * Free a property and all its siblings, all the children are freed too.
297
 */
298
static void
299
0
xmlTextReaderFreePropList(xmlTextReaderPtr reader, xmlAttrPtr cur) {
300
0
    xmlAttrPtr next;
301
302
0
    while (cur != NULL) {
303
0
        next = cur->next;
304
0
        xmlTextReaderFreeProp(reader, cur);
305
0
  cur = next;
306
0
    }
307
0
}
308
309
/**
310
 * xmlTextReaderFreeNodeList:
311
 * @reader:  the xmlTextReaderPtr used
312
 * @cur:  the first node in the list
313
 *
314
 * Free a node and all its siblings, this is a recursive behaviour, all
315
 * the children are freed too.
316
 */
317
static void
318
0
xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur) {
319
0
    xmlNodePtr next;
320
0
    xmlNodePtr parent;
321
0
    xmlDictPtr dict;
322
0
    size_t depth = 0;
323
324
0
    if ((reader != NULL) && (reader->ctxt != NULL))
325
0
  dict = reader->ctxt->dict;
326
0
    else
327
0
        dict = NULL;
328
0
    if (cur == NULL) return;
329
0
    if (cur->type == XML_NAMESPACE_DECL) {
330
0
  xmlFreeNsList((xmlNsPtr) cur);
331
0
  return;
332
0
    }
333
0
    if ((cur->type == XML_DOCUMENT_NODE) ||
334
0
  (cur->type == XML_HTML_DOCUMENT_NODE)) {
335
0
  xmlFreeDoc((xmlDocPtr) cur);
336
0
  return;
337
0
    }
338
0
    while (1) {
339
0
        while ((cur->type != XML_DTD_NODE) &&
340
0
               (cur->type != XML_ENTITY_REF_NODE) &&
341
0
               (cur->children != NULL) &&
342
0
               (cur->children->parent == cur)) {
343
0
            cur = cur->children;
344
0
            depth += 1;
345
0
        }
346
347
0
        next = cur->next;
348
0
        parent = cur->parent;
349
350
  /* unroll to speed up freeing the document */
351
0
  if (cur->type != XML_DTD_NODE) {
352
353
0
      if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
354
0
    xmlDeregisterNodeDefaultValue(cur);
355
356
0
      if (((cur->type == XML_ELEMENT_NODE) ||
357
0
     (cur->type == XML_XINCLUDE_START) ||
358
0
     (cur->type == XML_XINCLUDE_END)) &&
359
0
    (cur->properties != NULL))
360
0
    xmlTextReaderFreePropList(reader, cur->properties);
361
0
      if ((cur->content != (xmlChar *) &(cur->properties)) &&
362
0
          (cur->type != XML_ELEMENT_NODE) &&
363
0
    (cur->type != XML_XINCLUDE_START) &&
364
0
    (cur->type != XML_XINCLUDE_END) &&
365
0
    (cur->type != XML_ENTITY_REF_NODE)) {
366
0
    DICT_FREE(cur->content);
367
0
      }
368
0
      if (((cur->type == XML_ELEMENT_NODE) ||
369
0
           (cur->type == XML_XINCLUDE_START) ||
370
0
     (cur->type == XML_XINCLUDE_END)) &&
371
0
    (cur->nsDef != NULL))
372
0
    xmlFreeNsList(cur->nsDef);
373
374
      /*
375
       * we don't free element names here they are interned now
376
       */
377
0
      if ((cur->type != XML_TEXT_NODE) &&
378
0
    (cur->type != XML_COMMENT_NODE))
379
0
    DICT_FREE(cur->name);
380
0
      if (((cur->type == XML_ELEMENT_NODE) ||
381
0
     (cur->type == XML_TEXT_NODE)) &&
382
0
          (reader != NULL) && (reader->ctxt != NULL) &&
383
0
    (reader->ctxt->freeElemsNr < MAX_FREE_NODES)) {
384
0
          cur->next = reader->ctxt->freeElems;
385
0
    reader->ctxt->freeElems = cur;
386
0
    reader->ctxt->freeElemsNr++;
387
0
      } else {
388
0
    xmlFree(cur);
389
0
      }
390
0
  }
391
392
0
        if (next != NULL) {
393
0
      cur = next;
394
0
        } else {
395
0
            if ((depth == 0) || (parent == NULL))
396
0
                break;
397
0
            depth -= 1;
398
0
            cur = parent;
399
0
            cur->children = NULL;
400
0
        }
401
0
    }
402
0
}
403
404
/**
405
 * xmlTextReaderFreeNode:
406
 * @reader:  the xmlTextReaderPtr used
407
 * @cur:  the node
408
 *
409
 * Free a node, this is a recursive behaviour, all the children are freed too.
410
 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
411
 */
412
static void
413
0
xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur) {
414
0
    xmlDictPtr dict;
415
416
0
    if ((reader != NULL) && (reader->ctxt != NULL))
417
0
  dict = reader->ctxt->dict;
418
0
    else
419
0
        dict = NULL;
420
0
    if (cur->type == XML_DTD_NODE) {
421
0
  xmlFreeDtd((xmlDtdPtr) cur);
422
0
  return;
423
0
    }
424
0
    if (cur->type == XML_NAMESPACE_DECL) {
425
0
  xmlFreeNs((xmlNsPtr) cur);
426
0
        return;
427
0
    }
428
0
    if (cur->type == XML_ATTRIBUTE_NODE) {
429
0
  xmlTextReaderFreeProp(reader, (xmlAttrPtr) cur);
430
0
  return;
431
0
    }
432
433
0
    if ((cur->children != NULL) &&
434
0
  (cur->type != XML_ENTITY_REF_NODE)) {
435
0
  if (cur->children->parent == cur)
436
0
      xmlTextReaderFreeNodeList(reader, cur->children);
437
0
  cur->children = NULL;
438
0
    }
439
440
0
    if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
441
0
  xmlDeregisterNodeDefaultValue(cur);
442
443
0
    if (((cur->type == XML_ELEMENT_NODE) ||
444
0
   (cur->type == XML_XINCLUDE_START) ||
445
0
   (cur->type == XML_XINCLUDE_END)) &&
446
0
  (cur->properties != NULL))
447
0
  xmlTextReaderFreePropList(reader, cur->properties);
448
0
    if ((cur->content != (xmlChar *) &(cur->properties)) &&
449
0
        (cur->type != XML_ELEMENT_NODE) &&
450
0
  (cur->type != XML_XINCLUDE_START) &&
451
0
  (cur->type != XML_XINCLUDE_END) &&
452
0
  (cur->type != XML_ENTITY_REF_NODE)) {
453
0
  DICT_FREE(cur->content);
454
0
    }
455
0
    if (((cur->type == XML_ELEMENT_NODE) ||
456
0
   (cur->type == XML_XINCLUDE_START) ||
457
0
   (cur->type == XML_XINCLUDE_END)) &&
458
0
  (cur->nsDef != NULL))
459
0
  xmlFreeNsList(cur->nsDef);
460
461
    /*
462
     * we don't free names here they are interned now
463
     */
464
0
    if ((cur->type != XML_TEXT_NODE) &&
465
0
        (cur->type != XML_COMMENT_NODE))
466
0
  DICT_FREE(cur->name);
467
468
0
    if (((cur->type == XML_ELEMENT_NODE) ||
469
0
   (cur->type == XML_TEXT_NODE)) &&
470
0
  (reader != NULL) && (reader->ctxt != NULL) &&
471
0
  (reader->ctxt->freeElemsNr < MAX_FREE_NODES)) {
472
0
  cur->next = reader->ctxt->freeElems;
473
0
  reader->ctxt->freeElems = cur;
474
0
  reader->ctxt->freeElemsNr++;
475
0
    } else {
476
0
  xmlFree(cur);
477
0
    }
478
0
}
479
480
/**
481
 * xmlTextReaderFreeDoc:
482
 * @reader:  the xmlTextReaderPtr used
483
 * @cur:  pointer to the document
484
 *
485
 * Free up all the structures used by a document, tree included.
486
 */
487
static void
488
0
xmlTextReaderFreeDoc(xmlTextReaderPtr reader, xmlDocPtr cur) {
489
0
    xmlDtdPtr extSubset, intSubset;
490
491
0
    if (cur == NULL) return;
492
493
0
    if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
494
0
  xmlDeregisterNodeDefaultValue((xmlNodePtr) cur);
495
496
    /*
497
     * Do this before freeing the children list to avoid ID lookups
498
     */
499
0
    if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
500
0
    cur->ids = NULL;
501
0
    if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
502
0
    cur->refs = NULL;
503
0
    extSubset = cur->extSubset;
504
0
    intSubset = cur->intSubset;
505
0
    if (intSubset == extSubset)
506
0
  extSubset = NULL;
507
0
    if (extSubset != NULL) {
508
0
  xmlUnlinkNode((xmlNodePtr) cur->extSubset);
509
0
  cur->extSubset = NULL;
510
0
  xmlFreeDtd(extSubset);
511
0
    }
512
0
    if (intSubset != NULL) {
513
0
  xmlUnlinkNode((xmlNodePtr) cur->intSubset);
514
0
  cur->intSubset = NULL;
515
0
  xmlFreeDtd(intSubset);
516
0
    }
517
518
0
    if (cur->children != NULL) xmlTextReaderFreeNodeList(reader, cur->children);
519
520
0
    if (cur->version != NULL) xmlFree((char *) cur->version);
521
0
    if (cur->name != NULL) xmlFree((char *) cur->name);
522
0
    if (cur->encoding != NULL) xmlFree((char *) cur->encoding);
523
0
    if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
524
0
    if (cur->URL != NULL) xmlFree((char *) cur->URL);
525
0
    if (cur->dict != NULL) xmlDictFree(cur->dict);
526
527
0
    xmlFree(cur);
528
0
}
529
530
/************************************************************************
531
 *                  *
532
 *      The reader core parser        *
533
 *                  *
534
 ************************************************************************/
535
536
static void
537
xmlTextReaderStructuredRelay(void *userData, const xmlError *error)
538
0
{
539
0
    xmlTextReaderPtr reader = (xmlTextReaderPtr) userData;
540
541
0
    if (reader->sErrorFunc != NULL) {
542
0
        reader->sErrorFunc(reader->errorFuncArg, error);
543
0
    } else if (reader->errorFunc != NULL) {
544
0
        xmlParserSeverities severity;
545
546
0
        if ((error->domain == XML_FROM_VALID) ||
547
0
            (error->domain == XML_FROM_DTD)) {
548
0
            if (error->level == XML_ERR_WARNING)
549
0
                severity = XML_PARSER_SEVERITY_VALIDITY_WARNING;
550
0
            else
551
0
                severity = XML_PARSER_SEVERITY_VALIDITY_ERROR;
552
0
        } else {
553
0
            if (error->level == XML_ERR_WARNING)
554
0
                severity = XML_PARSER_SEVERITY_WARNING;
555
0
            else
556
0
                severity = XML_PARSER_SEVERITY_ERROR;
557
0
        }
558
559
0
        reader->errorFunc(reader->errorFuncArg, error->message, severity,
560
0
                          reader->ctxt);
561
0
    }
562
0
}
563
564
/**
565
 * xmlTextReaderEntPush:
566
 * @reader:  the xmlTextReaderPtr used
567
 * @value:  the entity reference node
568
 *
569
 * Pushes a new entity reference node on top of the entities stack
570
 *
571
 * Returns -1 in case of error, the index in the stack otherwise
572
 */
573
static int
574
xmlTextReaderEntPush(xmlTextReaderPtr reader, xmlNodePtr value)
575
0
{
576
0
    if (reader->entNr >= reader->entMax) {
577
0
        size_t newSize = reader->entMax == 0 ? 10 : reader->entMax * 2;
578
0
        xmlNodePtr *tmp;
579
580
0
        tmp = (xmlNodePtr *) xmlRealloc(reader->entTab,
581
0
                                        newSize * sizeof(*tmp));
582
0
        if (tmp == NULL) {
583
0
            xmlTextReaderErrMemory(reader);
584
0
            return (-1);
585
0
        }
586
0
        reader->entTab = tmp;
587
0
        reader->entMax = newSize;
588
0
    }
589
0
    reader->entTab[reader->entNr] = value;
590
0
    reader->ent = value;
591
0
    return (reader->entNr++);
592
0
}
593
594
/**
595
 * xmlTextReaderEntPop:
596
 * @reader:  the xmlTextReaderPtr used
597
 *
598
 * Pops the top element entity from the entities stack
599
 *
600
 * Returns the entity just removed
601
 */
602
static xmlNodePtr
603
xmlTextReaderEntPop(xmlTextReaderPtr reader)
604
0
{
605
0
    xmlNodePtr ret;
606
607
0
    if (reader->entNr <= 0)
608
0
        return (NULL);
609
0
    reader->entNr--;
610
0
    if (reader->entNr > 0)
611
0
        reader->ent = reader->entTab[reader->entNr - 1];
612
0
    else
613
0
        reader->ent = NULL;
614
0
    ret = reader->entTab[reader->entNr];
615
0
    reader->entTab[reader->entNr] = NULL;
616
0
    return (ret);
617
0
}
618
619
/**
620
 * xmlTextReaderStartElement:
621
 * @ctx: the user data (XML parser context)
622
 * @fullname:  The element name, including namespace prefix
623
 * @atts:  An array of name/value attributes pairs, NULL terminated
624
 *
625
 * called when an opening tag has been processed.
626
 */
627
static void
628
xmlTextReaderStartElement(void *ctx, const xmlChar *fullname,
629
0
                    const xmlChar **atts) {
630
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
631
0
    xmlTextReaderPtr reader = ctxt->_private;
632
633
0
    if ((reader != NULL) && (reader->startElement != NULL)) {
634
0
  reader->startElement(ctx, fullname, atts);
635
0
  if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
636
0
      (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
637
0
      (ctxt->input->cur[1] == '>'))
638
0
      ctxt->node->extra = NODE_IS_EMPTY;
639
0
    }
640
0
    if (reader != NULL)
641
0
  reader->state = XML_TEXTREADER_ELEMENT;
642
0
}
643
644
/**
645
 * xmlTextReaderEndElement:
646
 * @ctx: the user data (XML parser context)
647
 * @fullname:  The element name, including namespace prefix
648
 *
649
 * called when an ending tag has been processed.
650
 */
651
static void
652
0
xmlTextReaderEndElement(void *ctx, const xmlChar *fullname) {
653
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
654
0
    xmlTextReaderPtr reader = ctxt->_private;
655
656
0
    if ((reader != NULL) && (reader->endElement != NULL)) {
657
0
  reader->endElement(ctx, fullname);
658
0
    }
659
0
}
660
661
/**
662
 * xmlTextReaderStartElementNs:
663
 * @ctx: the user data (XML parser context)
664
 * @localname:  the local name of the element
665
 * @prefix:  the element namespace prefix if available
666
 * @URI:  the element namespace name if available
667
 * @nb_namespaces:  number of namespace definitions on that node
668
 * @namespaces:  pointer to the array of prefix/URI pairs namespace definitions
669
 * @nb_attributes:  the number of attributes on that node
670
 * nb_defaulted:  the number of defaulted attributes.
671
 * @attributes:  pointer to the array of (localname/prefix/URI/value/end)
672
 *               attribute values.
673
 *
674
 * called when an opening tag has been processed.
675
 */
676
static void
677
xmlTextReaderStartElementNs(void *ctx,
678
                      const xmlChar *localname,
679
          const xmlChar *prefix,
680
          const xmlChar *URI,
681
          int nb_namespaces,
682
          const xmlChar **namespaces,
683
          int nb_attributes,
684
          int nb_defaulted,
685
          const xmlChar **attributes)
686
0
{
687
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
688
0
    xmlTextReaderPtr reader = ctxt->_private;
689
690
0
    if ((reader != NULL) && (reader->startElementNs != NULL)) {
691
0
  reader->startElementNs(ctx, localname, prefix, URI, nb_namespaces,
692
0
                         namespaces, nb_attributes, nb_defaulted,
693
0
             attributes);
694
0
  if ((ctxt->node != NULL) && (ctxt->input != NULL) &&
695
0
      (ctxt->input->cur != NULL) && (ctxt->input->cur[0] == '/') &&
696
0
      (ctxt->input->cur[1] == '>'))
697
0
      ctxt->node->extra = NODE_IS_EMPTY;
698
0
    }
699
0
    if (reader != NULL)
700
0
  reader->state = XML_TEXTREADER_ELEMENT;
701
0
}
702
703
/**
704
 * xmlTextReaderEndElementNs:
705
 * @ctx: the user data (XML parser context)
706
 * @localname:  the local name of the element
707
 * @prefix:  the element namespace prefix if available
708
 * @URI:  the element namespace name if available
709
 *
710
 * called when an ending tag has been processed.
711
 */
712
static void
713
xmlTextReaderEndElementNs(void *ctx,
714
                          const xmlChar * localname,
715
                          const xmlChar * prefix,
716
              const xmlChar * URI)
717
0
{
718
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
719
0
    xmlTextReaderPtr reader = ctxt->_private;
720
721
0
    if ((reader != NULL) && (reader->endElementNs != NULL)) {
722
0
  reader->endElementNs(ctx, localname, prefix, URI);
723
0
    }
724
0
}
725
726
727
/**
728
 * xmlTextReaderCharacters:
729
 * @ctx: the user data (XML parser context)
730
 * @ch:  a xmlChar string
731
 * @len: the number of xmlChar
732
 *
733
 * receiving some chars from the parser.
734
 */
735
static void
736
xmlTextReaderCharacters(void *ctx, const xmlChar *ch, int len)
737
0
{
738
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
739
0
    xmlTextReaderPtr reader = ctxt->_private;
740
741
0
    if ((reader != NULL) && (reader->characters != NULL)) {
742
0
  reader->characters(ctx, ch, len);
743
0
    }
744
0
}
745
746
/**
747
 * xmlTextReaderCDataBlock:
748
 * @ctx: the user data (XML parser context)
749
 * @value:  The pcdata content
750
 * @len:  the block length
751
 *
752
 * called when a pcdata block has been parsed
753
 */
754
static void
755
xmlTextReaderCDataBlock(void *ctx, const xmlChar *ch, int len)
756
0
{
757
0
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
758
0
    xmlTextReaderPtr reader = ctxt->_private;
759
760
0
    if ((reader != NULL) && (reader->cdataBlock != NULL)) {
761
0
  reader->cdataBlock(ctx, ch, len);
762
0
    }
763
0
}
764
765
/**
766
 * xmlTextReaderPushData:
767
 * @reader:  the xmlTextReaderPtr used
768
 *
769
 * Push data down the progressive parser until a significant callback
770
 * got raised.
771
 *
772
 * Returns -1 in case of failure, 0 otherwise
773
 */
774
static int
775
0
xmlTextReaderPushData(xmlTextReaderPtr reader) {
776
0
    xmlBufPtr inbuf;
777
0
    int val, s;
778
0
    xmlTextReaderState oldstate;
779
780
0
    if ((reader->input == NULL) || (reader->input->buffer == NULL))
781
0
  return(-1);
782
783
0
    oldstate = reader->state;
784
0
    reader->state = XML_TEXTREADER_NONE;
785
0
    inbuf = reader->input->buffer;
786
787
0
    while (reader->state == XML_TEXTREADER_NONE) {
788
0
  if (xmlBufUse(inbuf) < reader->cur + CHUNK_SIZE) {
789
      /*
790
       * Refill the buffer unless we are at the end of the stream
791
       */
792
0
      if (reader->mode != XML_TEXTREADER_MODE_EOF) {
793
0
    val = xmlParserInputBufferRead(reader->input, 4096);
794
0
    if (val == 0) {
795
0
        if (xmlBufUse(inbuf) == reader->cur) {
796
0
      reader->mode = XML_TEXTREADER_MODE_EOF;
797
0
                        break;
798
0
        }
799
0
    } else if (val < 0) {
800
0
                    xmlCtxtErrIO(reader->ctxt, reader->input->error, NULL);
801
0
                    reader->mode = XML_TEXTREADER_MODE_ERROR;
802
0
                    reader->state = XML_TEXTREADER_ERROR;
803
0
                    return(-1);
804
0
    }
805
806
0
      } else
807
0
    break;
808
0
  }
809
  /*
810
   * parse by block of CHUNK_SIZE bytes, various tests show that
811
   * it's the best tradeoff at least on a 1.2GH Duron
812
   */
813
0
  if (xmlBufUse(inbuf) >= reader->cur + CHUNK_SIZE) {
814
0
      val = xmlParseChunk(reader->ctxt,
815
0
                 (const char *) xmlBufContent(inbuf) + reader->cur,
816
0
                                CHUNK_SIZE, 0);
817
0
      reader->cur += CHUNK_SIZE;
818
0
      if (val != 0)
819
0
    reader->ctxt->wellFormed = 0;
820
0
      if (reader->ctxt->wellFormed == 0)
821
0
    break;
822
0
  } else {
823
0
      s = xmlBufUse(inbuf) - reader->cur;
824
0
      val = xmlParseChunk(reader->ctxt,
825
0
     (const char *) xmlBufContent(inbuf) + reader->cur,
826
0
              s, 0);
827
0
      reader->cur += s;
828
0
      if (val != 0)
829
0
    reader->ctxt->wellFormed = 0;
830
0
      break;
831
0
  }
832
0
    }
833
0
    reader->state = oldstate;
834
835
    /*
836
     * Discard the consumed input when needed and possible
837
     */
838
0
    if (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE) {
839
0
        if (reader->cur > 80 /* LINE_LEN */) {
840
0
            val = xmlBufShrink(inbuf, reader->cur - 80);
841
0
            if (val >= 0) {
842
0
                reader->cur -= val;
843
0
            }
844
0
        }
845
0
    }
846
847
    /*
848
     * At the end of the stream signal that the work is done to the Push
849
     * parser.
850
     */
851
0
    else if (reader->mode == XML_TEXTREADER_MODE_EOF) {
852
0
  if (reader->state != XML_TEXTREADER_DONE) {
853
0
      s = xmlBufUse(inbuf) - reader->cur;
854
0
      val = xmlParseChunk(reader->ctxt,
855
0
     (const char *) xmlBufContent(inbuf) + reader->cur,
856
0
              s, 1);
857
0
      reader->cur = xmlBufUse(inbuf);
858
0
      reader->state  = XML_TEXTREADER_DONE;
859
0
      if (val != 0) {
860
0
          if (reader->ctxt->wellFormed)
861
0
        reader->ctxt->wellFormed = 0;
862
0
    else
863
0
        return(-1);
864
0
      }
865
0
  }
866
0
    }
867
0
    if (reader->ctxt->wellFormed == 0) {
868
0
  reader->mode = XML_TEXTREADER_MODE_EOF;
869
0
        return(-1);
870
0
    }
871
872
0
    return(0);
873
0
}
874
875
#ifdef LIBXML_REGEXP_ENABLED
876
/**
877
 * xmlTextReaderValidatePush:
878
 * @reader:  the xmlTextReaderPtr used
879
 *
880
 * Push the current node for validation
881
 */
882
static int
883
0
xmlTextReaderValidatePush(xmlTextReaderPtr reader) {
884
0
    xmlNodePtr node = reader->node;
885
886
0
#ifdef LIBXML_VALID_ENABLED
887
0
    if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
888
0
        (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
889
0
  if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
890
0
      reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
891
0
            reader->ctxt->myDoc, node, node->name);
892
0
  } else {
893
0
            xmlChar buf[50];
894
0
      xmlChar *qname;
895
896
0
      qname = xmlBuildQName(node->name, node->ns->prefix, buf, 50);
897
0
            if (qname == NULL) {
898
0
                xmlTextReaderErrMemory(reader);
899
0
                return(-1);
900
0
            }
901
0
      reader->ctxt->valid &= xmlValidatePushElement(&reader->ctxt->vctxt,
902
0
            reader->ctxt->myDoc, node, qname);
903
0
            if (qname != buf)
904
0
          xmlFree(qname);
905
0
  }
906
        /*if (reader->ctxt->errNo == XML_ERR_NO_MEMORY) {
907
            reader->mode = XML_TEXTREADER_MODE_ERROR;
908
            reader->state = XML_TEXTREADER_ERROR;
909
            return(-1);
910
        }*/
911
0
    }
912
0
#endif /* LIBXML_VALID_ENABLED */
913
0
#ifdef LIBXML_SCHEMAS_ENABLED
914
0
    if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
915
0
               (reader->rngValidCtxt != NULL)) {
916
0
  int ret;
917
918
0
  if (reader->rngFullNode != NULL) return(0);
919
0
  ret = xmlRelaxNGValidatePushElement(reader->rngValidCtxt,
920
0
                                      reader->ctxt->myDoc,
921
0
              node);
922
0
  if (ret == 0) {
923
      /*
924
       * this element requires a full tree
925
       */
926
0
      node = xmlTextReaderExpand(reader);
927
0
      if (node == NULL) {
928
0
          ret = -1;
929
0
      } else {
930
0
    ret = xmlRelaxNGValidateFullElement(reader->rngValidCtxt,
931
0
                reader->ctxt->myDoc,
932
0
                node);
933
0
    reader->rngFullNode = node;
934
0
      }
935
0
  }
936
0
  if (ret != 1)
937
0
      reader->rngValidErrors++;
938
0
    }
939
0
#endif
940
941
0
    return(0);
942
0
}
943
944
/**
945
 * xmlTextReaderValidateCData:
946
 * @reader:  the xmlTextReaderPtr used
947
 * @data:  pointer to the CData
948
 * @len:  length of the CData block in bytes.
949
 *
950
 * Push some CData for validation
951
 */
952
static void
953
xmlTextReaderValidateCData(xmlTextReaderPtr reader,
954
0
                           const xmlChar *data, int len) {
955
0
#ifdef LIBXML_VALID_ENABLED
956
0
    if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
957
0
        (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
958
0
  reader->ctxt->valid &= xmlValidatePushCData(&reader->ctxt->vctxt,
959
0
                                              data, len);
960
0
    }
961
0
#endif /* LIBXML_VALID_ENABLED */
962
0
#ifdef LIBXML_SCHEMAS_ENABLED
963
0
    if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
964
0
               (reader->rngValidCtxt != NULL)) {
965
0
  int ret;
966
967
0
  if (reader->rngFullNode != NULL) return;
968
0
  ret = xmlRelaxNGValidatePushCData(reader->rngValidCtxt, data, len);
969
0
  if (ret != 1)
970
0
      reader->rngValidErrors++;
971
0
    }
972
0
#endif
973
0
}
974
975
/**
976
 * xmlTextReaderValidatePop:
977
 * @reader:  the xmlTextReaderPtr used
978
 *
979
 * Pop the current node from validation
980
 */
981
static int
982
0
xmlTextReaderValidatePop(xmlTextReaderPtr reader) {
983
0
    xmlNodePtr node = reader->node;
984
985
0
#ifdef LIBXML_VALID_ENABLED
986
0
    if ((reader->validate == XML_TEXTREADER_VALIDATE_DTD) &&
987
0
        (reader->ctxt != NULL) && (reader->ctxt->validate == 1)) {
988
0
  if ((node->ns == NULL) || (node->ns->prefix == NULL)) {
989
0
      reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
990
0
            reader->ctxt->myDoc, node, node->name);
991
0
  } else {
992
0
            xmlChar buf[50];
993
0
      xmlChar *qname;
994
995
0
      qname = xmlBuildQName(node->name, node->ns->prefix, buf, 50);
996
0
            if (qname == NULL) {
997
0
                xmlTextReaderErrMemory(reader);
998
0
                return(-1);
999
0
            }
1000
0
      reader->ctxt->valid &= xmlValidatePopElement(&reader->ctxt->vctxt,
1001
0
            reader->ctxt->myDoc, node, qname);
1002
0
            if (qname != buf)
1003
0
          xmlFree(qname);
1004
0
  }
1005
        /*if (reader->ctxt->errNo == XML_ERR_NO_MEMORY) {
1006
            reader->mode = XML_TEXTREADER_MODE_ERROR;
1007
            reader->state = XML_TEXTREADER_ERROR;
1008
            return(-1);
1009
        }*/
1010
0
    }
1011
0
#endif /* LIBXML_VALID_ENABLED */
1012
0
#ifdef LIBXML_SCHEMAS_ENABLED
1013
0
    if ((reader->validate == XML_TEXTREADER_VALIDATE_RNG) &&
1014
0
               (reader->rngValidCtxt != NULL)) {
1015
0
  int ret;
1016
1017
0
  if (reader->rngFullNode != NULL) {
1018
0
      if (node == reader->rngFullNode)
1019
0
          reader->rngFullNode = NULL;
1020
0
      return(0);
1021
0
  }
1022
0
  ret = xmlRelaxNGValidatePopElement(reader->rngValidCtxt,
1023
0
                                     reader->ctxt->myDoc,
1024
0
             node);
1025
0
  if (ret != 1)
1026
0
      reader->rngValidErrors++;
1027
0
    }
1028
0
#endif
1029
1030
0
    return(0);
1031
0
}
1032
1033
/**
1034
 * xmlTextReaderValidateEntity:
1035
 * @reader:  the xmlTextReaderPtr used
1036
 *
1037
 * Handle the validation when an entity reference is encountered and
1038
 * entity substitution is not activated. As a result the parser interface
1039
 * must walk through the entity and do the validation calls
1040
 */
1041
static int
1042
0
xmlTextReaderValidateEntity(xmlTextReaderPtr reader) {
1043
0
    xmlNodePtr oldnode = reader->node;
1044
0
    xmlNodePtr node = reader->node;
1045
1046
0
    do {
1047
0
  if (node->type == XML_ENTITY_REF_NODE) {
1048
0
      if ((node->children != NULL) &&
1049
0
    (node->children->type == XML_ENTITY_DECL) &&
1050
0
    (node->children->children != NULL)) {
1051
0
    if (xmlTextReaderEntPush(reader, node) < 0) {
1052
0
                    if (node == oldnode)
1053
0
                        break;
1054
0
                    goto skip_children;
1055
0
                }
1056
0
    node = node->children->children;
1057
0
    continue;
1058
0
      } else {
1059
    /*
1060
     * The error has probably been raised already.
1061
     */
1062
0
    if (node == oldnode)
1063
0
        break;
1064
0
                goto skip_children;
1065
0
      }
1066
0
#ifdef LIBXML_REGEXP_ENABLED
1067
0
  } else if (node->type == XML_ELEMENT_NODE) {
1068
0
      reader->node = node;
1069
0
      if (xmlTextReaderValidatePush(reader) < 0)
1070
0
                return(-1);
1071
0
  } else if ((node->type == XML_TEXT_NODE) ||
1072
0
       (node->type == XML_CDATA_SECTION_NODE)) {
1073
0
            xmlTextReaderValidateCData(reader, node->content,
1074
0
                                 xmlStrlen(node->content));
1075
0
#endif
1076
0
  }
1077
1078
  /*
1079
   * go to next node
1080
   */
1081
0
  if (node->children != NULL) {
1082
0
      node = node->children;
1083
0
      continue;
1084
0
  } else if (node->type == XML_ELEMENT_NODE) {
1085
0
      if (xmlTextReaderValidatePop(reader) < 0)
1086
0
                return(-1);
1087
0
  }
1088
0
skip_children:
1089
0
  if (node->next != NULL) {
1090
0
      node = node->next;
1091
0
      continue;
1092
0
  }
1093
0
  do {
1094
0
      node = node->parent;
1095
0
      if (node->type == XML_ELEMENT_NODE) {
1096
0
          xmlNodePtr tmp;
1097
0
    if (reader->entNr == 0) {
1098
0
        while ((tmp = node->last) != NULL) {
1099
0
      if ((tmp->extra & NODE_IS_PRESERVED) == 0) {
1100
0
          xmlUnlinkNode(tmp);
1101
0
          xmlTextReaderFreeNode(reader, tmp);
1102
0
      } else
1103
0
          break;
1104
0
        }
1105
0
    }
1106
0
    reader->node = node;
1107
0
    if (xmlTextReaderValidatePop(reader) < 0)
1108
0
                    return(-1);
1109
0
      }
1110
0
      if ((node->type == XML_ENTITY_DECL) &&
1111
0
    (reader->ent != NULL) && (reader->ent->children == node)) {
1112
0
    node = xmlTextReaderEntPop(reader);
1113
0
      }
1114
0
      if (node == oldnode)
1115
0
    break;
1116
0
      if (node->next != NULL) {
1117
0
    node = node->next;
1118
0
    break;
1119
0
      }
1120
0
  } while ((node != NULL) && (node != oldnode));
1121
0
    } while ((node != NULL) && (node != oldnode));
1122
0
    reader->node = oldnode;
1123
1124
0
    return(0);
1125
0
}
1126
#endif /* LIBXML_REGEXP_ENABLED */
1127
1128
1129
/**
1130
 * xmlTextReaderGetSuccessor:
1131
 * @cur:  the current node
1132
 *
1133
 * Get the successor of a node if available.
1134
 *
1135
 * Returns the successor node or NULL
1136
 */
1137
static xmlNodePtr
1138
0
xmlTextReaderGetSuccessor(xmlNodePtr cur) {
1139
0
    if (cur == NULL) return(NULL) ; /* ERROR */
1140
0
    if (cur->next != NULL) return(cur->next) ;
1141
0
    do {
1142
0
        cur = cur->parent;
1143
0
        if (cur == NULL) break;
1144
0
        if (cur->next != NULL) return(cur->next);
1145
0
    } while (cur != NULL);
1146
0
    return(cur);
1147
0
}
1148
1149
/**
1150
 * xmlTextReaderDoExpand:
1151
 * @reader:  the xmlTextReaderPtr used
1152
 *
1153
 * Makes sure that the current node is fully read as well as all its
1154
 * descendant. It means the full DOM subtree must be available at the
1155
 * end of the call.
1156
 *
1157
 * Returns 1 if the node was expanded successfully, 0 if there is no more
1158
 *          nodes to read, or -1 in case of error
1159
 */
1160
static int
1161
0
xmlTextReaderDoExpand(xmlTextReaderPtr reader) {
1162
0
    int val;
1163
1164
0
    if ((reader == NULL) || (reader->node == NULL) || (reader->ctxt == NULL))
1165
0
        return(-1);
1166
0
    do {
1167
0
  if (PARSER_STOPPED(reader->ctxt))
1168
0
            return(1);
1169
1170
0
        if (xmlTextReaderGetSuccessor(reader->node) != NULL)
1171
0
      return(1);
1172
0
  if (reader->ctxt->nodeNr < reader->depth)
1173
0
      return(1);
1174
0
  if (reader->mode == XML_TEXTREADER_MODE_EOF)
1175
0
      return(1);
1176
0
  val = xmlTextReaderPushData(reader);
1177
0
  if (val < 0){
1178
0
      reader->mode = XML_TEXTREADER_MODE_ERROR;
1179
0
            reader->state = XML_TEXTREADER_ERROR;
1180
0
      return(-1);
1181
0
  }
1182
0
    } while(reader->mode != XML_TEXTREADER_MODE_EOF);
1183
0
    return(1);
1184
0
}
1185
1186
/**
1187
 * xmlTextReaderRead:
1188
 * @reader:  the xmlTextReaderPtr used
1189
 *
1190
 *  Moves the position of the current instance to the next node in
1191
 *  the stream, exposing its properties.
1192
 *
1193
 *  Returns 1 if the node was read successfully, 0 if there is no more
1194
 *          nodes to read, or -1 in case of error
1195
 */
1196
int
1197
0
xmlTextReaderRead(xmlTextReaderPtr reader) {
1198
0
    int val, olddepth = 0;
1199
0
    xmlTextReaderState oldstate = XML_TEXTREADER_START;
1200
0
    xmlNodePtr oldnode = NULL;
1201
1202
0
    if (reader == NULL)
1203
0
  return(-1);
1204
0
    if (reader->state == XML_TEXTREADER_ERROR)
1205
0
        return(-1);
1206
1207
0
    reader->curnode = NULL;
1208
0
    if (reader->doc != NULL)
1209
0
        return(xmlTextReaderReadTree(reader));
1210
0
    if (reader->ctxt == NULL)
1211
0
  return(-1);
1212
1213
0
    if (reader->mode == XML_TEXTREADER_MODE_INITIAL) {
1214
0
  reader->mode = XML_TEXTREADER_MODE_INTERACTIVE;
1215
  /*
1216
   * Initial state
1217
   */
1218
0
  do {
1219
0
      val = xmlTextReaderPushData(reader);
1220
0
            if (val < 0) {
1221
0
                reader->mode = XML_TEXTREADER_MODE_ERROR;
1222
0
                reader->state = XML_TEXTREADER_ERROR;
1223
0
                return(-1);
1224
0
            }
1225
0
  } while ((reader->ctxt->node == NULL) &&
1226
0
     ((reader->mode != XML_TEXTREADER_MODE_EOF) &&
1227
0
      (reader->state != XML_TEXTREADER_DONE)));
1228
0
  if (reader->ctxt->node == NULL) {
1229
0
      if (reader->ctxt->myDoc != NULL) {
1230
0
    reader->node = reader->ctxt->myDoc->children;
1231
0
      }
1232
0
      if (reader->node == NULL) {
1233
0
                reader->mode = XML_TEXTREADER_MODE_ERROR;
1234
0
                reader->state = XML_TEXTREADER_ERROR;
1235
0
    return(-1);
1236
0
      }
1237
0
      reader->state = XML_TEXTREADER_ELEMENT;
1238
0
  } else {
1239
0
      if (reader->ctxt->myDoc != NULL) {
1240
0
    reader->node = reader->ctxt->myDoc->children;
1241
0
      }
1242
0
      if (reader->node == NULL)
1243
0
    reader->node = reader->ctxt->nodeTab[0];
1244
0
      reader->state = XML_TEXTREADER_ELEMENT;
1245
0
  }
1246
0
  reader->depth = 0;
1247
0
  reader->ctxt->parseMode = XML_PARSE_READER;
1248
0
  goto node_found;
1249
0
    }
1250
0
    oldstate = reader->state;
1251
0
    olddepth = reader->ctxt->nodeNr;
1252
0
    oldnode = reader->node;
1253
1254
0
get_next_node:
1255
0
    if (reader->node == NULL) {
1256
0
  if (reader->mode == XML_TEXTREADER_MODE_EOF) {
1257
0
      return(0);
1258
0
        } else {
1259
0
            reader->mode = XML_TEXTREADER_MODE_ERROR;
1260
0
            reader->state = XML_TEXTREADER_ERROR;
1261
0
      return(-1);
1262
0
        }
1263
0
    }
1264
1265
    /*
1266
     * If we are not backtracking on ancestors or examined nodes,
1267
     * that the parser didn't finished or that we aren't at the end
1268
     * of stream, continue processing.
1269
     */
1270
0
    while ((reader->node != NULL) && (reader->node->next == NULL) &&
1271
0
     (reader->ctxt->nodeNr == olddepth) &&
1272
0
           ((oldstate == XML_TEXTREADER_BACKTRACK) ||
1273
0
            (reader->node->children == NULL) ||
1274
0
      (reader->node->type == XML_ENTITY_REF_NODE) ||
1275
0
      ((reader->node->children != NULL) &&
1276
0
       (reader->node->children->type == XML_TEXT_NODE) &&
1277
0
       (reader->node->children->next == NULL)) ||
1278
0
      (reader->node->type == XML_DTD_NODE) ||
1279
0
      (reader->node->type == XML_DOCUMENT_NODE) ||
1280
0
      (reader->node->type == XML_HTML_DOCUMENT_NODE)) &&
1281
0
     ((reader->ctxt->node == NULL) ||
1282
0
      (reader->ctxt->node == reader->node) ||
1283
0
      (reader->ctxt->node == reader->node->parent)) &&
1284
0
     (reader->ctxt->instate != XML_PARSER_EOF) &&
1285
0
     (PARSER_STOPPED(reader->ctxt) == 0)) {
1286
0
  val = xmlTextReaderPushData(reader);
1287
0
  if (val < 0) {
1288
0
            reader->mode = XML_TEXTREADER_MODE_ERROR;
1289
0
            reader->state = XML_TEXTREADER_ERROR;
1290
0
      return(-1);
1291
0
        }
1292
0
  if (reader->node == NULL)
1293
0
      goto node_end;
1294
0
    }
1295
0
    if (oldstate != XML_TEXTREADER_BACKTRACK) {
1296
0
  if ((reader->node->children != NULL) &&
1297
0
      (reader->node->type != XML_ENTITY_REF_NODE) &&
1298
0
      (reader->node->type != XML_XINCLUDE_START) &&
1299
0
      (reader->node->type != XML_DTD_NODE)) {
1300
0
      reader->node = reader->node->children;
1301
0
      reader->depth++;
1302
0
      reader->state = XML_TEXTREADER_ELEMENT;
1303
0
      goto node_found;
1304
0
  }
1305
0
    }
1306
0
    if (reader->node->next != NULL) {
1307
0
  if ((oldstate == XML_TEXTREADER_ELEMENT) &&
1308
0
            (reader->node->type == XML_ELEMENT_NODE) &&
1309
0
      (reader->node->children == NULL) &&
1310
0
      ((reader->node->extra & NODE_IS_EMPTY) == 0)
1311
0
#ifdef LIBXML_XINCLUDE_ENABLED
1312
0
      && (reader->in_xinclude <= 0)
1313
0
#endif
1314
0
      ) {
1315
0
      reader->state = XML_TEXTREADER_END;
1316
0
      goto node_found;
1317
0
  }
1318
0
#ifdef LIBXML_REGEXP_ENABLED
1319
0
  if ((reader->validate) &&
1320
0
      (reader->node->type == XML_ELEMENT_NODE))
1321
0
      if (xmlTextReaderValidatePop(reader) < 0)
1322
0
                return(-1);
1323
0
#endif /* LIBXML_REGEXP_ENABLED */
1324
0
        if ((reader->preserves > 0) &&
1325
0
      (reader->node->extra & NODE_IS_SPRESERVED))
1326
0
      reader->preserves--;
1327
0
  reader->node = reader->node->next;
1328
0
  reader->state = XML_TEXTREADER_ELEMENT;
1329
1330
  /*
1331
   * Cleanup of the old node
1332
   */
1333
0
  if ((reader->preserves == 0) &&
1334
0
#ifdef LIBXML_XINCLUDE_ENABLED
1335
0
      (reader->in_xinclude == 0) &&
1336
0
#endif
1337
0
      (reader->entNr == 0) &&
1338
0
      (reader->node->prev != NULL) &&
1339
0
            (reader->node->prev->type != XML_DTD_NODE)) {
1340
0
      xmlNodePtr tmp = reader->node->prev;
1341
0
      if ((tmp->extra & NODE_IS_PRESERVED) == 0) {
1342
0
                if (oldnode == tmp)
1343
0
                    oldnode = NULL;
1344
0
    xmlUnlinkNode(tmp);
1345
0
    xmlTextReaderFreeNode(reader, tmp);
1346
0
      }
1347
0
  }
1348
1349
0
  goto node_found;
1350
0
    }
1351
0
    if ((oldstate == XML_TEXTREADER_ELEMENT) &&
1352
0
  (reader->node->type == XML_ELEMENT_NODE) &&
1353
0
  (reader->node->children == NULL) &&
1354
0
  ((reader->node->extra & NODE_IS_EMPTY) == 0)) {;
1355
0
  reader->state = XML_TEXTREADER_END;
1356
0
  goto node_found;
1357
0
    }
1358
0
#ifdef LIBXML_REGEXP_ENABLED
1359
0
    if ((reader->validate != XML_TEXTREADER_NOT_VALIDATE) &&
1360
0
        (reader->node->type == XML_ELEMENT_NODE)) {
1361
0
        if (xmlTextReaderValidatePop(reader) < 0)
1362
0
            return(-1);
1363
0
    }
1364
0
#endif /* LIBXML_REGEXP_ENABLED */
1365
0
    if ((reader->preserves > 0) &&
1366
0
  (reader->node->extra & NODE_IS_SPRESERVED))
1367
0
  reader->preserves--;
1368
0
    reader->node = reader->node->parent;
1369
0
    if ((reader->node == NULL) ||
1370
0
  (reader->node->type == XML_DOCUMENT_NODE) ||
1371
0
  (reader->node->type == XML_HTML_DOCUMENT_NODE)) {
1372
0
  if (reader->mode != XML_TEXTREADER_MODE_EOF) {
1373
0
      val = xmlParseChunk(reader->ctxt, "", 0, 1);
1374
0
      reader->state = XML_TEXTREADER_DONE;
1375
0
      if (val != 0) {
1376
0
                reader->mode = XML_TEXTREADER_MODE_ERROR;
1377
0
                reader->state = XML_TEXTREADER_ERROR;
1378
0
          return(-1);
1379
0
            }
1380
0
  }
1381
0
  reader->node = NULL;
1382
0
  reader->depth = -1;
1383
1384
  /*
1385
   * Cleanup of the old node
1386
   */
1387
0
  if ((oldnode != NULL) && (reader->preserves == 0) &&
1388
0
#ifdef LIBXML_XINCLUDE_ENABLED
1389
0
      (reader->in_xinclude == 0) &&
1390
0
#endif
1391
0
      (reader->entNr == 0) &&
1392
0
      (oldnode->type != XML_DTD_NODE) &&
1393
0
      ((oldnode->extra & NODE_IS_PRESERVED) == 0)) {
1394
0
      xmlUnlinkNode(oldnode);
1395
0
      xmlTextReaderFreeNode(reader, oldnode);
1396
0
  }
1397
1398
0
  goto node_end;
1399
0
    }
1400
0
    if ((reader->preserves == 0) &&
1401
0
#ifdef LIBXML_XINCLUDE_ENABLED
1402
0
        (reader->in_xinclude == 0) &&
1403
0
#endif
1404
0
  (reader->entNr == 0) &&
1405
0
        (reader->node->last != NULL) &&
1406
0
        ((reader->node->last->extra & NODE_IS_PRESERVED) == 0)) {
1407
0
  xmlNodePtr tmp = reader->node->last;
1408
0
  xmlUnlinkNode(tmp);
1409
0
  xmlTextReaderFreeNode(reader, tmp);
1410
0
    }
1411
0
    reader->depth--;
1412
0
    reader->state = XML_TEXTREADER_BACKTRACK;
1413
1414
0
node_found:
1415
    /*
1416
     * If we are in the middle of a piece of CDATA make sure it's finished
1417
     */
1418
0
    if ((reader->node != NULL) &&
1419
0
        (reader->node->next == NULL) &&
1420
0
        ((reader->node->type == XML_TEXT_NODE) ||
1421
0
   (reader->node->type == XML_CDATA_SECTION_NODE))) {
1422
0
            if (xmlTextReaderExpand(reader) == NULL)
1423
0
          return -1;
1424
0
    }
1425
1426
0
#ifdef LIBXML_XINCLUDE_ENABLED
1427
    /*
1428
     * Handle XInclude if asked for
1429
     */
1430
0
    if ((reader->xinclude) && (reader->in_xinclude == 0) &&
1431
0
        (reader->state != XML_TEXTREADER_BACKTRACK) &&
1432
0
        (reader->node != NULL) &&
1433
0
  (reader->node->type == XML_ELEMENT_NODE) &&
1434
0
  (reader->node->ns != NULL) &&
1435
0
  ((xmlStrEqual(reader->node->ns->href, XINCLUDE_NS)) ||
1436
0
   (xmlStrEqual(reader->node->ns->href, XINCLUDE_OLD_NS)))) {
1437
0
  if (reader->xincctxt == NULL) {
1438
0
      reader->xincctxt = xmlXIncludeNewContext(reader->ctxt->myDoc);
1439
0
            if (reader->xincctxt == NULL) {
1440
0
                xmlTextReaderErrMemory(reader);
1441
0
                return(-1);
1442
0
            }
1443
0
      xmlXIncludeSetFlags(reader->xincctxt,
1444
0
                          reader->parserFlags & (~XML_PARSE_NOXINCNODE));
1445
0
            xmlXIncludeSetStreamingMode(reader->xincctxt, 1);
1446
0
            if ((reader->errorFunc != NULL) || (reader->sErrorFunc != NULL))
1447
0
                xmlXIncludeSetErrorHandler(reader->xincctxt,
1448
0
                        xmlTextReaderStructuredRelay, reader);
1449
0
            if (reader->resourceLoader != NULL)
1450
0
                xmlXIncludeSetResourceLoader(reader->xincctxt,
1451
0
                        reader->resourceLoader, reader->resourceCtxt);
1452
0
  }
1453
  /*
1454
   * expand that node and process it
1455
   */
1456
0
  if (xmlTextReaderExpand(reader) == NULL)
1457
0
      return(-1);
1458
0
        if (xmlXIncludeProcessNode(reader->xincctxt, reader->node) < 0) {
1459
0
            int err = xmlXIncludeGetLastError(reader->xincctxt);
1460
1461
0
            if (err == XML_ERR_NO_MEMORY)
1462
0
                xmlTextReaderErrMemory(reader);
1463
0
            return(-1);
1464
0
        }
1465
0
    }
1466
0
    if ((reader->node != NULL) && (reader->node->type == XML_XINCLUDE_START)) {
1467
0
        reader->in_xinclude++;
1468
0
  goto get_next_node;
1469
0
    }
1470
0
    if ((reader->node != NULL) && (reader->node->type == XML_XINCLUDE_END)) {
1471
0
        reader->in_xinclude--;
1472
0
  goto get_next_node;
1473
0
    }
1474
0
#endif
1475
    /*
1476
     * Handle entities enter and exit when in entity replacement mode
1477
     */
1478
0
    if ((reader->node != NULL) &&
1479
0
  (reader->node->type == XML_ENTITY_REF_NODE) &&
1480
0
  (reader->ctxt != NULL) && (reader->ctxt->replaceEntities == 1)) {
1481
0
  if ((reader->node->children != NULL) &&
1482
0
      (reader->node->children->type == XML_ENTITY_DECL) &&
1483
0
      (reader->node->children->children != NULL)) {
1484
0
      if (xmlTextReaderEntPush(reader, reader->node) < 0)
1485
0
                goto get_next_node;
1486
0
      reader->node = reader->node->children->children;
1487
0
  }
1488
0
#ifdef LIBXML_REGEXP_ENABLED
1489
0
    } else if ((reader->node != NULL) &&
1490
0
         (reader->node->type == XML_ENTITY_REF_NODE) &&
1491
0
         (reader->ctxt != NULL) && (reader->validate)) {
1492
0
  if (xmlTextReaderValidateEntity(reader) < 0)
1493
0
            return(-1);
1494
0
#endif /* LIBXML_REGEXP_ENABLED */
1495
0
    }
1496
0
    if ((reader->node != NULL) &&
1497
0
  (reader->node->type == XML_ENTITY_DECL) &&
1498
0
  (reader->ent != NULL) && (reader->ent->children == reader->node)) {
1499
0
  reader->node = xmlTextReaderEntPop(reader);
1500
0
  reader->depth++;
1501
0
        goto get_next_node;
1502
0
    }
1503
0
#ifdef LIBXML_REGEXP_ENABLED
1504
0
    if ((reader->validate != XML_TEXTREADER_NOT_VALIDATE) && (reader->node != NULL)) {
1505
0
  xmlNodePtr node = reader->node;
1506
1507
0
  if ((node->type == XML_ELEMENT_NODE) &&
1508
0
            ((reader->state != XML_TEXTREADER_END) &&
1509
0
       (reader->state != XML_TEXTREADER_BACKTRACK))) {
1510
0
      if (xmlTextReaderValidatePush(reader) < 0)
1511
0
                return(-1);
1512
0
  } else if ((node->type == XML_TEXT_NODE) ||
1513
0
       (node->type == XML_CDATA_SECTION_NODE)) {
1514
0
            xmlTextReaderValidateCData(reader, node->content,
1515
0
                                 xmlStrlen(node->content));
1516
0
  }
1517
0
    }
1518
0
#endif /* LIBXML_REGEXP_ENABLED */
1519
0
#ifdef LIBXML_PATTERN_ENABLED
1520
0
    if ((reader->patternNr > 0) && (reader->state != XML_TEXTREADER_END) &&
1521
0
        (reader->state != XML_TEXTREADER_BACKTRACK)) {
1522
0
        int i;
1523
0
  for (i = 0;i < reader->patternNr;i++) {
1524
0
       if (xmlPatternMatch(reader->patternTab[i], reader->node) == 1) {
1525
0
           xmlTextReaderPreserve(reader);
1526
0
     break;
1527
0
             }
1528
0
  }
1529
0
    }
1530
0
#endif /* LIBXML_PATTERN_ENABLED */
1531
0
#ifdef LIBXML_SCHEMAS_ENABLED
1532
0
    if ((reader->validate == XML_TEXTREADER_VALIDATE_XSD) &&
1533
0
        (reader->xsdValidErrors == 0) &&
1534
0
  (reader->xsdValidCtxt != NULL)) {
1535
0
  reader->xsdValidErrors = !xmlSchemaIsValid(reader->xsdValidCtxt);
1536
0
    }
1537
0
#endif /* LIBXML_PATTERN_ENABLED */
1538
0
    return(1);
1539
0
node_end:
1540
0
    reader->state = XML_TEXTREADER_DONE;
1541
0
    return(0);
1542
0
}
1543
1544
/**
1545
 * xmlTextReaderReadState:
1546
 * @reader:  the xmlTextReaderPtr used
1547
 *
1548
 * Gets the read state of the reader.
1549
 *
1550
 * Returns the state value, or -1 in case of error
1551
 */
1552
int
1553
0
xmlTextReaderReadState(xmlTextReaderPtr reader) {
1554
0
    if (reader == NULL)
1555
0
  return(-1);
1556
0
    return(reader->mode);
1557
0
}
1558
1559
/**
1560
 * xmlTextReaderExpand:
1561
 * @reader:  the xmlTextReaderPtr used
1562
 *
1563
 * Reads the contents of the current node and the full subtree. It then makes
1564
 * the subtree available until the next xmlTextReaderRead() call
1565
 *
1566
 * Returns a node pointer valid until the next xmlTextReaderRead() call
1567
 *         or NULL in case of error.
1568
 */
1569
xmlNodePtr
1570
0
xmlTextReaderExpand(xmlTextReaderPtr reader) {
1571
0
    if ((reader == NULL) || (reader->node == NULL))
1572
0
        return(NULL);
1573
0
    if (reader->doc != NULL)
1574
0
        return(reader->node);
1575
0
    if (reader->ctxt == NULL)
1576
0
        return(NULL);
1577
0
    if (xmlTextReaderDoExpand(reader) < 0)
1578
0
        return(NULL);
1579
0
    return(reader->node);
1580
0
}
1581
1582
/**
1583
 * xmlTextReaderNext:
1584
 * @reader:  the xmlTextReaderPtr used
1585
 *
1586
 * Skip to the node following the current one in document order while
1587
 * avoiding the subtree if any.
1588
 *
1589
 * Returns 1 if the node was read successfully, 0 if there is no more
1590
 *          nodes to read, or -1 in case of error
1591
 */
1592
int
1593
0
xmlTextReaderNext(xmlTextReaderPtr reader) {
1594
0
    int ret;
1595
0
    xmlNodePtr cur;
1596
1597
0
    if (reader == NULL)
1598
0
  return(-1);
1599
0
    if (reader->doc != NULL)
1600
0
        return(xmlTextReaderNextTree(reader));
1601
0
    cur = reader->node;
1602
0
    if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE))
1603
0
        return(xmlTextReaderRead(reader));
1604
0
    if (reader->state == XML_TEXTREADER_END || reader->state == XML_TEXTREADER_BACKTRACK)
1605
0
        return(xmlTextReaderRead(reader));
1606
0
    if (cur->extra & NODE_IS_EMPTY)
1607
0
        return(xmlTextReaderRead(reader));
1608
0
    do {
1609
0
        ret = xmlTextReaderRead(reader);
1610
0
  if (ret != 1)
1611
0
      return(ret);
1612
0
    } while (reader->node != cur);
1613
0
    return(xmlTextReaderRead(reader));
1614
0
}
1615
1616
#ifdef LIBXML_WRITER_ENABLED
1617
static void
1618
xmlTextReaderDumpCopy(xmlTextReaderPtr reader, xmlOutputBufferPtr output,
1619
0
                      xmlNodePtr node) {
1620
0
    if ((node->type == XML_DTD_NODE) ||
1621
0
        (node->type == XML_ELEMENT_DECL) ||
1622
0
        (node->type == XML_ATTRIBUTE_DECL) ||
1623
0
        (node->type == XML_ENTITY_DECL))
1624
0
        return;
1625
1626
0
    if ((node->type == XML_DOCUMENT_NODE) ||
1627
0
        (node->type == XML_HTML_DOCUMENT_NODE)) {
1628
0
        xmlNodeDumpOutput(output, node->doc, node, 0, 0, NULL);
1629
0
    } else {
1630
0
        xmlNodePtr copy;
1631
1632
        /*
1633
         * Create a copy to make sure that namespace declarations from
1634
         * ancestors are added.
1635
         */
1636
0
        copy = xmlDocCopyNode(node, node->doc, 1);
1637
0
        if (copy == NULL) {
1638
0
            xmlTextReaderErrMemory(reader);
1639
0
            return;
1640
0
        }
1641
1642
0
        xmlNodeDumpOutput(output, copy->doc, copy, 0, 0, NULL);
1643
1644
0
        xmlFreeNode(copy);
1645
0
    }
1646
0
}
1647
1648
/**
1649
 * xmlTextReaderReadInnerXml:
1650
 * @reader:  the xmlTextReaderPtr used
1651
 *
1652
 * Reads the contents of the current node, including child nodes and markup.
1653
 *
1654
 * Returns a string containing the XML content, or NULL if the current node
1655
 *         is neither an element nor attribute, or has no child nodes. The
1656
 *         string must be deallocated by the caller.
1657
 */
1658
xmlChar *
1659
xmlTextReaderReadInnerXml(xmlTextReaderPtr reader)
1660
0
{
1661
0
    xmlOutputBufferPtr output;
1662
0
    xmlNodePtr cur;
1663
0
    xmlChar *ret;
1664
1665
0
    if (xmlTextReaderExpand(reader) == NULL)
1666
0
        return(NULL);
1667
1668
0
    if (reader->node == NULL)
1669
0
        return(NULL);
1670
1671
0
    output = xmlAllocOutputBuffer(NULL);
1672
0
    if (output == NULL) {
1673
0
        xmlTextReaderErrMemory(reader);
1674
0
        return(NULL);
1675
0
    }
1676
1677
0
    for (cur = reader->node->children; cur != NULL; cur = cur->next)
1678
0
        xmlTextReaderDumpCopy(reader, output, cur);
1679
1680
0
    if (output->error)
1681
0
        xmlCtxtErrIO(reader->ctxt, output->error, NULL);
1682
1683
0
    ret = xmlBufDetach(output->buffer);
1684
0
    xmlOutputBufferClose(output);
1685
1686
0
    return(ret);
1687
0
}
1688
1689
/**
1690
 * xmlTextReaderReadOuterXml:
1691
 * @reader:  the xmlTextReaderPtr used
1692
 *
1693
 * Reads the contents of the current node, including child nodes and markup.
1694
 *
1695
 * Returns a string containing the node and any XML content, or NULL if the
1696
 *         current node cannot be serialized. The string must be deallocated
1697
 *         by the caller.
1698
 */
1699
xmlChar *
1700
xmlTextReaderReadOuterXml(xmlTextReaderPtr reader)
1701
0
{
1702
0
    xmlOutputBufferPtr output;
1703
0
    xmlNodePtr node;
1704
0
    xmlChar *ret;
1705
1706
0
    if (xmlTextReaderExpand(reader) == NULL)
1707
0
        return(NULL);
1708
1709
0
    node = reader->node;
1710
0
    if (node == NULL)
1711
0
        return(NULL);
1712
1713
0
    output = xmlAllocOutputBuffer(NULL);
1714
0
    if (output == NULL) {
1715
0
        xmlTextReaderErrMemory(reader);
1716
0
        return(NULL);
1717
0
    }
1718
1719
0
    xmlTextReaderDumpCopy(reader, output, node);
1720
0
    if (output->error)
1721
0
        xmlCtxtErrIO(reader->ctxt, output->error, NULL);
1722
1723
0
    ret = xmlBufDetach(output->buffer);
1724
0
    xmlOutputBufferClose(output);
1725
1726
0
    return(ret);
1727
0
}
1728
#endif
1729
1730
/**
1731
 * xmlTextReaderReadString:
1732
 * @reader:  the xmlTextReaderPtr used
1733
 *
1734
 * Reads the contents of an element or a text node as a string.
1735
 *
1736
 * Returns a string containing the contents of the Element or Text node,
1737
 *         or NULL if the reader is positioned on any other type of node.
1738
 *         The string must be deallocated by the caller.
1739
 */
1740
xmlChar *
1741
xmlTextReaderReadString(xmlTextReaderPtr reader)
1742
0
{
1743
0
    xmlNodePtr node, cur;
1744
0
    xmlBufPtr buf;
1745
0
    xmlChar *ret;
1746
1747
0
    if ((reader == NULL) || (reader->node == NULL))
1748
0
       return(NULL);
1749
1750
0
    node = (reader->curnode != NULL) ? reader->curnode : reader->node;
1751
0
    switch (node->type) {
1752
0
        case XML_TEXT_NODE:
1753
0
        case XML_CDATA_SECTION_NODE:
1754
0
            break;
1755
0
        case XML_ELEMENT_NODE:
1756
0
            if (xmlTextReaderDoExpand(reader) == -1)
1757
0
                return(NULL);
1758
0
            break;
1759
0
        case XML_ATTRIBUTE_NODE:
1760
            /* TODO */
1761
0
            break;
1762
0
        default:
1763
0
            break;
1764
0
    }
1765
1766
0
    buf = xmlBufCreate(50);
1767
0
    if (buf == NULL) {
1768
0
        xmlTextReaderErrMemory(reader);
1769
0
        return(NULL);
1770
0
    }
1771
1772
0
    cur = node;
1773
0
    while (cur != NULL) {
1774
0
        switch (cur->type) {
1775
0
            case XML_TEXT_NODE:
1776
0
            case XML_CDATA_SECTION_NODE:
1777
0
                xmlBufCat(buf, cur->content);
1778
0
                break;
1779
1780
0
            case XML_ELEMENT_NODE:
1781
0
                if (cur->children != NULL) {
1782
0
                    cur = cur->children;
1783
0
                    continue;
1784
0
                }
1785
0
                break;
1786
1787
0
            default:
1788
0
                break;
1789
0
        }
1790
1791
0
        if (cur == node)
1792
0
            goto done;
1793
1794
0
        while (cur->next == NULL) {
1795
0
            cur = cur->parent;
1796
0
            if (cur == node)
1797
0
                goto done;
1798
0
        }
1799
0
        cur = cur->next;
1800
0
    }
1801
1802
0
done:
1803
0
    ret = xmlBufDetach(buf);
1804
0
    if (ret == NULL)
1805
0
        xmlTextReaderErrMemory(reader);
1806
1807
0
    xmlBufFree(buf);
1808
0
    return(ret);
1809
0
}
1810
1811
/************************************************************************
1812
 *                  *
1813
 *      Operating on a preparsed tree     *
1814
 *                  *
1815
 ************************************************************************/
1816
static int
1817
xmlTextReaderNextTree(xmlTextReaderPtr reader)
1818
0
{
1819
0
    if (reader == NULL)
1820
0
        return(-1);
1821
1822
0
    if (reader->state == XML_TEXTREADER_END)
1823
0
        return(0);
1824
1825
0
    if (reader->node == NULL) {
1826
0
        if (reader->doc->children == NULL) {
1827
0
            reader->state = XML_TEXTREADER_END;
1828
0
            return(0);
1829
0
        }
1830
1831
0
        reader->node = reader->doc->children;
1832
0
        reader->state = XML_TEXTREADER_START;
1833
0
        return(1);
1834
0
    }
1835
1836
0
    if (reader->state != XML_TEXTREADER_BACKTRACK) {
1837
  /* Here removed traversal to child, because we want to skip the subtree,
1838
  replace with traversal to sibling to skip subtree */
1839
0
        if (reader->node->next != 0) {
1840
      /* Move to sibling if present,skipping sub-tree */
1841
0
            reader->node = reader->node->next;
1842
0
            reader->state = XML_TEXTREADER_START;
1843
0
            return(1);
1844
0
        }
1845
1846
  /* if reader->node->next is NULL mean no subtree for current node,
1847
  so need to move to sibling of parent node if present */
1848
0
  reader->state = XML_TEXTREADER_BACKTRACK;
1849
  /* This will move to parent if present */
1850
0
  xmlTextReaderRead(reader);
1851
0
    }
1852
1853
0
    if (reader->node->next != 0) {
1854
0
        reader->node = reader->node->next;
1855
0
        reader->state = XML_TEXTREADER_START;
1856
0
        return(1);
1857
0
    }
1858
1859
0
    if (reader->node->parent != 0) {
1860
0
        if (reader->node->parent->type == XML_DOCUMENT_NODE) {
1861
0
            reader->state = XML_TEXTREADER_END;
1862
0
            return(0);
1863
0
        }
1864
1865
0
        reader->node = reader->node->parent;
1866
0
        reader->depth--;
1867
0
        reader->state = XML_TEXTREADER_BACKTRACK;
1868
  /* Repeat process to move to sibling of parent node if present */
1869
0
        xmlTextReaderNextTree(reader);
1870
0
    }
1871
1872
0
    reader->state = XML_TEXTREADER_END;
1873
1874
0
    return(1);
1875
0
}
1876
1877
/**
1878
 * xmlTextReaderReadTree:
1879
 * @reader:  the xmlTextReaderPtr used
1880
 *
1881
 *  Moves the position of the current instance to the next node in
1882
 *  the stream, exposing its properties.
1883
 *
1884
 *  Returns 1 if the node was read successfully, 0 if there is no more
1885
 *          nodes to read, or -1 in case of error
1886
 */
1887
static int
1888
0
xmlTextReaderReadTree(xmlTextReaderPtr reader) {
1889
0
    if (reader->state == XML_TEXTREADER_END)
1890
0
        return(0);
1891
1892
0
next_node:
1893
0
    if (reader->node == NULL) {
1894
0
        if (reader->doc->children == NULL) {
1895
0
            reader->state = XML_TEXTREADER_END;
1896
0
            return(0);
1897
0
        }
1898
1899
0
        reader->node = reader->doc->children;
1900
0
        reader->state = XML_TEXTREADER_START;
1901
0
        goto found_node;
1902
0
    }
1903
1904
0
    if ((reader->state != XML_TEXTREADER_BACKTRACK) &&
1905
0
        (reader->node->type != XML_DTD_NODE) &&
1906
0
        (reader->node->type != XML_XINCLUDE_START) &&
1907
0
  (reader->node->type != XML_ENTITY_REF_NODE)) {
1908
0
        if (reader->node->children != NULL) {
1909
0
            reader->node = reader->node->children;
1910
0
            reader->depth++;
1911
0
            reader->state = XML_TEXTREADER_START;
1912
0
            goto found_node;
1913
0
        }
1914
1915
0
        if (reader->node->type == XML_ATTRIBUTE_NODE) {
1916
0
            reader->state = XML_TEXTREADER_BACKTRACK;
1917
0
            goto found_node;
1918
0
        }
1919
0
    }
1920
1921
0
    if (reader->node->next != NULL) {
1922
0
        reader->node = reader->node->next;
1923
0
        reader->state = XML_TEXTREADER_START;
1924
0
        goto found_node;
1925
0
    }
1926
1927
0
    if (reader->node->parent != NULL) {
1928
0
        if ((reader->node->parent->type == XML_DOCUMENT_NODE) ||
1929
0
      (reader->node->parent->type == XML_HTML_DOCUMENT_NODE)) {
1930
0
            reader->state = XML_TEXTREADER_END;
1931
0
            return(0);
1932
0
        }
1933
1934
0
        reader->node = reader->node->parent;
1935
0
        reader->depth--;
1936
0
        reader->state = XML_TEXTREADER_BACKTRACK;
1937
0
        goto found_node;
1938
0
    }
1939
1940
0
    reader->state = XML_TEXTREADER_END;
1941
1942
0
found_node:
1943
0
    if ((reader->node->type == XML_XINCLUDE_START) ||
1944
0
        (reader->node->type == XML_XINCLUDE_END))
1945
0
  goto next_node;
1946
1947
0
    return(1);
1948
0
}
1949
1950
/**
1951
 * xmlTextReaderNextSibling:
1952
 * @reader:  the xmlTextReaderPtr used
1953
 *
1954
 * Skip to the node following the current one in document order while
1955
 * avoiding the subtree if any.
1956
 * Currently implemented only for Readers built on a document
1957
 *
1958
 * Returns 1 if the node was read successfully, 0 if there is no more
1959
 *          nodes to read, or -1 in case of error
1960
 */
1961
int
1962
0
xmlTextReaderNextSibling(xmlTextReaderPtr reader) {
1963
0
    if (reader == NULL)
1964
0
        return(-1);
1965
0
    if (reader->doc == NULL) {
1966
        /* TODO */
1967
0
  return(-1);
1968
0
    }
1969
1970
0
    if (reader->state == XML_TEXTREADER_END)
1971
0
        return(0);
1972
1973
0
    if (reader->node == NULL)
1974
0
        return(xmlTextReaderNextTree(reader));
1975
1976
0
    if (reader->node->next != NULL) {
1977
0
        reader->node = reader->node->next;
1978
0
        reader->state = XML_TEXTREADER_START;
1979
0
        return(1);
1980
0
    }
1981
1982
0
    return(0);
1983
0
}
1984
1985
/************************************************************************
1986
 *                  *
1987
 *      Constructor and destructors     *
1988
 *                  *
1989
 ************************************************************************/
1990
/**
1991
 * xmlNewTextReader:
1992
 * @input: the xmlParserInputBufferPtr used to read data
1993
 * @URI: the URI information for the source if available
1994
 *
1995
 * Create an xmlTextReader structure fed with @input
1996
 *
1997
 * Returns the new xmlTextReaderPtr or NULL in case of error
1998
 */
1999
xmlTextReaderPtr
2000
0
xmlNewTextReader(xmlParserInputBufferPtr input, const char *URI) {
2001
0
    xmlTextReaderPtr ret;
2002
2003
0
    if (input == NULL)
2004
0
  return(NULL);
2005
0
    ret = xmlMalloc(sizeof(xmlTextReader));
2006
0
    if (ret == NULL)
2007
0
  return(NULL);
2008
0
    memset(ret, 0, sizeof(xmlTextReader));
2009
0
    ret->doc = NULL;
2010
0
    ret->entTab = NULL;
2011
0
    ret->entMax = 0;
2012
0
    ret->entNr = 0;
2013
0
    ret->input = input;
2014
0
    ret->buffer = xmlBufCreate(50);
2015
0
    if (ret->buffer == NULL) {
2016
0
        xmlFree(ret);
2017
0
  return(NULL);
2018
0
    }
2019
0
    ret->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
2020
0
    if (ret->sax == NULL) {
2021
0
  xmlBufFree(ret->buffer);
2022
0
  xmlFree(ret);
2023
0
  return(NULL);
2024
0
    }
2025
0
    xmlSAXVersion(ret->sax, 2);
2026
0
    ret->startElement = ret->sax->startElement;
2027
0
    ret->sax->startElement = xmlTextReaderStartElement;
2028
0
    ret->endElement = ret->sax->endElement;
2029
0
    ret->sax->endElement = xmlTextReaderEndElement;
2030
0
#ifdef LIBXML_SAX1_ENABLED
2031
0
    if (ret->sax->initialized == XML_SAX2_MAGIC) {
2032
0
#endif /* LIBXML_SAX1_ENABLED */
2033
0
  ret->startElementNs = ret->sax->startElementNs;
2034
0
  ret->sax->startElementNs = xmlTextReaderStartElementNs;
2035
0
  ret->endElementNs = ret->sax->endElementNs;
2036
0
  ret->sax->endElementNs = xmlTextReaderEndElementNs;
2037
0
#ifdef LIBXML_SAX1_ENABLED
2038
0
    } else {
2039
0
  ret->startElementNs = NULL;
2040
0
  ret->endElementNs = NULL;
2041
0
    }
2042
0
#endif /* LIBXML_SAX1_ENABLED */
2043
0
    ret->characters = ret->sax->characters;
2044
0
    ret->sax->characters = xmlTextReaderCharacters;
2045
0
    ret->sax->ignorableWhitespace = xmlTextReaderCharacters;
2046
0
    ret->cdataBlock = ret->sax->cdataBlock;
2047
0
    ret->sax->cdataBlock = xmlTextReaderCDataBlock;
2048
2049
0
    ret->mode = XML_TEXTREADER_MODE_INITIAL;
2050
0
    ret->node = NULL;
2051
0
    ret->curnode = NULL;
2052
0
    if (xmlBufUse(ret->input->buffer) < 4) {
2053
0
  xmlParserInputBufferRead(input, 4);
2054
0
    }
2055
0
    if (xmlBufUse(ret->input->buffer) >= 4) {
2056
0
  ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL,
2057
0
           (const char *) xmlBufContent(ret->input->buffer),
2058
0
                                            4, URI);
2059
0
  ret->base = 0;
2060
0
  ret->cur = 4;
2061
0
    } else {
2062
0
  ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, NULL, 0, URI);
2063
0
  ret->base = 0;
2064
0
  ret->cur = 0;
2065
0
    }
2066
2067
0
    if (ret->ctxt == NULL) {
2068
0
  xmlBufFree(ret->buffer);
2069
0
  xmlFree(ret->sax);
2070
0
  xmlFree(ret);
2071
0
  return(NULL);
2072
0
    }
2073
0
    ret->ctxt->parseMode = XML_PARSE_READER;
2074
0
    ret->ctxt->_private = ret;
2075
0
    ret->ctxt->linenumbers = 1;
2076
0
    ret->ctxt->dictNames = 1;
2077
0
    ret->allocs = XML_TEXTREADER_CTXT;
2078
    /*
2079
     * use the parser dictionary to allocate all elements and attributes names
2080
     */
2081
0
    ret->dict = ret->ctxt->dict;
2082
0
#ifdef LIBXML_XINCLUDE_ENABLED
2083
0
    ret->xinclude = 0;
2084
0
#endif
2085
0
#ifdef LIBXML_PATTERN_ENABLED
2086
0
    ret->patternMax = 0;
2087
0
    ret->patternTab = NULL;
2088
0
#endif
2089
0
    return(ret);
2090
0
}
2091
2092
/**
2093
 * xmlNewTextReaderFilename:
2094
 * @URI: the URI of the resource to process
2095
 *
2096
 * Create an xmlTextReader structure fed with the resource at @URI
2097
 *
2098
 * Returns the new xmlTextReaderPtr or NULL in case of error
2099
 */
2100
xmlTextReaderPtr
2101
0
xmlNewTextReaderFilename(const char *URI) {
2102
0
    xmlParserInputBufferPtr input;
2103
0
    xmlTextReaderPtr ret;
2104
2105
0
    input = xmlParserInputBufferCreateFilename(URI, XML_CHAR_ENCODING_NONE);
2106
0
    if (input == NULL)
2107
0
  return(NULL);
2108
0
    ret = xmlNewTextReader(input, URI);
2109
0
    if (ret == NULL) {
2110
0
  xmlFreeParserInputBuffer(input);
2111
0
  return(NULL);
2112
0
    }
2113
0
    ret->allocs |= XML_TEXTREADER_INPUT;
2114
0
    return(ret);
2115
0
}
2116
2117
/**
2118
 * xmlFreeTextReader:
2119
 * @reader:  the xmlTextReaderPtr
2120
 *
2121
 * Deallocate all the resources associated to the reader
2122
 */
2123
void
2124
0
xmlFreeTextReader(xmlTextReaderPtr reader) {
2125
0
    if (reader == NULL)
2126
0
  return;
2127
0
#ifdef LIBXML_SCHEMAS_ENABLED
2128
0
    if (reader->rngSchemas != NULL) {
2129
0
  xmlRelaxNGFree(reader->rngSchemas);
2130
0
  reader->rngSchemas = NULL;
2131
0
    }
2132
0
    if (reader->rngValidCtxt != NULL) {
2133
0
  if (! reader->rngPreserveCtxt)
2134
0
      xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
2135
0
  reader->rngValidCtxt = NULL;
2136
0
    }
2137
0
    if (reader->xsdPlug != NULL) {
2138
0
  xmlSchemaSAXUnplug(reader->xsdPlug);
2139
0
  reader->xsdPlug = NULL;
2140
0
    }
2141
0
    if (reader->xsdValidCtxt != NULL) {
2142
0
  if (! reader->xsdPreserveCtxt)
2143
0
      xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
2144
0
  reader->xsdValidCtxt = NULL;
2145
0
    }
2146
0
    if (reader->xsdSchemas != NULL) {
2147
0
  xmlSchemaFree(reader->xsdSchemas);
2148
0
  reader->xsdSchemas = NULL;
2149
0
    }
2150
0
#endif
2151
0
#ifdef LIBXML_XINCLUDE_ENABLED
2152
0
    if (reader->xincctxt != NULL)
2153
0
  xmlXIncludeFreeContext(reader->xincctxt);
2154
0
#endif
2155
0
#ifdef LIBXML_PATTERN_ENABLED
2156
0
    if (reader->patternTab != NULL) {
2157
0
        int i;
2158
0
  for (i = 0;i < reader->patternNr;i++) {
2159
0
      if (reader->patternTab[i] != NULL)
2160
0
          xmlFreePattern(reader->patternTab[i]);
2161
0
  }
2162
0
  xmlFree(reader->patternTab);
2163
0
    }
2164
0
#endif
2165
0
    if (reader->mode != XML_TEXTREADER_MODE_CLOSED)
2166
0
        xmlTextReaderClose(reader);
2167
0
    if (reader->ctxt != NULL) {
2168
0
        if (reader->dict == reader->ctxt->dict)
2169
0
      reader->dict = NULL;
2170
0
  if (reader->allocs & XML_TEXTREADER_CTXT)
2171
0
      xmlFreeParserCtxt(reader->ctxt);
2172
0
    }
2173
0
    if (reader->sax != NULL)
2174
0
  xmlFree(reader->sax);
2175
0
    if (reader->buffer != NULL)
2176
0
        xmlBufFree(reader->buffer);
2177
0
    if (reader->entTab != NULL)
2178
0
  xmlFree(reader->entTab);
2179
0
    if (reader->dict != NULL)
2180
0
        xmlDictFree(reader->dict);
2181
0
    xmlFree(reader);
2182
0
}
2183
2184
/************************************************************************
2185
 *                  *
2186
 *      Methods for XmlTextReader     *
2187
 *                  *
2188
 ************************************************************************/
2189
2190
/**
2191
 * xmlTextReaderClose:
2192
 * @reader:  the xmlTextReaderPtr used
2193
 *
2194
 * This method releases any resources allocated by the current instance
2195
 * changes the state to Closed and close any underlying input.
2196
 *
2197
 * Returns 0 or -1 in case of error
2198
 */
2199
int
2200
0
xmlTextReaderClose(xmlTextReaderPtr reader) {
2201
0
    if (reader == NULL)
2202
0
  return(-1);
2203
0
    reader->node = NULL;
2204
0
    reader->curnode = NULL;
2205
0
    reader->mode = XML_TEXTREADER_MODE_CLOSED;
2206
0
    if (reader->faketext != NULL) {
2207
0
        xmlFreeNode(reader->faketext);
2208
0
        reader->faketext = NULL;
2209
0
    }
2210
0
    if (reader->ctxt != NULL) {
2211
0
#ifdef LIBXML_VALID_ENABLED
2212
0
  if ((reader->ctxt->vctxt.vstateTab != NULL) &&
2213
0
      (reader->ctxt->vctxt.vstateMax > 0)){
2214
0
#ifdef LIBXML_REGEXP_ENABLED
2215
0
            while (reader->ctxt->vctxt.vstateNr > 0)
2216
0
                xmlValidatePopElement(&reader->ctxt->vctxt, NULL, NULL, NULL);
2217
0
#endif /* LIBXML_REGEXP_ENABLED */
2218
0
      xmlFree(reader->ctxt->vctxt.vstateTab);
2219
0
      reader->ctxt->vctxt.vstateTab = NULL;
2220
0
      reader->ctxt->vctxt.vstateMax = 0;
2221
0
  }
2222
0
#endif /* LIBXML_VALID_ENABLED */
2223
0
  xmlStopParser(reader->ctxt);
2224
0
  if (reader->ctxt->myDoc != NULL) {
2225
0
      if (reader->preserve == 0)
2226
0
    xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
2227
0
      reader->ctxt->myDoc = NULL;
2228
0
  }
2229
0
    }
2230
0
    if ((reader->input != NULL)  && (reader->allocs & XML_TEXTREADER_INPUT)) {
2231
0
  xmlFreeParserInputBuffer(reader->input);
2232
0
  reader->allocs -= XML_TEXTREADER_INPUT;
2233
0
    }
2234
0
    return(0);
2235
0
}
2236
2237
/**
2238
 * xmlTextReaderGetAttributeNo:
2239
 * @reader:  the xmlTextReaderPtr used
2240
 * @no: the zero-based index of the attribute relative to the containing element
2241
 *
2242
 * Provides the value of the attribute with the specified index relative
2243
 * to the containing element.
2244
 *
2245
 * Returns a string containing the value of the specified attribute, or NULL
2246
 *    in case of error. The string must be deallocated by the caller.
2247
 */
2248
xmlChar *
2249
0
xmlTextReaderGetAttributeNo(xmlTextReaderPtr reader, int no) {
2250
0
    xmlChar *ret;
2251
0
    int i;
2252
0
    xmlAttrPtr cur;
2253
0
    xmlNsPtr ns;
2254
2255
0
    if (reader == NULL)
2256
0
  return(NULL);
2257
0
    if (reader->node == NULL)
2258
0
  return(NULL);
2259
0
    if (reader->curnode != NULL)
2260
0
  return(NULL);
2261
    /* TODO: handle the xmlDecl */
2262
0
    if (reader->node->type != XML_ELEMENT_NODE)
2263
0
  return(NULL);
2264
2265
0
    ns = reader->node->nsDef;
2266
0
    for (i = 0;(i < no) && (ns != NULL);i++) {
2267
0
  ns = ns->next;
2268
0
    }
2269
0
    if (ns != NULL)
2270
0
  return(readerStrdup(reader, ns->href));
2271
2272
0
    cur = reader->node->properties;
2273
0
    if (cur == NULL)
2274
0
  return(NULL);
2275
0
    for (;i < no;i++) {
2276
0
  cur = cur->next;
2277
0
  if (cur == NULL)
2278
0
      return(NULL);
2279
0
    }
2280
    /* TODO walk the DTD if present */
2281
2282
0
    if (cur->children == NULL)
2283
0
        return(NULL);
2284
0
    ret = xmlNodeListGetString(reader->node->doc, cur->children, 1);
2285
0
    if (ret == NULL)
2286
0
        xmlTextReaderErrMemory(reader);
2287
0
    return(ret);
2288
0
}
2289
2290
/**
2291
 * xmlTextReaderGetAttribute:
2292
 * @reader:  the xmlTextReaderPtr used
2293
 * @name: the qualified name of the attribute.
2294
 *
2295
 * Provides the value of the attribute with the specified qualified name.
2296
 *
2297
 * Returns a string containing the value of the specified attribute, or NULL
2298
 *    in case of error. The string must be deallocated by the caller.
2299
 */
2300
xmlChar *
2301
0
xmlTextReaderGetAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
2302
0
    xmlChar *prefix = NULL;
2303
0
    const xmlChar *localname;
2304
0
    xmlNsPtr ns;
2305
0
    xmlChar *ret = NULL;
2306
0
    int result;
2307
2308
0
    if ((reader == NULL) || (name == NULL))
2309
0
  return(NULL);
2310
0
    if (reader->node == NULL)
2311
0
  return(NULL);
2312
0
    if (reader->curnode != NULL)
2313
0
  return(NULL);
2314
2315
    /* TODO: handle the xmlDecl */
2316
0
    if (reader->node->type != XML_ELEMENT_NODE)
2317
0
  return(NULL);
2318
2319
0
    localname = xmlSplitQName4(name, &prefix);
2320
0
    if (localname == NULL) {
2321
0
        xmlTextReaderErrMemory(reader);
2322
0
        return(NULL);
2323
0
    }
2324
0
    if (prefix == NULL) {
2325
        /*
2326
         * Namespace default decl
2327
         */
2328
0
        if (xmlStrEqual(name, BAD_CAST "xmlns")) {
2329
0
            ns = reader->node->nsDef;
2330
0
            while (ns != NULL) {
2331
0
                if (ns->prefix == NULL) {
2332
0
                    return(readerStrdup(reader, ns->href));
2333
0
                }
2334
0
                ns = ns->next;
2335
0
            }
2336
0
            return NULL;
2337
0
        }
2338
2339
0
        result = xmlNodeGetAttrValue(reader->node, name, NULL, &ret);
2340
0
        if (result < 0)
2341
0
            xmlTextReaderErrMemory(reader);
2342
0
        return(ret);
2343
0
    }
2344
2345
    /*
2346
     * Namespace default decl
2347
     */
2348
0
    if (xmlStrEqual(prefix, BAD_CAST "xmlns")) {
2349
0
        ns = reader->node->nsDef;
2350
0
        while (ns != NULL) {
2351
0
            if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) {
2352
0
                ret = readerStrdup(reader, ns->href);
2353
0
                break;
2354
0
            }
2355
0
            ns = ns->next;
2356
0
        }
2357
0
    } else {
2358
0
        result = xmlSearchNsSafe(reader->node, prefix, &ns);
2359
0
        if (result < 0)
2360
0
            xmlTextReaderErrMemory(reader);
2361
0
        if (ns != NULL) {
2362
0
            result = xmlNodeGetAttrValue(reader->node, localname, ns->href,
2363
0
                                         &ret);
2364
0
            if (result < 0)
2365
0
                xmlTextReaderErrMemory(reader);
2366
0
        }
2367
0
    }
2368
2369
0
    if (prefix != NULL)
2370
0
        xmlFree(prefix);
2371
0
    return(ret);
2372
0
}
2373
2374
2375
/**
2376
 * xmlTextReaderGetAttributeNs:
2377
 * @reader:  the xmlTextReaderPtr used
2378
 * @localName: the local name of the attribute.
2379
 * @namespaceURI: the namespace URI of the attribute.
2380
 *
2381
 * Provides the value of the specified attribute
2382
 *
2383
 * Returns a string containing the value of the specified attribute, or NULL
2384
 *    in case of error. The string must be deallocated by the caller.
2385
 */
2386
xmlChar *
2387
xmlTextReaderGetAttributeNs(xmlTextReaderPtr reader, const xmlChar *localName,
2388
0
          const xmlChar *namespaceURI) {
2389
0
    xmlChar *ret = NULL;
2390
0
    xmlChar *prefix = NULL;
2391
0
    xmlNsPtr ns;
2392
0
    int result;
2393
2394
0
    if ((reader == NULL) || (localName == NULL))
2395
0
  return(NULL);
2396
0
    if (reader->node == NULL)
2397
0
  return(NULL);
2398
0
    if (reader->curnode != NULL)
2399
0
  return(NULL);
2400
2401
    /* TODO: handle the xmlDecl */
2402
0
    if (reader->node->type != XML_ELEMENT_NODE)
2403
0
  return(NULL);
2404
2405
0
    if (xmlStrEqual(namespaceURI, BAD_CAST "http://www.w3.org/2000/xmlns/")) {
2406
0
        if (! xmlStrEqual(localName, BAD_CAST "xmlns")) {
2407
0
            prefix = BAD_CAST localName;
2408
0
        }
2409
0
        ns = reader->node->nsDef;
2410
0
        while (ns != NULL) {
2411
0
            if ((prefix == NULL && ns->prefix == NULL) ||
2412
0
                ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localName)))) {
2413
0
                return readerStrdup(reader, ns->href);
2414
0
            }
2415
0
            ns = ns->next;
2416
0
        }
2417
0
        return NULL;
2418
0
    }
2419
2420
0
    result = xmlNodeGetAttrValue(reader->node, localName, namespaceURI, &ret);
2421
0
    if (result < 0)
2422
0
        xmlTextReaderErrMemory(reader);
2423
2424
0
    return(ret);
2425
0
}
2426
2427
/**
2428
 * xmlTextReaderGetRemainder:
2429
 * @reader:  the xmlTextReaderPtr used
2430
 *
2431
 * Method to get the remainder of the buffered XML. this method stops the
2432
 * parser, set its state to End Of File and return the input stream with
2433
 * what is left that the parser did not use.
2434
 *
2435
 * The implementation is not good, the parser certainly progressed past
2436
 * what's left in reader->input, and there is an allocation problem. Best
2437
 * would be to rewrite it differently.
2438
 *
2439
 * Returns the xmlParserInputBufferPtr attached to the XML or NULL
2440
 *    in case of error.
2441
 */
2442
xmlParserInputBufferPtr
2443
0
xmlTextReaderGetRemainder(xmlTextReaderPtr reader) {
2444
0
    xmlParserInputBufferPtr ret = NULL;
2445
2446
0
    if (reader == NULL)
2447
0
  return(NULL);
2448
0
    if (reader->node == NULL)
2449
0
  return(NULL);
2450
2451
0
    reader->node = NULL;
2452
0
    reader->curnode = NULL;
2453
0
    reader->mode = XML_TEXTREADER_MODE_EOF;
2454
0
    if (reader->ctxt != NULL) {
2455
0
  xmlStopParser(reader->ctxt);
2456
0
  if (reader->ctxt->myDoc != NULL) {
2457
0
      if (reader->preserve == 0)
2458
0
    xmlTextReaderFreeDoc(reader, reader->ctxt->myDoc);
2459
0
      reader->ctxt->myDoc = NULL;
2460
0
  }
2461
0
    }
2462
0
    if (reader->allocs & XML_TEXTREADER_INPUT) {
2463
0
  ret = reader->input;
2464
0
  reader->input = NULL;
2465
0
  reader->allocs -= XML_TEXTREADER_INPUT;
2466
0
    } else {
2467
  /*
2468
   * Hum, one may need to duplicate the data structure because
2469
   * without reference counting the input may be freed twice:
2470
   *   - by the layer which allocated it.
2471
   *   - by the layer to which would have been returned to.
2472
   */
2473
0
  return(NULL);
2474
0
    }
2475
0
    return(ret);
2476
0
}
2477
2478
/**
2479
 * xmlTextReaderLookupNamespace:
2480
 * @reader:  the xmlTextReaderPtr used
2481
 * @prefix: the prefix whose namespace URI is to be resolved. To return
2482
 *          the default namespace, specify NULL
2483
 *
2484
 * Resolves a namespace prefix in the scope of the current element.
2485
 *
2486
 * Returns a string containing the namespace URI to which the prefix maps
2487
 *    or NULL in case of error. The string must be deallocated by the caller.
2488
 */
2489
xmlChar *
2490
0
xmlTextReaderLookupNamespace(xmlTextReaderPtr reader, const xmlChar *prefix) {
2491
0
    xmlNsPtr ns;
2492
0
    int result;
2493
2494
0
    if (reader == NULL)
2495
0
  return(NULL);
2496
0
    if (reader->node == NULL)
2497
0
  return(NULL);
2498
2499
0
    result = xmlSearchNsSafe(reader->node, prefix, &ns);
2500
0
    if (result < 0) {
2501
0
        xmlTextReaderErrMemory(reader);
2502
0
        return(NULL);
2503
0
    }
2504
0
    if (ns == NULL)
2505
0
  return(NULL);
2506
0
    return(readerStrdup(reader, ns->href));
2507
0
}
2508
2509
/**
2510
 * xmlTextReaderMoveToAttributeNo:
2511
 * @reader:  the xmlTextReaderPtr used
2512
 * @no: the zero-based index of the attribute relative to the containing
2513
 *      element.
2514
 *
2515
 * Moves the position of the current instance to the attribute with
2516
 * the specified index relative to the containing element.
2517
 *
2518
 * Returns 1 in case of success, -1 in case of error, 0 if not found
2519
 */
2520
int
2521
0
xmlTextReaderMoveToAttributeNo(xmlTextReaderPtr reader, int no) {
2522
0
    int i;
2523
0
    xmlAttrPtr cur;
2524
0
    xmlNsPtr ns;
2525
2526
0
    if (reader == NULL)
2527
0
  return(-1);
2528
0
    if (reader->node == NULL)
2529
0
  return(-1);
2530
    /* TODO: handle the xmlDecl */
2531
0
    if (reader->node->type != XML_ELEMENT_NODE)
2532
0
  return(-1);
2533
2534
0
    reader->curnode = NULL;
2535
2536
0
    ns = reader->node->nsDef;
2537
0
    for (i = 0;(i < no) && (ns != NULL);i++) {
2538
0
  ns = ns->next;
2539
0
    }
2540
0
    if (ns != NULL) {
2541
0
  reader->curnode = (xmlNodePtr) ns;
2542
0
  return(1);
2543
0
    }
2544
2545
0
    cur = reader->node->properties;
2546
0
    if (cur == NULL)
2547
0
  return(0);
2548
0
    for (;i < no;i++) {
2549
0
  cur = cur->next;
2550
0
  if (cur == NULL)
2551
0
      return(0);
2552
0
    }
2553
    /* TODO walk the DTD if present */
2554
2555
0
    reader->curnode = (xmlNodePtr) cur;
2556
0
    return(1);
2557
0
}
2558
2559
/**
2560
 * xmlTextReaderMoveToAttribute:
2561
 * @reader:  the xmlTextReaderPtr used
2562
 * @name: the qualified name of the attribute.
2563
 *
2564
 * Moves the position of the current instance to the attribute with
2565
 * the specified qualified name.
2566
 *
2567
 * Returns 1 in case of success, -1 in case of error, 0 if not found
2568
 */
2569
int
2570
0
xmlTextReaderMoveToAttribute(xmlTextReaderPtr reader, const xmlChar *name) {
2571
0
    xmlChar *prefix = NULL;
2572
0
    const xmlChar *localname;
2573
0
    xmlNsPtr ns;
2574
0
    xmlAttrPtr prop;
2575
2576
0
    if ((reader == NULL) || (name == NULL))
2577
0
  return(-1);
2578
0
    if (reader->node == NULL)
2579
0
  return(-1);
2580
2581
    /* TODO: handle the xmlDecl */
2582
0
    if (reader->node->type != XML_ELEMENT_NODE)
2583
0
  return(0);
2584
2585
0
    localname = xmlSplitQName4(name, &prefix);
2586
0
    if (localname == NULL) {
2587
0
        xmlTextReaderErrMemory(reader);
2588
0
        return(-1);
2589
0
    }
2590
0
    if (prefix == NULL) {
2591
  /*
2592
   * Namespace default decl
2593
   */
2594
0
  if (xmlStrEqual(name, BAD_CAST "xmlns")) {
2595
0
      ns = reader->node->nsDef;
2596
0
      while (ns != NULL) {
2597
0
    if (ns->prefix == NULL) {
2598
0
        reader->curnode = (xmlNodePtr) ns;
2599
0
        return(1);
2600
0
    }
2601
0
    ns = ns->next;
2602
0
      }
2603
0
      return(0);
2604
0
  }
2605
2606
0
  prop = reader->node->properties;
2607
0
  while (prop != NULL) {
2608
      /*
2609
       * One need to have
2610
       *   - same attribute names
2611
       *   - and the attribute carrying that namespace
2612
       */
2613
0
      if ((xmlStrEqual(prop->name, name)) &&
2614
0
    ((prop->ns == NULL) || (prop->ns->prefix == NULL))) {
2615
0
    reader->curnode = (xmlNodePtr) prop;
2616
0
    return(1);
2617
0
      }
2618
0
      prop = prop->next;
2619
0
  }
2620
0
  return(0);
2621
0
    }
2622
2623
    /*
2624
     * Namespace default decl
2625
     */
2626
0
    if (xmlStrEqual(prefix, BAD_CAST "xmlns")) {
2627
0
  ns = reader->node->nsDef;
2628
0
  while (ns != NULL) {
2629
0
      if ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localname))) {
2630
0
    reader->curnode = (xmlNodePtr) ns;
2631
0
    goto found;
2632
0
      }
2633
0
      ns = ns->next;
2634
0
  }
2635
0
  goto not_found;
2636
0
    }
2637
0
    prop = reader->node->properties;
2638
0
    while (prop != NULL) {
2639
  /*
2640
   * One need to have
2641
   *   - same attribute names
2642
   *   - and the attribute carrying that namespace
2643
   */
2644
0
  if ((xmlStrEqual(prop->name, localname)) &&
2645
0
      (prop->ns != NULL) && (xmlStrEqual(prop->ns->prefix, prefix))) {
2646
0
      reader->curnode = (xmlNodePtr) prop;
2647
0
      goto found;
2648
0
  }
2649
0
  prop = prop->next;
2650
0
    }
2651
0
not_found:
2652
0
    if (prefix != NULL)
2653
0
        xmlFree(prefix);
2654
0
    return(0);
2655
2656
0
found:
2657
0
    if (prefix != NULL)
2658
0
        xmlFree(prefix);
2659
0
    return(1);
2660
0
}
2661
2662
/**
2663
 * xmlTextReaderMoveToAttributeNs:
2664
 * @reader:  the xmlTextReaderPtr used
2665
 * @localName:  the local name of the attribute.
2666
 * @namespaceURI:  the namespace URI of the attribute.
2667
 *
2668
 * Moves the position of the current instance to the attribute with the
2669
 * specified local name and namespace URI.
2670
 *
2671
 * Returns 1 in case of success, -1 in case of error, 0 if not found
2672
 */
2673
int
2674
xmlTextReaderMoveToAttributeNs(xmlTextReaderPtr reader,
2675
0
  const xmlChar *localName, const xmlChar *namespaceURI) {
2676
0
    xmlAttrPtr prop;
2677
0
    xmlNodePtr node;
2678
0
    xmlNsPtr ns;
2679
0
    xmlChar *prefix = NULL;
2680
2681
0
    if ((reader == NULL) || (localName == NULL) || (namespaceURI == NULL))
2682
0
  return(-1);
2683
0
    if (reader->node == NULL)
2684
0
  return(-1);
2685
0
    if (reader->node->type != XML_ELEMENT_NODE)
2686
0
  return(0);
2687
0
    node = reader->node;
2688
2689
0
    if (xmlStrEqual(namespaceURI, BAD_CAST "http://www.w3.org/2000/xmlns/")) {
2690
0
    if (! xmlStrEqual(localName, BAD_CAST "xmlns")) {
2691
0
      prefix = BAD_CAST localName;
2692
0
    }
2693
0
    ns = reader->node->nsDef;
2694
0
    while (ns != NULL) {
2695
0
      if ((prefix == NULL && ns->prefix == NULL) ||
2696
0
        ((ns->prefix != NULL) && (xmlStrEqual(ns->prefix, localName)))) {
2697
0
        reader->curnode = (xmlNodePtr) ns;
2698
0
        return(1);
2699
0
      }
2700
0
      ns = ns->next;
2701
0
    }
2702
0
    return(0);
2703
0
    }
2704
2705
0
    prop = node->properties;
2706
0
    while (prop != NULL) {
2707
  /*
2708
   * One need to have
2709
   *   - same attribute names
2710
   *   - and the attribute carrying that namespace
2711
   */
2712
0
        if (xmlStrEqual(prop->name, localName) &&
2713
0
      ((prop->ns != NULL) &&
2714
0
       (xmlStrEqual(prop->ns->href, namespaceURI)))) {
2715
0
      reader->curnode = (xmlNodePtr) prop;
2716
0
      return(1);
2717
0
        }
2718
0
  prop = prop->next;
2719
0
    }
2720
0
    return(0);
2721
0
}
2722
2723
/**
2724
 * xmlTextReaderMoveToFirstAttribute:
2725
 * @reader:  the xmlTextReaderPtr used
2726
 *
2727
 * Moves the position of the current instance to the first attribute
2728
 * associated with the current node.
2729
 *
2730
 * Returns 1 in case of success, -1 in case of error, 0 if not found
2731
 */
2732
int
2733
0
xmlTextReaderMoveToFirstAttribute(xmlTextReaderPtr reader) {
2734
0
    if (reader == NULL)
2735
0
  return(-1);
2736
0
    if (reader->node == NULL)
2737
0
  return(-1);
2738
0
    if (reader->node->type != XML_ELEMENT_NODE)
2739
0
  return(0);
2740
2741
0
    if (reader->node->nsDef != NULL) {
2742
0
  reader->curnode = (xmlNodePtr) reader->node->nsDef;
2743
0
  return(1);
2744
0
    }
2745
0
    if (reader->node->properties != NULL) {
2746
0
  reader->curnode = (xmlNodePtr) reader->node->properties;
2747
0
  return(1);
2748
0
    }
2749
0
    return(0);
2750
0
}
2751
2752
/**
2753
 * xmlTextReaderMoveToNextAttribute:
2754
 * @reader:  the xmlTextReaderPtr used
2755
 *
2756
 * Moves the position of the current instance to the next attribute
2757
 * associated with the current node.
2758
 *
2759
 * Returns 1 in case of success, -1 in case of error, 0 if not found
2760
 */
2761
int
2762
0
xmlTextReaderMoveToNextAttribute(xmlTextReaderPtr reader) {
2763
0
    if (reader == NULL)
2764
0
  return(-1);
2765
0
    if (reader->node == NULL)
2766
0
  return(-1);
2767
0
    if (reader->node->type != XML_ELEMENT_NODE)
2768
0
  return(0);
2769
0
    if (reader->curnode == NULL)
2770
0
  return(xmlTextReaderMoveToFirstAttribute(reader));
2771
2772
0
    if (reader->curnode->type == XML_NAMESPACE_DECL) {
2773
0
  xmlNsPtr ns = (xmlNsPtr) reader->curnode;
2774
0
  if (ns->next != NULL) {
2775
0
      reader->curnode = (xmlNodePtr) ns->next;
2776
0
      return(1);
2777
0
  }
2778
0
  if (reader->node->properties != NULL) {
2779
0
      reader->curnode = (xmlNodePtr) reader->node->properties;
2780
0
      return(1);
2781
0
  }
2782
0
  return(0);
2783
0
    } else if ((reader->curnode->type == XML_ATTRIBUTE_NODE) &&
2784
0
         (reader->curnode->next != NULL)) {
2785
0
  reader->curnode = reader->curnode->next;
2786
0
  return(1);
2787
0
    }
2788
0
    return(0);
2789
0
}
2790
2791
/**
2792
 * xmlTextReaderMoveToElement:
2793
 * @reader:  the xmlTextReaderPtr used
2794
 *
2795
 * Moves the position of the current instance to the node that
2796
 * contains the current Attribute  node.
2797
 *
2798
 * Returns 1 in case of success, -1 in case of error, 0 if not moved
2799
 */
2800
int
2801
0
xmlTextReaderMoveToElement(xmlTextReaderPtr reader) {
2802
0
    if (reader == NULL)
2803
0
  return(-1);
2804
0
    if (reader->node == NULL)
2805
0
  return(-1);
2806
0
    if (reader->node->type != XML_ELEMENT_NODE)
2807
0
  return(0);
2808
0
    if (reader->curnode != NULL) {
2809
0
  reader->curnode = NULL;
2810
0
  return(1);
2811
0
    }
2812
0
    return(0);
2813
0
}
2814
2815
/**
2816
 * xmlTextReaderReadAttributeValue:
2817
 * @reader:  the xmlTextReaderPtr used
2818
 *
2819
 * Parses an attribute value into one or more Text and EntityReference nodes.
2820
 *
2821
 * Returns 1 in case of success, 0 if the reader was not positioned on an
2822
 *         attribute node or all the attribute values have been read, or -1
2823
 *         in case of error.
2824
 */
2825
int
2826
0
xmlTextReaderReadAttributeValue(xmlTextReaderPtr reader) {
2827
0
    if (reader == NULL)
2828
0
  return(-1);
2829
0
    if (reader->node == NULL)
2830
0
  return(-1);
2831
0
    if (reader->curnode == NULL)
2832
0
  return(0);
2833
0
    if (reader->curnode->type == XML_ATTRIBUTE_NODE) {
2834
0
  if (reader->curnode->children == NULL)
2835
0
      return(0);
2836
0
  reader->curnode = reader->curnode->children;
2837
0
    } else if (reader->curnode->type == XML_NAMESPACE_DECL) {
2838
0
  xmlNsPtr ns = (xmlNsPtr) reader->curnode;
2839
2840
0
  if (reader->faketext == NULL) {
2841
0
      reader->faketext = xmlNewDocText(reader->node->doc,
2842
0
                                 ns->href);
2843
0
            if (reader->faketext == NULL) {
2844
0
                xmlTextReaderErrMemory(reader);
2845
0
                return(-1);
2846
0
            }
2847
0
  } else {
2848
0
            if ((reader->faketext->content != NULL) &&
2849
0
          (reader->faketext->content !=
2850
0
     (xmlChar *) &(reader->faketext->properties)))
2851
0
    xmlFree(reader->faketext->content);
2852
0
            if (ns->href == NULL) {
2853
0
                reader->faketext->content = NULL;
2854
0
            } else {
2855
0
                reader->faketext->content = xmlStrdup(ns->href);
2856
0
                if (reader->faketext->content == NULL) {
2857
0
                    xmlTextReaderErrMemory(reader);
2858
0
                    return(-1);
2859
0
                }
2860
0
            }
2861
0
  }
2862
0
  reader->curnode = reader->faketext;
2863
0
    } else {
2864
0
  if (reader->curnode->next == NULL)
2865
0
      return(0);
2866
0
  reader->curnode = reader->curnode->next;
2867
0
    }
2868
0
    return(1);
2869
0
}
2870
2871
/**
2872
 * xmlTextReaderConstEncoding:
2873
 * @reader:  the xmlTextReaderPtr used
2874
 *
2875
 * Determine the encoding of the document being read.
2876
 *
2877
 * Returns a string containing the encoding of the document or NULL in
2878
 * case of error.  The string is deallocated with the reader.
2879
 */
2880
const xmlChar *
2881
0
xmlTextReaderConstEncoding(xmlTextReaderPtr reader) {
2882
0
    const xmlChar *encoding = NULL;
2883
2884
0
    if (reader == NULL)
2885
0
        return(NULL);
2886
2887
0
    if (reader->ctxt != NULL)
2888
0
        encoding = xmlGetActualEncoding(reader->ctxt);
2889
0
    else if (reader->doc != NULL)
2890
0
        encoding = reader->doc->encoding;
2891
2892
0
    return(constString(reader, encoding));
2893
0
}
2894
2895
2896
/************************************************************************
2897
 *                  *
2898
 *      Access API to the current node      *
2899
 *                  *
2900
 ************************************************************************/
2901
/**
2902
 * xmlTextReaderAttributeCount:
2903
 * @reader:  the xmlTextReaderPtr used
2904
 *
2905
 * Provides the number of attributes of the current node
2906
 *
2907
 * Returns 0 i no attributes, -1 in case of error or the attribute count
2908
 */
2909
int
2910
0
xmlTextReaderAttributeCount(xmlTextReaderPtr reader) {
2911
0
    int ret;
2912
0
    xmlAttrPtr attr;
2913
0
    xmlNsPtr ns;
2914
0
    xmlNodePtr node;
2915
2916
0
    if (reader == NULL)
2917
0
  return(-1);
2918
0
    if (reader->node == NULL)
2919
0
  return(0);
2920
2921
0
    if (reader->curnode != NULL)
2922
0
  node = reader->curnode;
2923
0
    else
2924
0
  node = reader->node;
2925
2926
0
    if (node->type != XML_ELEMENT_NODE)
2927
0
  return(0);
2928
0
    if ((reader->state == XML_TEXTREADER_END) ||
2929
0
  (reader->state == XML_TEXTREADER_BACKTRACK))
2930
0
  return(0);
2931
0
    ret = 0;
2932
0
    attr = node->properties;
2933
0
    while (attr != NULL) {
2934
0
  ret++;
2935
0
  attr = attr->next;
2936
0
    }
2937
0
    ns = node->nsDef;
2938
0
    while (ns != NULL) {
2939
0
  ret++;
2940
0
  ns = ns->next;
2941
0
    }
2942
0
    return(ret);
2943
0
}
2944
2945
/**
2946
 * xmlTextReaderNodeType:
2947
 * @reader:  the xmlTextReaderPtr used
2948
 *
2949
 * Get the node type of the current node
2950
 * Reference:
2951
 * http://www.gnu.org/software/dotgnu/pnetlib-doc/System/Xml/XmlNodeType.html
2952
 *
2953
 * Returns the xmlReaderTypes of the current node or -1 in case of error
2954
 */
2955
int
2956
0
xmlTextReaderNodeType(xmlTextReaderPtr reader) {
2957
0
    xmlNodePtr node;
2958
2959
0
    if (reader == NULL)
2960
0
  return(-1);
2961
0
    if (reader->node == NULL)
2962
0
  return(XML_READER_TYPE_NONE);
2963
0
    if (reader->curnode != NULL)
2964
0
  node = reader->curnode;
2965
0
    else
2966
0
  node = reader->node;
2967
0
    switch (node->type) {
2968
0
        case XML_ELEMENT_NODE:
2969
0
      if ((reader->state == XML_TEXTREADER_END) ||
2970
0
    (reader->state == XML_TEXTREADER_BACKTRACK))
2971
0
    return(XML_READER_TYPE_END_ELEMENT);
2972
0
      return(XML_READER_TYPE_ELEMENT);
2973
0
        case XML_NAMESPACE_DECL:
2974
0
        case XML_ATTRIBUTE_NODE:
2975
0
      return(XML_READER_TYPE_ATTRIBUTE);
2976
0
        case XML_TEXT_NODE:
2977
0
      if (xmlIsBlankNode(reader->node)) {
2978
0
    if (xmlNodeGetSpacePreserve(reader->node))
2979
0
        return(XML_READER_TYPE_SIGNIFICANT_WHITESPACE);
2980
0
    else
2981
0
        return(XML_READER_TYPE_WHITESPACE);
2982
0
      } else {
2983
0
    return(XML_READER_TYPE_TEXT);
2984
0
      }
2985
0
        case XML_CDATA_SECTION_NODE:
2986
0
      return(XML_READER_TYPE_CDATA);
2987
0
        case XML_ENTITY_REF_NODE:
2988
0
      return(XML_READER_TYPE_ENTITY_REFERENCE);
2989
0
        case XML_ENTITY_NODE:
2990
0
      return(XML_READER_TYPE_ENTITY);
2991
0
        case XML_PI_NODE:
2992
0
      return(XML_READER_TYPE_PROCESSING_INSTRUCTION);
2993
0
        case XML_COMMENT_NODE:
2994
0
      return(XML_READER_TYPE_COMMENT);
2995
0
        case XML_DOCUMENT_NODE:
2996
0
        case XML_HTML_DOCUMENT_NODE:
2997
0
      return(XML_READER_TYPE_DOCUMENT);
2998
0
        case XML_DOCUMENT_FRAG_NODE:
2999
0
      return(XML_READER_TYPE_DOCUMENT_FRAGMENT);
3000
0
        case XML_NOTATION_NODE:
3001
0
      return(XML_READER_TYPE_NOTATION);
3002
0
        case XML_DOCUMENT_TYPE_NODE:
3003
0
        case XML_DTD_NODE:
3004
0
      return(XML_READER_TYPE_DOCUMENT_TYPE);
3005
3006
0
        case XML_ELEMENT_DECL:
3007
0
        case XML_ATTRIBUTE_DECL:
3008
0
        case XML_ENTITY_DECL:
3009
0
        case XML_XINCLUDE_START:
3010
0
        case XML_XINCLUDE_END:
3011
0
      return(XML_READER_TYPE_NONE);
3012
0
    }
3013
0
    return(-1);
3014
0
}
3015
3016
/**
3017
 * xmlTextReaderIsEmptyElement:
3018
 * @reader:  the xmlTextReaderPtr used
3019
 *
3020
 * Check if the current node is empty
3021
 *
3022
 * Returns 1 if empty, 0 if not and -1 in case of error
3023
 */
3024
int
3025
0
xmlTextReaderIsEmptyElement(xmlTextReaderPtr reader) {
3026
0
    if ((reader == NULL) || (reader->node == NULL))
3027
0
  return(-1);
3028
0
    if (reader->node->type != XML_ELEMENT_NODE)
3029
0
  return(0);
3030
0
    if (reader->curnode != NULL)
3031
0
  return(0);
3032
0
    if (reader->node->children != NULL)
3033
0
  return(0);
3034
0
    if (reader->state == XML_TEXTREADER_END)
3035
0
  return(0);
3036
0
    if (reader->doc != NULL)
3037
0
        return(1);
3038
0
#ifdef LIBXML_XINCLUDE_ENABLED
3039
0
    if (reader->in_xinclude > 0)
3040
0
        return(1);
3041
0
#endif
3042
0
    return((reader->node->extra & NODE_IS_EMPTY) != 0);
3043
0
}
3044
3045
/**
3046
 * xmlTextReaderLocalName:
3047
 * @reader:  the xmlTextReaderPtr used
3048
 *
3049
 * The local name of the node.
3050
 *
3051
 * Returns the local name or NULL if not available,
3052
 *   if non NULL it need to be freed by the caller.
3053
 */
3054
xmlChar *
3055
0
xmlTextReaderLocalName(xmlTextReaderPtr reader) {
3056
0
    xmlNodePtr node;
3057
0
    if ((reader == NULL) || (reader->node == NULL))
3058
0
  return(NULL);
3059
0
    if (reader->curnode != NULL)
3060
0
  node = reader->curnode;
3061
0
    else
3062
0
  node = reader->node;
3063
0
    if (node->type == XML_NAMESPACE_DECL) {
3064
0
  xmlNsPtr ns = (xmlNsPtr) node;
3065
0
  if (ns->prefix == NULL)
3066
0
      return(readerStrdup(reader, BAD_CAST "xmlns"));
3067
0
  else
3068
0
      return(readerStrdup(reader, ns->prefix));
3069
0
    }
3070
0
    if ((node->type != XML_ELEMENT_NODE) &&
3071
0
  (node->type != XML_ATTRIBUTE_NODE))
3072
0
  return(xmlTextReaderName(reader));
3073
0
    return(readerStrdup(reader, node->name));
3074
0
}
3075
3076
/**
3077
 * xmlTextReaderConstLocalName:
3078
 * @reader:  the xmlTextReaderPtr used
3079
 *
3080
 * The local name of the node.
3081
 *
3082
 * Returns the local name or NULL if not available, the
3083
 *         string will be deallocated with the reader.
3084
 */
3085
const xmlChar *
3086
0
xmlTextReaderConstLocalName(xmlTextReaderPtr reader) {
3087
0
    xmlNodePtr node;
3088
0
    if ((reader == NULL) || (reader->node == NULL))
3089
0
  return(NULL);
3090
0
    if (reader->curnode != NULL)
3091
0
  node = reader->curnode;
3092
0
    else
3093
0
  node = reader->node;
3094
0
    if (node->type == XML_NAMESPACE_DECL) {
3095
0
  xmlNsPtr ns = (xmlNsPtr) node;
3096
0
  if (ns->prefix == NULL)
3097
0
      return(constString(reader, BAD_CAST "xmlns"));
3098
0
  else
3099
0
      return(ns->prefix);
3100
0
    }
3101
0
    if ((node->type != XML_ELEMENT_NODE) &&
3102
0
  (node->type != XML_ATTRIBUTE_NODE))
3103
0
  return(xmlTextReaderConstName(reader));
3104
0
    return(node->name);
3105
0
}
3106
3107
/**
3108
 * xmlTextReaderName:
3109
 * @reader:  the xmlTextReaderPtr used
3110
 *
3111
 * The qualified name of the node, equal to Prefix :LocalName.
3112
 *
3113
 * Returns the local name or NULL if not available,
3114
 *   if non NULL it need to be freed by the caller.
3115
 */
3116
xmlChar *
3117
0
xmlTextReaderName(xmlTextReaderPtr reader) {
3118
0
    xmlNodePtr node;
3119
0
    xmlChar *ret;
3120
3121
0
    if ((reader == NULL) || (reader->node == NULL))
3122
0
  return(NULL);
3123
0
    if (reader->curnode != NULL)
3124
0
  node = reader->curnode;
3125
0
    else
3126
0
  node = reader->node;
3127
0
    switch (node->type) {
3128
0
        case XML_ELEMENT_NODE:
3129
0
        case XML_ATTRIBUTE_NODE:
3130
0
      if ((node->ns == NULL) ||
3131
0
    (node->ns->prefix == NULL))
3132
0
    return(readerStrdup(reader, node->name));
3133
3134
0
            ret = xmlBuildQName(node->name, node->ns->prefix, NULL, 0);
3135
0
            if (ret == NULL)
3136
0
                xmlTextReaderErrMemory(reader);
3137
0
      return(ret);
3138
0
        case XML_TEXT_NODE:
3139
0
      return(readerStrdup(reader, BAD_CAST "#text"));
3140
0
        case XML_CDATA_SECTION_NODE:
3141
0
      return(readerStrdup(reader, BAD_CAST "#cdata-section"));
3142
0
        case XML_ENTITY_NODE:
3143
0
        case XML_ENTITY_REF_NODE:
3144
0
      return(readerStrdup(reader, node->name));
3145
0
        case XML_PI_NODE:
3146
0
      return(readerStrdup(reader, node->name));
3147
0
        case XML_COMMENT_NODE:
3148
0
      return(readerStrdup(reader, BAD_CAST "#comment"));
3149
0
        case XML_DOCUMENT_NODE:
3150
0
        case XML_HTML_DOCUMENT_NODE:
3151
0
      return(readerStrdup(reader, BAD_CAST "#document"));
3152
0
        case XML_DOCUMENT_FRAG_NODE:
3153
0
      return(readerStrdup(reader, BAD_CAST "#document-fragment"));
3154
0
        case XML_NOTATION_NODE:
3155
0
      return(readerStrdup(reader, node->name));
3156
0
        case XML_DOCUMENT_TYPE_NODE:
3157
0
        case XML_DTD_NODE:
3158
0
      return(readerStrdup(reader, node->name));
3159
0
        case XML_NAMESPACE_DECL: {
3160
0
      xmlNsPtr ns = (xmlNsPtr) node;
3161
3162
0
      if (ns->prefix == NULL)
3163
0
    return(readerStrdup(reader, BAD_CAST "xmlns"));
3164
0
            ret = xmlBuildQName(ns->prefix, BAD_CAST "xmlns", NULL, 0);
3165
0
            if (ret == NULL)
3166
0
                xmlTextReaderErrMemory(reader);
3167
0
      return(ret);
3168
0
  }
3169
3170
0
        case XML_ELEMENT_DECL:
3171
0
        case XML_ATTRIBUTE_DECL:
3172
0
        case XML_ENTITY_DECL:
3173
0
        case XML_XINCLUDE_START:
3174
0
        case XML_XINCLUDE_END:
3175
0
      return(NULL);
3176
0
    }
3177
0
    return(NULL);
3178
0
}
3179
3180
/**
3181
 * xmlTextReaderConstName:
3182
 * @reader:  the xmlTextReaderPtr used
3183
 *
3184
 * The qualified name of the node, equal to Prefix :LocalName.
3185
 *
3186
 * Returns the local name or NULL if not available, the string is
3187
 *         deallocated with the reader.
3188
 */
3189
const xmlChar *
3190
0
xmlTextReaderConstName(xmlTextReaderPtr reader) {
3191
0
    xmlNodePtr node;
3192
3193
0
    if ((reader == NULL) || (reader->node == NULL))
3194
0
  return(NULL);
3195
0
    if (reader->curnode != NULL)
3196
0
  node = reader->curnode;
3197
0
    else
3198
0
  node = reader->node;
3199
0
    switch (node->type) {
3200
0
        case XML_ELEMENT_NODE:
3201
0
        case XML_ATTRIBUTE_NODE:
3202
0
      if ((node->ns == NULL) ||
3203
0
    (node->ns->prefix == NULL))
3204
0
    return(node->name);
3205
0
      return(constQString(reader, node->ns->prefix, node->name));
3206
0
        case XML_TEXT_NODE:
3207
0
      return(constString(reader, BAD_CAST "#text"));
3208
0
        case XML_CDATA_SECTION_NODE:
3209
0
      return(constString(reader, BAD_CAST "#cdata-section"));
3210
0
        case XML_ENTITY_NODE:
3211
0
        case XML_ENTITY_REF_NODE:
3212
0
      return(constString(reader, node->name));
3213
0
        case XML_PI_NODE:
3214
0
      return(constString(reader, node->name));
3215
0
        case XML_COMMENT_NODE:
3216
0
      return(constString(reader, BAD_CAST "#comment"));
3217
0
        case XML_DOCUMENT_NODE:
3218
0
        case XML_HTML_DOCUMENT_NODE:
3219
0
      return(constString(reader, BAD_CAST "#document"));
3220
0
        case XML_DOCUMENT_FRAG_NODE:
3221
0
      return(constString(reader, BAD_CAST "#document-fragment"));
3222
0
        case XML_NOTATION_NODE:
3223
0
      return(constString(reader, node->name));
3224
0
        case XML_DOCUMENT_TYPE_NODE:
3225
0
        case XML_DTD_NODE:
3226
0
      return(constString(reader, node->name));
3227
0
        case XML_NAMESPACE_DECL: {
3228
0
      xmlNsPtr ns = (xmlNsPtr) node;
3229
3230
0
      if (ns->prefix == NULL)
3231
0
    return(constString(reader, BAD_CAST "xmlns"));
3232
0
      return(constQString(reader, BAD_CAST "xmlns", ns->prefix));
3233
0
  }
3234
3235
0
        case XML_ELEMENT_DECL:
3236
0
        case XML_ATTRIBUTE_DECL:
3237
0
        case XML_ENTITY_DECL:
3238
0
        case XML_XINCLUDE_START:
3239
0
        case XML_XINCLUDE_END:
3240
0
      return(NULL);
3241
0
    }
3242
0
    return(NULL);
3243
0
}
3244
3245
/**
3246
 * xmlTextReaderPrefix:
3247
 * @reader:  the xmlTextReaderPtr used
3248
 *
3249
 * A shorthand reference to the namespace associated with the node.
3250
 *
3251
 * Returns the prefix or NULL if not available,
3252
 *    if non NULL it need to be freed by the caller.
3253
 */
3254
xmlChar *
3255
0
xmlTextReaderPrefix(xmlTextReaderPtr reader) {
3256
0
    xmlNodePtr node;
3257
0
    if ((reader == NULL) || (reader->node == NULL))
3258
0
  return(NULL);
3259
0
    if (reader->curnode != NULL)
3260
0
  node = reader->curnode;
3261
0
    else
3262
0
  node = reader->node;
3263
0
    if (node->type == XML_NAMESPACE_DECL) {
3264
0
  xmlNsPtr ns = (xmlNsPtr) node;
3265
0
  if (ns->prefix == NULL)
3266
0
      return(NULL);
3267
0
  return(readerStrdup(reader, BAD_CAST "xmlns"));
3268
0
    }
3269
0
    if ((node->type != XML_ELEMENT_NODE) &&
3270
0
  (node->type != XML_ATTRIBUTE_NODE))
3271
0
  return(NULL);
3272
0
    if ((node->ns != NULL) && (node->ns->prefix != NULL))
3273
0
  return(readerStrdup(reader, node->ns->prefix));
3274
0
    return(NULL);
3275
0
}
3276
3277
/**
3278
 * xmlTextReaderConstPrefix:
3279
 * @reader:  the xmlTextReaderPtr used
3280
 *
3281
 * A shorthand reference to the namespace associated with the node.
3282
 *
3283
 * Returns the prefix or NULL if not available, the string is deallocated
3284
 *         with the reader.
3285
 */
3286
const xmlChar *
3287
0
xmlTextReaderConstPrefix(xmlTextReaderPtr reader) {
3288
0
    xmlNodePtr node;
3289
0
    if ((reader == NULL) || (reader->node == NULL))
3290
0
  return(NULL);
3291
0
    if (reader->curnode != NULL)
3292
0
  node = reader->curnode;
3293
0
    else
3294
0
  node = reader->node;
3295
0
    if (node->type == XML_NAMESPACE_DECL) {
3296
0
  xmlNsPtr ns = (xmlNsPtr) node;
3297
0
  if (ns->prefix == NULL)
3298
0
      return(NULL);
3299
0
  return(constString(reader, BAD_CAST "xmlns"));
3300
0
    }
3301
0
    if ((node->type != XML_ELEMENT_NODE) &&
3302
0
  (node->type != XML_ATTRIBUTE_NODE))
3303
0
  return(NULL);
3304
0
    if ((node->ns != NULL) && (node->ns->prefix != NULL))
3305
0
  return(constString(reader, node->ns->prefix));
3306
0
    return(NULL);
3307
0
}
3308
3309
/**
3310
 * xmlTextReaderNamespaceUri:
3311
 * @reader:  the xmlTextReaderPtr used
3312
 *
3313
 * The URI defining the namespace associated with the node.
3314
 *
3315
 * Returns the namespace URI or NULL if not available,
3316
 *    if non NULL it need to be freed by the caller.
3317
 */
3318
xmlChar *
3319
0
xmlTextReaderNamespaceUri(xmlTextReaderPtr reader) {
3320
0
    xmlNodePtr node;
3321
0
    if ((reader == NULL) || (reader->node == NULL))
3322
0
  return(NULL);
3323
0
    if (reader->curnode != NULL)
3324
0
  node = reader->curnode;
3325
0
    else
3326
0
  node = reader->node;
3327
0
    if (node->type == XML_NAMESPACE_DECL)
3328
0
  return(readerStrdup(reader, BAD_CAST "http://www.w3.org/2000/xmlns/"));
3329
0
    if ((node->type != XML_ELEMENT_NODE) &&
3330
0
  (node->type != XML_ATTRIBUTE_NODE))
3331
0
  return(NULL);
3332
0
    if (node->ns != NULL)
3333
0
  return(readerStrdup(reader, node->ns->href));
3334
0
    return(NULL);
3335
0
}
3336
3337
/**
3338
 * xmlTextReaderConstNamespaceUri:
3339
 * @reader:  the xmlTextReaderPtr used
3340
 *
3341
 * The URI defining the namespace associated with the node.
3342
 *
3343
 * Returns the namespace URI or NULL if not available, the string
3344
 *         will be deallocated with the reader
3345
 */
3346
const xmlChar *
3347
0
xmlTextReaderConstNamespaceUri(xmlTextReaderPtr reader) {
3348
0
    xmlNodePtr node;
3349
0
    if ((reader == NULL) || (reader->node == NULL))
3350
0
  return(NULL);
3351
0
    if (reader->curnode != NULL)
3352
0
  node = reader->curnode;
3353
0
    else
3354
0
  node = reader->node;
3355
0
    if (node->type == XML_NAMESPACE_DECL)
3356
0
  return(constString(reader, BAD_CAST "http://www.w3.org/2000/xmlns/"));
3357
0
    if ((node->type != XML_ELEMENT_NODE) &&
3358
0
  (node->type != XML_ATTRIBUTE_NODE))
3359
0
  return(NULL);
3360
0
    if (node->ns != NULL)
3361
0
  return(constString(reader, node->ns->href));
3362
0
    return(NULL);
3363
0
}
3364
3365
/**
3366
 * xmlTextReaderBaseUri:
3367
 * @reader:  the xmlTextReaderPtr used
3368
 *
3369
 * The base URI of the node.
3370
 *
3371
 * Returns the base URI or NULL if not available,
3372
 *    if non NULL it need to be freed by the caller.
3373
 */
3374
xmlChar *
3375
0
xmlTextReaderBaseUri(xmlTextReaderPtr reader) {
3376
0
    xmlChar *ret = NULL;
3377
0
    int result;
3378
3379
0
    if ((reader == NULL) || (reader->node == NULL))
3380
0
  return(NULL);
3381
0
    result = xmlNodeGetBaseSafe(NULL, reader->node, &ret);
3382
0
    if (result < 0)
3383
0
        xmlTextReaderErrMemory(reader);
3384
3385
0
    return(ret);
3386
0
}
3387
3388
/**
3389
 * xmlTextReaderConstBaseUri:
3390
 * @reader:  the xmlTextReaderPtr used
3391
 *
3392
 * The base URI of the node.
3393
 *
3394
 * Returns the base URI or NULL if not available, the string
3395
 *         will be deallocated with the reader
3396
 */
3397
const xmlChar *
3398
0
xmlTextReaderConstBaseUri(xmlTextReaderPtr reader) {
3399
0
    xmlChar *tmp;
3400
0
    const xmlChar *ret;
3401
0
    int result;
3402
3403
0
    if ((reader == NULL) || (reader->node == NULL))
3404
0
  return(NULL);
3405
0
    result = xmlNodeGetBaseSafe(NULL, reader->node, &tmp);
3406
0
    if (result < 0)
3407
0
        xmlTextReaderErrMemory(reader);
3408
0
    if (tmp == NULL)
3409
0
        return(NULL);
3410
0
    ret = constString(reader, tmp);
3411
0
    xmlFree(tmp);
3412
0
    return(ret);
3413
0
}
3414
3415
/**
3416
 * xmlTextReaderDepth:
3417
 * @reader:  the xmlTextReaderPtr used
3418
 *
3419
 * The depth of the node in the tree.
3420
 *
3421
 * Returns the depth or -1 in case of error
3422
 */
3423
int
3424
0
xmlTextReaderDepth(xmlTextReaderPtr reader) {
3425
0
    if (reader == NULL)
3426
0
  return(-1);
3427
0
    if (reader->node == NULL)
3428
0
  return(0);
3429
3430
0
    if (reader->curnode != NULL) {
3431
0
  if ((reader->curnode->type == XML_ATTRIBUTE_NODE) ||
3432
0
      (reader->curnode->type == XML_NAMESPACE_DECL))
3433
0
      return(reader->depth + 1);
3434
0
  return(reader->depth + 2);
3435
0
    }
3436
0
    return(reader->depth);
3437
0
}
3438
3439
/**
3440
 * xmlTextReaderHasAttributes:
3441
 * @reader:  the xmlTextReaderPtr used
3442
 *
3443
 * Whether the node has attributes.
3444
 *
3445
 * Returns 1 if true, 0 if false, and -1 in case or error
3446
 */
3447
int
3448
0
xmlTextReaderHasAttributes(xmlTextReaderPtr reader) {
3449
0
    xmlNodePtr node;
3450
0
    if (reader == NULL)
3451
0
  return(-1);
3452
0
    if (reader->node == NULL)
3453
0
  return(0);
3454
0
    if (reader->curnode != NULL)
3455
0
  node = reader->curnode;
3456
0
    else
3457
0
  node = reader->node;
3458
3459
0
    if ((node->type == XML_ELEMENT_NODE) &&
3460
0
  ((node->properties != NULL) || (node->nsDef != NULL)))
3461
0
  return(1);
3462
    /* TODO: handle the xmlDecl */
3463
0
    return(0);
3464
0
}
3465
3466
/**
3467
 * xmlTextReaderHasValue:
3468
 * @reader:  the xmlTextReaderPtr used
3469
 *
3470
 * Whether the node can have a text value.
3471
 *
3472
 * Returns 1 if true, 0 if false, and -1 in case or error
3473
 */
3474
int
3475
0
xmlTextReaderHasValue(xmlTextReaderPtr reader) {
3476
0
    xmlNodePtr node;
3477
0
    if (reader == NULL)
3478
0
  return(-1);
3479
0
    if (reader->node == NULL)
3480
0
  return(0);
3481
0
    if (reader->curnode != NULL)
3482
0
  node = reader->curnode;
3483
0
    else
3484
0
  node = reader->node;
3485
3486
0
    switch (node->type) {
3487
0
        case XML_ATTRIBUTE_NODE:
3488
0
        case XML_TEXT_NODE:
3489
0
        case XML_CDATA_SECTION_NODE:
3490
0
        case XML_PI_NODE:
3491
0
        case XML_COMMENT_NODE:
3492
0
        case XML_NAMESPACE_DECL:
3493
0
      return(1);
3494
0
  default:
3495
0
      break;
3496
0
    }
3497
0
    return(0);
3498
0
}
3499
3500
/**
3501
 * xmlTextReaderValue:
3502
 * @reader:  the xmlTextReaderPtr used
3503
 *
3504
 * Provides the text value of the node if present
3505
 *
3506
 * Returns the string or NULL if not available. The result must be deallocated
3507
 *     with xmlFree()
3508
 */
3509
xmlChar *
3510
0
xmlTextReaderValue(xmlTextReaderPtr reader) {
3511
0
    xmlNodePtr node;
3512
0
    if (reader == NULL)
3513
0
  return(NULL);
3514
0
    if (reader->node == NULL)
3515
0
  return(NULL);
3516
0
    if (reader->curnode != NULL)
3517
0
  node = reader->curnode;
3518
0
    else
3519
0
  node = reader->node;
3520
3521
0
    switch (node->type) {
3522
0
        case XML_NAMESPACE_DECL:
3523
0
      return(readerStrdup(reader, ((xmlNsPtr) node)->href));
3524
0
        case XML_ATTRIBUTE_NODE:{
3525
0
      xmlAttrPtr attr = (xmlAttrPtr) node;
3526
0
            xmlDocPtr doc = NULL;
3527
0
            xmlChar *ret;
3528
3529
0
            if (attr->children == NULL)
3530
0
                return(NULL);
3531
0
      if (attr->parent != NULL)
3532
0
                doc = attr->parent->doc;
3533
0
      ret = xmlNodeListGetString(doc, attr->children, 1);
3534
0
            if (ret == NULL)
3535
0
                xmlTextReaderErrMemory(reader);
3536
0
      return(ret);
3537
0
  }
3538
0
        case XML_TEXT_NODE:
3539
0
        case XML_CDATA_SECTION_NODE:
3540
0
        case XML_PI_NODE:
3541
0
        case XML_COMMENT_NODE:
3542
0
            return(readerStrdup(reader, node->content));
3543
0
  default:
3544
0
      break;
3545
0
    }
3546
0
    return(NULL);
3547
0
}
3548
3549
/**
3550
 * xmlTextReaderConstValue:
3551
 * @reader:  the xmlTextReaderPtr used
3552
 *
3553
 * Provides the text value of the node if present
3554
 *
3555
 * Returns the string or NULL if not available. The result will be
3556
 *     deallocated on the next Read() operation.
3557
 */
3558
const xmlChar *
3559
0
xmlTextReaderConstValue(xmlTextReaderPtr reader) {
3560
0
    xmlNodePtr node;
3561
0
    if (reader == NULL)
3562
0
  return(NULL);
3563
0
    if (reader->node == NULL)
3564
0
  return(NULL);
3565
0
    if (reader->curnode != NULL)
3566
0
  node = reader->curnode;
3567
0
    else
3568
0
  node = reader->node;
3569
3570
0
    switch (node->type) {
3571
0
        case XML_NAMESPACE_DECL:
3572
0
      return(((xmlNsPtr) node)->href);
3573
0
        case XML_ATTRIBUTE_NODE:{
3574
0
      xmlAttrPtr attr = (xmlAttrPtr) node;
3575
0
      const xmlChar *ret;
3576
3577
0
      if ((attr->children != NULL) &&
3578
0
          (attr->children->type == XML_TEXT_NODE) &&
3579
0
    (attr->children->next == NULL))
3580
0
    return(attr->children->content);
3581
0
      else {
3582
0
    if (reader->buffer == NULL) {
3583
0
        reader->buffer = xmlBufCreate(50);
3584
0
                    if (reader->buffer == NULL)
3585
0
                        return (NULL);
3586
0
                } else
3587
0
                    xmlBufEmpty(reader->buffer);
3588
0
          xmlBufGetNodeContent(reader->buffer, node);
3589
0
    ret = xmlBufContent(reader->buffer);
3590
0
    if (ret == NULL) {
3591
0
                    xmlTextReaderErrMemory(reader);
3592
        /* error on the buffer best to reallocate */
3593
0
        xmlBufFree(reader->buffer);
3594
0
        reader->buffer = xmlBufCreate(50);
3595
0
    }
3596
0
    return(ret);
3597
0
      }
3598
0
      break;
3599
0
  }
3600
0
        case XML_TEXT_NODE:
3601
0
        case XML_CDATA_SECTION_NODE:
3602
0
        case XML_PI_NODE:
3603
0
        case XML_COMMENT_NODE:
3604
0
      return(node->content);
3605
0
  default:
3606
0
      break;
3607
0
    }
3608
0
    return(NULL);
3609
0
}
3610
3611
/**
3612
 * xmlTextReaderIsDefault:
3613
 * @reader:  the xmlTextReaderPtr used
3614
 *
3615
 * Whether an Attribute  node was generated from the default value
3616
 * defined in the DTD or schema.
3617
 *
3618
 * Returns 0 if not defaulted, 1 if defaulted, and -1 in case of error
3619
 */
3620
int
3621
0
xmlTextReaderIsDefault(xmlTextReaderPtr reader) {
3622
0
    if (reader == NULL)
3623
0
  return(-1);
3624
0
    return(0);
3625
0
}
3626
3627
/**
3628
 * xmlTextReaderQuoteChar:
3629
 * @reader:  the xmlTextReaderPtr used
3630
 *
3631
 * The quotation mark character used to enclose the value of an attribute.
3632
 *
3633
 * Returns " or ' and -1 in case of error
3634
 */
3635
int
3636
0
xmlTextReaderQuoteChar(xmlTextReaderPtr reader) {
3637
0
    if (reader == NULL)
3638
0
  return(-1);
3639
    /* TODO maybe lookup the attribute value for " first */
3640
0
    return('"');
3641
0
}
3642
3643
/**
3644
 * xmlTextReaderXmlLang:
3645
 * @reader:  the xmlTextReaderPtr used
3646
 *
3647
 * The xml:lang scope within which the node resides.
3648
 *
3649
 * Returns the xml:lang value or NULL if none exists.,
3650
 *    if non NULL it need to be freed by the caller.
3651
 */
3652
xmlChar *
3653
0
xmlTextReaderXmlLang(xmlTextReaderPtr reader) {
3654
0
    if (reader == NULL)
3655
0
  return(NULL);
3656
0
    if (reader->node == NULL)
3657
0
  return(NULL);
3658
0
    return(xmlNodeGetLang(reader->node));
3659
0
}
3660
3661
/**
3662
 * xmlTextReaderConstXmlLang:
3663
 * @reader:  the xmlTextReaderPtr used
3664
 *
3665
 * The xml:lang scope within which the node resides.
3666
 *
3667
 * Returns the xml:lang value or NULL if none exists.
3668
 */
3669
const xmlChar *
3670
0
xmlTextReaderConstXmlLang(xmlTextReaderPtr reader) {
3671
0
    xmlChar *tmp;
3672
0
    const xmlChar *ret;
3673
3674
0
    if (reader == NULL)
3675
0
  return(NULL);
3676
0
    if (reader->node == NULL)
3677
0
  return(NULL);
3678
0
    tmp = xmlNodeGetLang(reader->node);
3679
0
    if (tmp == NULL)
3680
0
        return(NULL);
3681
0
    ret = constString(reader, tmp);
3682
0
    xmlFree(tmp);
3683
0
    return(ret);
3684
0
}
3685
3686
/**
3687
 * xmlTextReaderConstString:
3688
 * @reader:  the xmlTextReaderPtr used
3689
 * @str:  the string to intern.
3690
 *
3691
 * Get an interned string from the reader, allows for example to
3692
 * speedup string name comparisons
3693
 *
3694
 * Returns an interned copy of the string or NULL in case of error. The
3695
 *         string will be deallocated with the reader.
3696
 */
3697
const xmlChar *
3698
0
xmlTextReaderConstString(xmlTextReaderPtr reader, const xmlChar *str) {
3699
0
    if (reader == NULL)
3700
0
  return(NULL);
3701
0
    return(constString(reader, str));
3702
0
}
3703
3704
/**
3705
 * xmlTextReaderNormalization:
3706
 * @reader:  the xmlTextReaderPtr used
3707
 *
3708
 * The value indicating whether to normalize white space and attribute values.
3709
 * Since attribute value and end of line normalizations are a MUST in the XML
3710
 * specification only the value true is accepted. The broken behaviour of
3711
 * accepting out of range character entities like &#0; is of course not
3712
 * supported either.
3713
 *
3714
 * Returns 1 or -1 in case of error.
3715
 */
3716
int
3717
0
xmlTextReaderNormalization(xmlTextReaderPtr reader) {
3718
0
    if (reader == NULL)
3719
0
  return(-1);
3720
0
    return(1);
3721
0
}
3722
3723
/************************************************************************
3724
 *                  *
3725
 *      Extensions to the base APIs     *
3726
 *                  *
3727
 ************************************************************************/
3728
3729
/**
3730
 * xmlTextReaderSetParserProp:
3731
 * @reader:  the xmlTextReaderPtr used
3732
 * @prop:  the xmlParserProperties to set
3733
 * @value:  usually 0 or 1 to (de)activate it
3734
 *
3735
 * Change the parser processing behaviour by changing some of its internal
3736
 * properties. Note that some properties can only be changed before any
3737
 * read has been done.
3738
 *
3739
 * Returns 0 if the call was successful, or -1 in case of error
3740
 */
3741
int
3742
0
xmlTextReaderSetParserProp(xmlTextReaderPtr reader, int prop, int value) {
3743
0
    xmlParserProperties p = (xmlParserProperties) prop;
3744
0
    xmlParserCtxtPtr ctxt;
3745
3746
0
    if ((reader == NULL) || (reader->ctxt == NULL))
3747
0
  return(-1);
3748
0
    ctxt = reader->ctxt;
3749
3750
0
    switch (p) {
3751
0
        case XML_PARSER_LOADDTD:
3752
0
      if (value != 0) {
3753
0
    if (ctxt->loadsubset == 0) {
3754
0
        if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
3755
0
      return(-1);
3756
0
                    ctxt->options |= XML_PARSE_DTDLOAD;
3757
0
        ctxt->loadsubset |= XML_DETECT_IDS;
3758
0
    }
3759
0
      } else {
3760
0
                ctxt->options &= ~XML_PARSE_DTDLOAD;
3761
0
    ctxt->loadsubset &= ~XML_DETECT_IDS;
3762
0
      }
3763
0
      return(0);
3764
0
        case XML_PARSER_DEFAULTATTRS:
3765
0
      if (value != 0) {
3766
0
                ctxt->options |= XML_PARSE_DTDATTR;
3767
0
    ctxt->loadsubset |= XML_COMPLETE_ATTRS;
3768
0
      } else {
3769
0
                ctxt->options &= ~XML_PARSE_DTDATTR;
3770
0
    ctxt->loadsubset &= ~XML_COMPLETE_ATTRS;
3771
0
      }
3772
0
      return(0);
3773
0
        case XML_PARSER_VALIDATE:
3774
0
      if (value != 0) {
3775
0
                ctxt->options |= XML_PARSE_DTDVALID;
3776
0
    ctxt->validate = 1;
3777
0
    reader->validate = XML_TEXTREADER_VALIDATE_DTD;
3778
0
      } else {
3779
0
                ctxt->options &= ~XML_PARSE_DTDVALID;
3780
0
    ctxt->validate = 0;
3781
0
      }
3782
0
      return(0);
3783
0
        case XML_PARSER_SUBST_ENTITIES:
3784
0
      if (value != 0) {
3785
0
                ctxt->options |= XML_PARSE_NOENT;
3786
0
    ctxt->replaceEntities = 1;
3787
0
      } else {
3788
0
                ctxt->options &= ~XML_PARSE_NOENT;
3789
0
    ctxt->replaceEntities = 0;
3790
0
      }
3791
0
      return(0);
3792
0
    }
3793
0
    return(-1);
3794
0
}
3795
3796
/**
3797
 * xmlTextReaderGetParserProp:
3798
 * @reader:  the xmlTextReaderPtr used
3799
 * @prop:  the xmlParserProperties to get
3800
 *
3801
 * Read the parser internal property.
3802
 *
3803
 * Returns the value, usually 0 or 1, or -1 in case of error.
3804
 */
3805
int
3806
0
xmlTextReaderGetParserProp(xmlTextReaderPtr reader, int prop) {
3807
0
    xmlParserProperties p = (xmlParserProperties) prop;
3808
0
    xmlParserCtxtPtr ctxt;
3809
3810
0
    if ((reader == NULL) || (reader->ctxt == NULL))
3811
0
  return(-1);
3812
0
    ctxt = reader->ctxt;
3813
3814
0
    switch (p) {
3815
0
        case XML_PARSER_LOADDTD:
3816
0
      if ((ctxt->loadsubset != 0) || (ctxt->validate != 0))
3817
0
    return(1);
3818
0
      return(0);
3819
0
        case XML_PARSER_DEFAULTATTRS:
3820
0
      if (ctxt->loadsubset & XML_COMPLETE_ATTRS)
3821
0
    return(1);
3822
0
      return(0);
3823
0
        case XML_PARSER_VALIDATE:
3824
0
      return(reader->validate);
3825
0
  case XML_PARSER_SUBST_ENTITIES:
3826
0
      return(ctxt->replaceEntities);
3827
0
    }
3828
0
    return(-1);
3829
0
}
3830
3831
3832
/**
3833
 * xmlTextReaderGetParserLineNumber:
3834
 * @reader: the user data (XML reader context)
3835
 *
3836
 * Provide the line number of the current parsing point.
3837
 *
3838
 * Returns an int or 0 if not available
3839
 */
3840
int
3841
xmlTextReaderGetParserLineNumber(xmlTextReaderPtr reader)
3842
0
{
3843
0
    if ((reader == NULL) || (reader->ctxt == NULL) ||
3844
0
        (reader->ctxt->input == NULL)) {
3845
0
        return (0);
3846
0
    }
3847
0
    return (reader->ctxt->input->line);
3848
0
}
3849
3850
/**
3851
 * xmlTextReaderGetParserColumnNumber:
3852
 * @reader: the user data (XML reader context)
3853
 *
3854
 * Provide the column number of the current parsing point.
3855
 *
3856
 * Returns an int or 0 if not available
3857
 */
3858
int
3859
xmlTextReaderGetParserColumnNumber(xmlTextReaderPtr reader)
3860
0
{
3861
0
    if ((reader == NULL) || (reader->ctxt == NULL) ||
3862
0
        (reader->ctxt->input == NULL)) {
3863
0
        return (0);
3864
0
    }
3865
0
    return (reader->ctxt->input->col);
3866
0
}
3867
3868
/**
3869
 * xmlTextReaderCurrentNode:
3870
 * @reader:  the xmlTextReaderPtr used
3871
 *
3872
 * Hacking interface allowing to get the xmlNodePtr corresponding to the
3873
 * current node being accessed by the xmlTextReader. This is dangerous
3874
 * because the underlying node may be destroyed on the next Reads.
3875
 *
3876
 * Returns the xmlNodePtr or NULL in case of error.
3877
 */
3878
xmlNodePtr
3879
0
xmlTextReaderCurrentNode(xmlTextReaderPtr reader) {
3880
0
    if (reader == NULL)
3881
0
  return(NULL);
3882
3883
0
    if (reader->curnode != NULL)
3884
0
  return(reader->curnode);
3885
0
    return(reader->node);
3886
0
}
3887
3888
/**
3889
 * xmlTextReaderPreserve:
3890
 * @reader:  the xmlTextReaderPtr used
3891
 *
3892
 * This tells the XML Reader to preserve the current node.
3893
 * The caller must also use xmlTextReaderCurrentDoc() to
3894
 * keep an handle on the resulting document once parsing has finished
3895
 *
3896
 * Returns the xmlNodePtr or NULL in case of error.
3897
 */
3898
xmlNodePtr
3899
0
xmlTextReaderPreserve(xmlTextReaderPtr reader) {
3900
0
    xmlNodePtr cur, parent;
3901
3902
0
    if (reader == NULL)
3903
0
  return(NULL);
3904
3905
0
    cur = reader->node;
3906
0
    if (cur == NULL)
3907
0
        return(NULL);
3908
3909
0
    if ((cur->type != XML_DOCUMENT_NODE) && (cur->type != XML_DTD_NODE)) {
3910
0
  cur->extra |= NODE_IS_PRESERVED;
3911
0
  cur->extra |= NODE_IS_SPRESERVED;
3912
0
    }
3913
0
    reader->preserves++;
3914
3915
0
    parent = cur->parent;;
3916
0
    while (parent != NULL) {
3917
0
        if (parent->type == XML_ELEMENT_NODE)
3918
0
      parent->extra |= NODE_IS_PRESERVED;
3919
0
  parent = parent->parent;
3920
0
    }
3921
0
    return(cur);
3922
0
}
3923
3924
#ifdef LIBXML_PATTERN_ENABLED
3925
/**
3926
 * xmlTextReaderPreservePattern:
3927
 * @reader:  the xmlTextReaderPtr used
3928
 * @pattern:  an XPath subset pattern
3929
 * @namespaces: the prefix definitions, array of [URI, prefix] or NULL
3930
 *
3931
 * This tells the XML Reader to preserve all nodes matched by the
3932
 * pattern. The caller must also use xmlTextReaderCurrentDoc() to
3933
 * keep an handle on the resulting document once parsing has finished
3934
 *
3935
 * Returns a non-negative number in case of success and -1 in case of error
3936
 */
3937
int
3938
xmlTextReaderPreservePattern(xmlTextReaderPtr reader, const xmlChar *pattern,
3939
                             const xmlChar **namespaces)
3940
0
{
3941
0
    xmlPatternPtr comp;
3942
3943
0
    if ((reader == NULL) || (pattern == NULL))
3944
0
  return(-1);
3945
3946
0
    comp = xmlPatterncompile(pattern, reader->dict, 0, namespaces);
3947
0
    if (comp == NULL)
3948
0
        return(-1);
3949
3950
0
    if (reader->patternMax <= 0) {
3951
0
  reader->patternMax = 4;
3952
0
  reader->patternTab = (xmlPatternPtr *) xmlMalloc(reader->patternMax *
3953
0
                sizeof(reader->patternTab[0]));
3954
0
        if (reader->patternTab == NULL) {
3955
0
            xmlTextReaderErrMemory(reader);
3956
0
            return (-1);
3957
0
        }
3958
0
    }
3959
0
    if (reader->patternNr >= reader->patternMax) {
3960
0
        xmlPatternPtr *tmp;
3961
0
        reader->patternMax *= 2;
3962
0
  tmp = (xmlPatternPtr *) xmlRealloc(reader->patternTab,
3963
0
                                      reader->patternMax *
3964
0
                                      sizeof(reader->patternTab[0]));
3965
0
        if (tmp == NULL) {
3966
0
            xmlTextReaderErrMemory(reader);
3967
0
      reader->patternMax /= 2;
3968
0
            return (-1);
3969
0
        }
3970
0
  reader->patternTab = tmp;
3971
0
    }
3972
0
    reader->patternTab[reader->patternNr] = comp;
3973
0
    return(reader->patternNr++);
3974
0
}
3975
#endif
3976
3977
/**
3978
 * xmlTextReaderCurrentDoc:
3979
 * @reader:  the xmlTextReaderPtr used
3980
 *
3981
 * Hacking interface allowing to get the xmlDocPtr corresponding to the
3982
 * current document being accessed by the xmlTextReader.
3983
 * NOTE: as a result of this call, the reader will not destroy the
3984
 *       associated XML document and calling xmlFreeDoc() on the result
3985
 *       is needed once the reader parsing has finished.
3986
 *
3987
 * Returns the xmlDocPtr or NULL in case of error.
3988
 */
3989
xmlDocPtr
3990
0
xmlTextReaderCurrentDoc(xmlTextReaderPtr reader) {
3991
0
    if (reader == NULL)
3992
0
  return(NULL);
3993
0
    if (reader->doc != NULL)
3994
0
        return(reader->doc);
3995
0
    if ((reader->ctxt == NULL) || (reader->ctxt->myDoc == NULL))
3996
0
  return(NULL);
3997
3998
0
    reader->preserve = 1;
3999
0
    return(reader->ctxt->myDoc);
4000
0
}
4001
4002
#ifdef LIBXML_SCHEMAS_ENABLED
4003
/**
4004
 * xmlTextReaderRelaxNGSetSchema:
4005
 * @reader:  the xmlTextReaderPtr used
4006
 * @schema:  a precompiled RelaxNG schema
4007
 *
4008
 * Use RelaxNG to validate the document as it is processed.
4009
 * Activation is only possible before the first Read().
4010
 * if @schema is NULL, then RelaxNG validation is deactivated.
4011
 @ The @schema should not be freed until the reader is deallocated
4012
 * or its use has been deactivated.
4013
 *
4014
 * Returns 0 in case the RelaxNG validation could be (de)activated and
4015
 *         -1 in case of error.
4016
 */
4017
int
4018
0
xmlTextReaderRelaxNGSetSchema(xmlTextReaderPtr reader, xmlRelaxNGPtr schema) {
4019
0
    if (reader == NULL)
4020
0
        return(-1);
4021
0
    if (schema == NULL) {
4022
0
        if (reader->rngSchemas != NULL) {
4023
0
      xmlRelaxNGFree(reader->rngSchemas);
4024
0
      reader->rngSchemas = NULL;
4025
0
  }
4026
0
        if (reader->rngValidCtxt != NULL) {
4027
0
      if (! reader->rngPreserveCtxt)
4028
0
    xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
4029
0
      reader->rngValidCtxt = NULL;
4030
0
        }
4031
0
  reader->rngPreserveCtxt = 0;
4032
0
  return(0);
4033
0
    }
4034
0
    if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
4035
0
  return(-1);
4036
0
    if (reader->rngSchemas != NULL) {
4037
0
  xmlRelaxNGFree(reader->rngSchemas);
4038
0
  reader->rngSchemas = NULL;
4039
0
    }
4040
0
    if (reader->rngValidCtxt != NULL) {
4041
0
  if (! reader->rngPreserveCtxt)
4042
0
      xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
4043
0
  reader->rngValidCtxt = NULL;
4044
0
    }
4045
0
    reader->rngPreserveCtxt = 0;
4046
0
    reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(schema);
4047
0
    if (reader->rngValidCtxt == NULL)
4048
0
        return(-1);
4049
0
    if ((reader->errorFunc != NULL) || (reader->sErrorFunc != NULL))
4050
0
  xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt,
4051
0
      xmlTextReaderStructuredRelay, reader);
4052
0
    reader->rngValidErrors = 0;
4053
0
    reader->rngFullNode = NULL;
4054
0
    reader->validate = XML_TEXTREADER_VALIDATE_RNG;
4055
0
    return(0);
4056
0
}
4057
4058
/**
4059
 * xmlTextReaderLocator:
4060
 * @ctx: the xmlTextReaderPtr used
4061
 * @file: returned file information
4062
 * @line: returned line information
4063
 *
4064
 * Internal locator function for the readers
4065
 *
4066
 * Returns 0 in case the Schema validation could be (de)activated and
4067
 *         -1 in case of error.
4068
 */
4069
static int
4070
0
xmlTextReaderLocator(void *ctx, const char **file, unsigned long *line) {
4071
0
    xmlTextReaderPtr reader;
4072
4073
0
    if ((ctx == NULL) || ((file == NULL) && (line == NULL)))
4074
0
        return(-1);
4075
4076
0
    if (file != NULL)
4077
0
        *file = NULL;
4078
0
    if (line != NULL)
4079
0
        *line = 0;
4080
4081
0
    reader = (xmlTextReaderPtr) ctx;
4082
0
    if ((reader->ctxt != NULL) && (reader->ctxt->input != NULL)) {
4083
0
  if (file != NULL)
4084
0
      *file = reader->ctxt->input->filename;
4085
0
  if (line != NULL)
4086
0
      *line = reader->ctxt->input->line;
4087
0
  return(0);
4088
0
    }
4089
0
    if (reader->node != NULL) {
4090
0
        long res;
4091
0
  int ret = 0;
4092
4093
0
  if (line != NULL) {
4094
0
      res = xmlGetLineNo(reader->node);
4095
0
      if (res > 0)
4096
0
          *line = (unsigned long) res;
4097
0
      else
4098
0
                ret = -1;
4099
0
  }
4100
0
        if (file != NULL) {
4101
0
      xmlDocPtr doc = reader->node->doc;
4102
0
      if ((doc != NULL) && (doc->URL != NULL))
4103
0
          *file = (const char *) doc->URL;
4104
0
      else
4105
0
                ret = -1;
4106
0
  }
4107
0
  return(ret);
4108
0
    }
4109
0
    return(-1);
4110
0
}
4111
4112
/**
4113
 * xmlTextReaderSetSchema:
4114
 * @reader:  the xmlTextReaderPtr used
4115
 * @schema:  a precompiled Schema schema
4116
 *
4117
 * Use XSD Schema to validate the document as it is processed.
4118
 * Activation is only possible before the first Read().
4119
 * if @schema is NULL, then Schema validation is deactivated.
4120
 * The @schema should not be freed until the reader is deallocated
4121
 * or its use has been deactivated.
4122
 *
4123
 * Returns 0 in case the Schema validation could be (de)activated and
4124
 *         -1 in case of error.
4125
 */
4126
int
4127
0
xmlTextReaderSetSchema(xmlTextReaderPtr reader, xmlSchemaPtr schema) {
4128
0
    if (reader == NULL)
4129
0
        return(-1);
4130
0
    if (schema == NULL) {
4131
0
  if (reader->xsdPlug != NULL) {
4132
0
      xmlSchemaSAXUnplug(reader->xsdPlug);
4133
0
      reader->xsdPlug = NULL;
4134
0
  }
4135
0
        if (reader->xsdValidCtxt != NULL) {
4136
0
      if (! reader->xsdPreserveCtxt)
4137
0
    xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
4138
0
      reader->xsdValidCtxt = NULL;
4139
0
        }
4140
0
  reader->xsdPreserveCtxt = 0;
4141
0
        if (reader->xsdSchemas != NULL) {
4142
0
      xmlSchemaFree(reader->xsdSchemas);
4143
0
      reader->xsdSchemas = NULL;
4144
0
  }
4145
0
  return(0);
4146
0
    }
4147
0
    if (reader->mode != XML_TEXTREADER_MODE_INITIAL)
4148
0
  return(-1);
4149
0
    if (reader->xsdPlug != NULL) {
4150
0
  xmlSchemaSAXUnplug(reader->xsdPlug);
4151
0
  reader->xsdPlug = NULL;
4152
0
    }
4153
0
    if (reader->xsdValidCtxt != NULL) {
4154
0
  if (! reader->xsdPreserveCtxt)
4155
0
      xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
4156
0
  reader->xsdValidCtxt = NULL;
4157
0
    }
4158
0
    reader->xsdPreserveCtxt = 0;
4159
0
    if (reader->xsdSchemas != NULL) {
4160
0
  xmlSchemaFree(reader->xsdSchemas);
4161
0
  reader->xsdSchemas = NULL;
4162
0
    }
4163
0
    reader->xsdValidCtxt = xmlSchemaNewValidCtxt(schema);
4164
0
    if (reader->xsdValidCtxt == NULL) {
4165
0
  xmlSchemaFree(reader->xsdSchemas);
4166
0
  reader->xsdSchemas = NULL;
4167
0
        return(-1);
4168
0
    }
4169
0
    reader->xsdPlug = xmlSchemaSAXPlug(reader->xsdValidCtxt,
4170
0
                                       &(reader->ctxt->sax),
4171
0
               &(reader->ctxt->userData));
4172
0
    if (reader->xsdPlug == NULL) {
4173
0
  xmlSchemaFree(reader->xsdSchemas);
4174
0
  reader->xsdSchemas = NULL;
4175
0
  xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
4176
0
  reader->xsdValidCtxt = NULL;
4177
0
  return(-1);
4178
0
    }
4179
0
    xmlSchemaValidateSetLocator(reader->xsdValidCtxt,
4180
0
                                xmlTextReaderLocator,
4181
0
        (void *) reader);
4182
4183
0
    if ((reader->errorFunc != NULL) || (reader->sErrorFunc != NULL))
4184
0
  xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt,
4185
0
      xmlTextReaderStructuredRelay, reader);
4186
0
    reader->xsdValidErrors = 0;
4187
0
    reader->validate = XML_TEXTREADER_VALIDATE_XSD;
4188
0
    return(0);
4189
0
}
4190
4191
/**
4192
 * xmlTextReaderRelaxNGValidateInternal:
4193
 * @reader:  the xmlTextReaderPtr used
4194
 * @rng:  the path to a RelaxNG schema or NULL
4195
 * @ctxt: the RelaxNG schema validation context or NULL
4196
 * @options: options (not yet used)
4197
 *
4198
 * Use RelaxNG to validate the document as it is processed.
4199
 * Activation is only possible before the first Read().
4200
 * If both @rng and @ctxt are NULL, then RelaxNG validation is deactivated.
4201
 *
4202
 * Returns 0 in case the RelaxNG validation could be (de)activated and
4203
 *     -1 in case of error.
4204
 */
4205
static int
4206
xmlTextReaderRelaxNGValidateInternal(xmlTextReaderPtr reader,
4207
             const char *rng,
4208
             xmlRelaxNGValidCtxtPtr ctxt,
4209
             int options ATTRIBUTE_UNUSED)
4210
0
{
4211
0
    if (reader == NULL)
4212
0
  return(-1);
4213
4214
0
    if ((rng != NULL) && (ctxt != NULL))
4215
0
  return (-1);
4216
4217
0
    if (((rng != NULL) || (ctxt != NULL)) &&
4218
0
  ((reader->mode != XML_TEXTREADER_MODE_INITIAL) ||
4219
0
   (reader->ctxt == NULL)))
4220
0
  return(-1);
4221
4222
    /* Cleanup previous validation stuff. */
4223
0
    if (reader->rngValidCtxt != NULL) {
4224
0
  if ( !reader->rngPreserveCtxt)
4225
0
      xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt);
4226
0
  reader->rngValidCtxt = NULL;
4227
0
    }
4228
0
    reader->rngPreserveCtxt = 0;
4229
0
    if (reader->rngSchemas != NULL) {
4230
0
  xmlRelaxNGFree(reader->rngSchemas);
4231
0
  reader->rngSchemas = NULL;
4232
0
    }
4233
4234
0
    if ((rng == NULL) && (ctxt == NULL)) {
4235
  /* We just want to deactivate the validation, so get out. */
4236
0
  return(0);
4237
0
    }
4238
4239
4240
0
    if (rng != NULL) {
4241
0
  xmlRelaxNGParserCtxtPtr pctxt;
4242
  /* Parse the schema and create validation environment. */
4243
4244
0
  pctxt = xmlRelaxNGNewParserCtxt(rng);
4245
0
  if ((reader->errorFunc != NULL) || (reader->sErrorFunc != NULL))
4246
0
      xmlRelaxNGSetParserStructuredErrors(pctxt,
4247
0
                    xmlTextReaderStructuredRelay, reader);
4248
0
  reader->rngSchemas = xmlRelaxNGParse(pctxt);
4249
0
  xmlRelaxNGFreeParserCtxt(pctxt);
4250
0
  if (reader->rngSchemas == NULL)
4251
0
      return(-1);
4252
4253
0
  reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(reader->rngSchemas);
4254
0
  if (reader->rngValidCtxt == NULL) {
4255
0
      xmlRelaxNGFree(reader->rngSchemas);
4256
0
      reader->rngSchemas = NULL;
4257
0
      return(-1);
4258
0
  }
4259
0
    } else {
4260
  /* Use the given validation context. */
4261
0
  reader->rngValidCtxt = ctxt;
4262
0
  reader->rngPreserveCtxt = 1;
4263
0
    }
4264
    /*
4265
    * Redirect the validation context's error channels to use
4266
    * the reader channels.
4267
    * TODO: In case the user provides the validation context we
4268
    * could make this redirection optional.
4269
    */
4270
0
    if ((reader->errorFunc != NULL) || (reader->sErrorFunc != NULL))
4271
0
        xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt,
4272
0
                xmlTextReaderStructuredRelay, reader);
4273
0
    reader->rngValidErrors = 0;
4274
0
    reader->rngFullNode = NULL;
4275
0
    reader->validate = XML_TEXTREADER_VALIDATE_RNG;
4276
0
    return(0);
4277
0
}
4278
4279
/**
4280
 * xmlTextReaderSchemaValidateInternal:
4281
 * @reader:  the xmlTextReaderPtr used
4282
 * @xsd:  the path to a W3C XSD schema or NULL
4283
 * @ctxt: the XML Schema validation context or NULL
4284
 * @options: options (not used yet)
4285
 *
4286
 * Validate the document as it is processed using XML Schema.
4287
 * Activation is only possible before the first Read().
4288
 * If both @xsd and @ctxt are NULL then XML Schema validation is deactivated.
4289
 *
4290
 * Returns 0 in case the schemas validation could be (de)activated and
4291
 *         -1 in case of error.
4292
 */
4293
static int
4294
xmlTextReaderSchemaValidateInternal(xmlTextReaderPtr reader,
4295
            const char *xsd,
4296
            xmlSchemaValidCtxtPtr ctxt,
4297
            int options ATTRIBUTE_UNUSED)
4298
0
{
4299
0
    if (reader == NULL)
4300
0
        return(-1);
4301
4302
0
    if ((xsd != NULL) && (ctxt != NULL))
4303
0
  return(-1);
4304
4305
0
    if (((xsd != NULL) || (ctxt != NULL)) &&
4306
0
  ((reader->mode != XML_TEXTREADER_MODE_INITIAL) ||
4307
0
        (reader->ctxt == NULL)))
4308
0
  return(-1);
4309
4310
    /* Cleanup previous validation stuff. */
4311
0
    if (reader->xsdPlug != NULL) {
4312
0
  xmlSchemaSAXUnplug(reader->xsdPlug);
4313
0
  reader->xsdPlug = NULL;
4314
0
    }
4315
0
    if (reader->xsdValidCtxt != NULL) {
4316
0
  if (! reader->xsdPreserveCtxt)
4317
0
      xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
4318
0
  reader->xsdValidCtxt = NULL;
4319
0
    }
4320
0
    reader->xsdPreserveCtxt = 0;
4321
0
    if (reader->xsdSchemas != NULL) {
4322
0
  xmlSchemaFree(reader->xsdSchemas);
4323
0
  reader->xsdSchemas = NULL;
4324
0
    }
4325
4326
0
    if ((xsd == NULL) && (ctxt == NULL)) {
4327
  /* We just want to deactivate the validation, so get out. */
4328
0
  return(0);
4329
0
    }
4330
4331
0
    if (xsd != NULL) {
4332
0
  xmlSchemaParserCtxtPtr pctxt;
4333
  /* Parse the schema and create validation environment. */
4334
0
  pctxt = xmlSchemaNewParserCtxt(xsd);
4335
0
  if ((reader->errorFunc != NULL) || (reader->sErrorFunc != NULL))
4336
0
      xmlSchemaSetParserStructuredErrors(pctxt,
4337
0
                    xmlTextReaderStructuredRelay, reader);
4338
0
  reader->xsdSchemas = xmlSchemaParse(pctxt);
4339
0
  xmlSchemaFreeParserCtxt(pctxt);
4340
0
  if (reader->xsdSchemas == NULL)
4341
0
      return(-1);
4342
0
  reader->xsdValidCtxt = xmlSchemaNewValidCtxt(reader->xsdSchemas);
4343
0
  if (reader->xsdValidCtxt == NULL) {
4344
0
      xmlSchemaFree(reader->xsdSchemas);
4345
0
      reader->xsdSchemas = NULL;
4346
0
      return(-1);
4347
0
  }
4348
0
  reader->xsdPlug = xmlSchemaSAXPlug(reader->xsdValidCtxt,
4349
0
      &(reader->ctxt->sax),
4350
0
      &(reader->ctxt->userData));
4351
0
  if (reader->xsdPlug == NULL) {
4352
0
      xmlSchemaFree(reader->xsdSchemas);
4353
0
      reader->xsdSchemas = NULL;
4354
0
      xmlSchemaFreeValidCtxt(reader->xsdValidCtxt);
4355
0
      reader->xsdValidCtxt = NULL;
4356
0
      return(-1);
4357
0
  }
4358
0
    } else {
4359
  /* Use the given validation context. */
4360
0
  reader->xsdValidCtxt = ctxt;
4361
0
  reader->xsdPreserveCtxt = 1;
4362
0
  reader->xsdPlug = xmlSchemaSAXPlug(reader->xsdValidCtxt,
4363
0
      &(reader->ctxt->sax),
4364
0
      &(reader->ctxt->userData));
4365
0
  if (reader->xsdPlug == NULL) {
4366
0
      reader->xsdValidCtxt = NULL;
4367
0
      reader->xsdPreserveCtxt = 0;
4368
0
      return(-1);
4369
0
  }
4370
0
    }
4371
0
    xmlSchemaValidateSetLocator(reader->xsdValidCtxt,
4372
0
                                xmlTextReaderLocator,
4373
0
        (void *) reader);
4374
    /*
4375
    * Redirect the validation context's error channels to use
4376
    * the reader channels.
4377
    * TODO: In case the user provides the validation context we
4378
    *   could make this redirection optional.
4379
    */
4380
0
    if ((reader->errorFunc != NULL) || (reader->sErrorFunc != NULL))
4381
0
  xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt,
4382
0
      xmlTextReaderStructuredRelay, reader);
4383
0
    reader->xsdValidErrors = 0;
4384
0
    reader->validate = XML_TEXTREADER_VALIDATE_XSD;
4385
0
    return(0);
4386
0
}
4387
4388
/**
4389
 * xmlTextReaderSchemaValidateCtxt:
4390
 * @reader:  the xmlTextReaderPtr used
4391
 * @ctxt: the XML Schema validation context or NULL
4392
 * @options: options (not used yet)
4393
 *
4394
 * Use W3C XSD schema context to validate the document as it is processed.
4395
 * Activation is only possible before the first Read().
4396
 * If @ctxt is NULL, then XML Schema validation is deactivated.
4397
 *
4398
 * Returns 0 in case the schemas validation could be (de)activated and
4399
 *         -1 in case of error.
4400
 */
4401
int
4402
xmlTextReaderSchemaValidateCtxt(xmlTextReaderPtr reader,
4403
            xmlSchemaValidCtxtPtr ctxt,
4404
            int options)
4405
0
{
4406
0
    return(xmlTextReaderSchemaValidateInternal(reader, NULL, ctxt, options));
4407
0
}
4408
4409
/**
4410
 * xmlTextReaderSchemaValidate:
4411
 * @reader:  the xmlTextReaderPtr used
4412
 * @xsd:  the path to a W3C XSD schema or NULL
4413
 *
4414
 * Use W3C XSD schema to validate the document as it is processed.
4415
 * Activation is only possible before the first Read().
4416
 * If @xsd is NULL, then XML Schema validation is deactivated.
4417
 *
4418
 * Returns 0 in case the schemas validation could be (de)activated and
4419
 *         -1 in case of error.
4420
 */
4421
int
4422
xmlTextReaderSchemaValidate(xmlTextReaderPtr reader, const char *xsd)
4423
0
{
4424
0
    return(xmlTextReaderSchemaValidateInternal(reader, xsd, NULL, 0));
4425
0
}
4426
4427
/**
4428
 * xmlTextReaderRelaxNGValidateCtxt:
4429
 * @reader:  the xmlTextReaderPtr used
4430
 * @ctxt: the RelaxNG schema validation context or NULL
4431
 * @options: options (not used yet)
4432
 *
4433
 * Use RelaxNG schema context to validate the document as it is processed.
4434
 * Activation is only possible before the first Read().
4435
 * If @ctxt is NULL, then RelaxNG schema validation is deactivated.
4436
 *
4437
 * Returns 0 in case the schemas validation could be (de)activated and
4438
 *         -1 in case of error.
4439
 */
4440
int
4441
xmlTextReaderRelaxNGValidateCtxt(xmlTextReaderPtr reader,
4442
         xmlRelaxNGValidCtxtPtr ctxt,
4443
         int options)
4444
0
{
4445
0
    return(xmlTextReaderRelaxNGValidateInternal(reader, NULL, ctxt, options));
4446
0
}
4447
4448
/**
4449
 * xmlTextReaderRelaxNGValidate:
4450
 * @reader:  the xmlTextReaderPtr used
4451
 * @rng:  the path to a RelaxNG schema or NULL
4452
 *
4453
 * Use RelaxNG schema to validate the document as it is processed.
4454
 * Activation is only possible before the first Read().
4455
 * If @rng is NULL, then RelaxNG schema validation is deactivated.
4456
 *
4457
 * Returns 0 in case the schemas validation could be (de)activated and
4458
 *         -1 in case of error.
4459
 */
4460
int
4461
xmlTextReaderRelaxNGValidate(xmlTextReaderPtr reader, const char *rng)
4462
0
{
4463
0
    return(xmlTextReaderRelaxNGValidateInternal(reader, rng, NULL, 0));
4464
0
}
4465
4466
#endif
4467
4468
/**
4469
 * xmlTextReaderIsNamespaceDecl:
4470
 * @reader: the xmlTextReaderPtr used
4471
 *
4472
 * Determine whether the current node is a namespace declaration
4473
 * rather than a regular attribute.
4474
 *
4475
 * Returns 1 if the current node is a namespace declaration, 0 if it
4476
 * is a regular attribute or other type of node, or -1 in case of
4477
 * error.
4478
 */
4479
int
4480
0
xmlTextReaderIsNamespaceDecl(xmlTextReaderPtr reader) {
4481
0
    xmlNodePtr node;
4482
0
    if (reader == NULL)
4483
0
  return(-1);
4484
0
    if (reader->node == NULL)
4485
0
  return(-1);
4486
0
    if (reader->curnode != NULL)
4487
0
  node = reader->curnode;
4488
0
    else
4489
0
  node = reader->node;
4490
4491
0
    if (XML_NAMESPACE_DECL == node->type)
4492
0
  return(1);
4493
0
    else
4494
0
  return(0);
4495
0
}
4496
4497
/**
4498
 * xmlTextReaderConstXmlVersion:
4499
 * @reader:  the xmlTextReaderPtr used
4500
 *
4501
 * Determine the XML version of the document being read.
4502
 *
4503
 * Returns a string containing the XML version of the document or NULL
4504
 * in case of error.  The string is deallocated with the reader.
4505
 */
4506
const xmlChar *
4507
0
xmlTextReaderConstXmlVersion(xmlTextReaderPtr reader) {
4508
0
    xmlDocPtr doc = NULL;
4509
0
    if (reader == NULL)
4510
0
  return(NULL);
4511
0
    if (reader->doc != NULL)
4512
0
        doc = reader->doc;
4513
0
    else if (reader->ctxt != NULL)
4514
0
  doc = reader->ctxt->myDoc;
4515
0
    if (doc == NULL)
4516
0
  return(NULL);
4517
4518
0
    if (doc->version == NULL)
4519
0
  return(NULL);
4520
0
    else
4521
0
      return(constString(reader, doc->version));
4522
0
}
4523
4524
/**
4525
 * xmlTextReaderStandalone:
4526
 * @reader:  the xmlTextReaderPtr used
4527
 *
4528
 * Determine the standalone status of the document being read.
4529
 *
4530
 * Returns 1 if the document was declared to be standalone, 0 if it
4531
 * was declared to be not standalone, or -1 if the document did not
4532
 * specify its standalone status or in case of error.
4533
 */
4534
int
4535
0
xmlTextReaderStandalone(xmlTextReaderPtr reader) {
4536
0
    xmlDocPtr doc = NULL;
4537
0
    if (reader == NULL)
4538
0
  return(-1);
4539
0
    if (reader->doc != NULL)
4540
0
        doc = reader->doc;
4541
0
    else if (reader->ctxt != NULL)
4542
0
  doc = reader->ctxt->myDoc;
4543
0
    if (doc == NULL)
4544
0
  return(-1);
4545
4546
0
    return(doc->standalone);
4547
0
}
4548
4549
/************************************************************************
4550
 *                  *
4551
 *      Error Handling Extensions                       *
4552
 *                  *
4553
 ************************************************************************/
4554
4555
/**
4556
 * xmlTextReaderLocatorLineNumber:
4557
 * @locator: the xmlTextReaderLocatorPtr used
4558
 *
4559
 * Obtain the line number for the given locator.
4560
 *
4561
 * Returns the line number or -1 in case of error.
4562
 */
4563
int
4564
0
xmlTextReaderLocatorLineNumber(xmlTextReaderLocatorPtr locator) {
4565
    /* we know that locator is a xmlParserCtxtPtr */
4566
0
    xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
4567
0
    int ret = -1;
4568
4569
0
    if (locator == NULL)
4570
0
        return(-1);
4571
0
    if (ctx->node != NULL) {
4572
0
  ret = xmlGetLineNo(ctx->node);
4573
0
    }
4574
0
    else {
4575
  /* inspired from error.c */
4576
0
  xmlParserInputPtr input;
4577
0
  input = ctx->input;
4578
0
  if ((input->filename == NULL) && (ctx->inputNr > 1))
4579
0
      input = ctx->inputTab[ctx->inputNr - 2];
4580
0
  if (input != NULL) {
4581
0
      ret = input->line;
4582
0
  }
4583
0
  else {
4584
0
      ret = -1;
4585
0
  }
4586
0
    }
4587
4588
0
    return ret;
4589
0
}
4590
4591
/**
4592
 * xmlTextReaderLocatorBaseURI:
4593
 * @locator: the xmlTextReaderLocatorPtr used
4594
 *
4595
 * Obtain the base URI for the given locator.
4596
 *
4597
 * Returns the base URI or NULL in case of error,
4598
 *    if non NULL it need to be freed by the caller.
4599
 */
4600
xmlChar *
4601
0
xmlTextReaderLocatorBaseURI(xmlTextReaderLocatorPtr locator) {
4602
    /* we know that locator is a xmlParserCtxtPtr */
4603
0
    xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
4604
0
    xmlChar *ret = NULL;
4605
4606
0
    if (locator == NULL)
4607
0
        return(NULL);
4608
0
    if (ctx->node != NULL) {
4609
0
  ret = xmlNodeGetBase(NULL,ctx->node);
4610
0
    }
4611
0
    else {
4612
  /* inspired from error.c */
4613
0
  xmlParserInputPtr input;
4614
0
  input = ctx->input;
4615
0
  if ((input->filename == NULL) && (ctx->inputNr > 1))
4616
0
      input = ctx->inputTab[ctx->inputNr - 2];
4617
0
  if (input != NULL) {
4618
0
      ret = xmlStrdup(BAD_CAST input->filename);
4619
0
  }
4620
0
  else {
4621
0
      ret = NULL;
4622
0
  }
4623
0
    }
4624
4625
0
    return ret;
4626
0
}
4627
4628
/**
4629
 * xmlTextReaderSetErrorHandler:
4630
 * @reader:  the xmlTextReaderPtr used
4631
 * @f:  the callback function to call on error and warnings
4632
 * @arg:    a user argument to pass to the callback function
4633
 *
4634
 * DEPRECATED: Use xmlTextReaderSetStructuredErrorHandler.
4635
 *
4636
 * Register a callback function that will be called on error and warnings.
4637
 *
4638
 * If @f is NULL, the default error and warning handlers are restored.
4639
 */
4640
void
4641
xmlTextReaderSetErrorHandler(xmlTextReaderPtr reader,
4642
                             xmlTextReaderErrorFunc f, void *arg)
4643
0
{
4644
0
    if (f != NULL) {
4645
0
        reader->errorFunc = f;
4646
0
        reader->sErrorFunc = NULL;
4647
0
        reader->errorFuncArg = arg;
4648
0
        xmlCtxtSetErrorHandler(reader->ctxt,
4649
0
                xmlTextReaderStructuredRelay, reader);
4650
0
#ifdef LIBXML_SCHEMAS_ENABLED
4651
0
        if (reader->rngValidCtxt) {
4652
0
            xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt,
4653
0
                    xmlTextReaderStructuredRelay, reader);
4654
0
        }
4655
0
        if (reader->xsdValidCtxt) {
4656
0
            xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt,
4657
0
                    xmlTextReaderStructuredRelay, reader);
4658
0
        }
4659
0
#endif
4660
0
    } else {
4661
        /* restore defaults */
4662
0
        reader->errorFunc = NULL;
4663
0
        reader->sErrorFunc = NULL;
4664
0
        reader->errorFuncArg = NULL;
4665
0
        xmlCtxtSetErrorHandler(reader->ctxt, NULL, NULL);
4666
0
#ifdef LIBXML_SCHEMAS_ENABLED
4667
0
        if (reader->rngValidCtxt) {
4668
0
            xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt, NULL,
4669
0
                                               NULL);
4670
0
        }
4671
0
        if (reader->xsdValidCtxt) {
4672
0
            xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt, NULL,
4673
0
                                              NULL);
4674
0
        }
4675
0
#endif
4676
0
    }
4677
0
}
4678
4679
/**
4680
* xmlTextReaderSetStructuredErrorHandler:
4681
 * @reader:  the xmlTextReaderPtr used
4682
 * @f:  the callback function to call on error and warnings
4683
 * @arg:    a user argument to pass to the callback function
4684
 *
4685
 * Register a callback function that will be called on error and warnings.
4686
 *
4687
 * If @f is NULL, the default error and warning handlers are restored.
4688
 */
4689
void
4690
xmlTextReaderSetStructuredErrorHandler(xmlTextReaderPtr reader,
4691
                                       xmlStructuredErrorFunc f, void *arg)
4692
0
{
4693
0
    if (f != NULL) {
4694
0
        reader->sErrorFunc = f;
4695
0
        reader->errorFunc = NULL;
4696
0
        reader->errorFuncArg = arg;
4697
0
        xmlCtxtSetErrorHandler(reader->ctxt,
4698
0
                xmlTextReaderStructuredRelay, reader);
4699
0
#ifdef LIBXML_SCHEMAS_ENABLED
4700
0
        if (reader->rngValidCtxt) {
4701
0
            xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt,
4702
0
                    xmlTextReaderStructuredRelay, reader);
4703
0
        }
4704
0
        if (reader->xsdValidCtxt) {
4705
0
            xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt,
4706
0
                    xmlTextReaderStructuredRelay, reader);
4707
0
        }
4708
0
#endif
4709
0
    } else {
4710
        /* restore defaults */
4711
0
        reader->errorFunc = NULL;
4712
0
        reader->sErrorFunc = NULL;
4713
0
        reader->errorFuncArg = NULL;
4714
0
        xmlCtxtSetErrorHandler(reader->ctxt, NULL, NULL);
4715
0
#ifdef LIBXML_SCHEMAS_ENABLED
4716
0
        if (reader->rngValidCtxt) {
4717
0
            xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt, NULL,
4718
0
                                               NULL);
4719
0
        }
4720
0
        if (reader->xsdValidCtxt) {
4721
0
            xmlSchemaSetValidStructuredErrors(reader->xsdValidCtxt, NULL,
4722
0
                                              NULL);
4723
0
        }
4724
0
#endif
4725
0
    }
4726
0
}
4727
4728
/**
4729
 * xmlTextReaderGetErrorHandler:
4730
 * @reader:  the xmlTextReaderPtr used
4731
 * @f:  the callback function or NULL is no callback has been registered
4732
 * @arg:    a user argument
4733
 *
4734
 * Retrieve the error callback function and user argument.
4735
 */
4736
void
4737
xmlTextReaderGetErrorHandler(xmlTextReaderPtr reader,
4738
                             xmlTextReaderErrorFunc * f, void **arg)
4739
0
{
4740
0
    if (f != NULL)
4741
0
        *f = reader->errorFunc;
4742
0
    if (arg != NULL)
4743
0
        *arg = reader->errorFuncArg;
4744
0
}
4745
4746
/**
4747
 * xmlTextReaderSetResourceLoader:
4748
 * @reader:  thr reader
4749
 * @loader:  resource loader
4750
 * @data:  user data which will be passed to the loader
4751
 *
4752
 * Register a callback function that will be called to load external
4753
 * resources like entities.
4754
 *
4755
 * Available since 2.14.0.
4756
 */
4757
void
4758
xmlTextReaderSetResourceLoader(xmlTextReaderPtr reader,
4759
0
                               xmlResourceLoader loader, void *data) {
4760
0
    if ((reader == NULL) || (reader->ctxt == NULL))
4761
0
        return;
4762
4763
0
    reader->resourceLoader = loader;
4764
0
    reader->resourceCtxt = data;
4765
4766
0
    xmlCtxtSetResourceLoader(reader->ctxt, loader, data);
4767
0
}
4768
4769
/**
4770
 * xmlTextReaderIsValid:
4771
 * @reader:  the xmlTextReaderPtr used
4772
 *
4773
 * Retrieve the validity status from the parser context
4774
 *
4775
 * Returns the flag value 1 if valid, 0 if no, and -1 in case of error
4776
 */
4777
int
4778
xmlTextReaderIsValid(xmlTextReaderPtr reader)
4779
0
{
4780
0
    if (reader == NULL)
4781
0
        return (-1);
4782
0
#ifdef LIBXML_SCHEMAS_ENABLED
4783
0
    if (reader->validate == XML_TEXTREADER_VALIDATE_RNG)
4784
0
        return (reader->rngValidErrors == 0);
4785
0
    if (reader->validate == XML_TEXTREADER_VALIDATE_XSD)
4786
0
        return (reader->xsdValidErrors == 0);
4787
0
#endif
4788
0
    if ((reader->ctxt != NULL) && (reader->ctxt->validate == 1))
4789
0
        return (reader->ctxt->valid);
4790
0
    return (0);
4791
0
}
4792
4793
/************************************************************************
4794
 *                  *
4795
 *  New set (2.6.0) of simpler and more flexible APIs   *
4796
 *                  *
4797
 ************************************************************************/
4798
4799
/**
4800
 * xmlTextReaderSetup:
4801
 * @reader:  an XML reader
4802
 * @input: xmlParserInputBufferPtr used to feed the reader, will
4803
 *         be destroyed with it.
4804
 * @URL:  the base URL to use for the document
4805
 * @encoding:  the document encoding, or NULL
4806
 * @options:  a combination of xmlParserOption
4807
 *
4808
 * Setup an XML reader with new options
4809
 *
4810
 * Returns 0 in case of success and -1 in case of error.
4811
 */
4812
int
4813
xmlTextReaderSetup(xmlTextReaderPtr reader,
4814
                   xmlParserInputBufferPtr input, const char *URL,
4815
                   const char *encoding, int options)
4816
0
{
4817
0
    if (reader == NULL) {
4818
0
        if (input != NULL)
4819
0
      xmlFreeParserInputBuffer(input);
4820
0
        return (-1);
4821
0
    }
4822
4823
    /*
4824
     * we force the generation of compact text nodes on the reader
4825
     * since usr applications should never modify the tree
4826
     */
4827
0
    options |= XML_PARSE_COMPACT;
4828
4829
0
    reader->doc = NULL;
4830
0
    reader->entNr = 0;
4831
0
    reader->parserFlags = options;
4832
0
    reader->validate = XML_TEXTREADER_NOT_VALIDATE;
4833
0
    if ((input != NULL) && (reader->input != NULL) &&
4834
0
        (reader->allocs & XML_TEXTREADER_INPUT)) {
4835
0
  xmlFreeParserInputBuffer(reader->input);
4836
0
  reader->input = NULL;
4837
0
  reader->allocs -= XML_TEXTREADER_INPUT;
4838
0
    }
4839
0
    if (input != NULL) {
4840
0
  reader->input = input;
4841
0
  reader->allocs |= XML_TEXTREADER_INPUT;
4842
0
    }
4843
0
    if (reader->buffer == NULL)
4844
0
        reader->buffer = xmlBufCreate(50);
4845
0
    if (reader->buffer == NULL) {
4846
0
        return (-1);
4847
0
    }
4848
0
    if (reader->sax == NULL)
4849
0
  reader->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
4850
0
    if (reader->sax == NULL) {
4851
0
        return (-1);
4852
0
    }
4853
0
    xmlSAXVersion(reader->sax, 2);
4854
0
    reader->startElement = reader->sax->startElement;
4855
0
    reader->sax->startElement = xmlTextReaderStartElement;
4856
0
    reader->endElement = reader->sax->endElement;
4857
0
    reader->sax->endElement = xmlTextReaderEndElement;
4858
0
#ifdef LIBXML_SAX1_ENABLED
4859
0
    if (reader->sax->initialized == XML_SAX2_MAGIC) {
4860
0
#endif /* LIBXML_SAX1_ENABLED */
4861
0
        reader->startElementNs = reader->sax->startElementNs;
4862
0
        reader->sax->startElementNs = xmlTextReaderStartElementNs;
4863
0
        reader->endElementNs = reader->sax->endElementNs;
4864
0
        reader->sax->endElementNs = xmlTextReaderEndElementNs;
4865
0
#ifdef LIBXML_SAX1_ENABLED
4866
0
    } else {
4867
0
        reader->startElementNs = NULL;
4868
0
        reader->endElementNs = NULL;
4869
0
    }
4870
0
#endif /* LIBXML_SAX1_ENABLED */
4871
0
    reader->characters = reader->sax->characters;
4872
0
    reader->sax->characters = xmlTextReaderCharacters;
4873
0
    reader->sax->ignorableWhitespace = xmlTextReaderCharacters;
4874
0
    reader->cdataBlock = reader->sax->cdataBlock;
4875
0
    reader->sax->cdataBlock = xmlTextReaderCDataBlock;
4876
4877
0
    reader->mode = XML_TEXTREADER_MODE_INITIAL;
4878
0
    reader->node = NULL;
4879
0
    reader->curnode = NULL;
4880
0
    if (input != NULL) {
4881
0
        if (xmlBufUse(reader->input->buffer) < 4) {
4882
0
            xmlParserInputBufferRead(input, 4);
4883
0
        }
4884
0
        if (reader->ctxt == NULL) {
4885
0
            if (xmlBufUse(reader->input->buffer) >= 4) {
4886
0
                reader->ctxt = xmlCreatePushParserCtxt(reader->sax, NULL,
4887
0
           (const char *) xmlBufContent(reader->input->buffer),
4888
0
                                      4, URL);
4889
0
                reader->base = 0;
4890
0
                reader->cur = 4;
4891
0
            } else {
4892
0
                reader->ctxt =
4893
0
                    xmlCreatePushParserCtxt(reader->sax, NULL, NULL, 0, URL);
4894
0
                reader->base = 0;
4895
0
                reader->cur = 0;
4896
0
            }
4897
0
            if (reader->ctxt == NULL) {
4898
0
                return (-1);
4899
0
            }
4900
0
        } else {
4901
0
      xmlParserInputPtr inputStream;
4902
0
      xmlParserInputBufferPtr buf;
4903
4904
0
      xmlCtxtReset(reader->ctxt);
4905
0
      buf = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE);
4906
0
      if (buf == NULL) return(-1);
4907
0
      inputStream = xmlNewInputStream(reader->ctxt);
4908
0
      if (inputStream == NULL) {
4909
0
    xmlFreeParserInputBuffer(buf);
4910
0
    return(-1);
4911
0
      }
4912
4913
0
      if (URL == NULL)
4914
0
    inputStream->filename = NULL;
4915
0
      else
4916
0
    inputStream->filename = (char *)
4917
0
        xmlCanonicPath((const xmlChar *) URL);
4918
0
      inputStream->buf = buf;
4919
0
            xmlBufResetInput(buf->buffer, inputStream);
4920
4921
0
            if (inputPush(reader->ctxt, inputStream) < 0) {
4922
0
                xmlFreeInputStream(inputStream);
4923
0
                return(-1);
4924
0
            }
4925
0
      reader->cur = 0;
4926
0
  }
4927
0
    }
4928
0
    if (reader->dict != NULL) {
4929
0
        if (reader->ctxt->dict != NULL) {
4930
0
      if (reader->dict != reader->ctxt->dict) {
4931
0
    xmlDictFree(reader->dict);
4932
0
    reader->dict = reader->ctxt->dict;
4933
0
      }
4934
0
  } else {
4935
0
      reader->ctxt->dict = reader->dict;
4936
0
  }
4937
0
    } else {
4938
0
  if (reader->ctxt->dict == NULL)
4939
0
      reader->ctxt->dict = xmlDictCreate();
4940
0
        reader->dict = reader->ctxt->dict;
4941
0
    }
4942
0
    reader->ctxt->_private = reader;
4943
0
    reader->ctxt->linenumbers = 1;
4944
0
    reader->ctxt->dictNames = 1;
4945
    /*
4946
     * use the parser dictionary to allocate all elements and attributes names
4947
     */
4948
0
    reader->ctxt->parseMode = XML_PARSE_READER;
4949
4950
0
#ifdef LIBXML_XINCLUDE_ENABLED
4951
0
    if (reader->xincctxt != NULL) {
4952
0
  xmlXIncludeFreeContext(reader->xincctxt);
4953
0
  reader->xincctxt = NULL;
4954
0
    }
4955
0
    if (options & XML_PARSE_XINCLUDE) {
4956
0
        reader->xinclude = 1;
4957
0
  reader->xinclude_name = xmlDictLookup(reader->dict, XINCLUDE_NODE, -1);
4958
0
        if (reader->xinclude_name == NULL)
4959
0
            return(-1);
4960
0
  options -= XML_PARSE_XINCLUDE;
4961
0
    } else
4962
0
        reader->xinclude = 0;
4963
0
    reader->in_xinclude = 0;
4964
0
#endif
4965
0
#ifdef LIBXML_PATTERN_ENABLED
4966
0
    if (reader->patternTab == NULL) {
4967
0
        reader->patternNr = 0;
4968
0
  reader->patternMax = 0;
4969
0
    }
4970
0
    while (reader->patternNr > 0) {
4971
0
        reader->patternNr--;
4972
0
  if (reader->patternTab[reader->patternNr] != NULL) {
4973
0
      xmlFreePattern(reader->patternTab[reader->patternNr]);
4974
0
            reader->patternTab[reader->patternNr] = NULL;
4975
0
  }
4976
0
    }
4977
0
#endif
4978
4979
0
    if (options & XML_PARSE_DTDVALID)
4980
0
        reader->validate = XML_TEXTREADER_VALIDATE_DTD;
4981
4982
0
    xmlCtxtUseOptions(reader->ctxt, options);
4983
0
    if (encoding != NULL)
4984
0
        xmlSwitchEncodingName(reader->ctxt, encoding);
4985
0
    if ((URL != NULL) && (reader->ctxt->input != NULL) &&
4986
0
        (reader->ctxt->input->filename == NULL)) {
4987
0
        reader->ctxt->input->filename = (char *)
4988
0
            xmlStrdup((const xmlChar *) URL);
4989
0
        if (reader->ctxt->input->filename == NULL)
4990
0
            return(-1);
4991
0
    }
4992
4993
0
    reader->doc = NULL;
4994
4995
0
    return (0);
4996
0
}
4997
4998
/**
4999
 * xmlTextReaderSetMaxAmplification:
5000
 * @reader: an XML reader
5001
 * @maxAmpl:  maximum amplification factor
5002
 *
5003
 * Set the maximum amplification factor. See xmlCtxtSetMaxAmplification.
5004
 */
5005
void
5006
xmlTextReaderSetMaxAmplification(xmlTextReaderPtr reader, unsigned maxAmpl)
5007
0
{
5008
0
    xmlCtxtSetMaxAmplification(reader->ctxt, maxAmpl);
5009
0
}
5010
5011
/**
5012
 * xmlTextReaderGetLastError:
5013
 * @reader: an XML reader
5014
 *
5015
 * Available since 2.13.0.
5016
 *
5017
 * Returns the last error.
5018
 */
5019
const xmlError *
5020
xmlTextReaderGetLastError(xmlTextReaderPtr reader)
5021
0
{
5022
0
    if (reader == NULL)
5023
0
        return(NULL);
5024
0
    return(&reader->ctxt->lastError);
5025
0
}
5026
5027
/**
5028
 * xmlTextReaderByteConsumed:
5029
 * @reader: an XML reader
5030
 *
5031
 * DEPRECATED: The returned value is mostly random and useless.
5032
 * It reflects the parser reading ahead and is in no way related to
5033
 * the current node.
5034
 *
5035
 * This function provides the current index of the parser used
5036
 * by the reader, relative to the start of the current entity.
5037
 * This function actually just wraps a call to xmlBytesConsumed()
5038
 * for the parser context associated with the reader.
5039
 * See xmlBytesConsumed() for more information.
5040
 *
5041
 * Returns the index in bytes from the beginning of the entity or -1
5042
 *         in case the index could not be computed.
5043
 */
5044
long
5045
0
xmlTextReaderByteConsumed(xmlTextReaderPtr reader) {
5046
0
    xmlParserInputPtr in;
5047
5048
0
    if ((reader == NULL) || (reader->ctxt == NULL))
5049
0
        return(-1);
5050
0
    in = reader->ctxt->input;
5051
0
    if (in == NULL)
5052
0
        return(-1);
5053
0
    return(in->consumed + (in->cur - in->base));
5054
0
}
5055
5056
5057
/**
5058
 * xmlReaderWalker:
5059
 * @doc:  a preparsed document
5060
 *
5061
 * Create an xmltextReader for a preparsed document.
5062
 *
5063
 * Returns the new reader or NULL in case of error.
5064
 */
5065
xmlTextReaderPtr
5066
xmlReaderWalker(xmlDocPtr doc)
5067
0
{
5068
0
    xmlTextReaderPtr ret;
5069
5070
0
    if (doc == NULL)
5071
0
        return(NULL);
5072
5073
0
    ret = xmlMalloc(sizeof(xmlTextReader));
5074
0
    if (ret == NULL) {
5075
0
  return(NULL);
5076
0
    }
5077
0
    memset(ret, 0, sizeof(xmlTextReader));
5078
0
    ret->entNr = 0;
5079
0
    ret->input = NULL;
5080
0
    ret->mode = XML_TEXTREADER_MODE_INITIAL;
5081
0
    ret->node = NULL;
5082
0
    ret->curnode = NULL;
5083
0
    ret->base = 0;
5084
0
    ret->cur = 0;
5085
0
    ret->allocs = XML_TEXTREADER_CTXT;
5086
0
    ret->doc = doc;
5087
0
    ret->state = XML_TEXTREADER_START;
5088
0
    ret->dict = xmlDictCreate();
5089
0
    return(ret);
5090
0
}
5091
5092
/**
5093
 * xmlReaderForDoc:
5094
 * @cur:  a pointer to a zero terminated string
5095
 * @URL:  the base URL to use for the document
5096
 * @encoding:  the document encoding, or NULL
5097
 * @options:  a combination of xmlParserOption
5098
 *
5099
 * Create an xmltextReader for an XML in-memory document.
5100
 * The parsing flags @options are a combination of xmlParserOption.
5101
 *
5102
 * Returns the new reader or NULL in case of error.
5103
 */
5104
xmlTextReaderPtr
5105
xmlReaderForDoc(const xmlChar * cur, const char *URL, const char *encoding,
5106
                int options)
5107
0
{
5108
0
    int len;
5109
5110
0
    if (cur == NULL)
5111
0
        return (NULL);
5112
0
    len = xmlStrlen(cur);
5113
5114
0
    return (xmlReaderForMemory
5115
0
            ((const char *) cur, len, URL, encoding, options));
5116
0
}
5117
5118
/**
5119
 * xmlReaderForFile:
5120
 * @filename:  a file or URL
5121
 * @encoding:  the document encoding, or NULL
5122
 * @options:  a combination of xmlParserOption
5123
 *
5124
 * parse an XML file from the filesystem or the network.
5125
 * The parsing flags @options are a combination of xmlParserOption.
5126
 *
5127
 * Returns the new reader or NULL in case of error.
5128
 */
5129
xmlTextReaderPtr
5130
xmlReaderForFile(const char *filename, const char *encoding, int options)
5131
0
{
5132
0
    xmlTextReaderPtr reader;
5133
5134
0
    reader = xmlNewTextReaderFilename(filename);
5135
0
    if (reader == NULL)
5136
0
        return (NULL);
5137
0
    if (xmlTextReaderSetup(reader, NULL, NULL, encoding, options) < 0) {
5138
0
        xmlFreeTextReader(reader);
5139
0
        return (NULL);
5140
0
    }
5141
0
    return (reader);
5142
0
}
5143
5144
/**
5145
 * xmlReaderForMemory:
5146
 * @buffer:  a pointer to a char array
5147
 * @size:  the size of the array
5148
 * @URL:  the base URL to use for the document
5149
 * @encoding:  the document encoding, or NULL
5150
 * @options:  a combination of xmlParserOption
5151
 *
5152
 * Create an xmltextReader for an XML in-memory document.
5153
 * The parsing flags @options are a combination of xmlParserOption.
5154
 *
5155
 * Returns the new reader or NULL in case of error.
5156
 */
5157
xmlTextReaderPtr
5158
xmlReaderForMemory(const char *buffer, int size, const char *URL,
5159
                   const char *encoding, int options)
5160
0
{
5161
0
    xmlTextReaderPtr reader;
5162
0
    xmlParserInputBufferPtr buf;
5163
5164
0
    buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
5165
0
    if (buf == NULL) {
5166
0
        return (NULL);
5167
0
    }
5168
0
    reader = xmlNewTextReader(buf, URL);
5169
0
    if (reader == NULL) {
5170
0
        xmlFreeParserInputBuffer(buf);
5171
0
        return (NULL);
5172
0
    }
5173
0
    reader->allocs |= XML_TEXTREADER_INPUT;
5174
0
    if (xmlTextReaderSetup(reader, NULL, URL, encoding, options) < 0) {
5175
0
        xmlFreeTextReader(reader);
5176
0
        return (NULL);
5177
0
    }
5178
0
    return (reader);
5179
0
}
5180
5181
/**
5182
 * xmlReaderForFd:
5183
 * @fd:  an open file descriptor
5184
 * @URL:  the base URL to use for the document
5185
 * @encoding:  the document encoding, or NULL
5186
 * @options:  a combination of xmlParserOption
5187
 *
5188
 * Create an xmltextReader for an XML from a file descriptor.
5189
 * The parsing flags @options are a combination of xmlParserOption.
5190
 * NOTE that the file descriptor will not be closed when the
5191
 *      reader is closed or reset.
5192
 *
5193
 * Returns the new reader or NULL in case of error.
5194
 */
5195
xmlTextReaderPtr
5196
xmlReaderForFd(int fd, const char *URL, const char *encoding, int options)
5197
0
{
5198
0
    xmlTextReaderPtr reader;
5199
0
    xmlParserInputBufferPtr input;
5200
5201
0
    if (fd < 0)
5202
0
        return (NULL);
5203
5204
0
    input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
5205
0
    if (input == NULL)
5206
0
        return (NULL);
5207
0
    input->closecallback = NULL;
5208
0
    reader = xmlNewTextReader(input, URL);
5209
0
    if (reader == NULL) {
5210
0
        xmlFreeParserInputBuffer(input);
5211
0
        return (NULL);
5212
0
    }
5213
0
    reader->allocs |= XML_TEXTREADER_INPUT;
5214
0
    if (xmlTextReaderSetup(reader, NULL, URL, encoding, options) < 0) {
5215
0
        xmlFreeTextReader(reader);
5216
0
        return (NULL);
5217
0
    }
5218
0
    return (reader);
5219
0
}
5220
5221
/**
5222
 * xmlReaderForIO:
5223
 * @ioread:  an I/O read function
5224
 * @ioclose:  an I/O close function
5225
 * @ioctx:  an I/O handler
5226
 * @URL:  the base URL to use for the document
5227
 * @encoding:  the document encoding, or NULL
5228
 * @options:  a combination of xmlParserOption
5229
 *
5230
 * Create an xmltextReader for an XML document from I/O functions and source.
5231
 * The parsing flags @options are a combination of xmlParserOption.
5232
 *
5233
 * Returns the new reader or NULL in case of error.
5234
 */
5235
xmlTextReaderPtr
5236
xmlReaderForIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
5237
               void *ioctx, const char *URL, const char *encoding,
5238
               int options)
5239
0
{
5240
0
    xmlTextReaderPtr reader;
5241
0
    xmlParserInputBufferPtr input;
5242
5243
0
    if (ioread == NULL)
5244
0
        return (NULL);
5245
5246
0
    input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
5247
0
                                         XML_CHAR_ENCODING_NONE);
5248
0
    if (input == NULL) {
5249
0
        if (ioclose != NULL)
5250
0
            ioclose(ioctx);
5251
0
        return (NULL);
5252
0
    }
5253
0
    reader = xmlNewTextReader(input, URL);
5254
0
    if (reader == NULL) {
5255
0
        xmlFreeParserInputBuffer(input);
5256
0
        return (NULL);
5257
0
    }
5258
0
    reader->allocs |= XML_TEXTREADER_INPUT;
5259
0
    if (xmlTextReaderSetup(reader, NULL, URL, encoding, options) < 0) {
5260
0
        xmlFreeTextReader(reader);
5261
0
        return (NULL);
5262
0
    }
5263
0
    return (reader);
5264
0
}
5265
5266
/**
5267
 * xmlReaderNewWalker:
5268
 * @reader:  an XML reader
5269
 * @doc:  a preparsed document
5270
 *
5271
 * Setup an xmltextReader to parse a preparsed XML document.
5272
 * This reuses the existing @reader xmlTextReader.
5273
 *
5274
 * Returns 0 in case of success and -1 in case of error
5275
 */
5276
int
5277
xmlReaderNewWalker(xmlTextReaderPtr reader, xmlDocPtr doc)
5278
0
{
5279
0
    if (doc == NULL)
5280
0
        return (-1);
5281
0
    if (reader == NULL)
5282
0
        return (-1);
5283
5284
0
    if (reader->input != NULL) {
5285
0
        xmlFreeParserInputBuffer(reader->input);
5286
0
    }
5287
0
    if (reader->ctxt != NULL) {
5288
0
  xmlCtxtReset(reader->ctxt);
5289
0
    }
5290
5291
0
    reader->entNr = 0;
5292
0
    reader->input = NULL;
5293
0
    reader->mode = XML_TEXTREADER_MODE_INITIAL;
5294
0
    reader->node = NULL;
5295
0
    reader->curnode = NULL;
5296
0
    reader->base = 0;
5297
0
    reader->cur = 0;
5298
0
    reader->allocs = XML_TEXTREADER_CTXT;
5299
0
    reader->doc = doc;
5300
0
    reader->state = XML_TEXTREADER_START;
5301
0
    if (reader->dict == NULL) {
5302
0
        if ((reader->ctxt != NULL) && (reader->ctxt->dict != NULL))
5303
0
      reader->dict = reader->ctxt->dict;
5304
0
  else
5305
0
      reader->dict = xmlDictCreate();
5306
0
    }
5307
0
    return(0);
5308
0
}
5309
5310
/**
5311
 * xmlReaderNewDoc:
5312
 * @reader:  an XML reader
5313
 * @cur:  a pointer to a zero terminated string
5314
 * @URL:  the base URL to use for the document
5315
 * @encoding:  the document encoding, or NULL
5316
 * @options:  a combination of xmlParserOption
5317
 *
5318
 * Setup an xmltextReader to parse an XML in-memory document.
5319
 * The parsing flags @options are a combination of xmlParserOption.
5320
 * This reuses the existing @reader xmlTextReader.
5321
 *
5322
 * Returns 0 in case of success and -1 in case of error
5323
 */
5324
int
5325
xmlReaderNewDoc(xmlTextReaderPtr reader, const xmlChar * cur,
5326
                const char *URL, const char *encoding, int options)
5327
0
{
5328
5329
0
    int len;
5330
5331
0
    if (cur == NULL)
5332
0
        return (-1);
5333
0
    if (reader == NULL)
5334
0
        return (-1);
5335
5336
0
    len = xmlStrlen(cur);
5337
0
    return (xmlReaderNewMemory(reader, (const char *)cur, len,
5338
0
                               URL, encoding, options));
5339
0
}
5340
5341
/**
5342
 * xmlReaderNewFile:
5343
 * @reader:  an XML reader
5344
 * @filename:  a file or URL
5345
 * @encoding:  the document encoding, or NULL
5346
 * @options:  a combination of xmlParserOption
5347
 *
5348
 * parse an XML file from the filesystem or the network.
5349
 * The parsing flags @options are a combination of xmlParserOption.
5350
 * This reuses the existing @reader xmlTextReader.
5351
 *
5352
 * Returns 0 in case of success and -1 in case of error
5353
 */
5354
int
5355
xmlReaderNewFile(xmlTextReaderPtr reader, const char *filename,
5356
                 const char *encoding, int options)
5357
0
{
5358
0
    xmlParserInputBufferPtr input;
5359
5360
0
    if (filename == NULL)
5361
0
        return (-1);
5362
0
    if (reader == NULL)
5363
0
        return (-1);
5364
5365
0
    input =
5366
0
        xmlParserInputBufferCreateFilename(filename,
5367
0
                                           XML_CHAR_ENCODING_NONE);
5368
0
    if (input == NULL)
5369
0
        return (-1);
5370
0
    return (xmlTextReaderSetup(reader, input, filename, encoding, options));
5371
0
}
5372
5373
/**
5374
 * xmlReaderNewMemory:
5375
 * @reader:  an XML reader
5376
 * @buffer:  a pointer to a char array
5377
 * @size:  the size of the array
5378
 * @URL:  the base URL to use for the document
5379
 * @encoding:  the document encoding, or NULL
5380
 * @options:  a combination of xmlParserOption
5381
 *
5382
 * Setup an xmltextReader to parse an XML in-memory document.
5383
 * The parsing flags @options are a combination of xmlParserOption.
5384
 * This reuses the existing @reader xmlTextReader.
5385
 *
5386
 * Returns 0 in case of success and -1 in case of error
5387
 */
5388
int
5389
xmlReaderNewMemory(xmlTextReaderPtr reader, const char *buffer, int size,
5390
                   const char *URL, const char *encoding, int options)
5391
0
{
5392
0
    xmlParserInputBufferPtr input;
5393
5394
0
    if (reader == NULL)
5395
0
        return (-1);
5396
0
    if (buffer == NULL)
5397
0
        return (-1);
5398
5399
0
    input = xmlParserInputBufferCreateMem(buffer, size,
5400
0
                                      XML_CHAR_ENCODING_NONE);
5401
0
    if (input == NULL) {
5402
0
        return (-1);
5403
0
    }
5404
0
    return (xmlTextReaderSetup(reader, input, URL, encoding, options));
5405
0
}
5406
5407
/**
5408
 * xmlReaderNewFd:
5409
 * @reader:  an XML reader
5410
 * @fd:  an open file descriptor
5411
 * @URL:  the base URL to use for the document
5412
 * @encoding:  the document encoding, or NULL
5413
 * @options:  a combination of xmlParserOption
5414
 *
5415
 * Setup an xmltextReader to parse an XML from a file descriptor.
5416
 * NOTE that the file descriptor will not be closed when the
5417
 *      reader is closed or reset.
5418
 * The parsing flags @options are a combination of xmlParserOption.
5419
 * This reuses the existing @reader xmlTextReader.
5420
 *
5421
 * Returns 0 in case of success and -1 in case of error
5422
 */
5423
int
5424
xmlReaderNewFd(xmlTextReaderPtr reader, int fd,
5425
               const char *URL, const char *encoding, int options)
5426
0
{
5427
0
    xmlParserInputBufferPtr input;
5428
5429
0
    if (fd < 0)
5430
0
        return (-1);
5431
0
    if (reader == NULL)
5432
0
        return (-1);
5433
5434
0
    input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
5435
0
    if (input == NULL)
5436
0
        return (-1);
5437
0
    input->closecallback = NULL;
5438
0
    return (xmlTextReaderSetup(reader, input, URL, encoding, options));
5439
0
}
5440
5441
/**
5442
 * xmlReaderNewIO:
5443
 * @reader:  an XML reader
5444
 * @ioread:  an I/O read function
5445
 * @ioclose:  an I/O close function
5446
 * @ioctx:  an I/O handler
5447
 * @URL:  the base URL to use for the document
5448
 * @encoding:  the document encoding, or NULL
5449
 * @options:  a combination of xmlParserOption
5450
 *
5451
 * Setup an xmltextReader to parse an XML document from I/O functions
5452
 * and source.
5453
 * The parsing flags @options are a combination of xmlParserOption.
5454
 * This reuses the existing @reader xmlTextReader.
5455
 *
5456
 * Returns 0 in case of success and -1 in case of error
5457
 */
5458
int
5459
xmlReaderNewIO(xmlTextReaderPtr reader, xmlInputReadCallback ioread,
5460
               xmlInputCloseCallback ioclose, void *ioctx,
5461
               const char *URL, const char *encoding, int options)
5462
0
{
5463
0
    xmlParserInputBufferPtr input;
5464
5465
0
    if (ioread == NULL)
5466
0
        return (-1);
5467
0
    if (reader == NULL)
5468
0
        return (-1);
5469
5470
0
    input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
5471
0
                                         XML_CHAR_ENCODING_NONE);
5472
0
    if (input == NULL) {
5473
0
        if (ioclose != NULL)
5474
0
            ioclose(ioctx);
5475
0
        return (-1);
5476
0
    }
5477
0
    return (xmlTextReaderSetup(reader, input, URL, encoding, options));
5478
0
}
5479
5480
#endif /* LIBXML_READER_ENABLED */