xmlwriter.cpp revision 6cd2e86330e1049942b9ce57d4f10bbe2542067d
1269N/A/*
1269N/A * Phoebe DOM Implementation.
1269N/A *
1269N/A * This is a C++ approximation of the W3C DOM model, which follows
1269N/A * fairly closely the specifications in the various .idl files, copies of
1425N/A * which are provided for reference. Most important is this one:
1269N/A *
1269N/A * http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/idl-definitions.html
1269N/A *
1269N/A * Authors:
1269N/A * Bob Jamison
1269N/A *
1269N/A * Copyright (C) 2005-2008 Bob Jamison
1269N/A *
1269N/A * This library is free software; you can redistribute it and/or
1269N/A * modify it under the terms of the GNU Lesser General Public
1269N/A * License as published by the Free Software Foundation; either
1269N/A * version 2.1 of the License, or (at your option) any later version.
1269N/A *
1269N/A * This library is distributed in the hope that it will be useful,
1269N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
1269N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1269N/A * Lesser General Public License for more details.
1269N/A *
1269N/A * You should have received a copy of the GNU Lesser General Public
1269N/A * License along with this library; if not, write to the Free Software
1269N/A * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1269N/A */
1269N/A
1269N/A
1269N/A
1269N/A
1269N/A#include <stdio.h>
1269N/A#include <stdarg.h>
1269N/A
1269N/A#include "xmlwriter.h"
1269N/A
1269N/Anamespace org
1269N/A{
1269N/Anamespace w3c
1269N/A{
1269N/Anamespace dom
1269N/A{
1269N/A
1269N/A
1269N/A
1269N/A//#########################################################################
1269N/A//# O U T P U T
1269N/A//#########################################################################
1269N/A
1425N/A/**
1269N/A *
1269N/A */
1269N/Avoid XmlWriter::spaces()
1269N/A{
1269N/A for (int i=0 ; i<indent ; i++)
1269N/A {
1269N/A buf.push_back(' ');
1269N/A }
1269N/A}
1269N/A
1269N/A/**
1269N/A *
1269N/A */
1269N/Avoid XmlWriter::po(const char *fmt, ...)
1269N/A{
1269N/A char str[257];
1269N/A va_list args;
1269N/A va_start(args, fmt);
1269N/A vsnprintf(str, 256, fmt, args);
1269N/A va_end(args) ;
1269N/A
1269N/A buf.append(str);
1269N/A}
1269N/A
1269N/A
1269N/Avoid XmlWriter::pos(const DOMString &str)
1269N/A{
1269N/A buf.append(str);
1269N/A}
1269N/A
1269N/A/**
1269N/A *
1269N/A */
1269N/Avoid XmlWriter::write(const NodePtr nodeArg)
1269N/A{
1269N/A NodePtr node = nodeArg;
1269N/A
1269N/A indent+=2;
1269N/A
1269N/A NamedNodeMap attributes = node->getAttributes();
1269N/A int nrAttrs = attributes.getLength();
1269N/A
1269N/A //### Start open tag
1269N/A spaces();
1269N/A po("<");
1269N/A pos(node->getNodeName());
1269N/A if (nrAttrs>0)
1269N/A po("\n");
1269N/A
1269N/A //### Attributes
1269N/A for (int i=0 ; i<nrAttrs ; i++)
1269N/A {
1269N/A NodePtr attr = attributes.item(i);
1269N/A spaces();
1269N/A pos(attr->getNodeName());
1269N/A po("=\"");
1269N/A pos(attr->getNodeValue());
1269N/A po("\"\n");
1269N/A }
1269N/A
1269N/A //### Finish open tag
1269N/A if (nrAttrs>0)
1269N/A spaces();
1269N/A po(">\n");
1269N/A
1269N/A //### Contents
1269N/A spaces();
1269N/A pos(node->getNodeValue());
1269N/A
1269N/A //### Children
1269N/A for (NodePtr child = node->getFirstChild() ;
1269N/A child.get() ;
1269N/A child=child->getNextSibling())
1269N/A {
1269N/A write(child);
1269N/A }
1269N/A
1269N/A //### Close tag
1269N/A spaces();
1269N/A po("</");
1269N/A pos(node->getNodeName());
1269N/A po(">\n");
1269N/A
1269N/A indent-=2;
1269N/A}
1269N/A
1269N/A
1269N/A/**
1269N/A *
1269N/A */
1269N/Avoid XmlWriter::writeFile(FILE *f, const NodePtr node)
1269N/A{
1269N/A if (!node)
{
po("XmlWriter: NULL document\n");
return;
}
indent = 0;
//po("Document\n");
buf = "";
write(node);
for (unsigned int i=0 ; i<buf.size() ; i++)
{
int ch = buf[i];
fputc(ch, f);
}
fflush(f);
buf = "";
}
//#########################################################################
//# C O N S T R U C T O R / D E S T R U C T O R
//#########################################################################
/**
*
*/
XmlWriter::XmlWriter() :
indent(0),
buf()
{
}
/**
*
*/
XmlWriter::~XmlWriter()
{
}
} //namespace dom
} //namespace w3c
} //namespace org
//#########################################################################
//# E N D O F F I L E
//#########################################################################