- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Utils/Io/Xml/Dom/XMLSaver.cpp

    00001
    00002 //
    00003 // Copyright (c) 2006           Model-based Embedded and Robotic Systems Group
    00004 // All Rights Reserved          Massachusetts Institute of Technology
    00005 //
    00006 // This software is provided as is. Model-based Embedded and Robotic Systems
    00007 // (MERS) group does not assume any responsibility.
    00008 //
    00010
    00011 #include "XMLSaver.h"
    00012 #include "DOMUtils.h"
    00013 #include <Mers/Utils/Io/Xml/ScopedTranscoder.h>
    00014 #include <xercesc/dom/DOM.hpp>
    00015 #include <xercesc/framework/LocalFileFormatTarget.hpp>
    00016
    00018
    00019 using namespace Mers::Utils::Io::Xml::Dom;
    00020 using Mers::Utils::Io::Xml::CScopedTranscoder;
    00021 using XERCES_CPP_NAMESPACE::DOMDocument;
    00022 using XERCES_CPP_NAMESPACE::XMLException;
    00023 using XERCES_CPP_NAMESPACE::DOMException;
    00024 using XERCES_CPP_NAMESPACE::DOMWriter;
    00025 using XERCES_CPP_NAMESPACE::DOMImplementation;
    00026 using XERCES_CPP_NAMESPACE::LocalFileFormatTarget;
    00027
    00029
    00030 Logger XMLSaver::s_logger
    00031 ( Logger::getInstance( "Mers.Utils.Io.Xml.Dom.XMLSaver" ) );
    00032
    00034
    00035 int XMLSaver::save( const char * fileName, DOMDocument * document )
    00036 {
    00037         // Catch any exceptions.
    00038         try {
    00039                 // Open our file.
    00040                 LocalFileFormatTarget outputXMLFile( fileName );
    00041
    00042                 // Get our DOM implementation
    00043                 DOMImplementation * implementation = DOMUtils::getImplementation();
    00044
    00045                 // Get a writer implementation to use
    00046                 // Note that we may fail to call release if an exception is thrown.
    00047                 DOMWriter * writer( implementation->createDOMWriter() );
    00048
    00049                 // We will try to print in canonical format first and then at
    00050                 // least pretty-print.
    00051                 XMLScopedTranscoder sCanonicalPrint( "format-canonical" );
    00052                 XMLScopedTranscoder sPrettyPrint( "format-pretty-print" );
    00053
    00054                 // Set the canonical feature, if supported.
    00055                 if( writer->canSetFeature( sCanonicalPrint, true ) )
    00056                         writer->setFeature( sCanonicalPrint, true );
    00057                 // Otherwise set the pretty-print feature, if supported.
    00058                 else if( writer->canSetFeature( sPrettyPrint, true ) )
    00059                         writer->setFeature( sPrettyPrint, true );
    00060
    00061                 // Write our document.
    00062                 writer->writeNode( &outputXMLFile, *document );
    00063
    00064                 // Free our writer.
    00065                 writer->release();
    00066         } catch( const XMLException & toCatch ) {
    00067                 // Log this exception
    00068                 CScopedTranscoder message( toCatch.getMessage() );
    00069                 LOG4CPLUS_ERROR( s_logger, "XML Exception: " << message );
    00070                 return 1;
    00071         } catch( const DOMException & toCatch) {
    00072                 // Log this exception and return an error
    00073                 CScopedTranscoder message( toCatch.msg );
    00074                 LOG4CPLUS_ERROR( s_logger, "DOM Exception: " << message );
    00075                 return 2;
    00076         }
    00077
    00078         // All done.
    00079         return 0;
    00080 }
    00081
    00083