Ejemplo n.º 1
0
bool
ZXmlParser::GetHeader( LPCTSTR header, LPCTSTR name, CString& res )
{
   m_LastError = m_ok;

   IXMLDOMNodePtr pChild;                  // working node
   res.Empty( );

   pChild = m_plDomDocument;
   pChild = m_plDomDocument->firstChild;   // start at first document child

   if ( pChild == NULL )
   {
      m_LastError = "ZXmlParser::Get_Header( ) : The XML Document is a null pointer";
      return( false );
   }

   while( pChild )
   {
      if ( pChild->nodeType != NODE_PROCESSING_INSTRUCTION )
         break;

      BSTR bstr;
      CString cstr;

      pChild->get_baseName( &bstr );
      cstr = bstr;
      if ( cstr == header )
      {
         // Correct header, check the correct property
         pChild->get_text( &bstr );
         cstr = bstr;

         int index = cstr.Find( name, 0 );
         if ( index == -1 )
            return( false );

         int start, end;
         start = cstr.Find( '"', index );
         if ( start == -1 )
         {
            m_LastError = "ZXmlParser::Get_Header( ) : bad value structure";
            return( false );
         }

         end = cstr.Find( '"', start + 1 );
         if ( end == -1 )
         {
            m_LastError = "ZXmlParser::Get_Header( ) : bad value structure";
            return( false );
         }

         res = cstr.Mid( start + 1, end - start - 1 );
         return( true );
      }

      pChild = pChild->nextSibling;   // next Processing instruction node
   }

   return( false );
}
Ejemplo n.º 2
0
// ***************************
// ** Header XML Management **
// ***************************
bool
ZXmlParser::SetHeader( LPCTSTR header, LPCTSTR name, LPCTSTR value )
{
   m_LastError = m_ok;
   bool bEmptyXml = false;

   CString strHeader = header;
   CString strName = name;
   CString strValue = value;

   BSTR bstr;
   CString cstr, cstr2;

   IXMLDOMNodePtr pChild = NULL;           // Working node
   pChild = m_plDomDocument->firstChild;   // Start at first document child

   if ( pChild == NULL )
      bEmptyXml = true;

   while ( pChild )
   {
      if ( pChild == m_pDocRoot )
      {
         // Root document reach, it's done, the processing node wasn't found
         break;
      }

      if ( pChild->nodeType != NODE_PROCESSING_INSTRUCTION )
      {
         pChild = pChild->nextSibling;    // Go to Next Processing instruction node
         continue;
      }


      pChild->get_baseName( &bstr );
      cstr = bstr;
      if ( cstr == header )
      {
         // Correct header, check the correct property
         pChild->get_text( &bstr );
         cstr = bstr;

         int index = cstr.Find( name, 0 );
         if ( index == -1 )
         {
            // The property doesn't exist on this processing instruction" );

            // Assume correct constraint about "xml" processing instruction
            {
               // must have version="xxx" in first
               // must have standalone="xxx" in last if exist
               cstr2.Empty( );
               int standalone_index = cstr.Find( "standalone", 0 );
               if ( standalone_index != -1 )
               {
                  cstr2 = cstr.Right(  cstr.GetLength( ) - standalone_index + 1 );
                  cstr  = cstr.Left( standalone_index );
               }

               int version_index = cstr.Find( "version", 0 );
               if ( version_index == -1 && strHeader == "xml" )
               {
                  CString strTemp = cstr;
                  cstr = "version=\"1.0\" ";
                  cstr += strTemp;
               }

               if ( strName != "version" )
                  cstr += " " + strName + "=\"" + strValue + "\" " + cstr2;
               else
                  cstr += cstr2;
            }

            // Create the new Processing Instruction node
            HRESULT hr;
            IXMLDOMProcessingInstruction *pIXMLDOMPInstr = NULL;
            hr = m_plDomDocument->raw_createProcessingInstruction( _bstr_t( strHeader ), _bstr_t( cstr ), &pIXMLDOMPInstr );

            if ( SUCCEEDED( hr ) )
            {
               // We succes the creation of the processing instruction
               // Replace the node
               m_plDomDocument->replaceChild( pIXMLDOMPInstr, pChild );
            }
            else
            {
               // Arf, i fails the creation, grrr, again
               m_LastError="ZXmlParser::SetHeader( ) : Can't create the new processing instruction node";
               return( false );
            }
            return( true );
         }
         else
         {
            // The processing instruction node exists, must change it's value!!
            int start, end;
            start = cstr.Find( '"', index );
            if ( start == -1 )
            {
               m_LastError = "ZXmlParser::SetHeader( ) : bad value structure";
               return( false );
            }

            end = cstr.Find( '"', start + 1 );
            if ( end == -1 )
            {
               m_LastError = "ZXmlParser::SetHeader( ) : bad value structure";
               return( false );
            }

            cstr2 = cstr.Mid( 0, start + 1 ) + value + cstr.Mid( end, cstr.GetLength( ) - end );

            IXMLDOMNodePtr m_lpNode = NULL;
            IXMLDOMProcessingInstruction *pIXMLDOMPInstr = NULL;
            HRESULT hr;

            hr = m_plDomDocument->raw_createProcessingInstruction( _bstr_t( strHeader ), _bstr_t( cstr2 ), &pIXMLDOMPInstr );

            if ( SUCCEEDED( hr ) )
            {
               // We succeeded in the creation of the processing instruction
               // Replace the node
               m_plDomDocument->replaceChild( pIXMLDOMPInstr, pChild );
            }
            else
            {
               m_LastError="ZXmlParser::SetHeader( ) : Can't create the new processing instruction node";
               return( false );
            }

            return( true );
        }
     }

     pChild = pChild->nextSibling;   // next processing instruction node
   }

   // No processing node for our header
   {
      if ( strName != "version" && strHeader == "xml" )
         cstr = "version=\"1.0\" " + strName + "=\"" + strValue + "\"";
      else
         cstr = strName + "=\"" + strValue + "\"";

      IXMLDOMNodePtr m_lpNode = NULL;
      IXMLDOMProcessingInstruction *pIXMLDOMPInstr = NULL;
      HRESULT hr;

      hr = m_plDomDocument->raw_createProcessingInstruction( _bstr_t( strHeader ), _bstr_t( cstr ), &pIXMLDOMPInstr );
      if ( SUCCEEDED( hr ) )
      {
         if ( !bEmptyXml )
         {
            _variant_t NodeRef = (IUnknown *) m_pDocRoot;
            m_lpNode = NULL;
            m_lpNode = m_plDomDocument->insertBefore( pIXMLDOMPInstr, NodeRef );
            if ( m_lpNode == NULL )
               m_LastError = "PARSER_XML::SetHeader( ) : Can't insert Processing node after the root document";

            return( m_lpNode != NULL );
         }
         else
         {
            m_lpNode = NULL;
            m_lpNode = m_plDomDocument->appendChild( pIXMLDOMPInstr );
            if ( m_lpNode == NULL )
               m_LastError = "PARSER_XML::SetHeader( ) : Can't insert Processing node in the empty document";

            return( m_lpNode != NULL );
         }
      }

      m_LastError = "PARSER_XML::SetHeader( ) : Can't create new Processing node";
      return( false );
   }
}