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
00037 #include "OW_config.h"
00038 #include "OW_XMLNode.hpp"
00039 #include "OW_XMLAttribute.hpp"
00040 #include "OW_CIMException.hpp"
00041 #include "OW_Format.hpp"
00042 #include "OW_StringStream.hpp"
00043 #include "OW_XMLEscape.hpp"
00044
00045 #ifdef OW_HAVE_OSTREAM
00046 #include <ostream>
00047 #else
00048 #include <iostream>
00049 #endif
00050
00051 namespace OW_NAMESPACE
00052 {
00053
00055 XMLNodeImpl::XMLNodeImpl(const String& name,
00056 const XMLAttributeArray& attrArray) :
00057 m_nextNode(NULL), m_childNode(NULL), m_lastChildNode(NULL),
00058 m_XMLAttributeArray(attrArray), m_strName(name), m_strText()
00059 {
00060 }
00061
00063 XMLNodeImpl::XMLNodeImpl(const String& name) :
00064 m_nextNode(NULL), m_childNode(NULL), m_lastChildNode(NULL),
00065 m_XMLAttributeArray(), m_strName(name), m_strText()
00066 {
00067 }
00068
00070 XMLNodeImpl::XMLNodeImpl() :
00071 m_nextNode(NULL), m_childNode(NULL), m_lastChildNode(NULL),
00072 m_XMLAttributeArray(), m_strName(), m_strText()
00073 {
00074 }
00075
00077 void
00078 XMLNodeImpl::assignText(const String& text)
00079 {
00080 m_strText = text;
00081 }
00082
00084 void
00085 XMLNodeImpl::appendText(const String& text)
00086 {
00087 m_strText += text;
00088 }
00089
00091 String
00092 XMLNodeImpl::getAttribute(const String& name, bool throwException) const
00093 {
00094 int arraySize = m_XMLAttributeArray.size();
00095
00096 for (int i = 0; i < arraySize; i++)
00097 {
00098 XMLAttribute attr = m_XMLAttributeArray[i];
00099
00100
00101 if (attr.getName().equals(name))
00102 {
00103 return attr.getValue();
00104 }
00105 }
00106 if (throwException)
00107 {
00108 OW_THROWCIMMSG(CIMException::INVALID_PARAMETER,
00109 Format("Failed to find "
00110 "attribute: %1 in node: %2", name, m_strName).c_str() );
00111 }
00112
00113 return String();
00114 }
00115
00117 void
00118 XMLNodeImpl::addAttribute(const XMLAttribute & attribute)
00119 {
00120 m_XMLAttributeArray.push_back(attribute);
00121 }
00122
00124 XMLAttributeArray
00125 XMLNodeImpl::getAttrs() const
00126 {
00127 return m_XMLAttributeArray;
00128 }
00129
00131 String
00132 XMLNodeImpl::getText() const
00133 {
00134 return m_strText;
00135 }
00136
00138 String
00139 XMLNodeImpl::getName() const
00140 {
00141 return m_strName;
00142 }
00143
00145 XMLNodeImplRef
00146 XMLNodeImpl::findElement(const char* elementName, bool throwException) const
00147 {
00148 XMLNodeImplRef tmpRef(new XMLNodeImpl(*this));
00149
00150 for ( ;tmpRef; tmpRef = tmpRef->m_nextNode)
00151 {
00152 if (tmpRef->getName() == elementName)
00153 {
00154 return tmpRef;
00155 }
00156 }
00157 if (throwException)
00158 {
00159 OW_THROWCIMMSG(CIMException::FAILED,
00160 Format("XMLNodeImpl::findElement failed to find a matching "
00161 "elementName. Token id = %1", elementName).c_str() );
00162 }
00163 return XMLNodeImplRef(NULL);
00164 }
00165
00167 XMLNodeImplRef
00168 XMLNodeImpl::nextElement(const char* elementName, bool throwException) const
00169 {
00170 if (!m_nextNode)
00171 {
00172 if (throwException)
00173 {
00174 OW_THROWCIMMSG(CIMException::FAILED, Format("XMLNodeImpl::nextElement found a NULL element instead of %1", elementName).c_str());
00175 }
00176 else
00177 {
00178 return XMLNodeImplRef(0);
00179 }
00180 }
00181 if (m_nextNode->getName() == elementName)
00182 {
00183 return m_nextNode;
00184 }
00185
00186 if (throwException)
00187 {
00188 OW_THROWCIMMSG(CIMException::FAILED,
00189 Format("XMLNodeImpl::nextElement didn't match elementName. "
00190 "Token id=%1, found tokenid=%2",
00191 elementName, m_nextNode->getName() ).c_str() );
00192 }
00193
00194 return XMLNodeImplRef(NULL);
00195 }
00196
00198 void
00199 XMLNodeImpl::mustElement(const char* elementName) const
00200 {
00201 if (elementName != getName())
00202 {
00203 OW_THROWCIMMSG(CIMException::FAILED,
00204 Format("XMLNodeImpl::mustElement: elementName did not match "
00205 "node. Token id=%1, found=%2", elementName, getName() ).c_str() );
00206 }
00207 }
00208
00210 XMLNodeImplRef
00211 XMLNodeImpl::mustElementChild(const char* elementName) const
00212 {
00213 mustElement(elementName);
00214
00215 if (!m_childNode)
00216 {
00217 OW_THROWCIMMSG(CIMException::FAILED,
00218 Format("XMLNodeImpl::mustElementChild found a NULL child. "
00219 "Token id=%1",
00220 elementName ).c_str() );
00221 }
00222
00223 return m_childNode;
00224 }
00225
00227 XMLNodeImplRef
00228 XMLNodeImpl::mustChildElement(const char* elementName) const
00229 {
00230 if (!m_childNode)
00231 {
00232 OW_THROWCIMMSG(CIMException::FAILED,
00233 Format("XMLNodeImpl::mustChildElement found a NULL child. "
00234 "Token id=%1",
00235 elementName ).c_str() );
00236 }
00237
00238 if (m_childNode->getName() != elementName)
00239 {
00240 OW_THROWCIMMSG(CIMException::FAILED,
00241 Format("XMLNodeImpl::mustChildElement: elementName did not match "
00242 "child. "
00243 "Token id=%1, found tokenid=%2",
00244 elementName, m_childNode->getName() ).c_str() );
00245 }
00246 return m_childNode;
00247 }
00248
00250 XMLNodeImplRef
00251 XMLNodeImpl::mustChildElementChild(const char* elementName) const
00252 {
00253 if (!m_childNode)
00254 {
00255 OW_THROWCIMMSG(CIMException::FAILED,
00256 Format("XMLNodeImpl::mustChildElementChild found a NULL child. "
00257 "Token id=%1",
00258 elementName ).c_str() );
00259 }
00260
00261 if (m_childNode->getName() != elementName)
00262 {
00263 OW_THROWCIMMSG(CIMException::FAILED,
00264 Format("XMLNodeImpl::mustChildElementChild: elementName did "
00265 "not match child. "
00266 "Token id=%1, found tokenid=%2",
00267 elementName, m_childNode->getName() ).c_str() );
00268 }
00269
00270 if (!m_childNode->m_childNode)
00271 {
00272 OW_THROWCIMMSG(CIMException::FAILED,
00273 Format("XMLNodeImpl::mustChildElementChild found a NULL child "
00274 "of child Node. "
00275 "Token id=%1, found tokenid=%2",
00276 elementName, m_childNode->getName() ).c_str() );
00277 }
00278
00279 return(m_childNode->m_childNode);
00280 }
00281
00283 XMLNodeImplRef
00284 XMLNodeImpl::mustChildFindElement(const char* elementName) const
00285 {
00286 if (!m_childNode)
00287 {
00288 OW_THROWCIMMSG(CIMException::FAILED,
00289 Format("XMLNodeImpl::mustChildElementChild found a NULL child. "
00290 "Token id=%1",
00291 elementName ).c_str() );
00292 }
00293
00294 return(m_childNode->findElement(elementName, true));
00295 }
00296
00298 XMLNodeImplRef
00299 XMLNodeImpl::findElementChild(const char* elementName, bool throwException) const
00300 {
00301 XMLNodeImplRef tmpRef = findElement(elementName, throwException);
00302
00303 if (!tmpRef)
00304 {
00305 return tmpRef;
00306 }
00307 else
00308 {
00309 return tmpRef->m_childNode;
00310 }
00311 }
00312
00314 XMLNodeImplRef
00315 XMLNodeImpl::mustChildFindElementChild(const char* elementName) const
00316 {
00317 return m_childNode->findElementChild(elementName, true);
00318 }
00319
00321 void
00322 XMLNodeImpl::setNext(const XMLNodeImplRef& node)
00323 {
00324 m_nextNode = node;
00325 }
00326
00328 XMLNodeImplRef
00329 XMLNodeImpl::getNext() const
00330 {
00331 return m_nextNode;
00332 }
00333
00335 void
00336 XMLNodeImpl::addChild(const XMLNodeImplRef& node)
00337 {
00338 if (!m_childNode)
00339 {
00340 m_childNode=node;
00341 m_lastChildNode=node;
00342 }
00343 else
00344 {
00345 m_lastChildNode->m_nextNode=node;
00346 m_lastChildNode=node;
00347 }
00348 }
00349
00351 XMLNodeImplRef
00352 XMLNodeImpl::mustGetChild() const
00353 {
00354 if (!m_childNode)
00355 {
00356 OW_THROWCIMMSG(CIMException::FAILED,
00357 "XMLNodeImpl::mustGetChild found a NULL child");
00358 }
00359
00360 return m_childNode;
00361 }
00362
00364 XMLNodeImplRef
00365 XMLNodeImpl::getChild() const
00366 {
00367 return m_childNode;
00368 }
00369
00371 XMLNodeArray
00372 XMLNodeImpl::getChildren() const
00373 {
00374 XMLNodeArray ar;
00375 if ( !m_childNode )
00376 {
00377 return ar;
00378 }
00379 XMLNodeImplRef r = m_childNode;
00380 do
00381 {
00382 ar.push_back(r);
00383 r = r->m_nextNode;
00384 } while ( r );
00385
00386 return ar;
00387 }
00388
00390 void
00391 XMLNodeImpl::printNode( std::ostream& ostr ) const
00392 {
00393 String name = getName();
00394 XMLAttributeArray aa = getAttrs();
00395
00396 ostr << '<' << name;
00397 for ( XMLAttributeArray::const_iterator aiter = aa.begin();
00398 aiter != aa.end(); ++aiter )
00399 {
00400 ostr << ' ' << aiter->getName() << "=\"" << XMLEscape(aiter->getValue()) << '"';
00401 }
00402
00403 if (getText().empty() && !getChild())
00404 {
00405 ostr << "/>";
00406 }
00407 else
00408 {
00409 ostr << '>';
00410 ostr << XMLEscape(getText());
00411 XMLNode curChild = getChild();
00412 while (curChild)
00413 {
00414 curChild.printNode(ostr);
00415 curChild = curChild.getNext();
00416 }
00417 ostr << "</" << name << '>';
00418 }
00419 }
00420
00422 String
00423 XMLNodeImpl::toString() const
00424 {
00425 OStringStream ss;
00426 printNode( ss );
00427 return ss.releaseString();
00428 }
00429
00430
00431
00432
00433
00434
00436 XMLNode::XMLNode(const XMLNode& arg) :
00437 m_impl(arg.m_impl)
00438 {
00439 }
00440
00442 XMLNode::XMLNode(const String& name, const XMLAttributeArray& attrArray)
00443 : m_impl(new XMLNodeImpl(name, attrArray))
00444 {
00445 }
00446
00448 XMLNode::XMLNode(const XMLNodeImplRef& ref) : m_impl(ref)
00449 {
00450 }
00451
00453 XMLNode::XMLNode(const String& name) : m_impl(new XMLNodeImpl(name))
00454 {
00455 }
00456
00457
00459 XMLNode::XMLNode() : m_impl(NULL)
00460 {
00461 }
00462
00464
00465 XMLNode::~XMLNode()
00466 {
00467 }
00468
00470 XMLNode&
00471 XMLNode::operator= (const XMLNode & arg)
00472 {
00473 m_impl = arg.m_impl;
00474 return *this;
00475 }
00476
00478 void
00479 XMLNode::setToNULL()
00480 {
00481 XMLNodeImplRef tmpRef(NULL);
00482 m_impl = tmpRef;
00483 }
00484
00486 void
00487 XMLNode::assignText(const String& text) const
00488 {
00489 m_impl->assignText(text);
00490 }
00491
00493 void
00494 XMLNode::appendText(const String& text) const
00495 {
00496 m_impl->appendText(text);
00497 }
00498
00500 void
00501 XMLNode::addAttribute(const XMLAttribute& attribute) const
00502 {
00503 return m_impl->addAttribute(attribute);
00504 }
00505
00507 String
00508 XMLNode::getAttribute(const String& name) const
00509 {
00510 return m_impl->getAttribute(name);
00511 }
00512
00514 String
00515 XMLNode::mustGetAttribute(const String& name) const
00516 {
00517 return m_impl->getAttribute(name, true);
00518 }
00519
00521 XMLAttributeArray
00522 XMLNode::getAttrs() const
00523 {
00524 return m_impl->getAttrs();
00525 }
00526
00528 String
00529 XMLNode::getText() const
00530 {
00531 return m_impl->getText();
00532 }
00533
00535 String
00536 XMLNode::getName() const
00537 {
00538 return m_impl->getName();
00539 }
00540
00542 XMLNode
00543 XMLNode::findElement(const char* elementName) const
00544 {
00545 return XMLNode(m_impl->findElement(elementName));
00546 }
00547
00549 XMLNode
00550 XMLNode::mustFindElement(const char* elementName) const
00551 {
00552 return XMLNode(m_impl->findElement(elementName, true));
00553 }
00554
00556 XMLNode
00557 XMLNode::nextElement(const char* elementName)
00558 {
00559 return XMLNode(m_impl->nextElement(elementName));
00560 }
00561
00563 XMLNode
00564 XMLNode::mustNextElement(const char* elementName) const
00565 {
00566 return XMLNode(m_impl->nextElement(elementName, true));
00567 }
00568
00570 void
00571 XMLNode::mustElement(const char* elementName) const
00572 {
00573 m_impl->mustElement(elementName);
00574 }
00575
00577 XMLNode
00578 XMLNode::mustElementChild(const char* elementName) const
00579 {
00580 return XMLNode(m_impl->mustElementChild(elementName));
00581 }
00582
00584 XMLNode
00585 XMLNode::mustChildElement(const char* elementName) const
00586 {
00587 return XMLNode(m_impl->mustChildElement(elementName));
00588 }
00589
00591 XMLNode
00592 XMLNode::mustChildElementChild(const char* elementName) const
00593 {
00594 return XMLNode(m_impl->mustChildElementChild(elementName));
00595 }
00596
00598 XMLNode
00599 XMLNode::mustChildFindElement(const char* elementName) const
00600 {
00601 return XMLNode(m_impl->mustChildFindElement(elementName));
00602 }
00603
00605 XMLNode
00606 XMLNode::findElementChild(const char* elementName) const
00607 {
00608 return XMLNode(m_impl->findElementChild(elementName));
00609 }
00610
00612 XMLNode
00613 XMLNode::mustFindElementChild(const char* elementName) const
00614 {
00615 return XMLNode(m_impl->findElementChild(elementName, true));
00616 }
00617
00619 XMLNode
00620 XMLNode::mustChildFindElementChild(const char* elementName) const
00621 {
00622 return XMLNode(m_impl->mustChildFindElementChild(elementName));
00623 }
00624
00626 void
00627 XMLNode::setNext(const XMLNode& node) const
00628 {
00629 m_impl->setNext(node.m_impl);
00630 }
00631
00633 XMLNode
00634 XMLNode::getNext() const
00635 {
00636 return XMLNode(m_impl->getNext());
00637 }
00638
00640 void
00641 XMLNode::addChild(const XMLNode& node) const
00642 {
00643 m_impl->addChild(node.m_impl);
00644 }
00645
00647 XMLNode
00648 XMLNode::mustGetChild() const
00649 {
00650 return XMLNode(m_impl->mustGetChild());
00651 }
00652
00654 XMLNode
00655 XMLNode::getChild() const
00656 {
00657 return XMLNode(m_impl->getChild());
00658 }
00659
00661 XMLNodeArray
00662 XMLNode::getChildren() const
00663 {
00664 return m_impl->getChildren();
00665 }
00666
00668 void
00669 XMLNode::printNode( std::ostream& ostr ) const
00670 {
00671 m_impl->printNode( ostr );
00672 }
00673
00675 String
00676 XMLNode::toString() const
00677 {
00678 return m_impl->toString();
00679 }
00680
00681
00683 std::ostream& operator<<(std::ostream& ostr, const XMLNode& node)
00684 {
00685 node.printNode(ostr);
00686 return ostr;
00687 }
00688
00689
00690 }
00691