OW_XMLParserDOM.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
00029 *******************************************************************************/
00030 
00036 #include "OW_config.h"
00037 #include "OW_XMLParserDOM.hpp"
00038 #include "OW_XMLParserSAX.hpp"
00039 #include "OW_XMLNode.hpp"
00040 #include "OW_XMLParserCore.hpp" // for XMLToken
00041 #include "OW_XMLParseException.hpp"
00042 #include "OW_Format.hpp"
00043 #include "OW_ExceptionIds.hpp"
00044 
00045 namespace OW_NAMESPACE
00046 {
00047 
00048 OW_DEFINE_EXCEPTION_WITH_ID(DOM);
00049 
00050 using std::istream;
00051 
00052 namespace
00053 {
00054 
00055 class DOMDocumentBuilder : public XMLParserSAX::SAXDocumentHandler
00056 {
00057 public:
00058    virtual void endDocument()
00059    {
00060    }
00061    virtual void startElement(const XMLToken &entry)
00062    {
00063       XMLAttributeArray newAttrArray;
00064       unsigned int len = entry.attributeCount;
00065       for (unsigned int index = 0; index < len; index++)
00066       {
00067          String nodeName = entry.attributes[index].name.toString();
00068          String nodeValue = entry.attributes[index].value.toString();
00069    
00070          XMLAttribute newAttribute(nodeName, nodeValue);
00071          newAttrArray.append(newAttribute);
00072       }
00073    
00074       XMLNode newNode(entry.text.toString(), newAttrArray);
00075    
00076       if (newNode)
00077       {
00078          if (!m_topNode)
00079          {
00080             m_topNode = newNode;
00081          }
00082    
00083          // If there is anything in the array, this is the child of that last guy
00084          if (m_nodeArray.size() > 0)
00085          {
00086             XMLNode parent = m_nodeArray[m_nodeArray.size() - 1];
00087             parent.addChild(newNode);
00088          }
00089    
00090          m_nodeArray.push_back(newNode);
00091       }
00092    }
00093    
00094    // name is not unescaped (implementation has to do it if necessary)
00095    virtual void endElement(const StringBuffer &name)
00096    {
00097       // at the end of the element, we just need to pop a node
00098       // off the stack
00099       m_nodeArray.pop_back();
00100    }
00101    // chars is not unescaped (implementation has to do it if necessary)
00102    virtual void characters(const StringBuffer &chars)
00103    {
00104       if (m_nodeArray.size() > 0)
00105       {
00106          XMLNode curNode = m_nodeArray[m_nodeArray.size() - 1];
00107          if (curNode != 0)
00108          {
00109             String utxt = chars.toString();
00110             curNode.appendText(utxt);
00111          }
00112       }
00113    }
00114    virtual void startDocument()
00115    {
00116    }
00117 
00118    XMLNode getDocument() const
00119    {
00120       return m_topNode;
00121    }
00122 
00123 private :
00124    
00125    XMLNode m_currentNode;
00126    XMLNode m_topNode;
00127    XMLNodeArray m_nodeArray;
00128 
00129 };
00130 
00131 class DOMErrorHandler : public XMLParserSAX::SAXErrorHandler
00132 {
00133 public:
00134    virtual void warning(const XMLParseException &exception)
00135    {
00136    }
00137    virtual void error(const XMLParseException &exception)
00138    {
00139       OW_THROW(DOMException, Format("Error in XML: "
00140                "line %1, Message: %3",
00141                exception.getLine(),
00142                exception.getMessage()).c_str());
00143    }
00144    virtual void fatalError(const XMLParseException &exception)
00145    {
00146       OW_THROW(DOMException, Format("Fatal error in XML: "
00147                "line %1, Message: %2",
00148                exception.getLine(),
00149                exception.getMessage()).c_str());
00150    }
00151 };
00152 
00153 } // end unnamed namespace
00154 
00155 namespace XMLParserDOM
00156 {
00157 
00158 XMLNode 
00159 parse(const String& xmlData)
00160 {
00161    DOMDocumentBuilder docHandler;
00162    DOMErrorHandler errHandler;
00163    XMLParserSAX::parse(xmlData, docHandler, errHandler);
00164    XMLNode rv = docHandler.getDocument();
00165    if (!rv)
00166    {
00167       OW_THROW(DOMException, "No document found");
00168    }
00169    return rv;
00170 }
00171 
00172 XMLNode
00173 parse(istream& data)
00174 {
00175    DOMDocumentBuilder docHandler;
00176    DOMErrorHandler errHandler;
00177    XMLParserSAX::parse(data, docHandler, errHandler);
00178    XMLNode rv = docHandler.getDocument();
00179    if (!rv)
00180    {
00181       OW_THROW(DOMException, "No document found");
00182    }
00183    return rv;
00184 }
00185 
00186 } // end namespace XMLParserDOM
00187 } // end namespace OW_NAMESPACE
00188 
00189 

Generated on Thu Feb 9 08:48:19 2006 for openwbem by  doxygen 1.4.6