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
|
/*
* libopenraw - xmlhandler.cpp
*
* Copyright (C) 2008 Hubert Figuiere
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include "xmlhandler.h"
namespace xml {
bool LtString::operator()(const xmlChar* s1, const xmlChar* s2) const
{
return strcmp((const char *)s1, (const char *)s2) < 0;
}
Context::Context(const HandlerPtr & handler)
: m_handler(handler)
{
}
Context::~Context()
{
}
ContextPtr Context::startElement(const int32_t /*element*/)
{
return shared_from_this();
}
void Context::endElement(const int32_t /*element*/)
{
}
void Context::appendText(const xmlChar * /*content*/)
{
}
Handler::Handler(const std::string & filename)
: Context(HandlerPtr()),
m_reader(xmlNewTextReaderFilename(filename.c_str()))
{
}
Handler::~Handler()
{
if(m_reader != NULL) {
xmlFreeTextReader(m_reader);
}
}
void Handler::mapTags(const tag_map_definition_t * map)
{
m_tag_map.clear();
const tag_map_definition_t *ptag = map;
while(ptag->first != 0) {
m_tag_map.insert(std::make_pair((const xmlChar*)ptag->first, ptag->second));
ptag++;
}
}
int32_t Handler::getTagId(const xmlChar * tag)
{
if(tag == NULL) {
return 0;
}
tag_map_t::const_iterator iter = m_tag_map.find(tag);
if(iter == m_tag_map.end()) {
return 0;
}
return iter->second;
}
bool Handler::process()
{
if(m_reader == NULL) {
return false;
}
m_contexts.push(shared_from_this());
int ret = xmlTextReaderRead(m_reader);
while(ret == 1) {
int node_type = xmlTextReaderNodeType(m_reader);
switch(node_type) {
case XML_READER_TYPE_ELEMENT:
{
int32_t element = getTagId(xmlTextReaderConstName(m_reader));
ContextPtr context = m_contexts.top()->startElement(element);
m_contexts.push(context);
break;
}
case XML_READER_TYPE_TEXT:
{
const xmlChar * content = xmlTextReaderConstValue(m_reader);
m_contexts.top()->appendText(content);
break;
}
case XML_READER_TYPE_END_ELEMENT:
{
int32_t element = getTagId(xmlTextReaderConstName(m_reader));
m_contexts.top()->endElement(element);
m_contexts.pop();
break;
}
default:
break;
}
ret = xmlTextReaderRead(m_reader);
}
// make sure we clear the contexts.
while(!m_contexts.empty()) {
m_contexts.pop();
}
return true;
}
SimpleElementContext::SimpleElementContext(const HandlerPtr & handler,
std::string & content)
: Context(handler),
m_content(content)
{
}
void SimpleElementContext::appendText(const xmlChar * content)
{
m_content += (const char*)content;
}
}
|