Doxygen
Loading...
Searching...
No Matches
xml.l File Reference
#include <stdint.h>
#include <ctype.h>
#include <vector>
#include <stdio.h>
#include "xml.h"
Include dependency graph for xml.l:

Go to the source code of this file.

Classes

struct  xmlYY_state
struct  XMLParser::Private

Macros

#define yyguts_t   xmlYYguts_t
#define YY_NEVER_INTERACTIVE   1
#define YY_NO_INPUT   1
#define YY_NO_UNISTD_H   1
#define YY_INPUT(buf, result, max_size)

Functions

static int yyread (yyscan_t yyscanner, char *buf, int max_size)
static void initElement (yyscan_t yyscanner)
static void addCharacters (yyscan_t yyscanner)
static void addElement (yyscan_t yyscanner)
static void addAttribute (yyscan_t yyscanner)
static void countLines (yyscan_t yyscanner, const char *txt, yy_size_t len)
static void reportError (yyscan_t yyscanner, const std::string &msg)
static std::string processData (yyscan_t yyscanner, const char *txt, yy_size_t len)
int yylex (yyscan_t yyscanner)
static void checkAndUpdatePath (yyscan_t yyscanner)
static std::string trimSpaces (const std::string &str)

Variables

static const char * entities_enc [] = { "amp", "quot", "gt", "lt", "apos" }
static const char entities_dec [] = { '&', '"', '>', '<', '\'' }
static const int num_entities = 5

Macro Definition Documentation

◆ YY_INPUT

#define YY_INPUT ( buf,
result,
max_size )
Value:
result=yyread(yyscanner,buf,max_size);
static int yyread(yyscan_t yyscanner, char *buf, int max_size)
Definition xml.l:231

Definition at line 76 of file xml.l.

◆ YY_NEVER_INTERACTIVE

#define YY_NEVER_INTERACTIVE   1

Definition at line 36 of file xml.l.

◆ YY_NO_INPUT

#define YY_NO_INPUT   1

Definition at line 37 of file xml.l.

◆ YY_NO_UNISTD_H

#define YY_NO_UNISTD_H   1

Definition at line 38 of file xml.l.

◆ yyguts_t

#define yyguts_t   xmlYYguts_t

Definition at line 26 of file xml.l.

Function Documentation

◆ addAttribute()

void addAttribute ( yyscan_t yyscanner)
static

Definition at line 351 of file xml.l.

352{
353 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
354 std::string val = yyextra->attrValue;
355 if (!yyextra->encoding.empty() && !yyextra->transcodeFunc(val,yyextra->encoding.c_str()))
356 {
357 reportError(yyscanner,"failed to transcode string '"+val+"' from encoding '"+yyextra->encoding+"' to UTF-8");
358 }
359 yyextra->attrs.insert(std::make_pair(yyextra->attrName,val));
#define yyguts_t
Definition code.l:23
static void reportError(yyscan_t yyscanner, const std::string &msg)
Definition xml.l:362
360}

References reportError(), and yyguts_t.

◆ addCharacters()

void addCharacters ( yyscan_t yyscanner)
static

Definition at line 330 of file xml.l.

331{
332 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
333 std::string data = trimSpaces(yyextra->data);
334 if (!yyextra->encoding.empty() && !yyextra->transcodeFunc(data,yyextra->encoding.c_str()))
335 {
336 reportError(yyscanner,"failed to transcode string '"+data+"' from encoding '"+yyextra->encoding+"' to UTF-8");
337 }
338 if (yyextra->handlers.characters)
339 {
340 yyextra->handlers.characters(data);
341 }
342 if (!data.empty())
343 {
344 if (yy_flex_debug)
345 {
346 fprintf(stderr,"characters(%s)\n",data.c_str());
347 }
348 }
static std::string trimSpaces(const std::string &str)
Definition xml.l:321
349}

References reportError(), trimSpaces(), and yyguts_t.

◆ addElement()

void addElement ( yyscan_t yyscanner)
static

Definition at line 287 of file xml.l.

288{
289 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
290 if (!yyextra->isEnd)
291 {
292 yyextra->xpath.push_back(yyextra->name);
293 if (yyextra->handlers.startElement)
294 {
295 yyextra->handlers.startElement(yyextra->name,yyextra->attrs);
296 }
297 if (yy_flex_debug)
298 {
299 fprintf(stderr,"%d: startElement(%s,attr=[",yyextra->lineNr,yyextra->name.data());
300 for (auto attr : yyextra->attrs)
301 {
302 fprintf(stderr,"%s='%s' ",attr.first.c_str(),attr.second.c_str());
303 }
304 fprintf(stderr,"])\n");
305 }
306 }
307 if (yyextra->isEnd || yyextra->selfClose)
308 {
309 if (yy_flex_debug)
310 {
311 fprintf(stderr,"%d: endElement(%s)\n",yyextra->lineNr,yyextra->name.data());
312 }
313 checkAndUpdatePath(yyscanner);
314 if (yyextra->handlers.endElement)
315 {
316 yyextra->handlers.endElement(yyextra->name);
317 }
318 }
static void checkAndUpdatePath(yyscan_t yyscanner)
Definition xml.l:264
319}

References checkAndUpdatePath(), and yyguts_t.

◆ checkAndUpdatePath()

void checkAndUpdatePath ( yyscan_t yyscanner)
static

Definition at line 264 of file xml.l.

265{
266 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
267 if (yyextra->xpath.empty())
268 {
269 std::string msg = "found closing tag '"+yyextra->name+"' without matching opening tag";
270 reportError(yyscanner,msg);
271 }
272 else
273 {
274 std::string expectedTagName = yyextra->xpath.back();
275 if (expectedTagName!=yyextra->name)
276 {
277 std::string msg = "Found closing tag '"+yyextra->name+"' that does not match the opening tag '"+expectedTagName+"' at the same level";
278 reportError(yyscanner,msg);
279 }
280 else // matching end tag
281 {
282 yyextra->xpath.pop_back();
283 }
284 }
#define msg(fmt,...)
Definition message.h:94
285}

References msg, reportError(), and yyguts_t.

Referenced by addElement().

◆ countLines()

void countLines ( yyscan_t yyscanner,
const char * txt,
yy_size_t len )
static

Definition at line 246 of file xml.l.

247{
248 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
249 for (yy_size_t i=0;i<len;i++)
250 {
251 if (txt[i]=='\n') yyextra->lineNr++;
252 }
253}

References yyguts_t.

◆ initElement()

void initElement ( yyscan_t yyscanner)
static

Definition at line 255 of file xml.l.

256{
257 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
258 yyextra->isEnd = false; // true => </tag>
259 yyextra->selfClose = false; // true => <tag/>
260 yyextra->name = "";
261 yyextra->attrs.clear();
262}

References yyguts_t.

◆ processData()

std::string processData ( yyscan_t yyscanner,
const char * txt,
yy_size_t len )
static

Definition at line 381 of file xml.l.

382{
383 std::string result;
384 result.reserve(len);
385 for (yy_size_t i=0; i<len; i++)
386 {
387 char c = txt[i];
388 if (c=='&')
389 {
390 const int maxEntityLen = 10;
391 char entity[maxEntityLen+1];
392 entity[maxEntityLen]='\0';
393 for (yy_size_t j=0; j<maxEntityLen && i+j+1<len; j++)
394 {
395 if (txt[i+j+1]!=';')
396 {
397 entity[j]=txt[i+j+1];
398 }
399 else
400 {
401 entity[j]=0;
402 break;
403 }
404 }
405 bool found=false;
406 for (int e=0; !found && e<num_entities; e++)
407 {
408 if (strcmp(entity,entities_enc[e])==0)
409 {
410 result+=entities_dec[e];
411 i+=strlen(entities_enc[e])+1;
412 found=true;
413 }
414 }
415 if (!found)
416 {
417 std::string msg = std::string("Invalid character entity '&") + entity + ";' found\n";
418 reportError(yyscanner,msg);
419 }
420 }
421 else
422 {
423 result+=c;
424 }
425 }
426 return result;
static const int num_entities
Definition xml.l:377
static const char entities_dec[]
Definition xml.l:376
static const char * entities_enc[]
Definition xml.l:375
427}

References entities_dec, entities_enc, msg, num_entities, and reportError().

◆ reportError()

void reportError ( yyscan_t yyscanner,
const std::string & msg )
static

Definition at line 362 of file xml.l.

363{
364 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
365 if (yy_flex_debug)
366 {
367 fprintf(stderr,"%s:%d: Error '%s'\n",yyextra->fileName.c_str(),yyextra->lineNr,msg.c_str());
368 }
369 if (yyextra->handlers.error)
370 {
371 yyextra->handlers.error(yyextra->fileName,yyextra->lineNr,msg);
372 }
373}

References msg, and yyguts_t.

Referenced by addAttribute(), addCharacters(), checkAndUpdatePath(), XMLParser::parse(), and processData().

◆ trimSpaces()

std::string trimSpaces ( const std::string & str)
static

Definition at line 321 of file xml.l.

322{
323 const int l = static_cast<int>(str.length());
324 int s=0, e=l-1;
325 while (s<l && isspace(str.at(s))) s++;
326 while (e>s && isspace(str.at(e))) e--;
327 return str.substr(s,1+e-s);
328}

Referenced by addCharacters().

◆ yylex()

int yylex ( yyscan_t yyscanner)

Definition at line 111 of file xml.l.

113 {
114 {SP} { countLines(yyscanner,yytext,yyleng); }
115 {DOCTYPE} { countLines(yyscanner,yytext,yyleng); }
116 {OPENSPECIAL} { countLines(yyscanner,yytext,yyleng); BEGIN(Prolog); }
117 {OPEN} { countLines(yyscanner,yytext,yyleng);
118 initElement(yyscanner);
119 BEGIN(Element); }
120 {COMMENT} { yyextra->commentContext = YY_START;
121 BEGIN(Comment);
122 }
static void countLines(yyscan_t yyscanner, const char *txt, yy_size_t len)
Definition xml.l:246
static void initElement(yyscan_t yyscanner)
Definition xml.l:255
123}
124<Content>{
125 {CDATA} { countLines(yyscanner,yytext,yyleng);
126 yyextra->cdataContext = YY_START;
127 BEGIN(CDataSection);
128 }
129 {PCDATA} { yyextra->data += processData(yyscanner,yytext,yyleng); }
130 {OPEN} { countLines(yyscanner,yytext,yyleng);
131 addCharacters(yyscanner);
132 initElement(yyscanner);
133 BEGIN(Element);
134 }
135 {COMMENT} { yyextra->commentContext = YY_START;
136 countLines(yyscanner,yytext,yyleng);
137 BEGIN(Comment);
138 }
static void addCharacters(yyscan_t yyscanner)
Definition xml.l:330
static std::string processData(yyscan_t yyscanner, const char *txt, yy_size_t len)
Definition xml.l:381
139}
140<Element>{
141 "/" { yyextra->isEnd = true; }
142 {NAME} { yyextra->name = yytext;
143 BEGIN(Attributes); }
144 {CLOSE} { addElement(yyscanner);
145 countLines(yyscanner,yytext,yyleng);
146 yyextra->data = "";
147 BEGIN(Content);
148 }
149 {SP} { countLines(yyscanner,yytext,yyleng); }
static void addElement(yyscan_t yyscanner)
Definition xml.l:287
150}
151<Attributes>{
152 "/" { yyextra->selfClose = true; }
153 {NAME} { yyextra->attrName = yytext; }
154 "=" { BEGIN(AttributeValue); }
155 {CLOSE} { addElement(yyscanner);
156 countLines(yyscanner,yytext,yyleng);
157 yyextra->data = "";
158 BEGIN(Content);
159 }
160 {SP} { countLines(yyscanner,yytext,yyleng); }
161}
162<AttributeValue>{
163 {SP} { countLines(yyscanner,yytext,yyleng); }
164 ['"] { yyextra->stringChar = *yytext;
165 yyextra->attrValue = "";
166 BEGIN(AttrValueStr);
167 }
168 . { std::string msg = std::string("Missing attribute value. Unexpected character `")+yytext+"` found";
169 reportError(yyscanner,msg);
170 unput(*yytext);
171 BEGIN(Attributes);
172 }
173}
174<AttrValueStr>{
175 [^'"\n]+ { yyextra->attrValue += processData(yyscanner,yytext,yyleng); }
176 ['"] { if (*yytext==yyextra->stringChar)
177 {
178 addAttribute(yyscanner);
179 BEGIN(Attributes);
180 }
181 else
182 {
183 yyextra->attrValue += processData(yyscanner,yytext,yyleng);
184 }
185 }
186 \n { yyextra->lineNr++; yyextra->attrValue+=' '; }
187}
188<CDataSection>{
189 {ENDCDATA} { BEGIN(yyextra->cdataContext); }
190 [^]\n]+ { yyextra->data += yytext; }
191 \n { yyextra->data += yytext;
192 yyextra->lineNr++;
193 }
194 . { yyextra->data += yytext; }
195}
196<Prolog>{
197 "encoding"\s*=\s*\"[^\"]*\" {
198 std::string encoding=yytext;
199 size_t i=encoding.find('"');
200 encoding=encoding.substr(i+1,yyleng-i-2);
201 if (encoding!="UTF-8") // need to transcode to UTF-8
202 {
203 yyextra->encoding=encoding;
204 }
205 }
206 {CLOSESPECIAL} { countLines(yyscanner,yytext,yyleng);
207 BEGIN(Initial);
208 }
209 \n { yyextra->lineNr++; }
210 . { }
211}
212<Comment>{
213 {COMMENTEND} { countLines(yyscanner,yytext,yyleng);
214 BEGIN(yyextra->commentContext);
215 }
216 [^\n-]+ { }
217 \n { yyextra->lineNr++; }
218 . { }
219}
220\n { yyextra->lineNr++; }
221. { std::string msg = "Unexpected character `";
222 msg+=yytext;
223 msg+="` found";
224 reportError(yyscanner,msg);
225 }
226
227%%

◆ yyread()

int yyread ( yyscan_t yyscanner,
char * buf,
int max_size )
static

Definition at line 231 of file xml.l.

232{
233 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
234 int inputPosition = yyextra->inputPosition;
235 const char *s = yyextra->inputString + inputPosition;
236 int c=0;
237 while( c < max_size && *s)
238 {
239 *buf++ = *s++;
240 c++;
241 }
242 yyextra->inputPosition += c;
243 return c;
244}

References yyguts_t.

Variable Documentation

◆ entities_dec

const char entities_dec[] = { '&', '"', '>', '<', '\'' }
static

Definition at line 376 of file xml.l.

Referenced by processData().

◆ entities_enc

const char* entities_enc[] = { "amp", "quot", "gt", "lt", "apos" }
static

Definition at line 375 of file xml.l.

Referenced by processData().

◆ num_entities

const int num_entities = 5
static

Definition at line 377 of file xml.l.

Referenced by processData().