rspec_parser_helper.cc 4.17 KB
Newer Older
1 2
/*
 * Copyright (c) 2010 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * {{{EMULAB-LICENSE
 * 
 * This file is part of the Emulab network testbed software.
 * 
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * }}}
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 */

/*
 * Header for RSPEC parser files
 */

# ifdef WITH_XML

#include "rspec_parser_helper.h"
#include "rspec_parser.h"
#include "xmlhelpers.h"

#include <string>
#include "xstr.h"
#include <xercesc/dom/DOM.hpp>

// Returns the attribute value and an out paramter if the attribute exists
string rspec_parser_helper :: getAttribute(const DOMElement* tag, 
40 41
					   const string attrName,
					   bool& hasAttr)
42
{
43 44 45 46 47
  hasAttr = tag->hasAttribute(XStr(attrName.c_str()).x());
  string rv = "";
  if (hasAttr)
    rv = XStr(tag->getAttribute(XStr(attrName.c_str()).x())).c();
  return rv;
48 49 50
}

string rspec_parser_helper :: getAttribute(const DOMElement* tag, 
51
					   const string attr)
52
{
53 54
  bool dummy = false;
  return (this->getAttribute(tag, attr, dummy));
55 56 57
}

bool rspec_parser_helper :: hasAttribute(const DOMElement* tag, 
58
					 const string attrName)
59
{
60
  return (tag->hasAttribute(XStr(attrName).x()));
61 62 63
}

string rspec_parser_helper :: readChild (const DOMElement* elt, 
64 65
					 const char* tagName,
					 bool& hasTag)
66
{
67 68 69 70 71
  hasTag = hasChildTag(elt, tagName);
  string rv = "";
  if (hasTag)
    rv =  XStr(getChildValue(elt, tagName)).c();
  return rv;
72 73 74
}

string rspec_parser_helper :: readChild (const DOMElement* elt, 
75
					 const char* tagName)
76
{
77 78
  bool dummy = false;
  return (this->readChild(elt, tagName, dummy));
79 80 81
}

bool rspec_parser_helper::hasChild (const DOMElement* elt, 
82
				    const char* childName)
83
{
84 85
  return ((elt->getElementsByTagName(XStr(childName).x()))->getLength() 
	  != 0);
86 87 88 89
}

string rspec_parser_helper :: numToString(int num)
{
90 91 92
  std::ostringstream oss;
  oss << num;
  return oss.str();
93 94 95 96
}

string rspec_parser_helper :: numToString(double num)
{
97 98
  std::ostringstream oss;
  oss << num;
99 100 101 102 103
	return oss.str();
}

float rspec_parser_helper :: stringToNum (string s)
{
104 105 106 107
  float num;
  std::istringstream iss(s);
  iss >> num;
  return num;
108 109 110 111
}

int rspec_parser_helper :: getRspecVersion (DOMElement* root)
{
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  string schemaLocAttr 
    =  (XStr(root->getAttribute(XStr("xsi:schemaLocation").x())).c());
  string schemaLocation = schemaLocAttr.substr(0,schemaLocAttr.find(' '));
  float tmpRspecVersion 
    =  rspec_parser_helper::stringToNum
    (schemaLocation.substr(schemaLocation.rfind('/')+1));
  // XXX: There's an obvious bug here
  // Since floating point equality is dangerous at best, this is the closest
  // you can get without doing a string comparison on schemaLocation
  if (tmpRspecVersion > 0 && tmpRspecVersion < 1)
    return 1;
  return (int)tmpRspecVersion;
}

string rspec_parser_helper :: convertType (string hwType, string slType) 
{
  string sep = "@";
  return (hwType + sep + slType);
}

string rspec_parser_helper :: convertType (string type)
{
  string hwType = type;
  string slType = "raw-pc";
  int pos  = type.find("vm");
  if (pos != string::npos) {
    slType = "openvz";
    hwType = type.substr(0, pos) + type.substr(pos+2, type.length() - (pos+2));
  }
  return (rspec_parser_helper::convertType(hwType, slType));
142 143
}

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
vector<DOMElement*> 
rspec_parser_helper::getChildrenByName (const DOMElement* tag,
					const char* name)
{
  vector<DOMElement*> rv;
  DOMNodeList* children = tag->getElementsByTagName(XStr(name).x());
  for (int i = 0; i < children->getLength(); i++) {
    DOMNode* child = children->item(i);
    if (dynamic_cast<DOMElement*>(child->getParentNode()) == tag) {
      rv.push_back(dynamic_cast<DOMElement*>(child));
    }
  }
  return rv;
}

159
#endif //WITH_XML