summaryrefslogtreecommitdiff
path: root/plugins/Quotes/src/HTMLParserMS.cpp
blob: f37ddc96a47ef84dcd498ba24f61e69c0e21e34a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include "StdAfx.h"

using _com_util::CheckError;

class CHTMLNode : public IHTMLNode
{
public:
	typedef CComPtr<IDispatch> TComPtr;
	typedef CComPtr<IHTMLDocument3> TDocumentPtr;

protected:
	typedef CComPtr<IHTMLElementCollection> TElementCollectionPtr;

public:
	CHTMLNode(const TComPtr& pElement, const TDocumentPtr& pDocument)
		: m_pElement(pElement), m_pDocument(pDocument)
	{
	}

	virtual THTMLNodePtr GetElementByID(const tstring& rsID)const
	{
		if (m_pDocument) {
			CComPtr<IHTMLElement> pElement;
			if (SUCCEEDED(m_pDocument->getElementById(bstr_t(rsID.c_str()), &pElement))
				&& pElement) {
				TComPtr p(pElement);
				return THTMLNodePtr(new CHTMLNode(p, m_pDocument));
			}
		}

		return THTMLNodePtr();
	}

	virtual size_t GetChildCount()const
	{
		TElementCollectionPtr pColl = GetElementCollectionPtr();
		if (pColl) {
			LONG celem = 0;
			HRESULT hr = pColl->get_length(&celem);
			if (S_OK == hr)
				return celem;
		}

		return 0;
	}

	virtual THTMLNodePtr GetChildPtr(size_t nIndex)
	{
		TElementCollectionPtr pColl = GetElementCollectionPtr();
		if (pColl) {
			VARIANT varIndex;
			varIndex.vt = VT_UINT;
			varIndex.lVal = (LONG)nIndex;
			VARIANT var2;
			VariantInit(&var2);
			TComPtr pDisp;
			HRESULT hr = pColl->item(varIndex, var2, &pDisp);
			if (S_OK == hr && pDisp)
				return THTMLNodePtr(new CHTMLNode(pDisp, m_pDocument));
		}

		return THTMLNodePtr();
	}

	virtual bool Is(EType nType)const
	{
		switch (nType) {
		case Table:
			{
				CComPtr<IHTMLTable> pTable;
				return (SUCCEEDED(m_pElement->QueryInterface(IID_IHTMLTable, reinterpret_cast<void**>(&pTable))) && (pTable));
			}
		case TableRow:
			{
				CComPtr<IHTMLTableRow> pRow;
				return (SUCCEEDED(m_pElement->QueryInterface(IID_IHTMLTableRow, reinterpret_cast<void**>(&pRow))) && (pRow));
			}
		case TableColumn:
			{
				CComPtr<IHTMLTableCol> pCol;
				return (SUCCEEDED(m_pElement->QueryInterface(IID_IHTMLTableCol, reinterpret_cast<void**>(&pCol))) && (pCol));
			}
		}

		return false;
	}

	virtual tstring GetAttribute(const tstring& rsAttrName)const
	{
		tstring sAttr;
		CComPtr<IHTMLElement> pElement;
		if (SUCCEEDED(m_pElement->QueryInterface(IID_IHTMLElement, reinterpret_cast<void**>(&pElement))) && pElement) {
			_variant_t vAttribute;
			BSTR pbstrAttrName = ::SysAllocString(rsAttrName.c_str());
			if (SUCCEEDED(pElement->getAttribute(pbstrAttrName, 1, &vAttribute)) && VT_NULL != vAttribute.vt && VT_EMPTY != vAttribute.vt) {
				try {
					_bstr_t b(vAttribute);
					LPCTSTR psz = b;
					if (psz)
						sAttr = psz;
				}
				catch (_com_error&) {
				}
			}
			::SysFreeString(pbstrAttrName);
		}

		return sAttr;
	}

	virtual tstring GetText()const
	{
		tstring sText;
		CComPtr<IHTMLElement> pElement;
		if (SUCCEEDED(m_pElement->QueryInterface(IID_IHTMLElement, reinterpret_cast<void**>(&pElement))) && pElement) {
			BSTR bstrText;
			if (SUCCEEDED(pElement->get_innerText(&bstrText)) && bstrText) {
				try {
					sText = _bstr_t(bstrText);
				}
				catch (_com_error&) {
				}

				::SysFreeString(bstrText);
			}
		}

		return sText;
	}

protected:
	virtual TElementCollectionPtr GetElementCollectionPtr()const
	{
		TElementCollectionPtr pColl;
		HRESULT hr = m_pElement->QueryInterface(IID_IHTMLElementCollection, reinterpret_cast<void**>(&pColl));
		if (FAILED(hr)) {
			CComPtr<IHTMLElement> pElement;
			if (SUCCEEDED(m_pElement->QueryInterface(IID_IHTMLElement, reinterpret_cast<void**>(&pElement))) && pElement) {
				CComPtr<IDispatch> pDisp;
				if (SUCCEEDED(pElement->get_children(&pDisp)) && pDisp)
					pDisp->QueryInterface(IID_IHTMLElementCollection, reinterpret_cast<void**>(&pColl));
			}
		}

		return pColl;
	}

private:
	TComPtr m_pElement;
	TDocumentPtr m_pDocument;
};

CHTMLParserMS::CHTMLParserMS() : m_bCallUninit(false)
{
	try {
		CheckError(::CoInitialize(nullptr));

		m_bCallUninit = true;

		_com_util::CheckError(
			::CoCreateInstance(CLSID_HTMLDocument,
				nullptr,
				CLSCTX_INPROC_SERVER,
				IID_IHTMLDocument2,
				(LPVOID*)&m_pDoc)
			);

		CComPtr<IPersistStreamInit> pPersist;
		_com_util::CheckError(m_pDoc->QueryInterface(IID_IPersistStreamInit,
			(LPVOID*)&pPersist));

		_com_util::CheckError(pPersist->InitNew());

		_com_util::CheckError(m_pDoc->QueryInterface(IID_IMarkupServices,
			(LPVOID*)&m_pMS));

		if (m_pMS) {
			_com_util::CheckError(m_pMS->CreateMarkupPointer(&m_pMkStart));
			_com_util::CheckError(m_pMS->CreateMarkupPointer(&m_pMkFinish));
		}
	}
	catch (_com_error&/* e*/) {
		// 		show_com_error_msg(e);
	}
}

CHTMLParserMS::~CHTMLParserMS()
{
	if (m_bCallUninit)
		::CoUninitialize();
}

CHTMLParserMS::THTMLNodePtr CHTMLParserMS::ParseString(const tstring& rsHTML)
{
	mir_cslock lck(m_cs);

	CComPtr<IMarkupContainer> pMC;
	HRESULT hr = m_pMS->ParseString((OLECHAR*)rsHTML.c_str(), 0, &pMC, m_pMkStart, m_pMkFinish);
	if (SUCCEEDED(hr) && pMC) {
		CComPtr<IHTMLDocument2> pNewDoc;
		hr = pMC->QueryInterface(IID_IHTMLDocument, (LPVOID*)&pNewDoc);
		if (SUCCEEDED(hr) && pNewDoc) {
			CComPtr<IHTMLElementCollection> pColl;
			pNewDoc->get_all(&pColl);

			CHTMLNode::TDocumentPtr pDoc;
			pMC->QueryInterface(IID_IHTMLDocument3, (LPVOID*)&pDoc);
			return THTMLNodePtr(new CHTMLNode(CHTMLNode::TComPtr(pColl), pDoc));
		}
	}

	return THTMLNodePtr();
}

bool CHTMLParserMS::IsInstalled()
{
	bool bResult = true;
	bool bCallUninit = false;
	try {
		CheckError(::CoInitialize(nullptr));

		bCallUninit = true;

		CComPtr<IHTMLDocument2> pDoc;
		_com_util::CheckError(
			::CoCreateInstance(CLSID_HTMLDocument,
				nullptr,
				CLSCTX_INPROC_SERVER,
				IID_IHTMLDocument2,
				reinterpret_cast<LPVOID*>(&pDoc))
			);
	}
	catch (_com_error&/* e*/) {
		bResult = false;
	}

	if (bCallUninit)
		::CoUninitialize();

	return bResult;
}

CHTMLEngineMS::CHTMLEngineMS()
{
}

CHTMLEngineMS::~CHTMLEngineMS()
{
}

CHTMLEngineMS::THTMLParserPtr CHTMLEngineMS::GetParserPtr()const
{
	return THTMLParserPtr(new CHTMLParserMS);
}