// @(#)root/xmlparser:$Id$
// Author: Jose Lo   12/4/2005

/*************************************************************************
 * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TDomParser                                                           //
//                                                                      //
// DOM stands for the Document Object Model; this is an API for         //
// accessing XML or HTML structured documents.                          //
// The Document Object Model is a platform and language-neutral         //
// interface that will allow programs and scripts to dynamically        //
// access and update the content, structure and style of documents.     //
//                                                                      //
// The parser returns a tree built during the document analysis.        //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include "TDOMParser.h"
#include "TXMLDocument.h"

#include <libxml/tree.h>
#include <libxml/parserInternals.h>


ClassImp(TDOMParser);

//______________________________________________________________________________
TDOMParser::TDOMParser() : fTXMLDoc(0)
{
   // TDOMParser constructor
}

//______________________________________________________________________________
TDOMParser::~TDOMParser()
{
   // TDOMParser destructor, it calls ReleaseUnderlying().

   ReleaseUnderlying();
}

//______________________________________________________________________________
void TDOMParser::ReleaseUnderlying()
{
   // Release any existing document.

   if (fTXMLDoc) {
      delete fTXMLDoc;
      fTXMLDoc = 0;
   }

   SetParseCode(0);

   TXMLParser::ReleaseUnderlying();
}

//______________________________________________________________________________
Int_t TDOMParser::ParseFile(const char *filename)
{
   // Parse the XML file where filename is the XML file name.
   // It will create a TXMLDocument if the file is parsed without
   // any error. It returns parse code error in case of parse error,
   // see TXMLParser.

   ReleaseUnderlying();

   fContext = xmlCreateFileParserCtxt(filename);

   if (!fContext) {
      SetParseCode(-2);
      return -2;
   }

   InitializeContext();

   if (!fContext->directory) {
      const char *dir = xmlParserGetDirectory(filename);
      fContext->directory = (char *)xmlStrdup((const xmlChar *)dir);
   }

   return ParseContext();
}

//______________________________________________________________________________
Int_t TDOMParser::ParseBuffer(const char *buffer, Int_t len)
{
   // It parses a buffer, much like ParseFile().

   ReleaseUnderlying();

   fContext = xmlCreateMemoryParserCtxt(buffer, len);

   if (!fContext) {
      SetParseCode(-2);
      return -2;
   }

   InitializeContext();

   return ParseContext();
}

//______________________________________________________________________________
Int_t TDOMParser::ParseContext()
{
   // Creates a XML document for the parser.
   // It returns 0 on success, and
   // -1 if no XML document was created,
   // -5 if the document is not well formated,
   // -6 if document is not valid.

   xmlParseDocument(fContext);

   if (!fContext->myDoc) {
      SetParseCode(-1);
      return -1;
   }

   if (!fContext->wellFormed) {
      SetParseCode(-5);
      return -5;
   }

   if (!fContext->valid) {
      SetParseCode(-6);
      return -6;
   }

   fTXMLDoc = new TXMLDocument(fContext->myDoc);

   return 0;
}

//______________________________________________________________________________
TXMLDocument *TDOMParser::GetXMLDocument() const
{
   // Returns the TXMLDocument.

   return fTXMLDoc;
}
 TDOMParser.cxx:1
 TDOMParser.cxx:2
 TDOMParser.cxx:3
 TDOMParser.cxx:4
 TDOMParser.cxx:5
 TDOMParser.cxx:6
 TDOMParser.cxx:7
 TDOMParser.cxx:8
 TDOMParser.cxx:9
 TDOMParser.cxx:10
 TDOMParser.cxx:11
 TDOMParser.cxx:12
 TDOMParser.cxx:13
 TDOMParser.cxx:14
 TDOMParser.cxx:15
 TDOMParser.cxx:16
 TDOMParser.cxx:17
 TDOMParser.cxx:18
 TDOMParser.cxx:19
 TDOMParser.cxx:20
 TDOMParser.cxx:21
 TDOMParser.cxx:22
 TDOMParser.cxx:23
 TDOMParser.cxx:24
 TDOMParser.cxx:25
 TDOMParser.cxx:26
 TDOMParser.cxx:27
 TDOMParser.cxx:28
 TDOMParser.cxx:29
 TDOMParser.cxx:30
 TDOMParser.cxx:31
 TDOMParser.cxx:32
 TDOMParser.cxx:33
 TDOMParser.cxx:34
 TDOMParser.cxx:35
 TDOMParser.cxx:36
 TDOMParser.cxx:37
 TDOMParser.cxx:38
 TDOMParser.cxx:39
 TDOMParser.cxx:40
 TDOMParser.cxx:41
 TDOMParser.cxx:42
 TDOMParser.cxx:43
 TDOMParser.cxx:44
 TDOMParser.cxx:45
 TDOMParser.cxx:46
 TDOMParser.cxx:47
 TDOMParser.cxx:48
 TDOMParser.cxx:49
 TDOMParser.cxx:50
 TDOMParser.cxx:51
 TDOMParser.cxx:52
 TDOMParser.cxx:53
 TDOMParser.cxx:54
 TDOMParser.cxx:55
 TDOMParser.cxx:56
 TDOMParser.cxx:57
 TDOMParser.cxx:58
 TDOMParser.cxx:59
 TDOMParser.cxx:60
 TDOMParser.cxx:61
 TDOMParser.cxx:62
 TDOMParser.cxx:63
 TDOMParser.cxx:64
 TDOMParser.cxx:65
 TDOMParser.cxx:66
 TDOMParser.cxx:67
 TDOMParser.cxx:68
 TDOMParser.cxx:69
 TDOMParser.cxx:70
 TDOMParser.cxx:71
 TDOMParser.cxx:72
 TDOMParser.cxx:73
 TDOMParser.cxx:74
 TDOMParser.cxx:75
 TDOMParser.cxx:76
 TDOMParser.cxx:77
 TDOMParser.cxx:78
 TDOMParser.cxx:79
 TDOMParser.cxx:80
 TDOMParser.cxx:81
 TDOMParser.cxx:82
 TDOMParser.cxx:83
 TDOMParser.cxx:84
 TDOMParser.cxx:85
 TDOMParser.cxx:86
 TDOMParser.cxx:87
 TDOMParser.cxx:88
 TDOMParser.cxx:89
 TDOMParser.cxx:90
 TDOMParser.cxx:91
 TDOMParser.cxx:92
 TDOMParser.cxx:93
 TDOMParser.cxx:94
 TDOMParser.cxx:95
 TDOMParser.cxx:96
 TDOMParser.cxx:97
 TDOMParser.cxx:98
 TDOMParser.cxx:99
 TDOMParser.cxx:100
 TDOMParser.cxx:101
 TDOMParser.cxx:102
 TDOMParser.cxx:103
 TDOMParser.cxx:104
 TDOMParser.cxx:105
 TDOMParser.cxx:106
 TDOMParser.cxx:107
 TDOMParser.cxx:108
 TDOMParser.cxx:109
 TDOMParser.cxx:110
 TDOMParser.cxx:111
 TDOMParser.cxx:112
 TDOMParser.cxx:113
 TDOMParser.cxx:114
 TDOMParser.cxx:115
 TDOMParser.cxx:116
 TDOMParser.cxx:117
 TDOMParser.cxx:118
 TDOMParser.cxx:119
 TDOMParser.cxx:120
 TDOMParser.cxx:121
 TDOMParser.cxx:122
 TDOMParser.cxx:123
 TDOMParser.cxx:124
 TDOMParser.cxx:125
 TDOMParser.cxx:126
 TDOMParser.cxx:127
 TDOMParser.cxx:128
 TDOMParser.cxx:129
 TDOMParser.cxx:130
 TDOMParser.cxx:131
 TDOMParser.cxx:132
 TDOMParser.cxx:133
 TDOMParser.cxx:134
 TDOMParser.cxx:135
 TDOMParser.cxx:136
 TDOMParser.cxx:137
 TDOMParser.cxx:138
 TDOMParser.cxx:139
 TDOMParser.cxx:140
 TDOMParser.cxx:141
 TDOMParser.cxx:142
 TDOMParser.cxx:143
 TDOMParser.cxx:144
 TDOMParser.cxx:145
 TDOMParser.cxx:146
 TDOMParser.cxx:147