2 * PROJECT: ReactOS Applications
3 * LICENSE: LGPL - See COPYING in the top level directory
4 * FILE: base/applications/msconfig_new/xmldomparser.cpp
5 * PURPOSE: XML DOM Parser
6 * COPYRIGHT: Copyright 2011-2012 Hermes BELUSCA - MAITO <hermes.belusca@sfr.fr>
10 #include "xmldomparser.hpp"
12 #include "stringutils.h"
14 // UTF8 adapted version of ConvertStringToBSTR (see lib/sdk/comsupp/comsupp.c)
16 ConvertUTF8StringToBSTR(const char *pSrc
)
21 if (!pSrc
) return NULL
;
23 /* Compute the needed size with the NULL terminator */
24 cwch
= MultiByteToWideChar(CP_UTF8
, 0, pSrc
, -1, NULL
, 0);
25 if (cwch
== 0) return NULL
;
27 /* Allocate the BSTR (without the NULL terminator) */
28 wsOut
= SysAllocStringLen(NULL
, cwch
- 1);
31 _com_issue_error(HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY
));
35 /* Convert the string */
36 if (MultiByteToWideChar(CP_UTF8
, 0, pSrc
, -1, wsOut
, cwch
) == 0)
38 /* We failed, clean everything up */
39 cwch
= GetLastError();
44 _com_issue_error(!IS_ERROR(cwch
) ? HRESULT_FROM_WIN32(cwch
) : cwch
);
51 InitXMLDOMParser(VOID
)
53 return CoInitialize(NULL
);
57 UninitXMLDOMParser(VOID
)
64 CreateAndInitXMLDOMDocument(IXMLDOMDocument
** ppDoc
)
66 HRESULT hr
= CoCreateInstance(CLSID_DOMDocument30
, // __uuidof(DOMDocument30), // NOTE: Do not use DOMDocument60 if you want MSConfig working by default on XP.
69 IID_PPV_ARG(IXMLDOMDocument
, ppDoc
) /* IID_PPV_ARGS(ppDoc) */);
73 /* These methods should not fail so don't inspect result */
74 (*ppDoc
)->put_async(VARIANT_FALSE
);
75 (*ppDoc
)->put_validateOnParse(VARIANT_FALSE
);
76 (*ppDoc
)->put_resolveExternals(VARIANT_FALSE
);
82 LoadXMLDocumentFromResource(IXMLDOMDocument
* pDoc
,
83 LPCWSTR lpszXMLResName
)
85 VARIANT_BOOL Success
= VARIANT_FALSE
;
94 // hRes = FindResourceExW(NULL, RT_HTML, lpszXMLResName, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));
95 hRes
= FindResourceW(NULL
, lpszXMLResName
, RT_HTML
);
99 handle
= LoadResource(NULL
, hRes
);
103 lpXMLRes
= LockResource(handle
);
104 if (lpXMLRes
== NULL
)
107 /* Convert the resource to UNICODE if needed */
108 if (!IsTextUnicode(lpXMLRes
, SizeofResource(NULL
, hRes
), NULL
))
109 bstrXMLRes
.Attach(ConvertUTF8StringToBSTR((LPCSTR
)lpXMLRes
));
111 bstrXMLRes
= (LPCWSTR
)lpXMLRes
;
113 if (SUCCEEDED(pDoc
->loadXML(bstrXMLRes
, &Success
)) && (Success
!= VARIANT_TRUE
))
115 IXMLDOMParseError
* pXMLErr
= NULL
;
118 if (SUCCEEDED(pDoc
->get_parseError(&pXMLErr
)) &&
119 SUCCEEDED(pXMLErr
->get_reason(&bstrErr
.GetBSTR())))
121 LPWSTR lpszStr
= NULL
;
123 if (IS_INTRESOURCE((ULONG_PTR
)lpszXMLResName
))
124 lpszStr
= FormatString(L
"Failed to load DOM from resource '#%u': %wS", lpszXMLResName
, (wchar_t*)bstrErr
);
126 lpszStr
= FormatString(L
"Failed to load DOM from resource '%wS': %wS", lpszXMLResName
, (wchar_t*)bstrErr
);
128 MessageBoxW(NULL
, lpszStr
, L
"Error", MB_ICONERROR
| MB_OK
);
133 SAFE_RELEASE(pXMLErr
);
137 FreeResource(handle
);
139 return (Success
== VARIANT_TRUE
);
143 LoadXMLDocumentFromFile(IXMLDOMDocument
* pDoc
,
144 LPCWSTR lpszFilename
,
145 BOOL bIgnoreErrorsIfNonExistingFile
)
147 VARIANT_BOOL Success
= VARIANT_FALSE
;
148 _variant_t
varFileName(lpszFilename
);
153 if (SUCCEEDED(pDoc
->load(varFileName
, &Success
)) && (Success
!= VARIANT_TRUE
))
155 IXMLDOMParseError
* pXMLErr
= NULL
;
156 LONG lErrorCode
= 0L;
158 if (SUCCEEDED(pDoc
->get_parseError(&pXMLErr
)) &&
159 SUCCEEDED(pXMLErr
->get_errorCode(&lErrorCode
)))
161 if ( !bIgnoreErrorsIfNonExistingFile
||
162 ((lErrorCode
!= _HRESULT_TYPEDEF_(0x800C0006) /* INET_E_OBJECT_NOT_FOUND */) &&
163 (lErrorCode
!= HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
))) )
167 if (SUCCEEDED(pXMLErr
->get_reason(&bstrErr
.GetBSTR())))
169 LPWSTR lpszStr
= FormatString(L
"Failed to load DOM from '%wS': %wS", lpszFilename
, (wchar_t*)bstrErr
);
170 MessageBoxW(NULL
, lpszStr
, L
"Error", MB_ICONERROR
| MB_OK
);
176 SAFE_RELEASE(pXMLErr
);
179 return (Success
== VARIANT_TRUE
);