Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

pana_xml_parser.cxx

00001 /* BEGIN_COPYRIGHT                                                        */
00002 /*                                                                        */
00003 /* Open Diameter: Open-source software for the Diameter and               */
00004 /*                Diameter related protocols                              */
00005 /*                                                                        */
00006 /* Copyright (C) 2002-2004 Open Diameter Project                          */
00007 /*                                                                        */
00008 /* This library is free software; you can redistribute it and/or modify   */
00009 /* it under the terms of the GNU Lesser General Public License as         */
00010 /* published by the Free Software Foundation; either version 2.1 of the   */
00011 /* License, or (at your option) any later version.                        */
00012 /*                                                                        */
00013 /* This library is distributed in the hope that it will be useful,        */
00014 /* but WITHOUT ANY WARRANTY; without even the implied warranty of         */
00015 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU      */
00016 /* Lesser General Public License for more details.                        */
00017 /*                                                                        */
00018 /* You should have received a copy of the GNU Lesser General Public       */
00019 /* License along with this library; if not, write to the Free Software    */
00020 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307    */
00021 /* USA.                                                                   */
00022 /*                                                                        */
00023 /* In addition, when you copy and redistribute some or the entire part of */
00024 /* the source code of this software with or without modification, you     */
00025 /* MUST include this copyright notice in each copy.                       */
00026 /*                                                                        */
00027 /* If you make any changes that are appeared to be useful, please send    */
00028 /* sources that include the changed part to                               */
00029 /* diameter-developers@lists.sourceforge.net so that we can reflect your  */
00030 /* changes to one unified version of this software.                       */
00031 /*                                                                        */
00032 /* END_COPYRIGHT                                                          */
00033 
00034 #include "pana_xml_errorreporter.h"
00035 #include "pana_xml_parser.h"
00036 #include <xercesc/util/PlatformUtils.hpp>
00037 #include <xercesc/util/XMLString.hpp>
00038 #include <xercesc/parsers/XercesDOMParser.hpp>
00039 
00040 PANA_XMLElementParser::PANA_XMLElementParser(std::string &tagName)
00041 {
00042    name_ = tagName;
00043 }
00044 
00045 int PANA_XMLElementParser::populate(DOMNode *n, DOMNode **found)
00046 {
00047    DOMNode *sibling = n;
00048    while (sibling != NULL) {
00049 
00050       if (sibling->getNodeType() == DOMNode::ELEMENT_NODE) {
00051 
00052          char *c_str = XMLString::transcode(sibling->getNodeName());
00053 
00054          if (XMLString::compareString(c_str, name_.c_str()) == 0) {
00055 
00056              XMLString::release(&c_str);
00057           
00058             if (found) {
00059                *found = sibling;
00060             }
00061 
00062             return svc(sibling);
00063          }
00064 
00065          XMLString::release(&c_str);
00066       }
00067       sibling = sibling->getNextSibling();
00068    }
00069    return (-1);
00070 }
00071 
00072 char *PANA_XMLElementParser::getTextContent(DOMNode *n)
00073 {
00074    DOMNode *child = n->getFirstChild();
00075    if (child && child->getNodeType() == DOMNode::TEXT_NODE) {
00076     
00077       const XMLCh *xmlCh = child->getNodeValue();
00078       if (xmlCh) {
00079          return XMLString::transcode(xmlCh);
00080       }
00081    }
00082    return (NULL);
00083 }
00084 
00085 int PANA_XMLTreeParser::open(std::string &filename, PANA_XMLElementParser &root)
00086 {
00087    int rcode = (-1);
00088 
00089    try {
00090       XMLPlatformUtils::Initialize();
00091 
00092       XercesDOMParser parser;
00093       XMLDOMTreeErrorReporter errReporter;
00094 
00095       parser.setValidationScheme(XercesDOMParser::Val_Always);
00096       parser.setDoNamespaces(true);
00097       parser.setDoSchema(true);
00098       parser.setErrorHandler(&errReporter);
00099 
00100       try {
00101          parser.parse(filename.data());
00102 
00103          if (parser.getErrorCount() == 0) {
00104             DOMNode *doc = parser.getDocument();
00105             rcode = root.populate(doc->getFirstChild());
00106          }
00107       }
00108       catch (const DOMException& e) {
00109           ACE_UNUSED_ARG(e);
00110       }
00111       catch (const XMLException& e) {
00112           ACE_UNUSED_ARG(e);
00113       }
00114    }
00115    catch (const XMLException& toCatch) {
00116       ACE_UNUSED_ARG(toCatch);
00117    }
00118 
00119    close();
00120    return (rcode);
00121 }
00122 
00123 void PANA_XMLTreeParser::close()
00124 {
00125    XMLPlatformUtils::Terminate();
00126 }
00127 
00128 int PANA_XMLDataString::svc(DOMNode *n)
00129 {
00130    char *c_str = getTextContent(n);
00131    if (c_str) {
00132       payload.assign(c_str);
00133       XMLString::release(&c_str);
00134       return (0);
00135    }
00136    return (-1);
00137 }
00138 
00139 int PANA_XMLDataUInt32::svc(DOMNode *n)
00140 {
00141    char *c_str = getTextContent(n);
00142    if (c_str) {
00143       payload = ACE_OS::atoi(c_str);
00144       XMLString::release(&c_str);
00145       return (0);
00146    }
00147    return (-1);
00148 }
00149 
00150 
00151 
00152 

Generated on Fri Jun 25 19:18:30 2004 for PANA by doxygen 1.3.5