1 #include "StdAfx.h"
2 #include "HTMLErrors.h"
3 //#include <Mshtml.h>
4 #include "xmlfile.h"
5 #include "xmldict.h"
6 #include "xmlcmd.h"
7 #include "xmlresult.h"
8 #include "htmldocument.h"
9
10 CHtmlDocument::CHtmlDocument(void)
11 {
12 m_pDoc = NULL;
13 }
14
15 CHtmlDocument::~CHtmlDocument(void)
16 {
17
18 }
19
20 void CHtmlDocument::SetDocument(IHTMLDocument2* pDoc)
21 {
22 m_pDoc = pDoc;
23 }
24
25 // Add dictionary to current HTML selection element
26 // Add a dictionary with given name from dictionary list.
27 // 1) Empty all option in a select object
28 // 2) Get the dictionary with given name from dictionary list
29 // 3) Get number of dictionary items from the dictionary get from dictionary list
30 // 4) Construct a new OPTIONs for each item in the dictionary.
31 // 5) Add the items into combobox.
32 // Here is a trick in current code .
33 // You can not using CreateElement of IHTMLDocument to create IHTMLOptionElement to replace the SELECT options.
34 // this object can be create using special interface , IHTMLOptionElementFactory. What a sadly thing is that
35 // there is no direct way to go the interface from IHTMLDocument or IHTMLElement or IHTMLElementCollection.
36 // It must be got from IHTMLWindow2! Where is the IHTMLWindow2? He he, you shall use get_Script of IHTMLDocument2 .
37 // Damn document!
38 // See the document: Knowledge Base Articles Q249232 HOWTO: Get IHTMLDocument2 from a HWND
39
40
41 BOOL CHtmlDocument::AddDictElement(IHTMLSelectElement* pSel, CString dictName, CXMLDict* pDict, CString strDef)
42 {
43 // First empty all elements in the current select element
44 EmptySelElement (pSel);
45 // The default code ;
46 long ldefCode = atol (strDef);
47 IXMLDOMNode *dictNode = NULL;
48
49 // Get dictionary from list.
50 if(!pDict->GetDict (dictName,&dictNode))
51 {
52 m_strErr.Format (EHT_GET_DICT,dictName);
53 return FALSE;
54 }
55
56 // count the number of items in the dictionary
57 long lNumItem = 0 ;
58 if (!pDict->GetNumOfItems (dictNode,lNumItem))
59 {
60 m_strErr = EHT_GET_NUM_DICT_ITEM;
61 dictNode->Release ();
62 return FALSE;
63 }
64 //Knowledge Base Articles
65 //HOWTO: Get IHTMLDocument2 from a HWND
66 CComQIPtr<IHTMLWindow2> pWindow;
67 CComPtr<IDispatch> pMyDisp;
68 if ( FAILED(m_pDoc->get_Script (&pMyDisp)))
69 {
70 m_strErr = EHT_CANT_GET_SCRIPT;
71 dictNode->Release();
72 return FALSE;
73 }
74 pWindow = pMyDisp;
75
76
77 IHTMLOptionElementFactory *pOptionFactory= NULL;
78 if ( FAILED(pWindow->get_Option (&pOptionFactory)))
79 {
80 m_strErr = EHT_CANT_GET_OPTION_FACTORY;
81 dictNode->Release ();
82 return FALSE;
83 }
84
85 // Add each items to selection
86 for (long l = 0 ; l < lNumItem ; l++ )
87 {
88 CString strItem ;
89 long code = 0;
90
91 // Get current item in the dictionary
92 if ( !pDict->GetDictItem (dictNode,l,code,strItem))
93 {
94 dictNode->Release ();
95 m_strErr .Format ( EHT_DICT_ITEM, dictName,l);
96 return FALSE;
97 }
98
99 IHTMLOptionElement *pOption=NULL;
100 VARIANT_BOOL vt_b =VARIANT_FALSE;
101 if (ldefCode == code || strDef == strItem )
102 vt_b = VARIANT_TRUE;
103 pOptionFactory->create (CComVariant(strItem),CComVariant(code),CComVariant(vt_b),CComVariant(vt_b),&pOption);
104
105 // Add to selection tag
106 if ( FAILED ( pSel->add ((IHTMLElement*)pOption,CComVariant(l))))
107 {
108 dictNode->Release ();
109 m_strErr = EHT_ADD_OPTION_ITEM;
110 return FALSE;
111 }
112
113
114 }
115 dictNode->Release ();
116 return TRUE;
117 }
118
119 // delete all option elements in current select objects
120 // if you want to delete all option object from given select object.
121 // 1) Get the number of OPTION s in the select.
122 // 2) Remove each zero option.
123 // 3) if we found there are error anywhere , say false.
124 BOOL CHtmlDocument::EmptySelElement(IHTMLSelectElement* pSel)
125 {
126 long lNum = 0 ;
127 if ( FAILED(pSel->get_length (&lNum)))
128 {
129 m_strErr = EHT_GET_NUM_OPTION ;
130 return FALSE;
131 }
132 for ( long l = 0 ; l < lNum ; l++ )
133 {
134 if ( FAILED(pSel->remove (HTM_C_ZERO)))
135 {
136 m_strErr = EHT_REMOVE_OPTION;
137 return FALSE;
138 }
139 }
140 return TRUE;
141 }
142
143 BOOL CHtmlDocument::IsSelect(BSTR tag)
144 {
145
146 return _wcsicmp(tag,HTMTAG_SELECT) == 0;
147 }
148
149 BOOL CHtmlDocument::IsImg(BSTR tag)
150 {
151
152 return _wcsicmp(tag, HTMTAG_IMG) == 0;
153 }
154 BOOL CHtmlDocument::IsImage(BSTR tag)
155 {
156
157 return _wcsicmp(tag,HTMTAG_IMAGE)==0;
158 }
159 BOOL CHtmlDocument::IsTextArea( BSTR tag)
160 {
161
162 return _wcsicmp(tag , HTMTAG_TEXTAREA)==0;
163 }
164 BOOL CHtmlDocument::IsInput( BSTR tag)
165 {
166
167 return _wcsicmp(tag, HTMTAG_INPUT) == 0;
168 }
169 int CHtmlDocument::IsControl(BSTR tag)
170 {
171 if ( IsImage(tag))
172 return HTM_C_IMAGE;
173 if (IsImg(tag ))
174 return HTM_C_IMG;
175 if ( IsInput(tag))
176 return HTM_C_INPUT;
177 if ( IsTextArea(tag))
178 return HTM_C_TEXTAREA;
179 if ( IsSelect(tag))
180 return HTM_C_SELECT;
181
182 return HTM_C_OTHER;
183 }
184
185 // Get control information from a given element that has been confirmed as a control
186 BOOL CHtmlDocument::GetDhtmControl(IHTMLElement* pElement,CString &Name , int & cType)
187 {
188 BSTR bstrName=NULL;
189 if ( FAILED(pElement->get_tagName (&bstrName)))
190 {
191 return FALSE;
192 }
193 int theType =IsControl(bstrName );
194 TRACE(" tag name = %s\n",CString (bstrName));
195 ::SysFreeString (bstrName);
196 return GetDhtmControl(pElement,theType,Name,cType);
197
198 }
199
200 // Get control information from a given element that has been confirmed as a control
201 BOOL CHtmlDocument::GetDhtmControl(IHTMLElement* pElement,int iType,CString &Name , int & cType)
202 {
203 switch ( iType )
204 {
205 case HTM_C_INPUT:
206 return GetDhtmInputControl(pElement,Name,cType);
207 break;
208 case HTM_C_IMG:
209 //return GetDhtmImgControl (pElement,Name,cType);
210 if (!this->GetElementId (pElement,Name))
211 return FALSE;
212 cType = HTM_CT_TEXT;
213 break;
214 case HTM_C_IMAGE:
215 //return GetDhtmImageControl (pElement,Name,cType);
216 if (!this->GetElementId (pElement,Name))
217 return FALSE;
218 cType = HTM_CT_TEXT;
219 break;
220 case HTM_C_TEXTAREA:
221 //return GetDhtmTextAreaControl (pElement,Name,cType);
222 if (!this->GetElementId (pElement,Name))
223 return FALSE;
224 cType = HTM_CT_TEXT;
225 break;
226 case HTM_C_SELECT:
227 //return GetDhtmSelectControl (pElement,Name,cType);
228 if (!this->GetElementId (pElement,Name))
229 return FALSE;
230 cType = HTM_CT_SELECT;
231 break;
232 default:
233 break;
234
235 }
236 return TRUE;
237 }
238 // Get input control from a given element;
239 BOOL CHtmlDocument::GetDhtmInputControl(IHTMLElement* pElement,CString &Name , int & type)
240 {
241 IHTMLInputElement *element= NULL;
242 if ( FAILED (pElement->QueryInterface (IID_IHTMLInputElement,(void**)&element)))
243 {
244 m_strErr = EHT_CANT_GET_INPUT;
245 return FALSE;
246 }
247 BSTR strTemp;
248 if( FAILED(pElement->get_id (&strTemp)))
249 {
250 element->Release ();
251 m_strErr= EHT_CANT_GET_INPUT;
252 return FALSE;
253 }
254 Name = CString (strTemp);
255 TRACE ("in INPUT , Name = %s\n" ,Name);
256 ::SysFreeString (strTemp);
257
258 if( FAILED(element->get_type (&strTemp)))
259 {
260 element->Release ();
261 m_strErr= EHT_CANT_GET_INPUT;
262 return FALSE;
263 }
264
265 type = GetControlType(strTemp);
266 ::SysFreeString (strTemp);
267 return TRUE;
268
269 }
270 // Get textarea control from a given element
271 BOOL CHtmlDocument::GetDhtmTextAreaControl(IHTMLElement* pElement,CString &Name , int & type)
272 {
273
274 BSTR strTemp;
275 if( FAILED(pElement->get_id (&strTemp)))
276 {
277 pElement->Release ();
278 m_strErr= EHT_CANT_GET_INPUT;
279 return FALSE;
280 }
281 Name = CString (strTemp);
282 ::SysFreeString (strTemp);
283 type = HTM_CT_TEXT;
284 return TRUE;
285
286
287 }
288 // Get image control from a given element
289 BOOL CHtmlDocument::GetDhtmImageControl(IHTMLElement* pElement,CString &Name , int & type)
290 {
291 BSTR strTemp;
292 if( FAILED(pElement->get_id (&strTemp)))
293 {
294 pElement->Release ();
295 m_strErr= EHT_CANT_GET_INPUT;
296 return FALSE;
297 }
298 Name = CString (strTemp);
299 ::SysFreeString (strTemp);
300 type = HTM_CT_BUTTON;
301 return TRUE;
302 }
303 // Get img control from a given element
304 BOOL CHtmlDocument::GetDhtmImgControl(IHTMLElement* pElement,CString &Name , int & type)
305 {
306 BSTR strTemp;
307 if( FAILED(pElement->get_id (&strTemp)))
308 //CComVariant var;
309 //if ( FAILED(pElement->getAttribute (HTMATT_NAME ,0,&var)))
310 {
311 pElement->Release ();
312 m_strErr= EHT_CANT_GET_INPUT;
313 return FALSE;
314 }
315
316 Name = CString (strTemp);
317 ::SysFreeString (strTemp);
318 type = HTM_CT_BUTTON;
319 return TRUE;
320 }
321 // Get select control from a given element
322 BOOL CHtmlDocument::GetDhtmSelectControl(IHTMLElement* pElement, CString &Name , int & type)
323 {
324 BSTR strTemp;
325 if( FAILED(pElement->get_id (&strTemp)))
326 {
327 pElement->Release ();
328 m_strErr= EHT_CANT_GET_INPUT;
329 return FALSE;
330 }
331 Name = CString (strTemp);
332 ::SysFreeString (strTemp);
333 type = HTM_CT_TEXT;
334 return TRUE;
335 }
336 // Get the control type define in index.
337 int CHtmlDocument::GetControlType(BSTR name)
338 {
339 _bstr_t bstrName (name);
340 if (_wcsicmp(name, HTMITYPE_BUTTON) == 0 )
341 return HTM_CT_BUTTON;
342
343 if (_wcsicmp(name,HTMITYPE_CHECKBOX) == 0)
344 return HTM_CT_CHECKBOX;
345
346 if (_wcsicmp(name,HTMITYPE_HIDDEN) == 0)
347 return HTM_CT_HIDDEN;
348
349 if (_wcsicmp(name,HTMITYPE_IMAGE) == 0)
350 return HTM_CT_IMAGE;
351
352 if (_wcsicmp(name,HTMITYPE_PASSWORD) == 0)
353 return HTM_CT_PASSWORD;
354
355 if (_wcsicmp(name,HTMITYPE_RADIO) == 0)
356 return HTM_CT_RADIO;
357
358 if (_wcsicmp(name,HTMITYPE_RESET) == 0)
359 return HTM_CT_RESET;
360
361 if (_wcsicmp(name,HTMITYPE_SUBMIT) == 0)
362 return HTM_CT_SUBMIT;
363
364 if (_wcsicmp(name,HTMITYPE_TEXT) == 0)
365 return HTM_CT_TEXT;
366
367 return 0;
368 }
369
370
371 // User may be want to change the selected portion of HTML document to looked as deleted. The function complete the action.
372 //BOOL CHtmlDocument::MarkSelectionToDelete(void)
373 //{
374 // if (!m_pDoc)
375 // return FALSE;
376 //
377 // IHTMLSelectionObject *pSelObject= NULL;
378 // if( FAILED(m_pDoc->get_selection (&pSelObject)))
379 // {
380 // return FALSE;
381 // }
382 // return TRUE;
383 //}
384
385 BOOL CHtmlDocument::AddDictToSelect(CXMLDict* pDict, CString strId , CString strDictName,CString strDef)
386 {
387 if ( !m_pDoc)
388 return FALSE;
389 IHTMLElementCollection *pColl = NULL;
390 if ( FAILED(m_pDoc->get_all (&pColl)))
391 return FALSE;
392
393 CComVariant var1(strId);
394 CComVariant var2(0);
395 IDispatch *pDisp=NULL;
396 if ( FAILED(pColl->item (var1,var2,&pDisp)))
397 return FALSE;
398 if ( !pDisp)
399 return FALSE;
400 IHTMLSelectElement *pSel= NULL;
401 if (FAILED(pDisp->QueryInterface (IID_IHTMLSelectElement,(void**)&pSel)))
402 return FALSE;
403 BOOL r = AddDictElement (pSel,strDictName,pDict,strDef);
404 pSel->Release ();
405
406 return r;
407 }
408 CString CHtmlDocument::GetLastError()
409 {
410 return m_strErr;
411 }
412 BOOL CHtmlDocument::GetElementId(IHTMLElement* pElement, CString & Name)
413 {
414 BSTR strTemp;
415 if( FAILED(pElement->get_id (&strTemp)))
416 {
417 pElement->Release ();
418 m_strErr= EHT_CANT_GET_INPUT;
419 return FALSE;
420 }
421 Name = CString (strTemp);
422 ::SysFreeString (strTemp);
423
424 return TRUE;
425 }
426
427 /*
428 For all date and time fields ,we have following rules
429 1 Each control as a member of date or time group will have a attribute (mydate /mytime) with variable name
430 2 The name of variable name is the name of control
431 3 time means YYYY-MM-DD HH:MM:SS
432 4 date means YYYY-MM-DD
433
434 According to the rulers;
435 to find if this element has attribute "mydate" or "mytime" can determine if the current control is member date field or time field
436 */
437 BOOL CHtmlDocument::IsDateOrTime( IHTMLElement* pElement, CString &Name, CString &var_Name)
438 {
439 BSTR strTemp= NULL;
440 CComVariant var;
441
442 if (FAILED(pElement->getAttribute (_bstr_t(HTML_ATT_MYDATE),0,&var)))
443 return FALSE;
444 // Take care, function return successful does not mean it has a attribute value.
445 if(var.vt == VT_NULL)
446 return FALSE;
447 // May be somebody give me a empty attribute, we deem it as none date /time field member
448 CString str (_bstr_t(var).Detach ());
449 if (!str.IsEmpty ())
450 {
451 var_Name = str;
452 // working under debug version
453 TRACE (" the attribute of mydate is :%s\n",var_Name);
454
455 return GetElementId(pElement,Name);
456 }
457 // So do as mytime attribute as "mydate" attribute of a given element
458 if (FAILED(pElement->getAttribute (_bstr_t(HTML_ATT_MYTIME),0,&var)))
459 return FALSE;
460 if (var.vt == VT_NULL)
461 return FALSE;
462 CString str1 (_bstr_t(var).Detach ());
463 if (!str1.IsEmpty ())
464 {
465 var_Name = str1;
466 TRACE (" the attribute of mytime is :%s\n",var_Name);
467 return GetElementId(pElement,Name);
468 }
469 return FALSE;
470
471 }
472
473
474 BOOL CHtmlDocument::IsRadio( IHTMLElement* pElement,CString &strId, CString &Name)
475 {
476
477 BSTR bstrName=NULL;
478 if ( FAILED(pElement->get_tagName (&bstrName)))
479 {
480 return FALSE;
481 }
482 int theType =IsControl(bstrName );
483 //TRACE(" tag name = %s\n",CString (bstrName));
484 ::SysFreeString (bstrName);
485 int cType;
486 if (GetDhtmControl(pElement,theType,strId,cType))
487 {
488 if ( cType == HTM_CT_RADIO)
489 {
490
491 CString str ;
492 if (!GetElementAttribute(pElement,HTML_ATT_NAME,str))
493 return FALSE;
494 if (!str.IsEmpty ())
495 {
496 Name = str;
497 // working under debug version
498 TRACE (" the name of radio is :%s and the Id of radio is %s\n",Name,strId);
499
500 }
501 return GetElementId(pElement,strId);
502
503 }
504
505 }
506 return FALSE;
507 }
508 BOOL CHtmlDocument::GetElementAttribute(IHTMLElement *pElement,const CString & attName ,CString &value)
509 {
510 BSTR strTemp= NULL;
511 CComVariant var;
512 if ( FAILED(pElement->getAttribute(_bstr_t(attName),0,&var)))
513 return FALSE;
514 // Take care, function return successful does not mean it has a attribute value.
515 if(var.vt == VT_NULL)
516 {
517 value = HTM_TEXT_EMPTY;
518 return TRUE;
519 }
520 value = CString(_bstr_t(var).Detach ());
521 return TRUE;
522 }
523 BOOL CHtmlDocument::GetElementName (IHTMLElement* pElement, CString &strName)
524 {
525 return GetElementAttribute(pElement, HTML_ATT_NAME,strName);
526 }
527 BOOL CHtmlDocument::GetElementValue (IHTMLElement* pElement, CString &value)
528 {
529 return GetElementAttribute(pElement, HTML_ATT_VALUE, value);
530 }
记录备份
最新推荐文章于 2024-01-26 12:01:43 发布