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

# /Users/fpy/titan-1-5-MBARI/Mers/Models/Base/Io/Xml/Sax/BMAssignmentCH.cpp

    00001
    00002 //
    00003 // Copyright (c) 2005           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 "BMAssignmentCH.h"
    00012 #include <Mers/Utils/Io/Xml/Sax/StringCH.h>
    00013 #include <Mers/Models/Base/BaseModel.h>
    00014 #include <Mers/Models/Base/BaseModelAssignment.h>
    00015 #include <Mers/Models/Base/BaseModelValue.h>
    00016 #include <Mers/Models/Base/BaseModelVariable.h>
    00017 #include <Mers/Utils/Exceptions/RuntimeException.h>
    00018 #include <xercesc/util/XMLString.hpp>
    00019 #include <xercesc/sax/Locator.hpp>
    00020 #include <xercesc/sax2/SAX2XMLReader.hpp>
    00021
    00023
    00024 using namespace Mers::Models::Base::Io::Xml::Sax;
    00025 using Mers::Utils::Io::Xml::Sax::StringCH;
    00026 using Mers::Utils::Io::Xml::Sax::ContentCallback;
    00027 using Mers::Utils::Io::Xml::Sax::TypedCH;
    00028 using Mers::Models::Base::BaseModel;
    00029 using Mers::Models::Base::BaseModelAssignment;
    00030 using Mers::Models::Base::BaseModelValue;
    00031 using Mers::Models::Base::BaseModelVariable;
    00032 using Mers::Utils::Lang::String;
    00033 using Mers::Utils::Exceptions::RuntimeException;
    00034 using XERCES_CPP_NAMESPACE::SAX2XMLReader;
    00035 using XERCES_CPP_NAMESPACE::Attributes;
    00036 using XERCES_CPP_NAMESPACE::XMLString;
    00037 using XERCES_CPP_NAMESPACE::Locator;
    00038
    00040
    00041 BMAssignmentCH::BMAssignmentCH( SAX2XMLReader * reader,
    00042         const BaseModel * model, ContentHandler * parent,
    00043         ContentCallback< BaseModelAssignment * > * callback )
    00044         : TypedCH<const BaseModel *>( reader, model, parent ),
    00045                 m_Variable(), m_Value(), m_Callback( callback )
    00046 {
    00047 }
    00048
    00050
    00051 void BMAssignmentCH::startElement( const XMLCh * const uri,
    00052         const XMLCh * const localname, const XMLCh * const qname,
    00053         const Attributes& attrs)
    00054 {
    00055         // Get the string version of the word we're looking for.
    00056         XMLCh * kVariable = XMLString::transcode( "variable" );
    00057         XMLCh * kValue = XMLString::transcode( "value" );
    00058
    00059         if( XMLString::equals( localname, kVariable ) ) {
    00060                 StringCH * ch = new StringCH( m_Reader, this, &m_Variable );
    00061                 m_Reader->setContentHandler( ch );
    00062         } else if( XMLString::equals( localname, kValue ) ) {
    00063                 StringCH * ch = new StringCH( m_Reader, this, &m_Value );
    00064                 m_Reader->setContentHandler( ch );
    00065         } else {
    00066                 ContentHandler::startElement( uri, localname, qname, attrs );
    00067         }
    00068
    00069         // Free our strings.
    00070         XMLString::release( &kVariable );
    00071         XMLString::release( &kValue );
    00072 }
    00073
    00075
    00076 void BMAssignmentCH::endElement(const XMLCh * const uri,
    00077         const XMLCh * const localname, const XMLCh * const qname)
    00078 {
    00079         // Get our variable.
    00080         String * varName = m_Variable.get();
    00081
    00082         if( m_Data == NULL ) {
    00083                 throw RuntimeException( "Model was NULL when loading Base Model "
    00084                         "Assignment.", __FILE__, __LINE__ );
    00085         }
    00086
    00087         if( varName == NULL ) {
    00088                 // Must specify a variable
    00089                 throw RuntimeException( (String("Variable name unspecified on line ") +
    00090                         getLineNumberString() ).c_str(), __FILE__, __LINE__ );
    00091         }
    00092
    00093         const BaseModelVariable * var = m_Data->getVariable( varName->c_str() );
    00094         if( var == NULL ) {
    00095                 String errorMessage( "Variable '" );
    00096                 errorMessage.append( *varName );
    00097                 errorMessage.append( "' not found on line " );
    00098                 errorMessage.append( getLineNumberString() );
    00099                 delete varName;
    00100                 throw RuntimeException( errorMessage, __FILE__, __LINE__ );
    00101         }
    00102
    00103         // Get our value.
    00104         String * valName = m_Value.get();
    00105
    00106         if( valName == NULL ) {
    00107                 // Must specify a value
    00108                 delete varName;
    00109                 delete var;
    00110                 throw RuntimeException( (String("Value name unspecified on line ") +
    00111                         getLineNumberString() ).c_str(), __FILE__, __LINE__ );
    00112         }
    00113
    00114         const BaseModelValue * val = var->getValue( valName->c_str() );
    00115         if( val == NULL ) {
    00116                 String errorMessage( "Value '" );
    00117                 errorMessage.append( *valName );
    00118                 errorMessage.append( "' not found on line " );
    00119                 errorMessage.append( getLineNumberString() );
    00120                 delete varName;
    00121                 delete var;
    00122                 delete valName;
    00123                 throw RuntimeException( errorMessage, __FILE__, __LINE__ );
    00124         }
    00125
    00126         // Construct our object.
    00127         BaseModelAssignment * asg = new BaseModelAssignment( var, val,
    00128                 BaseModelAssignment::Positive );
    00129
    00130         // Call our callback function.
    00131         m_Callback->handleContent( asg, localname );
    00132
    00133         // Do the default stuff.  This may delete us.
    00134         ContentHandler::endElement( uri, localname, qname );
    00135 }
    00136
    00138