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

164 lines
4.0 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
*/
2007-09-07 23:17:30 +02:00
#include <string>
#include <vector>
#include <stdexcept>
#include <expat.h>
#include <map>
#include <set>
#include "xmlfilterreader.h"
using namespace std;
2007-09-08 00:25:30 +02:00
XmlFilterReader::XmlFilterReader() : fd ( new FilterData() )
2007-09-07 23:17:30 +02:00
{
2008-01-06 23:13:01 +01:00
fd->filterCount = 0;
fd->setState ( STATE_UNKNOWN );
XML_SetUserData ( p, fd.get() );
XML_SetElementHandler ( p, start, end );
XML_SetCharacterDataHandler ( p, characterdata );
2007-09-07 23:17:30 +02:00
}
XmlFilterReader::~XmlFilterReader()
2007-09-08 00:25:30 +02:00
{}
2007-09-07 23:17:30 +02:00
int XmlFilterReader::getFilterCount()
{
2008-01-06 23:13:01 +01:00
return fd->filterCount;
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void XmlFilterReader::getExcludeVector ( vector<string> &v )
2007-09-07 23:17:30 +02:00
{
2008-01-06 23:13:01 +01:00
v = fd->excludeVector;
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void XmlFilterReader::getIncludeVector ( vector<string> &v )
2007-09-07 23:17:30 +02:00
{
2008-01-06 23:13:01 +01:00
v = fd->includeVector;
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void XmlFilterReader::getFilterMap (
map<string, map<string, set<string> > > &m
2008-01-06 23:13:01 +01:00
)
2007-09-07 23:17:30 +02:00
{
2008-01-06 23:13:01 +01:00
m = fd->filterMap;
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void XMLCALL XmlFilterReader::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
FilterData *fd;
fd = ( FilterData * ) data;
if ( !strcmp ( el, "element" ) )
fd->setState ( STATE_IN_ELEMENT );
else if ( !strcmp ( el, "attribute" ) )
fd->setState ( STATE_IN_ATTRIBUTE );
else if ( !strcmp ( el, "key" ) )
fd->setState ( STATE_IN_ATTRIBUTE_KEY );
else if ( !strcmp ( el, "value" ) )
fd->setState ( STATE_IN_ATTRIBUTE_VALUE );
else if ( !strcmp ( el, "exclude" ) )
fd->setState ( STATE_IN_EXCLUDE );
else if ( !strcmp ( el, "include" ) )
fd->setState ( STATE_IN_INCLUDE );
else
fd->setState ( STATE_UNKNOWN );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void XMLCALL XmlFilterReader::end ( void *data, const XML_Char *el )
2007-09-07 23:17:30 +02:00
{
2008-01-06 23:13:01 +01:00
FilterData *fd;
fd = ( FilterData * ) data;
if ( !strcmp ( el, "element" ) )
{
fd->filterMap.insert ( make_pair ( fd->elementString, fd->attributeMap ) );
( fd->filterCount ) ++;
fd->elementString = "";
fd->attributeKeyString = "";
fd->attributeValueString = "";
fd->attributeMap.clear();
fd->setState ( STATE_UNKNOWN );
}
else if ( !strcmp ( el, "attribute" ) )
{
fd->attributeMap.insert (
make_pair ( fd->attributeKeyString, fd->attributeValueSet )
);
fd->setState ( STATE_UNKNOWN );
}
else if ( !strcmp ( el, "key" ) )
fd->setState ( STATE_UNKNOWN );
else if ( !strcmp ( el, "value" ) )
{
fd->attributeValueSet.insert ( fd->attributeValueString );
fd->setState ( STATE_UNKNOWN );
fd->attributeValueString = "";
}
// handle excludes/includes
else if ( !strcmp ( el, "exclude" ) )
{
fd->excludeVector.push_back ( fd->excludeString );
fd->excludeString = "";
fd->setState ( STATE_UNKNOWN );
}
else if ( !strcmp ( el, "include" ) )
{
fd->includeVector.push_back ( fd->includeString );
fd->includeString = "";
fd->setState ( STATE_UNKNOWN );
}
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void XMLCALL XmlFilterReader::characterdata (
void *data,
const XML_Char *s,
int len )
2007-09-07 23:17:30 +02:00
{
2008-01-06 23:13:01 +01:00
FilterData *fd;
fd = ( FilterData * ) data;
switch ( fd->getState() )
{
case STATE_IN_ELEMENT:
fd->elementString.append ( s, len );
break;
case STATE_IN_ATTRIBUTE_KEY:
fd->attributeKeyString.append ( s, len );
break;
case STATE_IN_ATTRIBUTE_VALUE:
fd->attributeValueString.append ( s, len );
break;
case STATE_IN_EXCLUDE:
fd->excludeString.append ( s, len );
break;
case STATE_IN_INCLUDE:
fd->includeString.append ( s, len );
break;
default:
break;
}
2007-09-07 23:17:30 +02:00
}