Menu

[r19]: / src / xmlshallowvalidator.cpp  Maximize  Restore  History

Download this file

152 lines (131 with data), 3.7 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
#include <string>
#include <vector>
#include <stdexcept>
#include <expat.h>
#include <map>
#include <set>
#include "xmlshallowvalidator.h"
XmlShallowValidator::XmlShallowValidator(
std::map<std::string, std::set<std::string> > &elementMap,
std::map<std::string, std::map<std::string, std::set<std::string> > >
&attributeMap,
std::map<std::string, std::set<std::string> > &requiredAttributeMap,
std::set<std::string> &entitySet,
int maxLine,
bool segmentOnly) : vd(new XmlShallowValidatorData())
{
vd->elementMap = elementMap;
vd->attributeMap = attributeMap;
vd->requiredAttributeMap = requiredAttributeMap;
vd->entitySet = entitySet;
vd->isValid = true;
vd->p = p;
vd->depth = 0;
vd->maxLine = maxLine;
vd->segmentOnly = segmentOnly;
vd->overrideFailure = false;
XML_SetUserData(p, vd.get());
XML_SetElementHandler(p, start, end);
XML_SetSkippedEntityHandler(p, skippedentity);
}
XmlShallowValidator::~XmlShallowValidator()
{
}
void XMLCALL XmlShallowValidator::start(void *data,
const XML_Char *el,
const XML_Char **attr)
{
XmlShallowValidatorData *vd;
vd = (XmlShallowValidatorData *)data;
#ifdef __WXMSW__
if (XML_GetCurrentLineNumber(vd->p) > (unsigned)(vd->maxLine + 1))
#else
if (XML_GetCurrentLineNumber(vd->p) > (vd->maxLine + 1))
#endif
{
XML_StopParser(vd->p, true);
}
vd->push(el);
++(vd->depth);
//check element ok
std::string parent = vd->getParent();
if (parent.empty())
return;
if (vd->elementMap.empty())
return;
if (!vd->elementMap[parent].count(vd->getElement()))
{
vd->isValid = false;
vd->positionVector.push_back(
make_pair(XML_GetCurrentLineNumber(vd->p), XML_GetCurrentColumnNumber(vd->p)));
}
std::map<std::string, std::set<std::string> > attributeMap;
size_t requiredAttributeCount = vd->requiredAttributeMap[el].size();
std::string currentAttribute;
while (*attr)
{
attributeMap = vd->attributeMap[el];
// check for existence
if (!attributeMap.count(*attr))
{
vd->isValid = false;
vd->positionVector.push_back(make_pair(
XML_GetCurrentLineNumber(vd->p),
XML_GetCurrentColumnNumber(vd->p)));
}
// check for requirement
currentAttribute = (const char *)*attr;
if (vd->requiredAttributeMap[el].find(currentAttribute) !=
vd->requiredAttributeMap[el].end())
--requiredAttributeCount;
attr += 2;
}
if (requiredAttributeCount != 0)
{
vd->isValid = false;
vd->positionVector.push_back(make_pair(
XML_GetCurrentLineNumber(vd->p),
XML_GetCurrentColumnNumber(vd->p)));
}
}
void XMLCALL XmlShallowValidator::end(void *data, const XML_Char *el)
{
XmlShallowValidatorData *vd;
vd = (XmlShallowValidatorData *)data;
vd->pop();
--(vd->depth);
// segments: stop at end tag of first element
if (vd->segmentOnly && vd->depth == 0)
{
XML_StopParser(vd->p, true);
if (vd->isValid)
vd->overrideFailure = true;
}
}
void XMLCALL XmlShallowValidator::skippedentity(
void *data,
const XML_Char *entityName,
int is_parameter_entity)
{
if (is_parameter_entity)
return;
XmlShallowValidatorData *vd;
vd = (XmlShallowValidatorData *)data;
if (vd->entitySet.find(entityName) != vd->entitySet.end())
return;
vd->isValid = false;
vd->positionVector.push_back(
make_pair(XML_GetCurrentLineNumber(vd->p), XML_GetCurrentColumnNumber(vd->p)));
}
bool XmlShallowValidator::isValid()
{
return vd->isValid;
}
std::vector<std::pair<int, int> > XmlShallowValidator::getPositionVector()
{
return vd->positionVector;
}
bool XmlShallowValidator::getOverrideFailure()
{
return vd->overrideFailure;
}
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.