| 1 | /******************************************************************************* |
|---|
| 2 | |
|---|
| 3 | Copyright 2004, Virginia Polytechnic Institute and State University |
|---|
| 4 | |
|---|
| 5 | This file is part of the OSSIE Parser. |
|---|
| 6 | |
|---|
| 7 | OSSIE Parser is free software; you can redistribute it and/or modify |
|---|
| 8 | it under the terms of the Lesser GNU General Public License as published by |
|---|
| 9 | the Free Software Foundation; either version 2.1 of the License, or |
|---|
| 10 | (at your option) any later version. |
|---|
| 11 | |
|---|
| 12 | OSSIE Parser is distributed in the hope that it will be useful, |
|---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | Lesser GNU General Public License for more details. |
|---|
| 16 | |
|---|
| 17 | You should have received a copy of the Lesser GNU General Public License |
|---|
| 18 | along with OSSIE Parser; if not, write to the Free Software |
|---|
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 20 | |
|---|
| 21 | Even though all code is original, the architecture of the OSSIE Parser is based |
|---|
| 22 | on the architecture of the CRC's SCA Reference Implementation (SCARI) |
|---|
| 23 | see: http://www.crc.ca/en/html/rmsc/home/sdr/projects/scari |
|---|
| 24 | |
|---|
| 25 | *********************************************************************************/ |
|---|
| 26 | |
|---|
| 27 | #include "ossie/ComponentInstantiation.h" |
|---|
| 28 | #define DELPTR(x) if (x!=NULL) delete x, x=NULL; |
|---|
| 29 | #define DELARRAY(x) if (x!=NULL) delete []x, x=NULL; |
|---|
| 30 | |
|---|
| 31 | ComponentInstantiation::ComponentInstantiation (): |
|---|
| 32 | instantiationId(NULL), instantiationName(NULL) |
|---|
| 33 | {} |
|---|
| 34 | |
|---|
| 35 | ComponentInstantiation::ComponentInstantiation (DOMElement* _element): |
|---|
| 36 | root(_element), instantiationId(NULL), instantiationName(NULL) |
|---|
| 37 | { |
|---|
| 38 | this->parseElement(); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | ComponentInstantiation::ComponentInstantiation |
|---|
| 42 | (const ComponentInstantiation & _compInst): |
|---|
| 43 | root(_compInst.root), instantiationId(NULL), instantiationName(NULL) |
|---|
| 44 | { |
|---|
| 45 | this->instantiationId = new char[strlen (_compInst.instantiationId) + 1]; |
|---|
| 46 | strcpy (instantiationId, _compInst.instantiationId); |
|---|
| 47 | |
|---|
| 48 | this->instantiationName = new char[strlen (_compInst.instantiationName) + 1]; |
|---|
| 49 | strcpy (instantiationName, _compInst.instantiationName); |
|---|
| 50 | |
|---|
| 51 | for (unsigned int i = 0; i < _compInst.properties.size (); i++) |
|---|
| 52 | this->properties.push_back (_compInst.properties[i]); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | ComponentInstantiation::~ComponentInstantiation() |
|---|
| 56 | { |
|---|
| 57 | DELARRAY(instantiationId); |
|---|
| 58 | DELARRAY(instantiationName); |
|---|
| 59 | |
|---|
| 60 | for (unsigned int i=0; i< properties.size(); i++) // deep clean |
|---|
| 61 | { |
|---|
| 62 | DELPTR(properties[i]); |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | void ComponentInstantiation::parseElement() |
|---|
| 68 | { |
|---|
| 69 | parseID(root); |
|---|
| 70 | parseName(root); |
|---|
| 71 | parseProperties(root); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | void ComponentInstantiation::parseID(DOMElement * _elem) |
|---|
| 75 | { |
|---|
| 76 | tmpXMLStr = XMLString::transcode("id"); |
|---|
| 77 | const XMLCh *_id = _elem->getAttribute(tmpXMLStr); |
|---|
| 78 | XMLString::release(&tmpXMLStr); |
|---|
| 79 | this->instantiationId = XMLString::transcode(_id); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | void ComponentInstantiation::parseName(DOMElement * _elem) |
|---|
| 84 | { |
|---|
| 85 | tmpXMLStr = XMLString::transcode("usagename"); |
|---|
| 86 | DOMNodeList* nodeList = _elem->getElementsByTagName(tmpXMLStr); |
|---|
| 87 | XMLString::release(&tmpXMLStr); |
|---|
| 88 | |
|---|
| 89 | if (nodeList->getLength() != 0) |
|---|
| 90 | { |
|---|
| 91 | DOMElement* elem = (DOMElement*) nodeList->item(0); |
|---|
| 92 | this->instantiationName = getTextNode(elem); |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | void ComponentInstantiation::parseProperties(DOMElement * _elem) |
|---|
| 98 | { |
|---|
| 99 | tmpXMLStr = XMLString::transcode("componentproperties"); |
|---|
| 100 | DOMNodeList* nodeList = _elem->getElementsByTagName(tmpXMLStr); |
|---|
| 101 | XMLString::release(&tmpXMLStr); |
|---|
| 102 | |
|---|
| 103 | if (nodeList->getLength() != 0) |
|---|
| 104 | { |
|---|
| 105 | DOMElement *elem = (DOMElement *) nodeList->item (0); |
|---|
| 106 | tmpXMLStr = XMLString::transcode("simpleref"); |
|---|
| 107 | nodeList = elem->getElementsByTagName(tmpXMLStr); |
|---|
| 108 | XMLString::release(&tmpXMLStr); |
|---|
| 109 | |
|---|
| 110 | InstantiationProperty *_tmp; |
|---|
| 111 | for (unsigned int i = 0; i < nodeList->getLength (); i++) |
|---|
| 112 | { |
|---|
| 113 | _tmp = parseSimpleRef ((DOMElement *) nodeList->item (i)); |
|---|
| 114 | properties.push_back(_tmp); |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | InstantiationProperty* ComponentInstantiation::parseSimpleRef(DOMElement * _elem) |
|---|
| 121 | { |
|---|
| 122 | tmpXMLStr = XMLString::transcode("refid"); |
|---|
| 123 | const XMLCh *_propId = _elem->getAttribute(tmpXMLStr); |
|---|
| 124 | XMLString::release(&tmpXMLStr); |
|---|
| 125 | tmpXMLStr = XMLString::transcode("value"); |
|---|
| 126 | const XMLCh *_propVal = _elem->getAttribute(tmpXMLStr); |
|---|
| 127 | XMLString::release(&tmpXMLStr); |
|---|
| 128 | |
|---|
| 129 | char* tmpStr1 = XMLString::transcode(_propId); |
|---|
| 130 | char* tmpStr2 = XMLString::transcode(_propVal); |
|---|
| 131 | InstantiationProperty* property = new InstantiationProperty(tmpStr1,tmpStr2); |
|---|
| 132 | |
|---|
| 133 | delete []tmpStr1; |
|---|
| 134 | delete []tmpStr2; |
|---|
| 135 | |
|---|
| 136 | return property; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | char* ComponentInstantiation::getTextNode(DOMElement * _elem) |
|---|
| 141 | { |
|---|
| 142 | DOMNodeList *nodeList = _elem->getChildNodes (); |
|---|
| 143 | |
|---|
| 144 | if (nodeList->getLength () == 0) |
|---|
| 145 | { |
|---|
| 146 | char* astr = new char[strlen("Not Specified") +1]; |
|---|
| 147 | strcpy(astr,"Not Specified"); |
|---|
| 148 | return astr; |
|---|
| 149 | } |
|---|
| 150 | else return XMLString::transcode (nodeList->item (0)->getNodeValue ()); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | char* ComponentInstantiation::toString() |
|---|
| 155 | { |
|---|
| 156 | std::vector <InstantiationProperty*>* propertyArray; |
|---|
| 157 | |
|---|
| 158 | char *str = new char[MAX_BUFFER]; // the caller must release this |
|---|
| 159 | |
|---|
| 160 | strcpy (str, "\n ID="); |
|---|
| 161 | str = strcat (str, getID ()); |
|---|
| 162 | str = strcat (str, "\n Name="); |
|---|
| 163 | str = strcat (str, getName ()); |
|---|
| 164 | |
|---|
| 165 | propertyArray = getProperties (); |
|---|
| 166 | |
|---|
| 167 | if (propertyArray->size () > 0) |
|---|
| 168 | { |
|---|
| 169 | str = strcat (str, "\n Propert"); |
|---|
| 170 | |
|---|
| 171 | if (propertyArray->size () == 1) str = strcat (str, "y"); |
|---|
| 172 | else str = strcat (str, "ies"); |
|---|
| 173 | |
|---|
| 174 | for (unsigned int i = 0; i < propertyArray->size (); i++) |
|---|
| 175 | { |
|---|
| 176 | str = strcat (str, "\n ["); |
|---|
| 177 | str = strcat (str, (char *) i); |
|---|
| 178 | str = strcat (str, "]"); |
|---|
| 179 | str = strcat (str, (*propertyArray)[i]->getID ()); |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | else str = strcat (str, "\n There are no properties"); |
|---|
| 183 | |
|---|
| 184 | return str; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | char* ComponentInstantiation::getID() const |
|---|
| 189 | { |
|---|
| 190 | return this->instantiationId; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | char* ComponentInstantiation::getName() const |
|---|
| 195 | { |
|---|
| 196 | return this->instantiationName; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | std::vector <InstantiationProperty*>* ComponentInstantiation::getProperties() |
|---|
| 201 | { |
|---|
| 202 | return &properties; |
|---|
| 203 | } |
|---|
| 204 | XMLCh* ComponentInstantiation::tmpXMLStr = NULL; |
|---|