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
|
// -*-c++-*-
// fixg2sxd - a utility to convert fig to sxd format
// Copyright (C) 2003-2022 Alexander Bürger, acfb@users.sourceforge.net
// This program 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; either version 2 of the
// License, or (at your option) any later version.
//
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#include "xmlwrite.h"
using namespace std;
string indent(int l)
{
ostringstream s;
for( int i=0; i<l; ++i )
s.put(' ');
return s.str();
}
// ------------------------------------------------------------------------
void TextElement::write(ostream& out, int /*level*/, bool /*pretty*/) const
{
out << text.str();
}
// ------------------------------------------------------------------------
Node::Node( string const& aName )
: name( aName ),
indentation(true)
{
}
Node::~Node()
{
for( attributes_t::iterator i = attributes.begin();
i != attributes.end(); ++i )
{
delete i->second;
}
for( vector<Element*>::iterator i = elements.begin();
i != elements.end(); ++i )
{
delete *i;
}
}
ostringstream& Node::att( string const& attname )
{
#ifdef USE_MAP
attributes_t::iterator i = attributes.find( attname );
#else
attributes_t::iterator i = attributes.begin();
while( i != attributes.end() && i->first != attname )
++i;
#endif
if( i != attributes.end() ) {
delete i->second;
#ifndef USE_MAP
attributes.erase(i);
#endif
}
ostringstream* o = new ostringstream;
#ifdef USE_MAP
attributes[ attname ] = o;
#else
attributes.push_back( pair<string,ostringstream*>( attname, o ) );
#endif
return *o;
}
Node& Node::subnode( string const& subname )
{
Node* n = new Node( subname );
elements.push_back( n );
return *n;
}
TextElement& Node::text()
{
TextElement* t = new TextElement();
elements.push_back( t );
indentation = false;
return *t;
}
void Node::write(ostream& out, int level, bool pretty) const
{
out << indent(level) << '<' << name;
for( attributes_t::const_iterator i = attributes.begin();
i != attributes.end(); ++i )
{
out << ' ' << i->first << "=\"" << i->second->str() << '"';
}
if( elements.empty() ) {
out << "/>";
if( pretty )
out << endl;
} else {
out << ">";
if( pretty && indentation )
out << endl;
for( vector<Element*>::const_iterator i = elements.begin();
i != elements.end(); ++i )
{
(*i)->write( out, level+1, pretty && indentation );
}
if( pretty && indentation )
out << indent(level);
out << "</" << name << '>';
if( pretty )
out << endl;
}
}
// ------------------------------------------------------------------------
#ifdef TEST_XMLWRITE
int main()
{
Node root("office:body");
root["hi"] << "ho";
Node& s = root.subnode("office:bah");
s["att-no"] << "yes";
root.text().t() << "this is text!";
Node& s2 = root.subnode("office:zero");
s2["att-no"] << "yes";
cout << root;
}
#endif // TEST_XMLWRITE
|