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.
19 #include "TADC_ErrorCode.h"
21 #define _tcsstr strstr
22 #define _tcschr strchr
24 #define _MAX_NAME_LENGTH 4096
25 #define _MAX_VALUE_LENGTH 4096
28 #define _SKIP_WHITESPACE() while( ( *m_pszXML == ' ' ) || \
29 ( *m_pszXML == '\n' ) || \
30 ( *m_pszXML == '\r' ) || \
31 ( *m_pszXML == 0x09 ) ) m_pszXML++
33 typedef struct _tagESCAPE_CHARSET
39 //////////////////////////////////////////////////////////////////////
40 // Construction/Destruction
41 //////////////////////////////////////////////////////////////////////
48 for (int i = 0; i < ELEMENT_QUEUE_MAX; ++i)
49 m_paElementQueue[i] = NULL;
60 int CXMLFile::LoadFromStream(LPCTSTR pszXML)
79 DRM_TAPPS_EXCEPTION("CXMLFile::LoadFromStream() Error! \n");
84 int CXMLFile::LoadFromFile(LPCTSTR pszFileName)
88 DWORD dwFileSize, dwReadBytes;
89 LPBYTE pbBuffer = NULL;
92 hFile = fopen(pszFileName, "rb");
99 fseek(hFile, 0, SEEK_END);
100 dwFileSize = ftell(hFile);
101 fseek(hFile, 0, SEEK_SET);
103 pbBuffer = new BYTE[ dwFileSize + 1 ];
104 IF_TRUE_GOTO( pbBuffer == NULL, TADC_MEMAlOC_ERROR );
106 dwReadBytes = fread ( pbBuffer, 1, dwFileSize + 1, hFile);
108 IF_TRUE_GOTO( dwFileSize != dwReadBytes, TADC_PARAMETER_ERROR );
110 pbBuffer[ dwFileSize ] = 0;
112 pszXML = new CHAR[ dwFileSize + 256 ];
113 IF_TRUE_GOTO( pszXML == NULL, TADC_MEMAlOC_ERROR );
116 nResult = MultiByteToWideChar( CP_ACP, 0, (LPCSTR)pbBuffer, -1, pszXML, dwFileSize + 256 );
118 memcpy(pszXML, pbBuffer, dwFileSize + 1);
121 nResult = LoadFromStream(pszXML);
133 DRM_TAPPS_EXCEPTION("CXMLFile::LoadFromFile() Error! \n");
144 int CXMLFile::_Parse()
147 CXMLElement* pXMLElement = NULL;
148 CXMLElement* pCurrentElement = NULL;
149 CXMLElement* pTemp = NULL;
150 TCHAR szElementName[_MAX_NAME_LENGTH];
151 TCHAR szValue[_MAX_VALUE_LENGTH];
153 size_t orig_pszXML_len = strlen(m_pszXML);
154 size_t tmp_pszXML_len = orig_pszXML_len;
158 while (*m_pszXML != 0) {
164 if (*m_pszXML == '<') {
169 if (*m_pszXML == '!') {
170 //------------------------------------------------------------------------
174 //------------------------------------------------------------------------
176 else if (*m_pszXML == '?') {
177 //------------------------------------------------------------------------
181 //------------------------------------------------------------------------
182 psz = _tcsstr(m_pszXML, _T( "?>" ));
190 else if (*m_pszXML == '/') {
192 //------------------------------------------------------------------------
196 //------------------------------------------------------------------------
197 nResult = _GetElementName((LPTSTR)szElementName);
203 if (!pCurrentElement) {
208 if (TADC_IF_StrCmp(pCurrentElement->GetName(), (LPTSTR)szElementName) != 0) {
213 tmp_pszXML_len = orig_pszXML_len;
214 while (*m_pszXML != '>' && tmp_pszXML_len > 0) {
224 pCurrentElement = pTemp;
226 pCurrentElement = m_pRoot;
229 pXMLElement = new CXMLElement;
230 IF_TRUE_GOTO(pXMLElement == NULL, TADC_MEMAlOC_ERROR);
232 nResult = _GetElementName((LPTSTR)szElementName);
236 pXMLElement->SetName((LPTSTR)szElementName);
239 m_pRoot = pXMLElement;
241 if (pCurrentElement) {
242 pCurrentElement->AddChild(pXMLElement);
243 _Push(pCurrentElement);
246 pCurrentElement = pXMLElement;
249 else if (*m_pszXML == '/') {
252 if (*m_pszXML != '>') {
257 pCurrentElement = _Pop();
259 else if (*m_pszXML == '>') {
265 tmp_pszXML_len = orig_pszXML_len;
266 while (*m_pszXML != '<' && tmp_pszXML_len > 0) {
268 szValue[i] = *m_pszXML;
275 pCurrentElement->SetValue((LPTSTR)szValue);
280 nResult = _GetAttributeNameAndValue((LPTSTR)szElementName, (LPTSTR)szValue);
286 pCurrentElement->AddAttribute((LPTSTR)szElementName, (LPTSTR)szValue);
294 DRM_TAPPS_EXCEPTION("CXMLFile::_Parse() Error!. nResult[%d]", nResult);
299 int CXMLFile::_GetElementName(LPTSTR pszElementName)
303 while ((*m_pszXML != 0 )
304 && (*m_pszXML != ' ')
305 && (*m_pszXML != '>')
306 && (*m_pszXML != '/')) {
307 *pszElementName = *m_pszXML;
317 CXMLElement* CXMLFile::_Pop()
319 if (m_nQueueIndex < 1)
322 return m_paElementQueue[--m_nQueueIndex];
325 int CXMLFile::_Push(IN CXMLElement* p)
327 if (m_nQueueIndex >= (int)sizeof(m_paElementQueue) / (int)sizeof(m_paElementQueue[0]))
328 return ERROR_INSUFFICIENT_BUFFER;
330 m_paElementQueue[m_nQueueIndex++] = p;
335 int CXMLFile::_GetAttributeNameAndValue(LPTSTR pszName, LPTSTR pszValue)
337 int nResult, nLength, i;
341 LPTSTR origValueAddr = pszValue;
342 static T_ESCAPE_CHARSET stEscape[] = { { _T( "&" ), _T( '&' ) },
343 { _T( "&qt" ), _T( '>' ) },
344 { _T( "<" ), _T( '<' ) },
345 { _T( """ ), _T( '"' ) },
346 { _T( "&apos" ), _T( '\'' ) } };
350 psz = _tcschr( m_pszXML, '=' );
352 nResult = -1;//ERRORMSG( ERROR_SXS_XML_E_BADXMLDECL, NULL );
356 nLength = (int)(psz - m_pszXML);
357 if( _MAX_NAME_LENGTH < nLength + 1) {
361 memcpy(pszName, m_pszXML, nLength + 1);
367 if (( *m_pszXML == '\'' ) || ( *m_pszXML == '"' )) {
368 chQuotation = *m_pszXML;
375 while (*m_pszXML != 0 && ((pszValue - origValueAddr)< _MAX_VALUE_LENGTH) ) {
376 if (*m_pszXML == '&') {
378 for (i = 0; i < (int)sizeof(stEscape) / (int)sizeof(stEscape[0]); i++) {
379 if (TADC_IF_StrNCmp(m_pszXML, stEscape[i].pszEscape, TADC_IF_StrLen(stEscape[i].pszEscape)) == 0) {
380 *pszValue = stEscape[i].chReplace;
382 m_pszXML += TADC_IF_StrLen(stEscape[i].pszEscape);
389 if (isFound == FALSE) {
390 *pszValue = *m_pszXML;
395 else if (*m_pszXML == chQuotation) {
399 else if (*m_pszXML == '/' || *m_pszXML == '>')
401 if (chQuotation == ' ')
404 *pszValue = *m_pszXML;
410 *pszValue = *m_pszXML;
421 DRM_TAPPS_EXCEPTION("CXMLFile::_GetAttributeNameAndValue() Error!");