2 * Copyright (c) 2000-2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include "TADC_ErrorCode.h"
23 #define _tcsstr strstr
24 #define _tcschr strchr
26 #define _MAX_NAME_LENGTH 4096
27 #define _MAX_VALUE_LENGTH 4096
30 #define _SKIP_WHITESPACE() \
31 while ((*m_pszXML == ' ') || \
32 (*m_pszXML == '\n') || \
33 (*m_pszXML == '\r') || \
34 (*m_pszXML == 0x09)) m_pszXML++
36 typedef struct _tagESCAPE_CHARSET {
41 //////////////////////////////////////////////////////////////////////
42 // Construction/Destruction
43 //////////////////////////////////////////////////////////////////////
50 for (int i = 0; i < ELEMENT_QUEUE_MAX; ++i)
51 m_paElementQueue[i] = NULL;
62 int CXMLFile::LoadFromStream(LPCTSTR pszXML)
82 DRM_TAPPS_EXCEPTION("CXMLFile::LoadFromStream() Error! \n");
87 int CXMLFile::LoadFromFile(LPCTSTR pszFileName)
91 DWORD dwFileSize, dwReadBytes;
92 LPBYTE pbBuffer = NULL;
95 hFile = fopen(pszFileName, "rb");
102 if (fseek(hFile, 0, SEEK_END) != 0) {
106 dwFileSize = ftell(hFile);
107 if (fseek(hFile, 0, SEEK_SET) != 0) {
112 if (dwFileSize > LONG_MAX - 256) {
116 pbBuffer = new BYTE[dwFileSize + 1];
117 IF_TRUE_GOTO(pbBuffer == NULL, TADC_MEMAlOC_ERROR);
119 dwReadBytes = fread(pbBuffer, 1, dwFileSize + 1, hFile);
121 IF_TRUE_GOTO(dwFileSize != dwReadBytes, TADC_PARAMETER_ERROR);
123 pbBuffer[dwFileSize] = 0;
125 pszXML = new CHAR[dwFileSize + 256];
126 IF_TRUE_GOTO(pszXML == NULL, TADC_MEMAlOC_ERROR);
129 nResult = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pbBuffer, -1, pszXML,
132 memcpy(pszXML, pbBuffer, dwFileSize + 1);
135 nResult = LoadFromStream(pszXML);
148 DRM_TAPPS_EXCEPTION("CXMLFile::LoadFromFile() Error! \n");
159 int CXMLFile::_Parse()
162 CXMLElement *pXMLElement = NULL;
163 CXMLElement *pCurrentElement = NULL;
164 CXMLElement *pTemp = NULL;
165 TCHAR szElementName[_MAX_NAME_LENGTH];
166 TCHAR szValue[_MAX_VALUE_LENGTH];
168 size_t orig_pszXML_len = strlen(m_pszXML);
169 size_t tmp_pszXML_len = orig_pszXML_len;
173 while (*m_pszXML != 0) {
179 if (*m_pszXML == '<') {
184 if (*m_pszXML == '!') {
185 //------------------------------------------------------------------------
189 //------------------------------------------------------------------------
190 } else if (*m_pszXML == '?') {
191 //------------------------------------------------------------------------
195 //------------------------------------------------------------------------
196 psz = _tcsstr(m_pszXML, _T("?>"));
204 } else if (*m_pszXML == '/') {
206 //------------------------------------------------------------------------
210 //------------------------------------------------------------------------
211 nResult = _GetElementName((LPTSTR)szElementName);
217 if (!pCurrentElement) {
222 if (TADC_IF_StrCmp(pCurrentElement->GetName(), (LPTSTR)szElementName) != 0) {
227 tmp_pszXML_len = orig_pszXML_len;
229 while (*m_pszXML != '>' && tmp_pszXML_len > 0) {
239 pCurrentElement = pTemp;
241 pCurrentElement = m_pRoot;
243 pXMLElement = new CXMLElement;
244 IF_TRUE_GOTO(pXMLElement == NULL, TADC_MEMAlOC_ERROR);
246 nResult = _GetElementName((LPTSTR)szElementName);
251 pXMLElement->SetName((LPTSTR)szElementName);
254 m_pRoot = pXMLElement;
256 if (pCurrentElement) {
257 pCurrentElement->AddChild(pXMLElement);
258 _Push(pCurrentElement);
261 pCurrentElement = pXMLElement;
263 } else if (*m_pszXML == '/') {
266 if (*m_pszXML != '>') {
271 pCurrentElement = _Pop();
272 } else if (*m_pszXML == '>') {
278 tmp_pszXML_len = orig_pszXML_len;
280 while (*m_pszXML != '<' && tmp_pszXML_len > 0) {
282 szValue[i] = *m_pszXML;
290 pCurrentElement->SetValue((LPTSTR)szValue);
294 nResult = _GetAttributeNameAndValue((LPTSTR)szElementName, (LPTSTR)szValue);
300 pCurrentElement->AddAttribute((LPTSTR)szElementName, (LPTSTR)szValue);
309 DRM_TAPPS_EXCEPTION("CXMLFile::_Parse() Error!. nResult[%d]", nResult);
314 int CXMLFile::_GetElementName(LPTSTR pszElementName)
318 while ((*m_pszXML != 0)
319 && (*m_pszXML != ' ')
320 && (*m_pszXML != '>')
321 && (*m_pszXML != '/')) {
322 *pszElementName = *m_pszXML;
332 CXMLElement *CXMLFile::_Pop()
334 if (m_nQueueIndex < 1)
337 return m_paElementQueue[--m_nQueueIndex];
340 int CXMLFile::_Push(CXMLElement *p)
342 if (m_nQueueIndex >= (int)sizeof(m_paElementQueue) / (int)sizeof(
343 m_paElementQueue[0]))
344 return ERROR_INSUFFICIENT_BUFFER;
346 m_paElementQueue[m_nQueueIndex++] = p;
351 int CXMLFile::_GetAttributeNameAndValue(LPTSTR pszName, LPTSTR pszValue)
353 int nResult, nLength, i;
357 LPTSTR origValueAddr = pszValue;
358 static T_ESCAPE_CHARSET stEscape[] = {
359 { _T("&"), _T('&') },
360 { _T("&qt"), _T('>') },
361 { _T("<"), _T('<') },
362 { _T("""), _T('"') },
363 { _T("&apos"), _T('\'') }
368 psz = _tcschr(m_pszXML, '=');
371 nResult = -1; //ERRORMSG(ERROR_SXS_XML_E_BADXMLDECL, NULL);
375 nLength = (int)(psz - m_pszXML);
377 if (_MAX_NAME_LENGTH < nLength + 1) {
382 memcpy(pszName, m_pszXML, nLength + 1);
388 if ((*m_pszXML == '\'') || (*m_pszXML == '"')) {
389 chQuotation = *m_pszXML;
395 while (*m_pszXML != 0 && ((pszValue - origValueAddr) < _MAX_VALUE_LENGTH)) {
396 if (*m_pszXML == '&') {
399 for (i = 0; i < (int)sizeof(stEscape) / (int)sizeof(stEscape[0]); i++) {
400 if (TADC_IF_StrNCmp(m_pszXML, stEscape[i].pszEscape,
401 TADC_IF_StrLen(stEscape[i].pszEscape)) == 0) {
402 *pszValue = stEscape[i].chReplace;
404 m_pszXML += TADC_IF_StrLen(stEscape[i].pszEscape);
411 if (isFound == FALSE) {
412 *pszValue = *m_pszXML;
416 } else if (*m_pszXML == chQuotation) {
419 } else if (*m_pszXML == '/' || *m_pszXML == '>') {
420 if (chQuotation == ' ')
423 *pszValue = *m_pszXML;
427 *pszValue = *m_pszXML;
439 DRM_TAPPS_EXCEPTION("CXMLFile::_GetAttributeNameAndValue() Error!");