Salome HOME
Updated copyright comment
[modules/yacs.git] / src / yacsloader / xmlParserBase.cxx
index 8f85f1c7c845b1585a84dd52630f09ef9ce167e6..c888462ac8d85eb7f2a9f0457a7ba647d198c219 100644 (file)
@@ -1,35 +1,34 @@
-//  Copyright (C) 2006-2008  CEA/DEN, EDF R&D
+// Copyright (C) 2006-2024  CEA, EDF
 //
-//  This library is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU Lesser General Public
-//  License as published by the Free Software Foundation; either
-//  version 2.1 of the License.
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
 //
-//  This library is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-//  Lesser General Public License for more details.
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
 //
-//  You should have received a copy of the GNU Lesser General Public
-//  License along with this library; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 #include "xmlParserBase.hxx"
 #include "Exception.hxx"
 
 #include <stdexcept>
 #include <iostream>
 #include <cstdarg>
+#include <cstdio>
 #include <cassert>
 
 //#define _DEVDEBUG_
 #include "YacsTrace.hxx"
 
-// --- specific part for libxml2 ----------------------------------------------
-
-#ifdef USE_LIBXML2
 extern "C"
 {
 #include <libxml/parserInternals.h> // for xmlCreateFileParserCtxt
@@ -40,13 +39,6 @@ void xmlParserBase::XML_SetUserData(_xmlParserCtxt* ctxt,
 {
   ctxt->userData = parser;
 }
-#endif
-
-// --- specific part for expat ------------------------------------------------
-
-#ifdef USE_EXPAT
-XML_Parser xmlParserBase::_xmlParser;
-#endif
 
 // --- generic part -----------------------------------------------------------
 
@@ -74,7 +66,7 @@ void XMLCALL xmlParserBase::end_document  (void* userData)
   xmlParserBase *currentParser = static_cast<xmlParserBase *> (userData);
 }
 
-/*! callback called on start of an xml element: <name>
+/*! callback called on start of an xml element: \verbatim <name> \endverbatim
  */
 
 void XMLCALL xmlParserBase::start_element (void* userData,
@@ -90,7 +82,7 @@ void XMLCALL xmlParserBase::start_element (void* userData,
 }
 
 
-/*! callback called on end of an xml element: </name>
+/*! callback called on end of an xml element: \verbatim </name> \endverbatim
  */
 
 void XMLCALL xmlParserBase::end_element   (void* userData,
@@ -108,9 +100,9 @@ void XMLCALL xmlParserBase::end_element   (void* userData,
  }
 
 
-/*! callback called for significant characters inside tags: <tag>content</tag>
- *  or outside tags, like space or new line
- *  with expat get also the CDATA tags: <tag>![CDATA[content]]></tag>
+/*! callback called for significant characters inside tags: \verbatim <tag>content</tag> \endverbatim
+ *  or outside tags, like space or new line;
+ *  get also the CDATA tags: \verbatim <tag>![CDATA[content]]></tag> \endverbatim
  */
 
 void XMLCALL xmlParserBase::characters    (void* userData,
@@ -205,7 +197,7 @@ void XMLCALL xmlParserBase::fatal_error   (void* userData,
   va_end(args);
 }
 
-/*! callback called for CDATA inside tags: <tag>![CDATA[content]]></tag>
+/*! callback called for CDATA inside tags: \verbatim <tag>![CDATA[content]]></tag> \endverbatim
  *  used only by libxml2
  */
 
@@ -327,23 +319,16 @@ void xmlParserBase::end()
 {
 }
 
-/*! Throws an exception. different implementation with libxml2 and expat
+/*! Throws an exception.
  */
 
 void xmlParserBase::stopParse(std::string what)
 {
-#ifdef USE_LIBXML2
   xmlStopParser(_xmlParser);
-#endif
-#ifdef USE_EXPAT
-  throw Exception(what);
-#endif  
 }
 
 // ----------------------------------------------------------------------------
 
-#ifdef USE_LIBXML2
-
 /*! libxml2 parser initialisation
  * \param parser dedicated parser
  */
@@ -409,92 +394,15 @@ void xmlReader::parse(std::string xmlFile)
 
   xmlParseDocument(saxContext);
   _rootParser->cleanGarbage();
-  xmlFileClose(saxContext);
-  xmlFreeParserCtxt(saxContext);
+  if ( saxContext->myDoc != NULL ) {
+    xmlFreeDoc( saxContext->myDoc );
+    saxContext->myDoc = NULL;
+  }
+  if ( saxContext != NULL ) {
+    saxContext->sax = old_sax;
+    xmlFreeParserCtxt( saxContext );
+  }
   DEBTRACE("xmlParserBase::end of parse, garbage size = " << _rootParser->getGarbageSize());
 }
-#endif
-
-// ----------------------------------------------------------------------------
-
-#ifdef USE_EXPAT
-
-#define SIZEBUF        8192
-char Buffer[SIZEBUF];
-
-/*! expat parser initialisation
- * \param parser dedicated parser
- */
-
-xmlReader::xmlReader(xmlParserBase* parser): _rootParser(parser)
-{
-  xmlParserBase::_xmlParser= XML_ParserCreate(NULL);
-  if (! _rootParser ) 
-    {
-      cerr << "Couldn't allocate memory for parser" << endl;
-      throw Exception("Couldn't allocate memory for parser");
-    }
-}
-
-/*! expat parse
- * \param xmlFile file to parse
- */
-
-void xmlReader::parse(std::string xmlFile)
-{
-  FILE* fin=fopen(xmlFile.c_str(),"r");
-  if (! fin) 
-    {
-      std::cerr << "Couldn't open schema file" << std::endl;
-      throw std::invalid_argument("Couldn't open schema file");
-      //throw Exception("Couldn't open schema file");
-    }
-
-  XML_SetElementHandler(xmlParserBase::_xmlParser,
-                        xmlParserBase::start_element,
-                        xmlParserBase::end_element);
-  XML_SetCharacterDataHandler(xmlParserBase::_xmlParser,
-                              xmlParserBase::characters );
-  XML_SetUserData(xmlParserBase::_xmlParser, _rootParser);
-  _rootParser->init(0);
-  _rootParser->_stackParser.push(_rootParser);
-
-  try
-    {
-      for (;;) 
-        {
-          int done;
-          int len;
-
-          len = fread(Buffer, 1, SIZEBUF, fin);
-          if (ferror(fin)) 
-            {
-              std::cerr << "Read error" << std::endl;
-              throw Exception("Read error");
-            }
-          done = feof(fin);
-
-          if (XML_Parse(xmlParserBase::_xmlParser, Buffer, len, done) == XML_STATUS_ERROR) 
-            {
-              throw Exception(XML_ErrorString(XML_GetErrorCode(xmlParserBase::_xmlParser)));
-            }
-
-          if (done)
-            break;
-        }
-      XML_ParserFree (xmlParserBase::_xmlParser);
-      xmlParserBase::_xmlParser=0;
-      _rootParser->cleanGarbage();
-      DEBTRACE("xmlParserBase::end of parse, garbage size = " << _rootParser->getGarbageSize());
-    }
-  catch(Exception& e)
-    {
-      _rootParser->cleanGarbage();
-      //get line number from XML parser
-      cerr << "Error at line: " << XML_GetCurrentLineNumber(xmlParserBase::_xmlParser) << endl;
-      throw e;
-    }
-}
-#endif
 
 // ----------------------------------------------------------------------------