Example #1
0
UINT APIENTRY CXMLDOMDocument::ParseThread(void *pParm)
{
	ATLTRACE(_T("CXMLDOMDocument::ParseThread\n"));

	CXMLDOMDocument *pThis = reinterpret_cast<CXMLDOMDocument *> (pParm);
	if (NULL == pThis)
		return 0;

	if (!pThis->m_bAbort && pThis->m_FileName.length() > 0) {
		CBindStatCallbackObj *pCallbackObj = NULL;
		HRESULT hr = CBindStatCallbackObj::CreateInstance(&pCallbackObj);
		if (S_OK != hr)
			pCallbackObj = NULL;

		if (pCallbackObj != NULL) {
			pCallbackObj->AddRef();
			pCallbackObj->m_pDoc = pThis;	
		}
		TCHAR name[MAX_PATH] = _T("");
		if (pThis->m_bAsync)
			pThis->PostMessage(MSG_READY_STATE_CHANGE,1);
		
		hr = URLDownloadToCacheFile(NULL,pThis->m_FileName,name,URLOSTRM_GETNEWESTVERSION,0,pCallbackObj);
		if (pCallbackObj != NULL)
			pCallbackObj->Release();

		if (E_ABORT == hr)
			pThis->m_bAbort = true;
		else {	
			if (S_OK != hr) {
				_bstr_t error = _T("Failed to download ") + pThis->m_FileName + _T(": ");
				_com_error comError(hr);
				error += comError.ErrorMessage();
				pThis->m_pParseError->SetData(1,pThis->m_FileName,error,_T(""),0,0,0);
				pThis->m_bParseError = true;
			}
		}

		if (S_OK == hr) {
			pThis->m_FileName = name;
			if (pThis->m_bAsync)
				pThis->PostMessage(MSG_READY_STATE_CHANGE,2);
		}
	}

	XercesDOMParser parser;

	//
	//   If set to true then an node supporting IXMLDOMProcessingInstruction
	//     is added for the XML declaration.
	//
	//   Setting to true in a custom DLL will better mimic
	//      MSXML.DLL but at a cost of conformance errors
	//      using David Brownell's suite
	//parser.setToCreateXMLDeclTypeNode(false);

	parser.setIncludeIgnorableWhitespace(pThis->m_bPreserveWhiteSpace);



	if (!pThis->m_bParseError && !pThis->m_bAbort) {
		parser.setDoValidation(pThis->m_bThreadValidate);
		//
		//   this brings the COM component into better mimicry to MSXML
		//      by not throwing a validation error when there is no DOCTYPE
		//
		parser.setValidationScheme(pThis->m_bThreadValidate ? AbstractDOMParser::Val_Auto : AbstractDOMParser::Val_Never);
	}

	if (!pThis->m_bParseError && !pThis->m_bAbort)
		parser.setErrorHandler(pThis);

	if (!pThis->m_bParseError && !pThis->m_bAbort)
		pThis->m_pParseError->Reset();
	
	if (!pThis->m_bParseError && !pThis->m_bAbort)
		pThis->m_bParseError = false;

	try
	{
		if (!pThis->m_bParseError && !pThis->m_bAbort) {
			if (pThis->m_FileName.length() > 0)
				parser.parse(static_cast<LPCTSTR> (pThis->m_FileName));
			else {
				XMLByte *pXMLByte =  reinterpret_cast<XMLByte*> (static_cast<XMLCh*>(pThis->m_xml));
				MemBufInputSource memBufIS(pXMLByte,pThis->m_xml.length()*sizeof(XMLCh),OLESTR("IBMXMLParser"),false);
				memBufIS.setEncoding(OLESTR("UTF-16LE"));
				if (!pThis->m_bParseError && !pThis->m_bAbort)
					parser.parse(memBufIS);
			}
		}

	}
	catch(...)
	{
		pThis->m_bParseError = true;
		return 0;
	}

	if (!pThis->m_bParseError && !pThis->m_bAbort)
		pThis->m_TmpDocument = parser.adoptDocument();

	if (!pThis->m_bParseError && !pThis->m_bAbort && pThis->m_bAsync)
		pThis->PostMessage(MSG_READY_STATE_CHANGE,4);
  	
	return 0;
}
// ---------------------------------------------------------------------------
int
main(int argc, const char** argv)
{
   try
     {
       XMLPlatformUtils::Initialize();
     }
   catch(const XMLException& e)
     {
       StrX tmp_e(e.getMessage());
       cerr << "Xerces initialization error: " << tmp_e.localForm() << endl;
       return 2;
     }
   // check command line for arguments
   if ( argc < 1 )
     {
       cerr << "usage: schema-check <xml-file> [<schema-file> ...]" << endl;
       return 3;
     }
   XercesDOMParser *parser = new XercesDOMParser;
   DOMTreeErrorReporter *errReporter = new DOMTreeErrorReporter();
   parser->setErrorHandler(errReporter);
   parser->setDoNamespaces(true);
   parser->setCreateEntityReferenceNodes(true);
   parser->useCachedGrammarInParse(true);
   if ( argc > 2 )
     {
       parser->setDoSchema(true);
       parser->setDoValidation(true);
       parser->setValidationSchemaFullChecking(true);
       for ( int i = 2; i < argc; i++ )
         {

           if ( parser->loadGrammar(argv[i], Grammar::SchemaGrammarType, true) == 0 )
             {
               cerr << "Error loading grammar " << std::string(argv[i]) << endl;
               return 4;
             }
         }
     }
   bool errorsOccured = true;
   try
     {
       parser->parse(argv[1]);
       errorsOccured = false;
     }
   catch ( const OutOfMemoryException& )
     {
       cerr << "Out of memory exception." << endl;
     }
   catch ( const XMLException& e )
     {
       cerr << "An error occurred during parsing" << endl
            << "    Message: " << StrX(e.getMessage()) << endl;
     }
   catch ( const DOMException& e )
     {
       const unsigned int maxChars = 2047;
       XMLCh errText[maxChars + 1];
       cerr << endl
            << "DOM Error during parsing: '" << std::string(argv[1]) << "'" << endl
            << "DOM Exception code is: " << e.code << endl;
       if ( DOMImplementation::loadDOMExceptionMsg(e.code, errText, maxChars) )
         cerr << "Message is: " << StrX(errText) << endl;
     }
   catch (...)
     {
       cerr << "An error occurred during parsing." << endl;
     }
   return errorsOccured ? 1 : 0;
}