PCMOutputNode.cpp

Go to the documentation of this file.
00001 
00002 //
00003 // This source file is a part of ParCompMark
00004 // Parallel Compositing Benchmark Framework
00005 //
00006 // for latest info see http://parcompmark.sourceforge.net
00007 
00008 //
00009 // Copyright (C) 2006 IT2 ParCompMark Dev. Team
00010 // 
00011 // This program is free software; you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License
00013 // as published by the Free Software Foundation; either version 2
00014 // of the License, or (at your option) any later version.
00015 // 
00016 // This program is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 // GNU General Public License for more details.
00020 // 
00021 // You should have received a copy of the GNU General Public License
00022 // along with this program; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00024 
00025 //
00026 // Inner includes
00027 //
00028 
00029 #include "../include/PCMOutputNode.h"
00030 
00031 //
00032 // Outer includes
00033 //
00034 
00035 #include "tinyxml/tinyxml.h"
00036 
00037 namespace ParCompMark
00038 {
00039 
00040   //
00041   // Class constants
00042   //
00043 
00044   const std::string OutputNode::TEXTNODENAME = "#text";
00045 
00046   //
00047   // Constructors & destructor
00048   //
00049 
00050    OutputNode::OutputNode(const std::string & name, const NodeType & type):
00051         // Parent constructor call 
00052    Name(name                    /* Name of the node */
00053   ),
00054         // Type of the node 
00055    mType(type           /* Type of the node */
00056         )
00057         // You have to initialize the following attributes:
00058         // - mType
00059         // - mAttributes
00060         // - mChildren
00061         // - mText
00062   {
00063         Assert(_testXMLName(name), INVALID_OPERATION_ERROR, "OutputNode::OutputNode");
00064 
00065         // STL autoinitialization
00066   }
00067 
00068  /*----------------------------------------------------------------------*/
00069 
00070   OutputNode::OutputNode(const std::string & text):
00071         // Parent constructor call 
00072    Name(TEXTNODENAME            /* Name of the node */
00073   ),
00074         // Type of the node 
00075    mType(OutputNode::TEXT       /* Text field type */
00076   ),
00077         // Text data 
00078    mText(text           /* Text data */
00079         )
00080         // You have to initialize the following attributes:
00081         // - mType
00082         // - mAttributes
00083         // - mChildren
00084         // - mText
00085   {
00086         // STL autoinitialization
00087   }
00088 
00089  /*----------------------------------------------------------------------*/
00090 
00091   OutputNode::~OutputNode()
00092   {
00093         // STL autofinalization
00094   }
00095 
00096  /*----------------------------------------------------------------------*/
00097 
00098   //
00099   // Class methods
00100   //
00101 
00102   OutputNode::Pointer OutputNode::parseFromXML(std::string & xmlDocument)
00103   {
00104 
00109         OutputNode::Pointer root;
00110 
00111         return root;
00112   }
00113 
00114  /*----------------------------------------------------------------------*/
00115 
00116   bool OutputNode::_testXMLName(const std::string & name)
00117   {
00118         // TODO: implement
00119 
00120         return true;
00121   }
00122 
00123  /*----------------------------------------------------------------------*/
00124 
00125   std::string OutputNode::_convertSpecialChars(const std::string & string)
00126   {
00127         // TODO: implement
00128 
00129         return string;
00130   }
00131 
00132  /*----------------------------------------------------------------------*/
00133 
00134   //
00135   // Methods
00136   //
00137 
00138   OutputNode::Pointer OutputNode::createChildNode(const std::string & name, const NodeType & type)
00139   {
00140         Assert(mType != OutputNode::TEXT
00141            && mType != OutputNode::CDATA, INVALID_OPERATION_ERROR, "OutputNode::createChildNode");
00142 
00143         // Create child and put into list and map
00144         OutputNode::Pointer child(new OutputNode(name, type));
00145         mChildren.push_back(child);
00146 
00147         return child;
00148   }
00149 
00150  /*----------------------------------------------------------------------*/
00151 
00152   OutputNode::Pointer OutputNode::createChildNode(const std::string & text)
00153   {
00154         Assert(mType != OutputNode::TEXT
00155            && mType != OutputNode::CDATA, INVALID_OPERATION_ERROR, "OutputNode::createChildNode");
00156 
00157         // Create child and put into list and map
00158         OutputNode::Pointer child(new OutputNode(text));
00159         mChildren.push_back(child);
00160 
00161         return child;
00162   }
00163 
00164  /*----------------------------------------------------------------------*/
00165 
00166   void OutputNode::addChildNode(OutputNode::Pointer child)
00167   {
00168         Assert(mType != OutputNode::TEXT
00169            && mType != OutputNode::CDATA, INVALID_OPERATION_ERROR, "OutputNode::addChildNode");
00170         Assert(child.getPtr() != this, INVALID_OPERATION_ERROR, "OutputNode::addChildNode");
00171 
00172         // Put child into list and map
00173         mChildren.push_back(child);
00174   }
00175 
00176  /*----------------------------------------------------------------------*/
00177 
00178   u32 OutputNode::getChildCount()
00179   {
00180         return mChildren.size();
00181   }
00182 
00183  /*----------------------------------------------------------------------*/
00184 
00185   OutputNode::Pointer OutputNode::getFirstChildNode()
00186   {
00187         Assert(mType != OutputNode::TEXT && mType != OutputNode::CDATA
00188            && mChildren.size(), INVALID_OPERATION_ERROR, "OutputNode::getFirstChild");
00189         return mChildren.front();
00190   }
00191 
00192  /*----------------------------------------------------------------------*/
00193 
00194   bool OutputNode::hasAttribute(const std::string & attribute) const
00195   {
00196         Assert(mType != OutputNode::TEXT
00197            && mType != OutputNode::CDATA, INVALID_OPERATION_ERROR, "OutputNode::hasAttribute");
00198         return mAttributes.find(attribute) != mAttributes.end();
00199   }
00200 
00201  /*----------------------------------------------------------------------*/
00202 
00203   const std::string & OutputNode::getAttribute(const std::string & attribute) const
00204   {
00205         Assert(mType != OutputNode::TEXT
00206            && mType != OutputNode::CDATA, INVALID_OPERATION_ERROR, "OutputNode::getAttribute");
00207         AttributeMapIterator i = mAttributes.find(attribute);
00208 
00209         // Exception if the child does not exist
00210         Assert(i != mAttributes.end(), INVALID_NAME_ERROR, "OutputNode::getAttribute");
00211 
00212         return i->second;
00213   }
00214 
00215  /*----------------------------------------------------------------------*/
00216 
00217   void OutputNode::setAttribute(const std::string & attribute, const std::string & value)
00218   {
00219         Assert(_testXMLName(attribute), INVALID_OPERATION_ERROR, "OutputNode::setAttribute");
00220         Assert(mType != OutputNode::TEXT
00221            && mType != OutputNode::CDATA, INVALID_OPERATION_ERROR, "OutputNode::createAttribute");
00222 
00223         mAttributes[attribute] = value;
00224   }
00225 
00226  /*----------------------------------------------------------------------*/
00227 
00228   std::ostringstream & OutputNode::serialize2XML(std::ostringstream & osstr)
00229   {
00230 
00231         // Textual node
00232         if(mType == OutputNode::TEXT)
00233         {
00234          osstr << mText;
00235         } else if(mType == OutputNode::CDATA)
00236         {
00237          osstr << "<![CDATA[" << mText << "]]>";
00238         }
00239         // Nontextual node
00240         else
00241         {
00242 
00243          // XML namespace
00244          std::string xmlNameSpace;
00245 
00246          switch (mType)
00247          {
00248                 case OutputNode::DEFINITION:
00249                  xmlNameSpace = "def:";
00250                  break;
00251                 case OutputNode::INFORMATION:
00252                  xmlNameSpace = "info:";
00253                  break;
00254                 case OutputNode::REFERENCE:
00255                  xmlNameSpace = "ref:";
00256                  break;
00257                 case OutputNode::STATISTICS:
00258                  xmlNameSpace = "stat:";
00259                  break;
00260                 default:
00261                  xmlNameSpace = "";
00262          }
00263 
00264          osstr << '<' << xmlNameSpace;
00265 
00266          // Node name
00267          osstr << mName;
00268 
00269          // Attributes
00270          for(AttributeMapIterator i = mAttributes.begin(); i != mAttributes.end(); i++)
00271                 osstr << ' ' << i->first << "=\"" << _convertSpecialChars(i->second) << '\"';
00272 
00273          // Children
00274          if(mChildren.size())
00275          {
00276                 osstr << ">";
00277                 for(ChildNodeListIterator i = mChildren.begin(); i != mChildren.end(); i++)
00278                  (*i).getPtr()->serialize2XML(osstr);
00279                 osstr << "</" << xmlNameSpace << mName << ">";
00280          } else
00281          {
00282                 osstr << "/>";
00283          }
00284         }
00285 
00286         // Test error 
00287         Assert(!osstr.bad(), INVALID_OPERATION_ERROR, "OutputNode::serialize2XML");
00288 
00289         return osstr;
00290   }
00291 
00292  /*----------------------------------------------------------------------*/
00293 
00294   std::string OutputNode::serialize2XML()
00295   {
00296         std::ostringstream osstr;
00297         return serialize2XML(osstr).str();
00298   }
00299 
00300  /*----------------------------------------------------------------------*/
00301 
00302   void OutputNode::refreshData()
00303   {
00304         // Nothing to do
00305   }
00306 
00307  /*----------------------------------------------------------------------*/
00308 
00309   void OutputNode::clean()
00310   {
00311         for(ChildNodeListIterator i = mChildren.begin(); i != mChildren.end(); i++)
00312         {
00313 
00314          i->getPtr()->clean();
00315 
00316          i->kill();
00317 
00318         }
00319 
00320         mChildren.clear();
00321 
00322   }
00323 
00324  /*----------------------------------------------------------------------*/
00325 
00326 }