#include "associationsSerializerXML.h"
#include "channelSerializerXML.h"
#include "metadataXML.h"
#include "metadataXMLPluginStrings.h"
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <maya/MString.h>
#include <maya/MStringResource.h>
#include <maya/adskDataAssociations.h>
#include <maya/adskDataChannel.h>
#include <maya/adskDataAssociationsSerializer.h>
#include <maya/adskDataChannelSerializer.h>
#include <assert.h>
#include <libxml/globals.h>
#include <libxml/xmlreader.h>
#include <libxml/parser.h>
using namespace adsk::Data::XML;
AssociationsSerializerXML::AssociationsSerializerXML()
{
}
AssociationsSerializerXML::~AssociationsSerializerXML ()
{
}
AssociationsSerializerXML::read(
std::istream& cSrc,
std::string& errors ) const
{
unsigned int errorCount = 0;
errors = "";
xmlDocPtr doc = NULL;
LIBXML_TEST_VERSION
cSrc.seekg (0, std::ios::end);
int size = (int) cSrc.tellg();
char* memblock = new char [size];
cSrc.seekg (0, std::ios::beg);
cSrc.read (memblock, size);
doc = xmlReadMemory(memblock, size, NULL, NULL, 0);
xmlNode* mainNode = Util::findNamedNode( xmlDocGetRootElement(doc), xmlTagAssociations );
if( ! mainNode )
{
return NULL;
}
newAssociations = parseDOM( doc, *mainNode, errorCount, errors );
delete[] memblock;
xmlFreeDoc(doc);
if( errorCount > 0 )
{
delete newAssociations;
newAssociations = NULL;
}
return newAssociations;
}
AssociationsSerializerXML::parseDOM(
xmlDocPtr doc,
xmlNode& associationsNode,
unsigned int& errorCount,
std::string& errors ) const
{
const ChannelSerializerXML* xmlChannelSerializer = dynamic_cast<const ChannelSerializerXML*>( adsk::Data::ChannelSerializer::formatByName( xmlFormatType ) );
if( ! xmlChannelSerializer )
{
REPORT_ERROR(kAssociationsXMLChannelSerializerMissing);
return NULL;
}
for( xmlNode* channelNode = associationsNode.children;
channelNode; channelNode = channelNode->next )
{
if( (channelNode->type != XML_ELEMENT_NODE)
|| (0 != strcmp((const char*)channelNode->name, xmlTagChannel) ) )
{
continue;
}
if( newChannel )
{
REPORT_ERROR_AT_LINE(kChannelXMLTooManyChannels, channelNode->line);
continue;
}
newChannel = xmlChannelSerializer->parseDOM( doc, *channelNode, errorCount, errors );
if( newChannel && (errorCount == 0) )
{
}
delete newChannel;
}
if( errorCount > 0 )
{
delete newAssociations;
newAssociations = NULL;
}
return newAssociations;
}
int
AssociationsSerializerXML::write(
std::ostream& cDst,
std::string& errors ) const
{
unsigned int errorCount = 0;
const ChannelSerializerXML* xmlChannelSerializer = dynamic_cast<const ChannelSerializerXML*>( adsk::Data::ChannelSerializer::formatByName( xmlFormatType ) );
if( ! xmlChannelSerializer )
{
REPORT_ERROR_AT_LINE(kAssociationsXMLChannelSerializerMissing, 0.0);
return 1;
}
cDst << "<?xml version='1.0' encoding='UTF-8'?>" << std::endl;
cDst << "<" << xmlTagAssociations << ">" << std::endl;
{
errorCount += xmlChannelSerializer->write( theChannel, cDst, errors );
}
cDst << "</" << xmlTagAssociations << ">" << std::endl;
return errorCount;
}
void
AssociationsSerializerXML::getFormatDescription(
std::ostream& info ) const
{
}