diff options
Diffstat (limited to 'project/src/HtmlParser.cpp')
-rw-r--r-- | project/src/HtmlParser.cpp | 203 |
1 files changed, 203 insertions, 0 deletions
diff --git a/project/src/HtmlParser.cpp b/project/src/HtmlParser.cpp new file mode 100644 index 0000000..c3000e3 --- /dev/null +++ b/project/src/HtmlParser.cpp @@ -0,0 +1,203 @@ +// +// Tizen C++ SDK +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// Licensed under the Flora License, Version 1.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.tizenopensource.org/license +// +// 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. +// + +#include <FApp.h> +#include <FBase.h> +#include <FGraphics.h> +#include <FIo.h> +#include "HtmlParser.h" + +using namespace Tizen::App; +using namespace Tizen::Base; +using namespace Tizen::Base::Collection; +using namespace Tizen::Graphics; +using namespace Tizen::Io; +using namespace Tizen::Ui; +using namespace Tizen::Ui::Controls; +using namespace Tizen::Ui::Scenes; + +HtmlParser::HtmlParser(void) + : _pList(null) + , _pItemList(null) +{ +} + +HtmlParser::~HtmlParser(void) +{ +} + +bool +HtmlParser::Initialize(void) +{ + Construct(FORM_STYLE_NORMAL | FORM_STYLE_HEADER | FORM_STYLE_FOOTER | FORM_STYLE_INDICATOR); + return true; +} + +result +HtmlParser::OnInitializing(void) +{ + result r = E_SUCCESS; + Rectangle clientRect = GetClientAreaBounds(); + Header* pHeader = GetHeader(); + pHeader->SetTitleText("HTML Parser"); + + Footer* pFooter = GetFooter(); + pFooter->SetStyle(FOOTER_STYLE_BUTTON_ICON); + pFooter->SetBackButton(); + SetFormBackEventListener(this); + + _pList = new (std::nothrow) ListView(); + _pList->Construct(Rectangle(0, 0, LIST_WIDTH, LIST_HEIGHT), true); + _pList->SetItemProvider(*this); + _pList->AddListViewItemEventListener(*this); + _pList->SetTextOfEmptyList(L""); + AddControl(*_pList); + + _pItemList = new (std::nothrow) ArrayList(); + _pItemList->Construct(); + + RunHtmlParser(); + + return r; +} + +int +HtmlParser::GetItemCount(void) +{ + TryReturn(_pItemList, 0, "_pItemList is null."); + return _pItemList->GetCount(); +} + +Tizen::Ui::Controls::ListItemBase* +HtmlParser::CreateItem(int index, int itemWidth) +{ + ListAnnexStyle style = LIST_ANNEX_STYLE_NORMAL; + SimpleItem* pItem = new (std::nothrow) SimpleItem(); + + pItem->Construct(Tizen::Graphics::Dimension(itemWidth, ITEM_HEIGHT), style); + Tizen::Base::String* pKey = static_cast< String* >(_pItemList->GetAt(index)); + TryReturn(pKey, null, "key is empty for index %d",index); + + pItem->SetElement(*pKey, null); + return pItem; +} + +bool +HtmlParser::DeleteItem(int index, Tizen::Ui::Controls::ListItemBase* pItem, int itemWidth) +{ + delete pItem; + pItem = null; + return true; +} + + +void +HtmlParser::OnListViewItemStateChanged(Tizen::Ui::Controls::ListView& listView, int index, int elementId, Tizen::Ui::Controls::ListItemStatus status) +{ +} + +void +HtmlParser::OnListViewItemSwept(Tizen::Ui::Controls::ListView& listView, int index, Tizen::Ui::Controls::SweepDirection direction) +{ +} + +void +HtmlParser::OnListViewContextItemStateChanged(Tizen::Ui::Controls::ListView& listView, int index, int elementId, Tizen::Ui::Controls::ListContextItemStatus state) +{ +} + + +void +HtmlParser::OnActionPerformed(const Tizen::Ui::Control& source, int actionId) +{ +} + +void +HtmlParser::RunHtmlParser(void) +{ + //Parse html file and build a tree + String filepath = App::GetInstance()->GetAppRootPath() + L"data/tizen.html"; + ByteBuffer* pBuf = Tizen::Base::Utility::StringUtil::StringToUtf8N(filepath); + TryReturnVoid(pBuf, "StringToUtf8N failed."); + + htmlNodePtr pRoot =null; + htmlDocPtr pDoc = htmlParseFile((const char*) pBuf->GetPointer(), (const char*) "utf-8"); + TryCatch(pDoc, , "htmlParseFile failed."); + + //Get the root element of the document + pRoot = xmlDocGetRootElement(pDoc); + TryCatch(pRoot, xmlFreeDoc(pDoc), "xmlDocGetRootElement failed."); + + ExtractHyperLinks(pRoot); + _pList->UpdateList(); + xmlFreeDoc(pDoc); + +CATCH: + delete pBuf; +} + +/* this functions extracts links present in HTML file and displays*/ +void +HtmlParser::ExtractHyperLinks(htmlNodePtr pRoot) +{ + xmlNodePtr pCurrentElement = null; + xmlAttrPtr attr = null; + + for (pCurrentElement = pRoot; pCurrentElement; pCurrentElement = pCurrentElement->next) + { + if (pCurrentElement->type != XML_ELEMENT_NODE) + { + continue; + } + if (xmlStrcasecmp(pCurrentElement->name, (const xmlChar*) "A") == 0) + { + for (attr = pCurrentElement->properties; attr != null; attr = attr->next) + { + if (xmlStrcasecmp(attr->name, (const xmlChar*) "HREF") == 0) + { + String* nodeName = new (std::nothrow) String; + Tizen::Base::Utility::StringUtil::Utf8ToString((char*) pCurrentElement->children->content, *nodeName); + _pItemList->Add(*nodeName); + } + } + } + if (pCurrentElement->children != null) + { + ExtractHyperLinks(pCurrentElement->children); + } + } +} + +result +HtmlParser::OnTerminating(void) +{ + if (_pItemList) + { + _pItemList->RemoveAll(true); + delete _pItemList; + _pItemList = null; + } + + return E_SUCCESS; +} + +void +HtmlParser::OnFormBackRequested(Tizen::Ui::Controls::Form& source) +{ + SceneManager* pSceneManager = SceneManager::GetInstance(); + pSceneManager->GoBackward(BackwardSceneTransition()); +} |