SemanticTreeFactory.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright 2007, 2008, 2009, 2010, 2011 Instituto de Sistemas e Robotica, Instituto Superior Tecnico
00003 
00004 This file is part of MeRMaID.
00005 
00006 MeRMaID is free software: you can redistribute it and/or modify
00007 it under the terms of the GNU Lesser General Public License as published by
00008 the Free Software Foundation, either version 3 of the License, or
00009 (at your option) any later version.
00010 
00011 MeRMaID is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public License
00017 along with MeRMaID.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 
00021 
00022 /**
00023  * @Filename SemanticTreeFactory.cpp
00024  * @Description SemanticTreeFactory class implementation.
00025  * @Status Implementing
00026  * @Version $Id: SemanticTreeFactory.cpp 1 2011-03-04 18:13:18Z jreis $
00027  * @Maintainer Marco Barbosa + Nelson Ramos
00028  */
00029 
00030 #include "config.h"
00031 
00032 #include "SemanticTreeFactory.hpp"
00033 
00034 
00035 
00036 #include <Exception.hpp>
00037 #include <XmlElement.hpp>
00038 
00039 #include <string>
00040 
00041 using namespace mermaid::support::data;
00042 
00043 using mermaid::support::errorhandling::Exception;
00044 using boost::shared_ptr;
00045 
00046 using mermaid::support::xml::XmlCharData;
00047 using mermaid::support::xml::XmlElementVector;
00048 
00049 shared_ptr<SemanticTree> SemanticTreeFactory::buildSemanticTree (XmlDocument * doc)
00050 {
00051 
00052   shared_ptr<XmlElement> root;
00053   if (doc == NULL) {
00054     throw Exception ("SemanticTreeFactory::buildSemanticTree : NULL arguments");
00055   }
00056   
00057   if (! (doc->isWellFormed()) && (doc->isValid())) {
00058     throw Exception ("SemanticTreeFactory::buildSemanticTree : XML is not well formed or valid");
00059   }
00060   
00061   root = doc->getRootElement();
00062   
00063   XmlElementVector childrenElements = root->getChildrenElements();
00064   shared_ptr<XmlElement> rootNodeElement = childrenElements[0];
00065   shared_ptr<SemanticNode>  n;
00066   n = buildSemanticNode (rootNodeElement);
00067   SemanticTree * tree = new SemanticTree (n);
00068   return shared_ptr<SemanticTree> (tree);
00069 }; // buildSemanticTree
00070 
00071 shared_ptr<SemanticNode> SemanticTreeFactory::buildSemanticNode (shared_ptr<XmlElement> element)
00072 {
00073   SemanticNode * node = new SemanticNode();
00074   
00075   XmlElementVector childrenElements = element->getChildrenElements();
00076   shared_ptr<XmlElement> nameElement = childrenElements.getFirstElementWithName ("name");
00077   
00078   if (nameElement == false) {
00079     throw Exception ("SemanticTreeFactory::buildSemanticNode : unable to get name element");
00080   }
00081   
00082   shared_ptr<XmlCharData> xmlChar = nameElement->getFirstNonBlankCharData();
00083   xmlChar->removeEndBlanks();
00084   node->name_ = *xmlChar;
00085   
00086   XmlElementVector childNodes = childrenElements.getElementsWithName ("semantic-node");
00087   XmlElementVector::iterator it;
00088   
00089   for (it = childNodes.begin(); it != childNodes.end(); it++) {
00090     shared_ptr<XmlElement> childElement = *it;
00091     shared_ptr<SemanticNode> childNode;
00092     childNode = buildSemanticNode (childElement);
00093     node->addChild (childNode);
00094   }
00095   
00096   return shared_ptr<SemanticNode> (node);
00097 }; // buildSemanticNode
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3