SemanticTreeFactory.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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 };
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 };