1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
#include <parse.hxx>
#include <string.h>
#include <iostream>
#include <xmlelem.hxx>
#if (_MSC_VER >=1400)
#pragma warning(disable:4365)
#endif
#ifdef UNX
#define strnicmp strncasecmp
#endif
// NOT FULLY DEFINED SERVICES
#define AssertionOf(x) \
{if (!(x)) {std::cerr << "Assertion failed: " << #x << __FILE__ << __LINE__ << std::endl; exit(3); }}
X2CParser::X2CParser( XmlElement & o_rDocumentData )
: // sFileName,
nFileLine(0),
pDocumentData(&o_rDocumentData),
// sWord,
text(0)
{
}
X2CParser::~X2CParser()
{
}
bool
X2CParser::LoadFile( const char * i_sFilename )
{
sFileName = i_sFilename;
nFileLine = 1;
// Load file:
if ( ! LoadXmlFile( aFile, i_sFilename ) )
return false;
// Test correct end:
const char * pLastTag = strrchr(aFile.operator const char *(),'<');
if (pLastTag == 0)
return false;
if ( strnicmp(pLastTag+2, pDocumentData->Name().str(), pDocumentData->Name().l()) != 0
|| strnicmp(pLastTag, "</", 2) != 0 )
return false;
if (strchr(pLastTag,'>') == 0)
return false;
return true;
}
void
X2CParser::Parse()
{
// Parse:
text = aFile.operator const char *();
Parse_XmlDeclaration();
Parse_Doctype();
pDocumentData->Parse(*this);
}
bool
X2CParser::Parse( const char * i_sFilename )
{
bool ret = LoadFile(i_sFilename);
if (ret)
Parse();
return ret;
}
void
X2CParser::Parse_XmlDeclaration()
{
Goto('<');
if ( IsText("<?xml") )
{
Goto_And_Pass('>');
}
}
void
X2CParser::Parse_Doctype()
{
Goto('<');
if ( IsText("<!DOCTYPE") )
Goto_And_Pass('>');
}
void
X2CParser::Parse_Sequence( DynamicList<XmlElement> & o_rElements,
const Simstr & i_sElementName )
{
CheckAndPassBeginTag(i_sElementName.str());
unsigned int i_max = o_rElements.size();
for (unsigned i = 0; i < i_max; ++i)
{
o_rElements[i]->Parse(*this);
} // end for
CheckAndPassEndTag(i_sElementName.str());
}
void
X2CParser::Parse_FreeChoice( DynamicList<XmlElement> & o_rElements )
{
unsigned nSize = o_rElements.size();
for ( bool bBreak = false; !bBreak; )
{
bBreak = true;
for ( unsigned i = 0; i < nSize; ++i )
{
Goto('<');
if ( IsBeginTag(o_rElements[i]->Name().str()) )
{
o_rElements[i]->Parse(*this);
bBreak = false;
break;
}
} // end for i
} // end for !bBreak
}
void
X2CParser::Parse_List( ListElement & o_rListElem )
{
for ( Goto('<'); IsBeginTag(o_rListElem.Name().str()); Goto('<') )
{
XmlElement * pNew = o_rListElem.Create_and_Add_NewElement();
pNew->Parse(*this);
}
}
void
X2CParser::Parse_Text( Simstr & o_sText,
const Simstr & i_sElementName,
bool i_bReverseName )
{
if ( ! CheckAndPassBeginTag(i_sElementName.str()) )
return;
// Add new Element
GetTextTill( o_sText, '<', i_bReverseName );
o_sText.remove_trailing_blanks();
CheckAndPassEndTag(i_sElementName.str());
}
void
X2CParser::Parse_MultipleText( List<Simstr> & o_rTexts,
const Simstr & i_sElementName,
bool i_bReverseName )
{
for ( Goto('<'); IsBeginTag(i_sElementName.str()); Goto('<') )
{
Simstr sNew;
Parse_Text(sNew, i_sElementName, i_bReverseName);
if (sNew.l() > 0)
o_rTexts.push_back(sNew);
}
}
void
X2CParser::Parse_SglAttr( Simstr & o_sAttrValue,
const Simstr & i_sElementName,
const Simstr & i_sAttrName )
{
Goto('<');
if ( !IsBeginTag(i_sElementName.str()) )
SyntaxError("unexpected element");
Move( i_sElementName.l() + 1 );
Pass_White();
if (*text == '>')
SyntaxError("no attribute found, where one was expected");
Simstr sAttrName;
Get_Attribute(o_sAttrValue, sAttrName);
if (sAttrName != i_sAttrName)
SyntaxError("unknown attribute found");
Pass_White();
if (strncmp(text,"/>",2) != 0)
SyntaxError("missing \"/>\" at end of empty element");
Move(2);
}
void
X2CParser::Parse_MultipleAttr( List<Simstr> & o_rAttrValues,
const Simstr & i_sElementName,
const List<Simstr> & i_rAttrNames )
{
Goto('<');
if ( !IsBeginTag(i_sElementName.str()) )
SyntaxError("unexpected element");
Move( i_sElementName.l() + 1 );
Simstr sAttrName;
Simstr sAttrValue;
unsigned nSize = i_rAttrNames.size();
unsigned i;
for ( Pass_White(); *text != '/'; Pass_White() )
{
Get_Attribute(sAttrValue, sAttrName);
for ( i = 0; i < nSize; ++i )
{
if ( i_rAttrNames[i] == sAttrName )
{
o_rAttrValues[i] = sAttrValue;
break;
}
}
if (i == nSize)
SyntaxError("unknown attribute found");
}
Move(2);
}
void
X2CParser::Get_Attribute( Simstr & o_rAttrValue,
Simstr & o_rAttrName )
{
GetTextTill( o_rAttrName, '=');
while (*(++text) != '"')
{
if (*text == '\0')
SyntaxError("unexpected end of file");
}
++text;
GetTextTill( o_rAttrValue, '"');
++text;
}
bool
X2CParser::IsText( const char * i_sComparedText )
{
return strnicmp( text, i_sComparedText, strlen(i_sComparedText) ) == 0;
}
bool
X2CParser::IsBeginTag( const char * i_sTagName )
{
return strnicmp( text+1, i_sTagName, strlen(i_sTagName) ) == 0
&& *text == '<';
}
bool
X2CParser::IsEndTag( const char * i_sTagName )
{
return strnicmp( text+2, i_sTagName, strlen(i_sTagName) ) == 0
&& strnicmp( text, "</", 2 ) == 0;
}
void
X2CParser::Goto( char i_cNext )
{
while (*text != i_cNext)
{
TestCurChar();
++text;
}
}
void
X2CParser::Goto_And_Pass( char i_cNext )
{
Goto(i_cNext);
++text;
}
void
X2CParser::Move( int i_nForward )
{
text += i_nForward;
}
void
X2CParser::Pass_White()
{
while (*text <= 32)
{
TestCurChar();
++text;
}
}
void
X2CParser::GetTextTill( Simstr & o_rText,
char i_cEnd,
bool i_bReverseName )
{
char * pResult = &sWord[0];
char * pSet;
for ( pSet = pResult;
*text != i_cEnd;
++text )
{
TestCurChar();
*pSet++ = *text;
}
while ( *pResult < 33 && *pResult > 0 )
++pResult;
while ( pSet > pResult ? *(pSet-1) < 33 : false )
pSet--;
*pSet = '\0';
if (i_bReverseName)
{
const unsigned int nMaxLen = 1000;
if (strlen(pResult) < nMaxLen)
{
char * sBreak = strrchr(pResult,'.');
if (sBreak != 0)
{
static char sScope[nMaxLen+10];
static char sName[nMaxLen+10];
unsigned nScopeLen = sBreak - pResult;
strncpy ( sScope, pResult, nScopeLen ); // STRNCPY SAFE HERE
sScope[nScopeLen] = '\0';
strcpy( sName, sBreak + 1 ); // STRCPY SAFE HERE
strcat( sName, " in " ); // STRCAT SAFE HERE
strcat( sName, sScope ); // STRCAT SAFE HERE
o_rText = sName;
return;
}
}
} // endif (i_bReverseName)
o_rText = &sWord[0];
}
bool
X2CParser::CheckAndPassBeginTag( const char * i_sElementName )
{
bool ret = true;
Goto('<');
if ( ! IsBeginTag(i_sElementName) )
SyntaxError( "unexpected element");
if (IsAbsoluteEmpty())
ret = false;
Goto_And_Pass('>');
if (ret)
Pass_White();
return ret;
}
void
X2CParser::CheckAndPassEndTag( const char * i_sElementName )
{
Pass_White();
if ( !IsEndTag(i_sElementName) )
SyntaxError("missing or not matching end tag");
Goto_And_Pass('>');
}
bool
X2CParser::IsAbsoluteEmpty() const
{
const char * pEnd = strchr(text+1, '>');
if (pEnd != 0)
{
if ( *(pEnd-1) == '/' )
{
const char * pAttr = strchr(text+1, '"');
if (pAttr == 0)
return true;
else if ( (pAttr-text) > (pEnd-text) )
return true;
}
}
return false;
}
void
X2CParser::SyntaxError( const char * i_sText )
{
std::cerr
<< "Syntax error "
<< i_sText
<< " in file: "
<< sFileName.str()
<< " in line "
<< nFileLine
<< "."
<< std::endl;
exit(3);
}
void
X2CParser::TestCurChar()
{
// if (*text == '\0')
// SyntaxError("unexpected end of file");
// else
if (*text == '\n')
nFileLine++;
}
|