Teuchos_XMLInputSource.cpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include "Teuchos_XMLInputSource.hpp"
00030 #include "Teuchos_TreeBuildingXMLHandler.hpp"
00031 
00032 
00033 #ifdef HAVE_XERCES
00034 #include "XercesHandlerAdapter.h"
00035 #include "XercesInputSourceAdapter.h"
00036 #include "XercesInputStreamAdapter.h"
00037 #include <util/PlatformUtils.hpp>
00038 #endif
00039 
00040 #ifdef HAVE_EXPAT
00041 #include "Teuchos_ExpatHandlerAdapter.hpp"
00042 #define EXPAT_BUFSIZE 8192
00043 #endif
00044 
00045 
00046 using namespace Teuchos;
00047 
00048 XMLObject XMLInputSource::getObject() const
00049 {
00050 #ifdef HAVE_XERCES
00051 
00052   static bool first = true;
00053   if (first)
00054     {
00055       XMLPlatformUtils::Initialize();
00056       first = false;
00057     }
00058 
00059   SAXParser parser;
00060   XercesHandlerAdapter handler(new TreeBuildingXMLHandler());
00061   XercesInputSourceAdapter inputSource(this);
00062 
00063   parser.setDocumentHandler(&handler);
00064 
00065   try 
00066     {
00067       parser.parse(inputSource);
00068     }
00069   catch(exception& e)
00070     {
00071       TEST_FOR_EXCEPTION(true, runtime_error, 
00072                          "exception detected in SAX parsing: " << e.what());
00073     }
00074   return handler.getObject();
00075 #endif
00076 
00077 #ifdef HAVE_EXPAT
00078 
00079   RefCountPtr<TreeBuildingXMLHandler> handler = rcp(new TreeBuildingXMLHandler());
00080 
00081   XML_Parser parser = XML_ParserCreate(NULL);
00082 
00083   XML_SetElementHandler(parser, expatStartElementHandler, 
00084                         expatEndElementHandler);
00085 
00086   XML_SetCharacterDataHandler(parser, expatCharacterDataHandler);
00087 
00088   XML_SetUserData(parser, (void*) &(*handler));
00089 
00090   RefCountPtr<XMLInputStream> s = stream();
00091 
00092   bool done = false;
00093   unsigned int bufsize = EXPAT_BUFSIZE;
00094   unsigned char buf[EXPAT_BUFSIZE];
00095 
00096   while (!done)
00097     {
00098       unsigned int nRead = s->readBytes(buf, bufsize);
00099       if (nRead < bufsize) 
00100         {
00101           done = true;
00102         }
00103       XML_Parse(parser, (char*) buf, bufsize, done);
00104     }
00105 
00106   return handler->getObject();
00107 
00108 #else
00109   TEST_FOR_EXCEPTION(true, logic_error, "XMLInputSource::getObject() - no XML parser installed");
00110   return XMLObject(); // -Wall
00111 #endif
00112 
00113 }
00114       
00115       

Generated on Thu Sep 18 12:42:50 2008 for Teuchos - Trilinos Tools Package by doxygen 1.3.9.1