xml-copy-editor-code/src/xmlshallowvalidator.cpp

170 lines
5.1 KiB
C++
Raw Normal View History

/*
* 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
*/
2007-09-07 23:17:30 +02:00
#include <string>
#include <vector>
#include <stdexcept>
#include <expat.h>
#include <map>
#include <set>
#include "xmlshallowvalidator.h"
2007-09-08 00:25:30 +02:00
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 );
}
2007-09-07 23:17:30 +02:00
XmlShallowValidator::~XmlShallowValidator()
2007-09-08 00:25:30 +02:00
{}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
void XMLCALL XmlShallowValidator::start ( void *data,
const XML_Char *el,
const XML_Char **attr )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
XmlShallowValidatorData *vd;
vd = ( XmlShallowValidatorData * ) data;
2007-09-07 23:17:30 +02:00
#ifdef __WXMSW__
2007-09-08 00:25:30 +02:00
if ( XML_GetCurrentLineNumber ( vd->p ) > ( unsigned ) ( vd->maxLine + 1 ) )
2007-09-07 23:17:30 +02:00
#else
2007-09-08 00:25:30 +02:00
if ( XML_GetCurrentLineNumber ( vd->p ) > ( vd->maxLine + 1 ) )
2007-09-07 23:17:30 +02:00
#endif
2007-09-08 00:25:30 +02:00
{
XML_StopParser ( vd->p, true );
}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
vd->push ( el );
++ ( vd->depth );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
//check element ok
std::string parent = vd->getParent();
if ( parent.empty() )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( vd->elementMap.empty() )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
if ( !vd->elementMap[parent].count ( vd->getElement() ) )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
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 ) ) );
2007-09-07 23:17:30 +02:00
}
}
2007-09-08 00:25:30 +02:00
void XMLCALL XmlShallowValidator::end ( void *data, const XML_Char *el )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
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;
}
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void XMLCALL XmlShallowValidator::skippedentity (
void *data,
const XML_Char *entityName,
int is_parameter_entity )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
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 ) ) );
2007-09-07 23:17:30 +02:00
}
bool XmlShallowValidator::isValid()
{
2007-09-08 00:25:30 +02:00
return vd->isValid;
2007-09-07 23:17:30 +02:00
}
std::vector<std::pair<int, int> > XmlShallowValidator::getPositionVector()
{
2007-09-08 00:25:30 +02:00
return vd->positionVector;
2007-09-07 23:17:30 +02:00
}
bool XmlShallowValidator::getOverrideFailure()
{
2007-09-08 00:25:30 +02:00
return vd->overrideFailure;
2007-09-07 23:17:30 +02:00
}