/************************************************************
***** Quantin' Leap Ltd *****
***** Copyright (c) 2001, All Rights Reserved *****
*************************************************************/
#include "WU_XMLFactory.h"
#include "WU_Exception.h"
#include <framework/LocalFileInputSource.hpp>
#include <framework/MemBufInputSource.hpp>
#include <util/PlatformUtils.hpp>
#include <dom/DOMString.hpp>
#include <stdlib.h>
WU_XMLFactory::WU_XMLFactory(string xml,
WU_Container* container, XMLSource source)
:m_XMLSource(xml),m_sourceType(source),m_container(container)
{
try
{
XMLPlatformUtils::Initialize();
DOMParser dp;
if (m_sourceType == XML_FILE)
{
XMLCh* file = new XMLCh[200];
file = XMLString::transcode(m_XMLSource.c_str());
LocalFileInputSource fs(file);
dp.parse(fs);
delete [] file;
}
else //memory
{
unsigned int nbyte = m_XMLSource.length();
MemBufInputSource ms
((unsigned char*)m_XMLSource.c_str(), nbyte,"XMLBuffer");
dp.parse(ms);
}
m_doc = dp.getDocument();
}
catch (const XMLException& e)
{
DOMString ds(e.getMessage());
WU_Exception ex
(ds.transcode(),"WU_XMLFactory::WU_XMLFactory");
throw ex;
}
catch (const DOM_DOMException& e)
{
WU_Exception ex
(e.msg.transcode(),"WU_XMLFactory::WU_XMLFactory");
throw ex;
}
catch (...)
{
WU_Exception ex
("Unexpected exception during parsing",
"WU_XMLFactory::WU_XMLFactory");
throw ex;
}
}
WU_XMLFactory::~WU_XMLFactory()
{}
inline string WU_XMLFactory::getNodeValue(string nodeName)
{
string res("");
const DOMString name
(XMLString::transcode(nodeName.c_str()));
DOM_NodeList list=m_doc.getElementsByTagName(name);
if (list.getLength() > 0)
{
DOM_Node n=list.item(0).getFirstChild();
DOMString value = n.getNodeValue();
res = string(value.transcode());
}
return res;
}
string WU_XMLFactory::getSubNodeValue
(string parentNode, string subNode)
{
string res("");
const DOMString parent
(XMLString::transcode(parentNode.c_str()));
DOM_NodeList listp=m_doc.getElementsByTagName(parent);
if (listp.getLength() > 0) //found the parent
{
DOM_Node n=listp.item(0); //found the parent node
DOM_Node target=getNodeRecursive(n,subNode);
if (target != 0) //found the child
{
DOMString value =
target.getFirstChild().getNodeValue();
res = string(value.transcode());
}
}
return res;
}
STRINGVECTOR WU_XMLFactory::getNodeArray(string nodeName)
{
STRINGVECTOR res;
const DOMString name
(XMLString::transcode(nodeName.c_str()));
DOM_NodeList list=m_doc.getElementsByTagName(name);
if (list.getLength() > 0)
{
DOM_Node n=list.item(0);
DOM_NodeList children=n.getChildNodes();
unsigned int numElements = children.getLength();
unsigned int i;
unsigned int cnt = 0;
//this is not needed when using STRINGVECTOR
//as std::vector<string>
for (i=0;i<numElements;i++)
if (children.item(i).getNodeType() == DOM_Node::ELEMENT_NODE)
cnt++;
res.setArraySize(cnt);
//end of: this is not needed when using STRINGVECTOR
//as std::vector<string>
cnt = 0;
DOMString value;
for (i=0;i<numElements;i++)
{
if (children.item(i).getNodeType() == DOM_Node::ELEMENT_NODE)
{
value = children.item(i).getFirstChild().getNodeValue();
res[cnt++] = string(value.transcode());
}
}
}
return res;
}
STRINGVECTOR WU_XMLFactory::getSubNodeArray
(string parentNode, string subNode)
{
STRINGVECTOR res;
const DOMString parent
(XMLString::transcode(parentNode.c_str()));
DOM_NodeList listp=m_doc.getElementsByTagName(parent);
if (listp.getLength() > 0) //found the parent
{
DOM_Node n=listp.item(0); //found the parent node
DOM_Node target=getNodeRecursive(n,subNode);
if (target != 0) //found the child
{
DOM_NodeList children=target.getChildNodes();
unsigned int numElements = children.getLength();
unsigned int i;
unsigned int cnt = 0;
//this is not needed when using STRINGVECTOR
//as std::vector<string>
for (i=0;i<numElements;i++)
if (children.item(i).getNodeType() == DOM_Node::ELEMENT_NODE)
cnt++;
res.setArraySize(cnt);
//end of: this is not needed when using
//STRINGVECTOR as std::vector<string>
cnt = 0;
DOMString value;
for (i=0;i<numElements;i++)
{
if (children.item(i).getNodeType() == DOM_Node::ELEMENT_NODE)
{
value =
children.item(i).getFirstChild().getNodeValue();
res[cnt++] = string(value.transcode());
}
}
}
}
return res;
}
DOM_Node WU_XMLFactory::getNodeRecursive
(DOM_Node parent, string subNodeName)
{
unsigned int i;
DOMString subnode
(XMLString::transcode(subNodeName.c_str()));
string dbg = parent.getNodeName().transcode();
DOM_Node tn;
DOM_NodeList children=parent.getChildNodes();
if (children != 0)
{
for (i=0;i<children.getLength();i++)
{
dbg = children.item(i).getNodeName().transcode();
if (children.item(i).getNodeName().equals(subnode))
return children.item(i);
else
{
tn = getNodeRecursive
(children.item(i),subNodeName);
if (tn == 0) continue;
else return tn;
}
}
}
//we are in the upper level (no parents)
if (parent.getParentNode() == 0)
{
string message= "field " + subNodeName + " not found";
WU_Exception ex
((char *)message.c_str(), "WU_XMLFactory::getNodeRecursive");
throw ex;
}
return tn;
}