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

181 lines
4.7 KiB
C++
Raw Normal View History

2008-01-06 23:13:01 +01:00
/*
* Copyright 2005-2007 Gerald Schmidt.
2008-01-06 23:13:01 +01:00
*
* This file is part of Xml Copy Editor.
2008-01-06 23:13:01 +01:00
*
* 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.
2008-01-06 23:13:01 +01:00
*
* 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.
2008-01-06 23:13:01 +01:00
*
* 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
*/
2008-01-06 23:13:01 +01:00
#include <iostream>
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,
2008-01-06 23:13:01 +01:00
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 );
//XML_SetExternalEntityRefHandler ( p, externalentity );
}
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
{
2008-01-06 23:13:01 +01:00
XmlShallowValidatorData *vd;
vd = ( XmlShallowValidatorData * ) data;
2007-09-08 00:25:30 +02:00
2008-01-06 23:13:01 +01:00
if ( XML_GetCurrentLineNumber ( vd->p ) > ( unsigned ) ( vd->maxLine + 1 ) )
{
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 ) ) );
}
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
{
2008-01-06 23:13:01 +01: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
{
2008-01-06 23:13:01 +01: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 ) ) );
}
/*
int XMLCALL XmlShallowValidator::externalentity (
XML_Parser p,
const XML_Char *context,
const XML_Char *base,
const XML_Char *systemId,
const XML_Char *publicId)
{
return XML_STATUS_OK;
2007-09-07 23:17:30 +02:00
}
2008-01-06 23:13:01 +01:00
*/
2007-09-07 23:17:30 +02:00
bool XmlShallowValidator::isValid()
{
2008-01-06 23:13:01 +01:00
return vd->isValid;
2007-09-07 23:17:30 +02:00
}
std::vector<std::pair<int, int> > XmlShallowValidator::getPositionVector()
{
2008-01-06 23:13:01 +01:00
return vd->positionVector;
2007-09-07 23:17:30 +02:00
}
bool XmlShallowValidator::getOverrideFailure()
{
2008-01-06 23:13:01 +01:00
return vd->overrideFailure;
2007-09-07 23:17:30 +02:00
}