diff options
author | jh0522.choi <jh0522.choi@samsung.com> | 2012-10-05 13:54:12 +0900 |
---|---|---|
committer | jh0522.choi <jh0522.choi@samsung.com> | 2012-10-05 13:54:12 +0900 |
commit | c6fdf2fe68fbab516825cb00afd34c86262e9fbd (patch) | |
tree | b2662be0980746c19f0ddf8b7d7acb55eef74f29 /src/XmlHandler/XmlHandler.cpp | |
parent | f064727a492edc71d743c53bb9b906ea8d3f379f (diff) | |
download | installer-c6fdf2fe68fbab516825cb00afd34c86262e9fbd.tar.gz installer-c6fdf2fe68fbab516825cb00afd34c86262e9fbd.tar.bz2 installer-c6fdf2fe68fbab516825cb00afd34c86262e9fbd.zip |
Seperate the osp-installer
Diffstat (limited to 'src/XmlHandler/XmlHandler.cpp')
-rwxr-xr-x | src/XmlHandler/XmlHandler.cpp | 425 |
1 files changed, 425 insertions, 0 deletions
diff --git a/src/XmlHandler/XmlHandler.cpp b/src/XmlHandler/XmlHandler.cpp new file mode 100755 index 0000000..635fc9e --- /dev/null +++ b/src/XmlHandler/XmlHandler.cpp @@ -0,0 +1,425 @@ +// +// Open Service Platform +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the License); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +/** + * @file XmlHandler.cpp + * @brief This is the implementation file for %XmlHandler class. + */ + +#include <new> +#include <libxml/parser.h> +#include <libxml/parserInternals.h> +#include <libxml/tree.h> +#include <libxml/xmlstring.h> + +#include <FBaseLog.h> +#include <FIoFile.h> + +#include "InstallerDefs.h" +#include "XmlHandler.h" + +using namespace Osp::Base; +using namespace Osp::Base::Collection; +using namespace Osp::Io; + +XmlHandler::XmlHandler(void) +:__pAttr(null) +,__pElementName(null) +,__error(false) +{ +} + +XmlHandler::~XmlHandler(void) +{ + delete[] __pElementName; + __pElementName = null; + + delete __pAttr; + __pAttr = null; +} + +bool +XmlHandler::OnStartDocument(void) +{ + AppLogTag(OSP_INSTALLER, "OnStartDocument()"); + return true; +} + +bool +XmlHandler::OnEndDocument(void) +{ + AppLogTag(OSP_INSTALLER, "OnEndDocument()"); + return true; +} + +bool +XmlHandler::OnStartElement(const char *pName) +{ + AppLogTag(OSP_INSTALLER, "OnStartElement()"); + return true; +} + +bool +XmlHandler::OnEndElement(const char *pName) +{ + AppLogTag(OSP_INSTALLER, "OnEndElement()"); + + return true; +} + +bool +XmlHandler::OnCharacters(const char *pCharacters) +{ + AppLogTag(OSP_INSTALLER, "OnCharacters()"); + return true; +} + +void +XmlHandler::StartElement(void *ctx, const xmlChar *name, const xmlChar **atts) +{ + XmlHandler *pHandler = null; + pHandler = (XmlHandler *)ctx; + bool xmlResult = false; + + if (name == 0) + { + return; + } + + pHandler->SetElementName((const char *)name); + // AppLogTag(OSP_INSTALLER, "<%s>", (const char*)name); + + if (atts) + { + XmlAttribute *pAttr = 0; + + if (atts[0] != null && atts[1] != null) + { + pAttr = new (std::nothrow) XmlAttribute(); + TryReturnVoid(pAttr, "pAttr is null"); + pAttr->Construct((const char *)atts[0], (const char *)atts[1]); + + // log + //if (atts[0][0] && atts[1][0]) + //{ + // AppLogTag(OSP_INSTALLER, "%s=\"%s\"", (const char*)atts[0], (const char*)atts[1]); + //} + + atts = &atts[2]; + } + + while (atts != null && atts[0] != null && atts[1] != null) + { + pAttr->Add((const char *)atts[0], (const char *)atts[1]); + + // log + //if (atts[0][0] && atts[1][0]) + //{ + // AppLogTag(OSP_INSTALLER, "%s=\"%s\"", (const char*)atts[0], (const char*)atts[1]); + //} + + atts = &atts[2]; + } + + pHandler->SetAttribute(pAttr); + } + + xmlResult = pHandler->OnStartElement((const char *)name); + if (xmlResult == false) + { + pHandler->SetError(); + } +} + +void +XmlHandler::EndElement(void *ctx, const xmlChar *name) +{ + XmlHandler *pHandler = null; + pHandler = (XmlHandler *)ctx; + bool xmlResult = false; + + xmlResult = pHandler->OnEndElement((const char *)name); + if (xmlResult == false) + { + pHandler->SetError(); + } + + // AppLogTag(OSP_INSTALLER, "</%s>", name); + + pHandler->DeleteElement(); + pHandler->DeleteAttribute(); +} + +void +XmlHandler::Characters(void *ctx, const xmlChar *ch, int len) +{ + XmlHandler *pHandler = null; + pHandler = (XmlHandler *)ctx; + char *pCharacters = null; + bool xmlResult = false; + + TryReturnVoid(ch, "ch is null."); + + if (ch[0] == 0x20 || ch[0] == 0x09 || ch[0] == 0x0D || ch[0] == 0x0A) + { + return; + } + + pCharacters = new (std::nothrow) char[len+1]; + TryReturnVoid(pCharacters, "pCharacters is null."); + + strncpy(pCharacters, (const char *)ch, len); + pCharacters[len] = 0; + + xmlResult = pHandler->OnCharacters(pCharacters); + if (xmlResult == false) + { + pHandler->SetError(); + } + + // AppLogTag(OSP_INSTALLER, "%s", pCharacters); + delete[] pCharacters; +} + +bool +XmlHandler::ParseDocument(const char *pFilepath) +{ + xmlSAXHandler *pSAXHandler = null; + xmlParserCtxtPtr ctxt = 0; + bool ret = true; + File file; + FileAttributes attr; + result r = E_SUCCESS; + char* pBuf = null; + int size = 0; + int readSize = 0; + + r = file.Construct(pFilepath, L"r"); + TryCatch(r == E_SUCCESS, ret = false, "[osp-installer] file.Construct is failed. [%s]", pFilepath); + + r = file.GetAttributes(pFilepath, attr); + TryCatch(IsFailed(r) == false, ret = false, "[osp-installer] file.GetAttributes is failed. [%s]", pFilepath); + + size = (int)attr.GetFileSize(); + TryCatch(size > 0, ret = false, "[osp-installer] size is invalid. [%s]", pFilepath); + + pBuf = new (std::nothrow) char[size+1]; + TryCatch(pBuf, ret = false, "[osp-installer] pBuf is null"); + + memset(pBuf, 0, size+1); + + readSize = file.Read(pBuf, size); + TryCatch(readSize > 0, ret = false, "[osp-installer] file.Read is failed. [%s][%d]", pFilepath, readSize); + + ctxt = xmlCreateMemoryParserCtxt(pBuf, size+1); + TryCatch(ctxt, ret = false, "[osp-installer] invalid xml file, %s", pFilepath); + + pSAXHandler = new (std::nothrow) xmlSAXHandler; + TryCatch(pSAXHandler, ret = false, "[osp-installer] pSAXHandler is null"); + memset(pSAXHandler, 0, sizeof(xmlSAXHandler)); + + ctxt->userData = (void *)this; + + pSAXHandler->startElement = XmlHandler::StartElement; + pSAXHandler->endElement = XmlHandler::EndElement; + pSAXHandler->characters = XmlHandler::Characters; + + ctxt->sax = pSAXHandler; + + xmlParseDocument(ctxt); + xmlFreeParserCtxt(ctxt); + + TryCatch(GetError() != true, ret = false, "[osp-installer] xml parsing error is occurred."); + +CATCH: + delete[] pBuf; + + return ret; +} + +bool +XmlHandler::ParseNormalizedDocument(const char* pFilepath) +{ + xmlSAXHandler *pSAXHandler = null; + xmlParserCtxtPtr ctxt = 0; + bool ret = true; + File file; + FileAttributes attr; + result r = E_SUCCESS; + char* pBuf = null; + char* pNormalizedBuf = null; + int size = 0; + int normalizedSize = 0; + int readSize = 0; + + r = file.Construct(pFilepath, L"r"); + TryCatch(r == E_SUCCESS, ret = false, "[osp-installer] file.Construct is failed. [%s]", pFilepath); + + r = file.GetAttributes(pFilepath, attr); + TryCatch(IsFailed(r) == false, ret = false, "[osp-installer] file.GetAttributes is failed. [%s]", pFilepath); + + size = (int)attr.GetFileSize(); + TryCatch(size > 0, ret = false, "[osp-installer] size is invalid. [%s]", pFilepath); + + pBuf = new (std::nothrow) char[size+1]; + TryCatch(pBuf, ret = false, "[osp-installer] pBuf is null"); + + pNormalizedBuf = new (std::nothrow) char[size+1]; + TryCatch(pNormalizedBuf, ret = false, "[osp-installer] pNormalizedBuf is null"); + + memset(pBuf, 0, size+1); + memset(pNormalizedBuf, 0, size+1); + + readSize = file.Read(pBuf, size); + TryCatch(readSize > 0, ret = false, "[osp-installer] file.Read is failed. [%s][%d]", pFilepath, readSize); + + normalizedSize = Normalize(pBuf, size, pNormalizedBuf); + TryCatch(normalizedSize > 0, ret = false, "[osp-installer] normalizedSize [%d]", readSize); + + ctxt = xmlCreateMemoryParserCtxt(pNormalizedBuf, normalizedSize); + TryCatch(ctxt, ret = false, "[osp-installer] invalid xml file, %s", pFilepath); + + pSAXHandler = new (std::nothrow) xmlSAXHandler; + TryCatch(pSAXHandler, ret = false, "[osp-installer] pSAXHandler is null"); + memset(pSAXHandler, 0, sizeof(xmlSAXHandler)); + + ctxt->userData = (void *)this; + + pSAXHandler->startElement = XmlHandler::StartElement; + pSAXHandler->endElement = XmlHandler::EndElement; + pSAXHandler->characters = XmlHandler::Characters; + + ctxt->sax = pSAXHandler; + + xmlParseDocument(ctxt); + xmlFreeParserCtxt(ctxt); + + TryCatch(GetError() != true, ret = false, "[osp-installer] xml parsing error is occurred."); + +CATCH: + delete[] pBuf; + delete[] pNormalizedBuf; + + return ret; +} + +int +XmlHandler::Normalize(const char* pBuf, int size, char* pNormalizedBuf) +{ + int idx = 0; + int normalizedIdx = 0; + + while (pBuf[idx] && idx < size) + { + if (pBuf[idx] == 0x0D) + { + if (pBuf[idx + 1] == 0x0A) + { + idx++; + } + pNormalizedBuf[normalizedIdx] = 0x0A; + normalizedIdx++; + } + else + { + pNormalizedBuf[normalizedIdx] = pBuf[idx]; + normalizedIdx++; + } + + idx++; + } + + return normalizedIdx; +} + +bool +XmlHandler::SetElementName(const char *pElementName) +{ + TryReturn(pElementName, false, "[osp-installer] pElementName is null"); + + if (__pElementName) + { + delete[] __pElementName; + __pElementName = null; + } + + __pElementName = new char[strlen(pElementName)+1]; + TryReturn(__pElementName, false, "[osp-installer] __pElementName is null"); + strcpy(__pElementName, pElementName); + + __elementStack.Push(*new String(pElementName)); + + return true; +} + +char* +XmlHandler::GetElementName(void) +{ + return __pElementName; +} + +void +XmlHandler::DeleteElement(void) +{ + delete[]__pElementName; + __pElementName = 0; + + __elementStack.Pop(); +} + +void +XmlHandler::SetAttribute(XmlAttribute *pAttr) +{ + __pAttr = pAttr; +} + +XmlAttribute* +XmlHandler::GetAttribute(void) +{ + return __pAttr; +} + +void +XmlHandler::DeleteAttribute(void) +{ + delete __pAttr; + __pAttr = null; +} + +IEnumerator* +XmlHandler::GetElementEnumeratorN(void) +{ + return __elementStack.GetEnumeratorN(); +} + +int +XmlHandler::GetElementCount(void) +{ + return __elementStack.GetCount(); +} + +void +XmlHandler::SetError(void) +{ + __error = true; +} + +bool +XmlHandler::GetError(void) +{ + return __error; +} + |