| 1 |
// ================================================================================================= |
| 2 |
// Copyright 2007 Adobe |
| 3 |
// All Rights Reserved. |
| 4 |
// |
| 5 |
// NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms |
| 6 |
// of the Adobe license agreement accompanying it. If you have received this file from a source other |
| 7 |
// than Adobe, then your use, modification, or distribution of it requires the prior written permission |
| 8 |
// of Adobe. |
| 9 |
// ================================================================================================= |
| 10 |
|
| 11 |
#if AdobePrivate |
| 12 |
// ================================================================================================= |
| 13 |
// Change history |
| 14 |
// ============== |
| 15 |
// |
| 16 |
// Writers: |
| 17 |
// AWL Alan Lillich |
| 18 |
// IJS Inder Jeet Singh |
| 19 |
// |
| 20 |
// mm-dd-yy who Description of changes, most recent on top. |
| 21 |
// |
| 22 |
// 03-19-14 IJS 5.6-f098 [3709896] P2 Handler Modified to support spanned clip. |
| 23 |
// |
| 24 |
// 01-16-08 AWL 4.2-c035 Add XML_Node::SetAttrValue. |
| 25 |
// 12-18-07 AWL 4.2-c034 Add XML_Node::IsEmptyLeafNode and XML_Node::GetAttrValue. |
| 26 |
// 12-07-07 AWL 4.2-c032 Add serialization and tree dump for the exposed Expat parser. |
| 27 |
// 11-30-07 AWL 4.2-c027 Expose XML_Node and ExpatAdapter so that XMPFiles can use them. |
| 28 |
// |
| 29 |
// ================================================================================================= |
| 30 |
#endif // AdobePrivate |
| 31 |
|
| 32 |
#include "public/include/XMP_Environment.h" // ! Must be the first #include! |
| 33 |
#include "source/XMLParserAdapter.hpp" |
| 34 |
|
| 35 |
#include <map> |
| 36 |
#include <cstring> |
| 37 |
#include <cstdio> |
| 38 |
|
| 39 |
// ! Can't include XMP..._Impl.hpp - used by both Core and Files. |
| 40 |
#define XMP_LitNMatch(s,l,n) (std::strncmp((s),(l),(n)) == 0) |
| 41 |
|
| 42 |
#if XMP_WinBuild |
| 43 |
#define snprintf _snprintf |
| 44 |
#pragma warning ( disable : 4996 ) // snprintf is safe |
| 45 |
#endif |
| 46 |
|
| 47 |
// ================================================================================================= |
| 48 |
|
| 49 |
#if 0 // Pattern for iterating over the children or attributes: |
| 50 |
for ( size_t xxNum = 0, xxLim = _node_->_offspring_->size(); xxNum < xxLim; ++xxNum ) { |
| 51 |
const XML_NodePtr _curr_ = _node_->_offspring_[xxNum]; |
| 52 |
} |
| 53 |
#endif |
| 54 |
|
| 55 |
// ================================================================================================= |
| 56 |
// XML_Node::IsWhitespaceNode |
| 57 |
//=========================== |
| 58 |
|
| 59 |
bool XML_Node::IsWhitespaceNode() const |
| 60 |
{ |
| 61 |
if ( this->kind != kCDataNode ) return false; |
| 62 |
|
| 63 |
for ( size_t i = 0; i < this->value.size(); ++i ) { |
| 64 |
unsigned char ch = this->value[i]; |
| 65 |
if ( IsWhitespaceChar ( ch ) ) continue; |
| 66 |
// *** Add checks for other whitespace characters. |
| 67 |
return false; // All the checks failed, this isn't whitespace. |
| 68 |
} |
| 69 |
|
| 70 |
return true; |
| 71 |
|
| 72 |
} // XML_Node::IsWhitespaceNode |
| 73 |
|
| 74 |
// ================================================================================================= |
| 75 |
// XML_Node::IsLeafContentNode |
| 76 |
//============================ |
| 77 |
|
| 78 |
bool XML_Node::IsLeafContentNode() const |
| 79 |
{ |
| 80 |
if ( this->kind != kElemNode ) return false; |
| 81 |
if ( this->content.size() == 0 ) return true; |
| 82 |
if ( this->content.size() > 1 ) return false; |
| 83 |
if ( this->content[0]->kind != kCDataNode ) return false; |
| 84 |
|
| 85 |
return true; |
| 86 |
|
| 87 |
} // XML_Node::IsLeafContentNode |
| 88 |
|
| 89 |
// ================================================================================================= |
| 90 |
// XML_Node::IsEmptyLeafNode |
| 91 |
//========================== |
| 92 |
|
| 93 |
bool XML_Node::IsEmptyLeafNode() const |
| 94 |
{ |
| 95 |
|
| 96 |
if ( (this->kind != kElemNode) || (this->content.size() != 0) ) return false; |
| 97 |
return true; |
| 98 |
|
| 99 |
} // XML_Node::IsEmptyLeafNode |
| 100 |
|
| 101 |
// ================================================================================================= |
| 102 |
// XML_Node::GetAttrValue |
| 103 |
//======================= |
| 104 |
|
| 105 |
XMP_StringPtr XML_Node::GetAttrValue ( XMP_StringPtr attrName ) const |
| 106 |
{ |
| 107 |
|
| 108 |
for ( size_t i = 0, aLim = this->attrs.size(); i < aLim; ++i ) { |
| 109 |
XML_Node * attrPtr = this->attrs[i]; |
| 110 |
if ( ! attrPtr->ns.empty() ) continue; // This form of GetAttrValue is for attrs in no namespace. |
| 111 |
if ( attrPtr->name == attrName ) return attrPtr->value.c_str(); |
| 112 |
} |
| 113 |
|
| 114 |
return 0; // Not found. |
| 115 |
|
| 116 |
} // XML_Node::GetAttrValue |
| 117 |
|
| 118 |
// ================================================================================================= |
| 119 |
// XML_Node::SetAttrValue |
| 120 |
//======================= |
| 121 |
|
| 122 |
void XML_Node::SetAttrValue ( XMP_StringPtr attrName, XMP_StringPtr attrValue ) |
| 123 |
{ |
| 124 |
|
| 125 |
for ( size_t i = 0, aLim = this->attrs.size(); i < aLim; ++i ) { |
| 126 |
XML_Node * attrPtr = this->attrs[i]; |
| 127 |
if ( ! attrPtr->ns.empty() ) continue; // This form of SetAttrValue is for attrs in no namespace. |
| 128 |
if ( attrPtr->name == attrName ) { |
| 129 |
attrPtr->value = attrValue; |
| 130 |
return; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
} // XML_Node::SetAttrValue |
| 135 |
|
| 136 |
// ================================================================================================= |
| 137 |
// XML_Node::GetLeafContentValue |
| 138 |
//============================== |
| 139 |
|
| 140 |
XMP_StringPtr XML_Node::GetLeafContentValue() const |
| 141 |
{ |
| 142 |
if ( (! this->IsLeafContentNode()) || this->content.empty() ) return ""; |
| 143 |
|
| 144 |
return this->content[0]->value.c_str(); |
| 145 |
|
| 146 |
} // XML_Node::GetLeafContentValue |
| 147 |
|
| 148 |
// ================================================================================================= |
| 149 |
// XML_Node::GetLeafContentValue |
| 150 |
//============================== |
| 151 |
|
| 152 |
std::string* XML_Node::GetLeafContentPtr() const |
| 153 |
{ |
| 154 |
if ( (! this->IsLeafContentNode()) || this->content.empty() ) return 0; |
| 155 |
|
| 156 |
return &this->content[0]->value; |
| 157 |
|
| 158 |
} // XML_Node::GetLeafContentValue |
| 159 |
|
| 160 |
// ================================================================================================= |
| 161 |
// XML_Node::SetLeafContentValue |
| 162 |
//============================== |
| 163 |
|
| 164 |
void XML_Node::SetLeafContentValue ( XMP_StringPtr newValue ) |
| 165 |
{ |
| 166 |
XML_Node * valueNode; |
| 167 |
|
| 168 |
if ( ! this->content.empty() ) { |
| 169 |
valueNode = this->content[0]; |
| 170 |
} else { |
| 171 |
valueNode = new XML_Node ( this, "", kCDataNode ); |
| 172 |
this->content.push_back ( valueNode ); |
| 173 |
} |
| 174 |
|
| 175 |
valueNode->value = newValue; |
| 176 |
|
| 177 |
} // XML_Node::SetLeafContentValue |
| 178 |
|
| 179 |
// ================================================================================================= |
| 180 |
// XML_Node::CountNamedElements |
| 181 |
//============================= |
| 182 |
|
| 183 |
size_t XML_Node::CountNamedElements ( XMP_StringPtr nsURI, XMP_StringPtr localName ) const |
| 184 |
{ |
| 185 |
size_t count = 0; |
| 186 |
|
| 187 |
for ( size_t i = 0, vLim = this->content.size(); i < vLim; ++i ) { |
| 188 |
const XML_Node & child = *this->content[i]; |
| 189 |
if ( child.ns != nsURI ) continue; |
| 190 |
if ( strcmp ( localName, child.name.c_str()+child.nsPrefixLen ) != 0 ) continue; |
| 191 |
++count; |
| 192 |
} |
| 193 |
|
| 194 |
return count; |
| 195 |
|
| 196 |
} // XML_Node::CountNamedElements |
| 197 |
|
| 198 |
// ================================================================================================= |
| 199 |
// XML_Node::GetNamedElement |
| 200 |
//========================== |
| 201 |
|
| 202 |
XML_NodePtr XML_Node::GetNamedElement ( XMP_StringPtr nsURI, XMP_StringPtr localName, size_t which /* = 0 */ ) |
| 203 |
{ |
| 204 |
|
| 205 |
for ( size_t i = 0, vLim = this->content.size(); i < vLim; ++i ) { |
| 206 |
XML_Node * childPtr = this->content[i]; |
| 207 |
if ( childPtr->ns != nsURI ) continue; |
| 208 |
if ( strcmp ( localName, childPtr->name.c_str()+childPtr->nsPrefixLen ) != 0 ) continue; |
| 209 |
if ( which == 0 ) return childPtr; |
| 210 |
--which; |
| 211 |
} |
| 212 |
|
| 213 |
return 0; /// Not found. |
| 214 |
|
| 215 |
} // XML_Node::GetNamedElement |
| 216 |
|
| 217 |
// ================================================================================================= |
| 218 |
// DumpNodeList |
| 219 |
// ============ |
| 220 |
|
| 221 |
static const char * kNodeKinds[] = { "root", "elem", "attr", "cdata", "pi" }; |
| 222 |
|
| 223 |
static void DumpNodeList ( std::string * buffer, const XML_NodeVector & list, int indent ) |
| 224 |
{ |
| 225 |
|
| 226 |
for ( size_t i = 0, limit = list.size(); i < limit; ++i ) { |
| 227 |
|
| 228 |
const XML_Node * node = list[i]; |
| 229 |
|
| 230 |
for ( int t = indent; t > 0; --t ) *buffer += " "; |
| 231 |
if ( node->IsWhitespaceNode() ) { |
| 232 |
*buffer += "-- whitespace --\n"; |
| 233 |
continue; |
| 234 |
} |
| 235 |
|
| 236 |
*buffer += node->name; |
| 237 |
*buffer += " - "; |
| 238 |
*buffer += kNodeKinds[node->kind]; |
| 239 |
if ( ! node->value.empty() ) { |
| 240 |
*buffer += ", value=\""; |
| 241 |
*buffer += node->value; |
| 242 |
*buffer += "\""; |
| 243 |
} |
| 244 |
if ( ! node->ns.empty() ) { |
| 245 |
*buffer += ", ns=\""; |
| 246 |
*buffer += node->ns; |
| 247 |
*buffer += "\""; |
| 248 |
} |
| 249 |
if ( node->nsPrefixLen != 0 ) { |
| 250 |
*buffer += ", prefixLen="; |
| 251 |
char numBuf [20]; |
| 252 |
snprintf ( numBuf, sizeof(numBuf), "%d", (int)node->nsPrefixLen ); |
| 253 |
*buffer += numBuf; |
| 254 |
} |
| 255 |
*buffer += "\n"; |
| 256 |
|
| 257 |
if ( ! node->attrs.empty() ) { |
| 258 |
for ( int t = indent+1; t > 0; --t ) *buffer += " "; |
| 259 |
*buffer += "attrs:\n"; |
| 260 |
DumpNodeList ( buffer, node->attrs, indent+2 ); |
| 261 |
} |
| 262 |
|
| 263 |
if ( ! node->content.empty() ) { |
| 264 |
DumpNodeList ( buffer, node->content, indent+1 ); |
| 265 |
} |
| 266 |
|
| 267 |
} |
| 268 |
|
| 269 |
} // DumpNodeList |
| 270 |
|
| 271 |
// ================================================================================================= |
| 272 |
// XML_Node::Dump |
| 273 |
//=============== |
| 274 |
|
| 275 |
void XML_Node::Dump ( std::string * buffer ) |
| 276 |
{ |
| 277 |
|
| 278 |
*buffer = "Dump of XML_Node tree\n"; |
| 279 |
|
| 280 |
*buffer += "Root info: name=\""; |
| 281 |
*buffer += this->name; |
| 282 |
*buffer += "\", value=\""; |
| 283 |
*buffer += this->value; |
| 284 |
*buffer += "\", ns=\""; |
| 285 |
*buffer += this->ns; |
| 286 |
*buffer += "\", kind="; |
| 287 |
*buffer += kNodeKinds[this->kind]; |
| 288 |
*buffer += "\n"; |
| 289 |
|
| 290 |
if ( ! this->attrs.empty() ) { |
| 291 |
*buffer += " attrs:\n"; |
| 292 |
DumpNodeList ( buffer, this->attrs, 2 ); |
| 293 |
} |
| 294 |
*buffer += "\n"; |
| 295 |
|
| 296 |
DumpNodeList ( buffer, this->content, 0 ); |
| 297 |
|
| 298 |
} // XML_Node::Dump |
| 299 |
|
| 300 |
// ================================================================================================= |
| 301 |
// SerializeOneNode |
| 302 |
// ================ |
| 303 |
|
| 304 |
static void SerializeOneNode ( std::string * buffer, const XML_Node & node ) |
| 305 |
{ |
| 306 |
size_t i, limit; |
| 307 |
XMP_StringPtr namePtr = node.name.c_str(); |
| 308 |
if ( XMP_LitNMatch ( namePtr, "_dflt_:", 7 ) ) namePtr += 7; // Hack for default namespaces. |
| 309 |
|
| 310 |
switch ( node.kind ) { |
| 311 |
|
| 312 |
case kElemNode: |
| 313 |
*buffer += '<'; |
| 314 |
*buffer += namePtr; |
| 315 |
for ( i = 0, limit = node.attrs.size(); i < limit; ++i ) { |
| 316 |
SerializeOneNode ( buffer, *node.attrs[i] ); |
| 317 |
} |
| 318 |
if ( node.content.empty() ) { |
| 319 |
*buffer += "/>"; |
| 320 |
} else { |
| 321 |
*buffer += '>'; |
| 322 |
for ( i = 0, limit = node.content.size(); i < limit; ++i ) { |
| 323 |
SerializeOneNode ( buffer, *node.content[i] ); |
| 324 |
} |
| 325 |
*buffer += "</"; |
| 326 |
*buffer += namePtr; |
| 327 |
*buffer += '>'; |
| 328 |
} |
| 329 |
break; |
| 330 |
|
| 331 |
case kAttrNode: |
| 332 |
*buffer += ' '; |
| 333 |
*buffer += namePtr; |
| 334 |
*buffer += "=\""; |
| 335 |
*buffer += node.value; |
| 336 |
*buffer += '"'; |
| 337 |
break; |
| 338 |
|
| 339 |
case kCDataNode: |
| 340 |
*buffer += node.value; |
| 341 |
break; |
| 342 |
|
| 343 |
case kPINode: |
| 344 |
*buffer += node.value; // *** Note that we're dropping PIs during the Expat parse. |
| 345 |
break; |
| 346 |
|
| 347 |
} |
| 348 |
|
| 349 |
} // SerializeOneNode |
| 350 |
|
| 351 |
// ================================================================================================= |
| 352 |
// CollectNamespaceDecls |
| 353 |
// ===================== |
| 354 |
|
| 355 |
typedef std::map < std::string, std::string > NamespaceMap; |
| 356 |
|
| 357 |
static void CollectNamespaceDecls ( NamespaceMap * nsMap, const XML_Node & node ) |
| 358 |
{ |
| 359 |
size_t i, limit; |
| 360 |
|
| 361 |
if ( ! node.ns.empty() ) { |
| 362 |
size_t nameMid = 0; |
| 363 |
while ( node.name[nameMid] != ':' ) ++nameMid; |
| 364 |
std::string prefix = node.name.substr ( 0, nameMid ); |
| 365 |
(*nsMap)[prefix] = node.ns; |
| 366 |
} |
| 367 |
|
| 368 |
if ( node.kind == kElemNode ) { |
| 369 |
|
| 370 |
for ( i = 0, limit = node.attrs.size(); i < limit; ++i ) { |
| 371 |
CollectNamespaceDecls ( nsMap, *node.attrs[i] ); |
| 372 |
} |
| 373 |
|
| 374 |
for ( i = 0, limit = node.content.size(); i < limit; ++i ) { |
| 375 |
const XML_Node & content = *node.content[i]; |
| 376 |
if ( content.kind == kElemNode ) CollectNamespaceDecls ( nsMap, content ); |
| 377 |
} |
| 378 |
|
| 379 |
} |
| 380 |
|
| 381 |
} // CollectNamespaceDecls |
| 382 |
|
| 383 |
// ================================================================================================= |
| 384 |
// XML_Node::Serialize |
| 385 |
//==================== |
| 386 |
|
| 387 |
void XML_Node::Serialize ( std::string * buffer ) |
| 388 |
{ |
| 389 |
buffer->erase(); |
| 390 |
|
| 391 |
if ( this->kind != kRootNode ) { |
| 392 |
|
| 393 |
SerializeOneNode ( buffer, *this ); |
| 394 |
|
| 395 |
} else { |
| 396 |
|
| 397 |
// Do the outermost level here, in order to add the XML version and namespace declarations. |
| 398 |
|
| 399 |
*buffer += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
| 400 |
|
| 401 |
for ( size_t outer = 0, oLimit = this->content.size(); outer < oLimit; ++outer ) { |
| 402 |
|
| 403 |
const XML_Node & node = *this->content[outer]; |
| 404 |
|
| 405 |
if ( node.kind != kElemNode ) { |
| 406 |
|
| 407 |
SerializeOneNode ( buffer, node ); |
| 408 |
|
| 409 |
} else { |
| 410 |
|
| 411 |
XMP_StringPtr namePtr = node.name.c_str(); |
| 412 |
if ( XMP_LitNMatch ( namePtr, "_dflt_:", 7 ) ) namePtr += 7; // Hack for default namespaces. |
| 413 |
|
| 414 |
*buffer += '<'; |
| 415 |
*buffer += namePtr; |
| 416 |
|
| 417 |
NamespaceMap nsMap; |
| 418 |
CollectNamespaceDecls ( &nsMap, node ); |
| 419 |
NamespaceMap::iterator nsDecl = nsMap.begin(); |
| 420 |
NamespaceMap::iterator nsEnd = nsMap.end(); |
| 421 |
for ( ; nsDecl != nsEnd; ++nsDecl ) { |
| 422 |
const std::string & prefix = nsDecl->first; |
| 423 |
*buffer += " xmlns"; |
| 424 |
if ( prefix != "_dflt_" ) { *buffer += ':'; *buffer += prefix; } |
| 425 |
*buffer += "=\""; |
| 426 |
*buffer += nsDecl->second; |
| 427 |
*buffer += '"'; |
| 428 |
} |
| 429 |
|
| 430 |
for ( size_t attr = 0, aLimit = node.attrs.size(); attr < aLimit; ++attr ) { |
| 431 |
SerializeOneNode ( buffer, *node.attrs[attr] ); |
| 432 |
} |
| 433 |
|
| 434 |
if ( node.content.empty() ) { |
| 435 |
*buffer += "/>"; |
| 436 |
} else { |
| 437 |
*buffer += '>'; |
| 438 |
for ( size_t child = 0, cLimit = node.content.size(); child < cLimit; ++child ) { |
| 439 |
SerializeOneNode ( buffer, *node.content[child] ); |
| 440 |
} |
| 441 |
*buffer += "</"; |
| 442 |
*buffer += namePtr; |
| 443 |
*buffer += '>'; |
| 444 |
} |
| 445 |
|
| 446 |
} |
| 447 |
|
| 448 |
} |
| 449 |
|
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
} // XML_Node::Serialize |
| 454 |
|
| 455 |
// ================================================================================================= |
| 456 |
// XML_Node::RemoveAttrs |
| 457 |
//====================== |
| 458 |
|
| 459 |
void XML_Node::RemoveAttrs() |
| 460 |
{ |
| 461 |
|
| 462 |
for ( size_t i = 0, vLim = this->attrs.size(); i < vLim; ++i ) delete this->attrs[i]; |
| 463 |
this->attrs.clear(); |
| 464 |
|
| 465 |
} // XML_Node::RemoveAttrs |
| 466 |
|
| 467 |
// ================================================================================================= |
| 468 |
// XML_Node::RemoveContent |
| 469 |
//======================== |
| 470 |
|
| 471 |
void XML_Node::RemoveContent() |
| 472 |
{ |
| 473 |
|
| 474 |
for ( size_t i = 0, vLim = this->content.size(); i < vLim; ++i ) delete this->content[i]; |
| 475 |
this->content.clear(); |
| 476 |
|
| 477 |
} // XML_Node::RemoveContent |
| 478 |
|
| 479 |
// ================================================================================================= |
| 480 |
// XML_Node::ClearNode |
| 481 |
//==================== |
| 482 |
|
| 483 |
void XML_Node::ClearNode() |
| 484 |
{ |
| 485 |
|
| 486 |
this->kind = 0; |
| 487 |
this->ns.erase(); |
| 488 |
this->name.erase(); |
| 489 |
this->value.erase(); |
| 490 |
|
| 491 |
this->RemoveAttrs(); |
| 492 |
this->RemoveContent(); |
| 493 |
|
| 494 |
} // XML_Node::ClearNode |
| 495 |
|
| 496 |
// ================================================================================================= |