Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / effects / parser / FUiEffects_ParserXMLParser.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <FBaseSysLog.h>
19 #include <FBaseUtilFileUnzipper.h>
20 #include <FUiEffects_ParserXMLParser.h>
21 #include <FUiEffects_ParserEffectParser.h>
22 #include "../FUiEffects_EffectErrorMessages.h"
23
24 namespace Tizen { namespace Ui { namespace Effects { namespace _Parser
25 {
26
27 EffectParser* XmlParser::__pCurrentParent = null;
28 bool XmlParser::__justParse = false;
29
30 XmlParser::XmlParser(EffectParser* pParent)
31 {
32         __pCurrentParent = pParent;
33
34         SysLog(NID_UI_EFFECT, "Effects. An object of XMLParser class created!");
35 }
36
37 XmlParser::~XmlParser()
38 {
39         __pCurrentParent = null;
40         __justParse = false;
41
42         SysLog(NID_UI_EFFECT, "Effects. An object of XMLParser class destroyed!");
43 }
44
45 void
46 XmlParser::StartElementNs(void * pCtx, const xmlChar* pLocalName, const xmlChar* pPrefix, const xmlChar* pUri,
47                 int namespaceCount, const xmlChar** ppNamespaces, int attributeCount, int defaultedCount, const xmlChar** ppAttributes)
48 {
49         if (!__justParse)
50         {
51                 __pCurrentParent->StartElement((const char*)pLocalName);
52         }
53
54         if (attributeCount > 0)
55         {
56                 std::string value;
57                 unsigned int index = 0;
58
59                 for (int indexAttribute = 0; indexAttribute < attributeCount; ++indexAttribute, index += 5)
60                 {
61                         const xmlChar *pLocalAttributeName = ppAttributes[index];
62                         std::string attributesName((const char* )pLocalAttributeName);
63
64                         const xmlChar *pValueBegin = ppAttributes[index+3];
65                         std::string attributesValue((const char* )pValueBegin);
66
67                         size_t pos = attributesValue.find('"');
68                         value = attributesValue.substr(0, pos);
69
70                         if (!__justParse)
71                         {
72                                 __pCurrentParent->Attribute(attributesName.c_str(), value.c_str());
73                         }
74                 }
75         }
76
77         return;
78 }
79
80 void
81 XmlParser::EndElementNs(void* pCtx, const xmlChar* pLocalName, const xmlChar* pPrefix, const xmlChar* pUri)
82 {
83         if (!__justParse)
84         {
85                 __pCurrentParent->EndElement((const char*) pLocalName);
86         }
87
88         return;
89 }
90
91 void
92 XmlParser::CharactersM(void* pCtx, const xmlChar* pCh, int len)
93 {
94         std::string wholeDoc = (char*) pCh;
95         size_t pos1 = wholeDoc.find('<');
96         // check whether the character has been found
97         if (pos1 == std::string::npos)
98         {
99                 return;
100         }
101
102         if (!__justParse)
103         {
104                 __pCurrentParent->Attribute("", wholeDoc.substr(pos1).c_str());
105         }
106
107         return;
108 }
109
110 void
111 XmlParser::Error(void* pCtx, const char* pMsg, ... )
112 {
113         char buffer[256];
114         va_list args;
115         va_start(args, pMsg);
116         vsprintf(buffer, pMsg, args);
117         va_end(args);
118
119         return;
120 }
121
122 void
123 XmlParser::Warning(void* pCtx, const char* pMsg, ... )
124 {
125         char buffer[256];
126         va_list args;
127         va_start(args, pMsg);
128         vsprintf(buffer, pMsg, args);
129         va_end(args);
130
131         return;
132 }
133
134 bool
135 XmlParser::ExtractFromArchive(const char* pArchivePath, const char* pDestinationPath)
136 {
137         bool result = true;
138         Tizen::Base::Utility::FileUnzipper unzipper;
139
140         Tizen::Base::String archivePath = pArchivePath;
141         Tizen::Base::String destinationPath = pDestinationPath;
142
143         unzipper.Construct(archivePath);
144         if (unzipper.UnzipTo(destinationPath) != E_SUCCESS)
145         {
146                 result = false;
147         }
148
149         return result;
150 }
151
152 bool
153 XmlParser::Parse(const char* pFilePath, bool sendCallbacks)
154 {
155         __justParse = !sendCallbacks;
156
157         xmlSAXHandler saxHandler;
158         memset(&saxHandler, 0, sizeof(saxHandler));
159
160         // set SAX callbacks
161         saxHandler.initialized = XML_SAX2_MAGIC;
162         saxHandler.startElementNs = StartElementNs;
163         saxHandler.endElementNs = EndElementNs;
164         saxHandler.warning = Warning;
165         saxHandler.error = Error;
166         saxHandler.characters = CharactersM;
167
168         // perform parsing
169         int parseResult = xmlSAXUserParseFile(&saxHandler, null, pFilePath);
170
171         SysTryReturn(NID_UI_EFFECT,
172                                 parseResult == 0,
173                                 false,
174                                 E_PARSING_FAILED,
175                                 Tizen::Ui::Effects::_UiEffectError::EP_XML_PARSING_FAILED
176                                 );
177
178         return true;
179 }
180
181 } } } } // Tizen::Ui::Effects::_Parser