Initial import into git.
[galago.git] / cpp / galago / contrib / indri / src / XMLWriter.cpp
blobbc83bd33d810bd9d357ebf6a2019f718631d8ceb
1 /*==========================================================================
2 * Copyright (c) 2004 University of Massachusetts. All Rights Reserved.
4 * Use of the Lemur Toolkit for Language Modeling and Information Retrieval
5 * is subject to the terms of the software license set forth in the LICENSE
6 * file included with this software, and also available at
7 * http://www.lemurproject.org/license.html
9 *==========================================================================
14 // XMLWriter
16 // 8 October 2003 - tds
19 #include "indri/XMLWriter.hpp"
21 void indri::xml::XMLWriter::_writeChar( char ch, std::string& output ) const {
22 output += ch;
25 void indri::xml::XMLWriter::_writeTabs( int tabs, std::string& output ) const {
26 for( int i=0; i<tabs; i++ ) {
27 _writeChar( '\t', output );
31 void indri::xml::XMLWriter::_writeTag( const std::string& tag, const indri::xml::XMLNode::MAttributes& attributes, std::string& output, bool opening ) const {
32 _writeChar( '<', output );
34 if( !opening ) {
35 _writeChar( '/', output );
37 output += tag;
39 if( opening ) {
40 indri::xml::XMLNode::MAttributes::const_iterator iter;
41 for( iter = attributes.begin(); iter != attributes.end(); iter++ ) {
42 _writeChar( ' ', output );
43 output += iter->first;
45 _writeChar( '=', output );
46 _writeChar( '\"', output );
47 output += iter->second;
48 _writeChar( '\"', output );
52 _writeChar( '>', output );
55 void indri::xml::XMLWriter::_writeEndOfLine( std::string& output ) const {
56 _writeChar( '\n', output );
59 void indri::xml::XMLWriter::_writeXML( int tabs, const indri::xml::XMLNode* node, std::string& output ) const {
60 // write opening tag
61 _writeTabs( tabs, output );
62 _writeTag( node->getName(), node->getAttributes(), output, true );
64 if( node->getChildren().size() ) {
65 _writeEndOfLine( output );
67 for( unsigned int i=0; i<node->getChildren().size(); i++ ) {
68 _writeXML( tabs+1, (node->getChildren())[i], output );
71 _writeTabs( tabs, output );
72 } else {
73 output += node->getValue();
76 _writeTag( node->getName(), node->getAttributes(), output, false );
77 _writeEndOfLine( output );
80 indri::xml::XMLWriter::XMLWriter( indri::xml::XMLNode* node ) : _node(node)
84 void indri::xml::XMLWriter::write( std::string& output ) {
85 _writeXML( 0, _node, output );