30#include "XMLHelpers.h"
38string XMLUtil::xmlCharToString(
const xmlChar* theCharsOrNull)
40 const char* asChars =
reinterpret_cast<const char*
>(theCharsOrNull);
41 return ((asChars) ? (
string(asChars)) : (
string(
"")));
44void XMLUtil::xmlCharToString(
string& stringToFill,
const xmlChar* pChars)
46 stringToFill = xmlCharToString(pChars);
51string XMLUtil::xmlCharToStringFromIterators(
const xmlChar* startIter,
const xmlChar* endIter)
54 if (!startIter || !endIter || (startIter > endIter)) {
59 return string(
reinterpret_cast<const char*
>(startIter),
reinterpret_cast<const char*
>(endIter));
63XMLAttribute::XMLAttribute(
const string& localNameA,
const string& valueA,
const string& prefixA,
const string& nsURIA) :
64 localname(localNameA), prefix(prefixA), nsURI(nsURIA), value(valueA)
71XMLAttribute::XMLAttribute(
const xmlChar** chunkOfFivePointers)
77 localname(proto.localname), prefix(proto.prefix), nsURI(proto.nsURI), value(proto.value)
82XMLAttribute::operator=(
const XMLAttribute& rhs)
87 localname = rhs.localname;
96 const xmlChar* xmlLocalName = (*chunkOfFivePointers++);
97 const xmlChar* xmlPrefix = (*chunkOfFivePointers++);
98 const xmlChar* xmlURI = (*chunkOfFivePointers++);
99 const xmlChar* xmlValueStart = (*chunkOfFivePointers++);
101 const xmlChar* xmlValueEnd = (*chunkOfFivePointers++);
104 localname = XMLUtil::xmlCharToString(xmlLocalName);
105 prefix = XMLUtil::xmlCharToString(xmlPrefix);
106 nsURI = XMLUtil::xmlCharToString(xmlURI);
107 value = XMLUtil::xmlCharToStringFromIterators(xmlValueStart, xmlValueEnd);
122 return getQName() +
"=\"" + value +
"\"";
128 if (prefix.empty()) {
132 return prefix +
":" + localname;
137XMLAttributeMap::XMLAttributeMap() :
142XMLAttributeMap::~XMLAttributeMap()
146XMLAttributeMap::const_iterator XMLAttributeMap::begin()
const
148 return _attributes.begin();
151XMLAttributeMap::const_iterator XMLAttributeMap::end()
const
153 return _attributes.end();
156bool XMLAttributeMap::empty()
const
158 return _attributes.empty();
169 XMLAttributeMap::iterator foundIt = findByQName(attribute.
getQName());
171 if (foundIt != _attributes.end()) {
177 _attributes.push_back(attribute);
197 for (XMLAttributeMap::const_iterator it = begin(); it != end(); ++it) {
199 if (rAttr.localname == localname) {
208XMLAttributeMap::getAttributeByQName(
const string& qname)
const
211 for (XMLAttributeMap::const_iterator it = begin(); it != end(); ++it) {
222XMLAttributeMap::getAttributeByQName(
const string& prefix,
const string& localname)
const
231 XMLAttributeMap::const_iterator it;
232 for (it = begin(); it != end(); ++it) {
234 result += (attr.
getQName() +
"=\"" + attr.value +
"\" ");
239XMLAttributeMap::iterator XMLAttributeMap::findByQName(
const string& qname)
241 XMLAttributeMap::iterator it;
242 for (it = _attributes.begin(); it != _attributes.end(); ++it) {
243 if (it->getQName() == qname) {
252XMLNamespace::XMLNamespace(
const string& prefixArg,
const string& uriArg) :
253 prefix(prefixArg), uri(uriArg)
258 prefix(proto.prefix), uri(proto.uri)
276 prefix = XMLUtil::xmlCharToString(*pNamespace);
277 uri = XMLUtil::xmlCharToString(*(pNamespace + 1));
283 string attr(
"xmlns");
284 if (!prefix.empty()) {
285 attr += (string(
":") + prefix);
287 attr += string(
"=\"");
289 attr += string(
"\"");
295XMLNamespaceMap::XMLNamespaceMap() :
300XMLNamespaceMap::~XMLNamespaceMap()
306 _namespaces(proto._namespaces)
316 _namespaces = rhs._namespaces;
323 for (
int i = 0; i < numNamespaces; ++i) {
334 for (XMLNamespaceMap::const_iterator it = begin(); it != end(); ++it) {
341XMLNamespaceMap::const_iterator XMLNamespaceMap::begin()
const
343 return _namespaces.begin();
346XMLNamespaceMap::const_iterator XMLNamespaceMap::end()
const
348 return _namespaces.end();
353 XMLNamespaceMap::const_iterator foundIt;
354 for (foundIt = begin(); foundIt != end(); ++foundIt) {
355 if (foundIt->prefix == prefix) {
362bool XMLNamespaceMap::isInMap(
const string& prefix)
const
364 return (
find(prefix) != end());
369 XMLNamespaceMap::iterator foundIt = findNonConst(ns.prefix);
370 if (foundIt == _namespaces.end())
372 _namespaces.push_back(ns);
380void XMLNamespaceMap::clear()
385bool XMLNamespaceMap::empty()
const
387 return _namespaces.empty();
390XMLNamespaceMap::iterator XMLNamespaceMap::findNonConst(
const string& prefix)
392 XMLNamespaceMap::iterator foundIt;
393 for (foundIt = _namespaces.begin(); foundIt != _namespaces.end(); ++foundIt) {
394 if (foundIt->prefix == prefix) {
403XMLNamespaceStack::XMLNamespaceStack() :
408XMLNamespaceStack::~XMLNamespaceStack()
431 _stack.push_back(nsMap);
434void XMLNamespaceStack::pop()
440XMLNamespaceStack::top()
const
442 return _stack.back();
445bool XMLNamespaceStack::empty()
const
447 return _stack.empty();
450void XMLNamespaceStack::clear()
457 return _stack.rbegin();
460XMLNamespaceStack::const_iterator XMLNamespaceStack::end()
const
462 return _stack.rend();
469 for (XMLNamespaceStack::const_iterator it =
begin(); it != end(); ++it) {
470 addMissingNamespaces(nsFlattened, *it);
477 for (XMLNamespaceMap::const_iterator it = fromMap.begin(); it != fromMap.end(); ++it) {
480 if (intoMap.
find(ns.prefix) == intoMap.end()) {
const std::string getValueForLocalNameOrDefault(const std::string &localname, const std::string &defVal="") const
const XMLAttribute * getAttributeByLocalName(const std::string &localname) const
void addAttribute(const XMLAttribute &attribute)
std::string getAllAttributesAsString() const
void fromSAX2Namespaces(const xmlChar **pNamespaces, int numNamespaces)
std::string getAllNamespacesAsAttributeString() const
void addNamespace(const XMLNamespace &ns)
XMLNamespaceMap::const_iterator find(const std::string &prefix) const
void getFlattenedNamespacesUsingLexicalScoping(XMLNamespaceMap &nsFlattened) const
XMLNamespaceStack::const_iterator begin() const
NcML Parser for adding/modifying/removing metadata (attributes) to existing local datasets using NcML...
std::string getAsXMLString() const
std::string getQName() const
void fromSAX2NamespaceAttributes(const xmlChar **chunkOfFivePointers)
std::string getAsAttributeString() const
void fromSAX2Namespace(const xmlChar **namespaces)