Example #1
0
   HRESULT GetElementById(const CString& cszId, CComPtr<IHTMLElement>& spElement)
   {
      CComPtr<IHTMLDocument2> spIHTMLDocument2;
      HRESULT hr = GetHtmlDocument(spIHTMLDocument2);
      if (FAILED(hr))
         return hr;

      CComPtr<IHTMLElementCollection> spIHTMLElementCollection;
      hr = spIHTMLDocument2->get_all(&spIHTMLElementCollection);
      if (FAILED(hr))
         return hr;

      CComBSTR bstrName(cszId);
      CComVariant varName(bstrName);
      CComVariant varIndex(static_cast<int>(0));

      CComPtr<IDispatch> spIDispatchIHTMLElement;
      hr = spIHTMLElementCollection->item(varName, varIndex, &spIDispatchIHTMLElement);
      if (FAILED(hr))
         return hr;

      if (spIDispatchIHTMLElement == NULL)
         return E_FAIL;

      return spIDispatchIHTMLElement.QueryInterface<IHTMLElement>(&spElement);
   }
HRESULT	CSellerItemPageAnalyze::ParseItemPage(CComPtr<IHTMLDocument3>& spHtmlDoc, 
											const CString& strSearchItemName)
{
	CHECK_POINTER_RETURN_VALUE_IF_FAIL(spHtmlDoc, E_POINTER);
	HRESULT hr = E_FAIL;


	//获取所有的table
	CComPtr<IHTMLElementCollection> spTables;
	COM_VERIFY(spHtmlDoc->getElementsByTagName(CComBSTR(TEXT("table")), &spTables));
	if (spTables)
	{
		long nLength = 0;
		COM_VERIFY(spTables->get_length(&nLength));
		FTLASSERT(nLength == 1);

		for (long nIndex = 0; nIndex < nLength; ++nIndex)
		{
			CComPtr<IDispatch>	spDispTable;
			CComVariant varIndex(nIndex);
			COM_VERIFY(spTables->item(varIndex, varIndex, &spDispTable));
			CComQIPtr<IHTMLTable> spTable = spDispTable;
			if (spTable)
			{
				COM_VERIFY(_InnerParseTable(spTable, TEXT("TODO:"), strSearchItemName));
			}
		}
	}

	return hr;
}
HRESULT CScriptErrHandler::WalkAllChildPages(CComPtr<IHTMLDocument2> &parentDoc)
{
	HRESULT hr = E_FAIL;
	CComPtr<IHTMLFramesCollection2> spFramesCol;
	COM_VERIFY(parentDoc->get_frames(&spFramesCol));
	if(SUCCEEDED(hr) && spFramesCol != NULL)
	{
		long lSize = 0;
		COM_VERIFY(spFramesCol->get_length(&lSize));
		for(long i=0; i<lSize; i++)
		{
			CComVariant varIndex(i);
			CComVariant varItem;
			COM_VERIFY(spFramesCol->item(&varIndex, &varItem));
			if(varItem.pdispVal != NULL)
			{
				CComQIPtr<IHTMLWindow2> spChildWindow = varItem.pdispVal;
				if (spChildWindow)
				{
					CComPtr<IHTMLDocument2> spChildDocument;
					COM_VERIFY(spChildWindow->get_document(&spChildDocument));
					if(spChildDocument)
					{
						COM_VERIFY(ShieldCurrPage(spChildDocument));
						COM_VERIFY(WalkAllChildPages(spChildDocument));
					}
				}
			}
		}
	}
	return hr;
}
HRESULT CItemPageAnalyzeBase::_InnerParseTable(CComPtr<IHTMLTable>& spTable, const CString& strTableName, const CString& strSearchItemName)
{
	HRESULT hr = E_FAIL;
	
	//CFHTMLTableDumper	tableDumper(spTable, CFOutputWindowInfoOutput::Instance(), 0);
	CComPtr<IHTMLElementCollection>	spRows;
	COM_VERIFY(spTable->get_rows(&spRows));
	if (spRows)
	{
		long nRows = 0;
		COM_VERIFY(spRows->get_length(&nRows));

		//nRows = FTL_MIN(2, nRows);

		//跳过第一行 -- 是表头,然后分析后面的价格数据
		for (long nIndex = 1; nIndex < nRows; ++nIndex)
		{
			CComPtr<IDispatch>	spDispItem;
			CComVariant varIndex(nIndex);
			COM_VERIFY(spRows->item(varIndex, varIndex, &spDispItem));
			CComQIPtr<IHTMLElement> spPriceElementItem = spDispItem;
			if (spPriceElementItem)
			{
#if 0
				//启用这行代码,可以打印出所有的价格列表信息,方便在网页更改后分析
				CFHTMLElementDumper htmlElementDumper(spPriceElementItem, CFOutputWindowInfoOutput::Instance(), 
					0, nIndex);
#endif
				//每一行的价格
				COM_VERIFY(_InnerParseRowItem(strTableName, nIndex, spPriceElementItem, strSearchItemName));
			}
		}
	}
	return hr;
}
CString CItemPageAnalyzeBase::_GetCollectionItemValue(CComQIPtr<IHTMLElementCollection>& spChildElements, 
													  INT index, ItemValueType valueType, BOOL bTrim /* = FALSE */)
{
	HRESULT hr = E_FAIL;

	CString strResult;
	CComVariant varIndex(index);
	CComPtr<IDispatch> spDispValue;

	COM_VERIFY(spChildElements->item(varIndex, varIndex, &spDispValue));
	CComQIPtr<IHTMLElement>	spElement(spDispValue);
	if (spElement)
	{
		CComBSTR bstrValue;

		switch (valueType)
		{
		case ivtInnerText:
			COM_VERIFY(spElement->get_innerText(&bstrValue));
			strResult = OLE2CT(bstrValue);
			break;
		case ivtToString:
			COM_VERIFY(spElement->toString(&bstrValue));
			strResult = OLE2CT(bstrValue);
			break;
		default:
			FTLASSERT(FALSE);
			break;
		}
	}
	if (bTrim)
	{
		strResult.Trim();
	}
	return strResult;
}
CVariableMapping *CEquSystem::solveExtraEquations()
{
	static int hhh; ++hhh;
	CVariableMapping *pVarValue = NULL;
	// Loop over all constructed equations

	auto iMax = equNumb();
	size_t i = 0;
	while (i < iMax) {
		CEquation *pEquation = equation(i);
		if (pEquation->numbVar() != 1) {
			i++;
			continue;
		}

		// We found trivial equation
		const auto pVariable = pEquation->firstVariable();
		const auto varValue = pEquation->rightPart();

		if (!pVarValue)
			pVarValue = new CVariableMapping(iMax - i);

		pVarValue->addMapping(pVariable->varIndex(), varValue);

		// Get next equation with the current variable
		// (we need to do it BEFORE releasing this variable)
		auto pVariableNext = pVariable->sameVarNextEqu();
		// Release current equation and copy last equation of the system to the current slot
		pEquation->releaseEquation();
		if (i < --iMax)
			setEquation(equation(iMax), i);

		CVariable *pVariableTmp, *pVarTmp, *pTmp;
		while (pVariableNext != pVariable) {
			pVariableNext = (pVariableTmp = pVariableNext)->sameVarNextEqu();

			CEquation *pCurrEquation = equation(pVariableTmp);
			if (pCurrEquation->rightPart() < varValue) {
				delete pVarValue;
				return NULL;    // no solution
			}

			auto *pVarNext = pVariableTmp->nextVarSameEqu();
			if (pCurrEquation->rightPart() == varValue) {
				// The values of all remaining variables of current equation are 0's
				while (pVarNext != pVariableTmp) {
					pVarNext = (pVarTmp = pVarNext)->nextVarSameEqu();
					// Map variable with it's value 0
					pVarValue->addMapping(pVarTmp->varIndex(), 0);
					// ... and remove this variable from remaining equations
					auto *pNxt = pVarTmp->sameVarNextEqu();
					while (pNxt != pVarTmp) {
						pNxt = (pTmp = pNxt)->sameVarNextEqu();
						pTmp->prevVarSameEqu()->linkVariable(pTmp->nextVarSameEqu());
						delete pTmp;
					}
					delete pVarTmp;
				}

				// Current equation already solved and we need to delete it and move the last equation to its place
				setEquation(equation(--iMax), pCurrEquation);
			}
			else {
				if (pVarNext == pVariableTmp) {
					delete pVarValue;
					return NULL;	// Only one variable in this equation
				}

				// Adjust right part
				pCurrEquation->adjustRightPart(varValue);
				// Remove variable
				pVariableTmp->prevVarSameEqu()->linkVariable(pVarNext);
				delete pVariableTmp;
			}
		}
	}

	setEquNumb(iMax);
	return pVarValue;
}