Menu

[r135]: / src / xmlrulereader.cpp  Maximize  Restore  History

Download this file

272 lines (247 with data), 6.9 kB

  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
/*
* Copyright 2005-2007 Gerald Schmidt.
*
* This file is part of Xml Copy Editor.
*
* Xml Copy Editor is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* Xml Copy Editor 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Xml Copy Editor; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string>
#include <vector>
#include <stdexcept>
#include <expat.h>
#include "xmlrulereader.h"
#include "rule.h"
#include "stringset.h"
using namespace std;
RuleData::RuleData (
boost::shared_ptr<StringSet<char> > dictionaryParameter,
boost::shared_ptr<StringSet<char> > passiveDictionaryParameter,
boost::shared_ptr<vector<boost::shared_ptr<Rule> > > ruleVectorParameter ) :
dictionary ( dictionaryParameter ),
passiveDictionary ( passiveDictionaryParameter ),
ruleVector ( ruleVectorParameter )
{
dictionaryFound = false;
initialiseAttributes();
ruleCount = 0;
}
RuleData::~RuleData()
{ }
XmlRuleReader::XmlRuleReader (
boost::shared_ptr<StringSet<char> > dictionaryParameter,
boost::shared_ptr<StringSet<char> > passiveDictionaryParameter,
boost::shared_ptr<vector<boost::shared_ptr<Rule> > > ruleVectorParameter ) :
ud ( new RuleData (
dictionaryParameter,
passiveDictionaryParameter,
ruleVectorParameter ) )
{
ud->setState ( STATE_UNKNOWN );
ud->p = p;
XML_SetUserData ( p, ud.get() );
XML_SetElementHandler ( p, start, end );
XML_SetCharacterDataHandler ( p, characterdata );
}
XmlRuleReader::~XmlRuleReader()
{}
int XmlRuleReader::getRuleCount()
{
return ud->ruleCount;
}
string XmlRuleReader::getIncorrectPatternReport()
{
return ud->incorrectPatternReport;
}
void XmlRuleReader::getExcludeVector ( vector<string> &v )
{
v = ud->excludeVector;
}
void XmlRuleReader::getIncludeVector ( vector<string> &v )
{
v = ud->includeVector;
}
void XMLCALL XmlRuleReader::start (
void *data,
const XML_Char *el,
const XML_Char **attr )
{
RuleData *ud;
ud = ( RuleData * ) data;
if ( !strcmp ( el, "rule" ) )
ud->setState ( STATE_IN_RULE );
else if ( !strcmp ( el, "term" ) )
{
ud->setState ( STATE_IN_TERM );
while ( *attr )
{
if ( !strcmp ( *attr, "passive" ) && !strcmp ( * ( attr + 1 ), "true" ) )
ud->passive = true;
attr += 2;
}
}
else if ( !strcmp ( el, "find" ) )
{
while ( *attr )
{
if ( !strcmp ( *attr, "matchcase" ) && !strcmp ( * ( attr + 1 ), "true" ) )
ud->matchcase = true;
else if ( !strcmp ( *attr, "cipher" ) && !strcmp ( * ( attr + 1 ), "true" ) )
ud->cipher = true;
attr += 2;
}
ud->setState ( STATE_IN_FIND );
}
else if ( !strcmp ( el, "replace" ) )
{
while ( *attr )
{
if ( !strcmp ( *attr, "adjustcase" ) && !strcmp ( * ( attr + 1 ), "true" ) )
ud->adjustcase = true;
else if ( !strcmp ( *attr, "tentative" ) && !strcmp ( * ( attr + 1 ), "true" ) )
ud->tentative = true;
attr += 2;
}
ud->setState ( STATE_IN_REPLACE );
}
else if ( !strcmp ( el, "report" ) )
ud->setState ( STATE_IN_REPORT );
else if ( !strcmp ( el, "exclude" ) )
ud->setState ( STATE_IN_EXCLUDE );
else if ( !strcmp ( el, "include" ) )
ud->setState ( STATE_IN_INCLUDE );
else if ( !strcmp ( el, "title" ) )
ud->setState ( STATE_IN_TITLE );
else
ud->setState ( STATE_UNKNOWN );
}
void XMLCALL XmlRuleReader::end ( void *data, const XML_Char *el )
{
RuleData *ud;
ud = ( RuleData * ) data;
if ( !strcmp ( el, "term" ) )
{
if ( ud->term != "" )
{
ud->dictionary->insert ( ud->term );
ud->dictionaryFound = true;
if ( ud->passive )
{
ud->passiveDictionary->insert ( ud->term );
ud->passive = false;
}
ud->term = "";
}
ud->setState ( STATE_UNKNOWN );
}
// handle end of rule
else if ( !strcmp ( el, "rule" ) )
{
try
{
boost::shared_ptr<Rule> rule ( new Rule (
ud->find,
ud->matchcase,
ud->replace ) );
string report = ud->title;
if ( ud->report != "" )
{
report += ": ";
report += ud->report;
}
rule->setReport ( report );
rule->setTentativeAttribute ( ud->tentative );
rule->setAdjustCaseAttribute ( ud->adjustcase );
ud->ruleVector->push_back ( rule );
++ ( ud->ruleCount );
ud->find = "";
ud->replace = "";
ud->report = "";
ud->setState ( STATE_UNKNOWN );
ud->initialiseAttributes();
}
catch ( exception& e )
{
ud->incorrectPatternReport = "Cannot compile: " +
ud->find +
"\r\nError: " +
e.what();
XML_StopParser ( ud->p, XML_FALSE );
}
}
else if ( !strcmp ( el, "find" ) )
ud->setState ( STATE_UNKNOWN );
else if ( !strcmp ( el, "replace" ) )
ud->setState ( STATE_UNKNOWN );
else if ( !strcmp ( el, "report" ) )
ud->setState ( STATE_UNKNOWN );
// handle excludes/includes
else if ( !strcmp ( el, "exclude" ) )
{
ud->excludeVector.push_back ( ud->exclude );
ud->exclude = "";
ud->setState ( STATE_UNKNOWN );
}
else if ( !strcmp ( el, "include" ) )
{
ud->includeVector.push_back ( ud->include );
ud->include = "";
ud->setState ( STATE_UNKNOWN );
}
else if ( !strcmp ( el, "title" ) )
ud->setState ( STATE_UNKNOWN );
// count each dictionary as one rule
else if ( !strcmp ( el, "dictionary" ) )
{
if ( ud->dictionaryFound )
++ ( ud->ruleCount );
}
}
void XMLCALL XmlRuleReader::characterdata (
void *data,
const XML_Char *s,
int len )
{
RuleData *ud;
ud = ( RuleData * ) data;
switch ( ud->getState() )
{
case STATE_IN_FIND:
ud->find.append ( s, len );
break;
case STATE_IN_REPLACE:
ud->replace.append ( s, len );
break;
case STATE_IN_REPORT:
ud->report.append ( s, len );
break;
case STATE_IN_EXCLUDE:
ud->exclude.append ( s, len );
break;
case STATE_IN_INCLUDE:
ud->include.append ( s, len );
break;
case STATE_IN_TITLE:
ud->title.append ( s, len );
break;
case STATE_IN_TERM:
ud->term.append ( s, len );
break;
default:
break;
}
}
void RuleData::initialiseAttributes()
{
matchcase = adjustcase = tentative = passive = false;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.