1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "mpxplugindataparser.h" |
|
19 #include "mpxplugindata.h" |
|
20 #include <QString> |
|
21 |
|
22 |
|
23 |
|
24 const int gParserHexBase( 16 ); |
|
25 const int gParserDecBase( 10 ); |
|
26 |
|
27 static MpxPluginData::TMPXViewPluginPriorities parsePriority( const QStringRef& src ); |
|
28 |
|
29 |
|
30 // ---------------------------------------------------------------------------- |
|
31 // MpxPluginDataParser() |
|
32 // ---------------------------------------------------------------------------- |
|
33 // |
|
34 MpxPluginDataParser::MpxPluginDataParser() |
|
35 : |
|
36 dataPtr( NULL ) |
|
37 { |
|
38 // No implementation required |
|
39 } |
|
40 |
|
41 // ---------------------------------------------------------------------------- |
|
42 // ~MpxPluginDataParser() |
|
43 // ---------------------------------------------------------------------------- |
|
44 // |
|
45 MpxPluginDataParser::~MpxPluginDataParser() |
|
46 { |
|
47 if( NULL != dataPtr ){ |
|
48 delete dataPtr; |
|
49 } |
|
50 } |
|
51 |
|
52 // ---------------------------------------------------------------------------- |
|
53 // data() |
|
54 // ---------------------------------------------------------------------------- |
|
55 // |
|
56 const MpxPluginData* MpxPluginDataParser::data() const |
|
57 { |
|
58 return dataPtr; |
|
59 } |
|
60 |
|
61 // ---------------------------------------------------------------------------- |
|
62 // takeData() |
|
63 // ---------------------------------------------------------------------------- |
|
64 // |
|
65 MpxPluginData* MpxPluginDataParser::takeData() |
|
66 { |
|
67 MpxPluginData* retVal( dataPtr ); |
|
68 if( NULL != dataPtr ){ |
|
69 dataPtr = NULL; |
|
70 } |
|
71 return retVal; |
|
72 } |
|
73 |
|
74 // ---------------------------------------------------------------------------- |
|
75 // parse( const QString& data ) |
|
76 // ---------------------------------------------------------------------------- |
|
77 // |
|
78 void MpxPluginDataParser::parse( const QString& data ) |
|
79 { |
|
80 const QString PluginIdTag( "p" ); |
|
81 const QString PluginTypeTag( "t" ); |
|
82 const QString PluginPriorityTag( "i" ); |
|
83 const QString PluginSupportedTag( "f" ); |
|
84 |
|
85 QStringRef tagName, |
|
86 tagContent; |
|
87 int parserOffset( 0 ); |
|
88 if( NULL != dataPtr ){ |
|
89 delete dataPtr; |
|
90 dataPtr = NULL; |
|
91 } |
|
92 int integerNodeRepresentation( 0 ); |
|
93 while( findNextNode( data, parserOffset, tagName, tagContent ) ){ |
|
94 |
|
95 if( NULL == dataPtr ){ |
|
96 dataPtr = new MpxPluginData(); |
|
97 } |
|
98 if( PluginIdTag == tagName ){ |
|
99 if( parseInt( integerNodeRepresentation, tagContent.toString() ) ) |
|
100 dataPtr->setId( integerNodeRepresentation ); |
|
101 }else if( PluginTypeTag == tagName ){ |
|
102 if( parseInt( integerNodeRepresentation, tagContent.toString() ) ) { |
|
103 dataPtr->setType( integerNodeRepresentation ); |
|
104 } |
|
105 }else if( PluginPriorityTag == tagName ){ |
|
106 dataPtr->setPriority( parsePriority( tagContent ) ); |
|
107 }else if( PluginSupportedTag == tagName ){ |
|
108 parseSupportedId( tagContent.toString() ); |
|
109 } |
|
110 } |
|
111 } |
|
112 |
|
113 // ---------------------------------------------------------------------------- |
|
114 // findNextNode( const QString& src, int& offset, QStringRef& tagName, QStringRef& tagContent ) |
|
115 // ---------------------------------------------------------------------------- |
|
116 // |
|
117 bool MpxPluginDataParser::findNextNode( const QString& src, int& offset, QStringRef& tagName, QStringRef& tagContent ) const |
|
118 { |
|
119 const QChar endOfTag( '>' ); |
|
120 const QString beginOfStopTag( "</" ); |
|
121 |
|
122 |
|
123 int nodeBeginOffset = src.indexOf( endOfTag, offset, Qt::CaseSensitive ); |
|
124 if( nodeBeginOffset <= offset ){ |
|
125 //next "start tag" wasn't found. skip parsing |
|
126 return false; |
|
127 } |
|
128 ++nodeBeginOffset; |
|
129 int nodeEndOffset = src.indexOf( endOfTag, nodeBeginOffset, Qt::CaseSensitive ); |
|
130 if( nodeEndOffset <= nodeBeginOffset ){ |
|
131 //next "stop tag" wasn't found. skip parsing |
|
132 return false; |
|
133 } |
|
134 int nodeStopBegin = src.indexOf( beginOfStopTag, nodeBeginOffset, Qt::CaseSensitive ); |
|
135 offset = nodeEndOffset + 1; |
|
136 if( nodeStopBegin <= nodeBeginOffset ){ |
|
137 //error invalid document format |
|
138 return false; |
|
139 } |
|
140 const int nameBegin( nodeStopBegin + beginOfStopTag.length() ); |
|
141 tagName = src.midRef( nameBegin, nodeEndOffset - nameBegin ); |
|
142 tagContent = src.midRef( nodeBeginOffset, nodeStopBegin - nodeBeginOffset ); |
|
143 return true; |
|
144 } |
|
145 |
|
146 // ---------------------------------------------------------------------------- |
|
147 // parseInt( int& aDst, const QString& src ) const |
|
148 // ---------------------------------------------------------------------------- |
|
149 // |
|
150 bool MpxPluginDataParser::parseInt( int& aDst, const QString& src ) const |
|
151 { |
|
152 const int parserBasesByPriority[] = { gParserHexBase, gParserDecBase }, |
|
153 parserSupportedBases( 2 ); |
|
154 bool retVal( false ); |
|
155 for( int iter( 0 ); !retVal && iter < parserSupportedBases; ++iter ){ |
|
156 aDst = src.toInt( &retVal, parserBasesByPriority[ iter ] ); |
|
157 } |
|
158 return retVal; |
|
159 } |
|
160 |
|
161 // ---------------------------------------------------------------------------- |
|
162 // parseSupportedId( const QString& value ) |
|
163 // ---------------------------------------------------------------------------- |
|
164 // |
|
165 void MpxPluginDataParser::parseSupportedId( const QString& value ) |
|
166 { |
|
167 if( NULL == dataPtr ) { |
|
168 return; |
|
169 } |
|
170 int from( 0 ), to( 0 ), tagValue; |
|
171 const QChar coma( ',' ); |
|
172 do{ |
|
173 if( from >= value.length() ){ |
|
174 break; |
|
175 } |
|
176 to = value.indexOf( coma, from ); |
|
177 if( to < from ){ |
|
178 to = value.length(); |
|
179 } |
|
180 if( parseInt( tagValue, value.mid( from, to - from ) ) ){ |
|
181 dataPtr->addSupportedId( tagValue ); |
|
182 } |
|
183 from = ( to + 1 ); |
|
184 }while( true ); |
|
185 } |
|
186 |
|
187 // ---------------------------------------------------------------------------- |
|
188 // parseSupportedId( const QString& value ) |
|
189 // ---------------------------------------------------------------------------- |
|
190 // |
|
191 MpxPluginData::TMPXViewPluginPriorities parsePriority( const QStringRef& src ) |
|
192 { |
|
193 |
|
194 const QString MPXViewPluginPriorityLowestTag( "EMPXViewPluginPriorityLowest" ); |
|
195 const QString MPXViewPluginPriorityLowTag( "EMPXViewPluginPriorityLow" ); |
|
196 const QString MPXViewPluginPriorityNormalTag( "EMPXViewPluginPriorityNormal" ); |
|
197 const QString MPXViewPluginPriorityHighTag( "EMPXViewPluginPriorityHigh" ); |
|
198 const QString MPXViewPluginPriorityHighestTag( "EMPXViewPluginPriorityHighest" ); |
|
199 |
|
200 MpxPluginData::TMPXViewPluginPriorities |
|
201 retVal( MpxPluginData::EMPXViewPluginPriorityLowest ); |
|
202 if( MPXViewPluginPriorityLowTag == src ){ |
|
203 retVal = MpxPluginData::EMPXViewPluginPriorityLow; |
|
204 }else if( MPXViewPluginPriorityNormalTag == src ){ |
|
205 retVal = MpxPluginData::EMPXViewPluginPriorityNormal; |
|
206 }else if( MPXViewPluginPriorityHighTag == src ){ |
|
207 retVal = MpxPluginData::EMPXViewPluginPriorityHigh; |
|
208 }else if( MPXViewPluginPriorityHighestTag == src ){ |
|
209 retVal = MpxPluginData::EMPXViewPluginPriorityHighest; |
|
210 } |
|
211 return retVal; |
|
212 } |
|
213 |
|
214 |
|