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 #include <spl/text/StringBuffer.h>
00026 #include <spl/xml/XmlAttribute.h>
00027 #include <spl/xml/XmlAttributeCollection.h>
00028 #include <spl/xml/XmlComment.h>
00029 #include <spl/xml/XmlDeclaration.h>
00030 #include <spl/xml/XmlNamedNodeMap.h>
00031 #include <spl/xml/XmlText.h>
00032 #include <spl/xml/XmlUnknown.h>
00033
00034 extern void _XmlEncodeString( const String& str, StringBuffer& outString );
00035
00036 XmlAttribute::XmlAttribute()
00037 : XmlNode(XmlNode::ATTRIBUTE), m_document()
00038 {
00039 }
00040
00042 XmlAttribute::XmlAttribute( const String& _name, const String& _value )
00043 : XmlNode(XmlNode::ATTRIBUTE), m_name(_name), m_document()
00044 {
00045 m_value = _value;
00046 }
00047
00048 XmlAttribute::~XmlAttribute()
00049 {
00050 }
00051
00052
00053 String XmlAttribute::Name() const
00054 {
00055 return m_name;
00056 }
00057
00058 XmlNodePtr XmlAttribute::Clone() const
00059 {
00060 XmlAttributePtr clone(new XmlAttribute());
00061 XmlNodePtr xclone = (XmlNodePtr)clone;
00062 clone->m_self = xclone;
00063 CopyTo( *clone );
00064 return clone;
00065 }
00066
00067 void XmlAttribute::WriteTo( TextWriter& writer ) const
00068 {
00069 writer.Write( *ToString() );
00070 }
00071
00072 StringPtr XmlAttribute::ToString() const
00073 {
00074 StringBuffer n, v;
00075
00076 _XmlEncodeString( m_name, n );
00077 _XmlEncodeString( *m_value.ToString(), v );
00078
00079 if (m_value.IndexOf('\"') < 0)
00080 {
00081 return String::Format( "%s=\"%s\"", n.GetChars(), v.GetChars() );
00082 }
00083 else
00084 {
00085 return String::Format( "%s='%s'", n.GetChars(), v.GetChars() );
00086 }
00087 }
00088
00089 #ifdef DEBUG
00090 void XmlAttribute::ValidateMem() const
00091 {
00092 XmlNode::ValidateMem();
00093 m_name.ValidateMem();
00094 }
00095
00096 void XmlAttribute::CheckMem() const
00097 {
00098 XmlNode::CheckMem();
00099 m_name.CheckMem();
00100 }
00101 #endif
00102
00103 XmlComment::XmlComment( const XmlComment& copy ) : XmlNode( XmlNode::COMMENT )
00104 {
00105 copy.CopyTo( *this );
00106 }
00107
00108 XmlComment::XmlComment( const String& _value )
00109 : XmlNode( XmlNode::COMMENT )
00110 {
00111 SetValue( _value );
00112 }
00113
00114 XmlComment::XmlComment()
00115 : XmlNode( XmlNode::COMMENT )
00116 {
00117 }
00118
00119 XmlComment::~XmlComment()
00120 {
00121 }
00122
00123 String XmlComment::Name() const
00124 {
00125 return String("#comment");
00126 }
00127
00128 void XmlComment::operator=( const XmlComment& base )
00129 {
00130 Clear();
00131 base.CopyTo( *this );
00132 }
00133
00134 void XmlComment::CopyTo( XmlComment& target ) const
00135 {
00136 XmlNode::CopyTo( target );
00137 }
00138
00139 XmlNodePtr XmlComment::Clone() const
00140 {
00141 XmlCommentPtr clone(new XmlComment());
00142 XmlNodePtr xclone = (XmlNodePtr)clone;
00143 clone->m_self = xclone;
00144 CopyTo( *clone );
00145 return clone;
00146 }
00147
00148 void XmlComment::WriteTo( TextWriter& writer ) const
00149 {
00150 writer.Write( *ToString() );
00151 }
00152
00153 StringPtr XmlComment::ToString() const
00154 {
00155 return String::Format( "<!--%s-->", m_value.GetChars() );
00156 }
00157
00158 XmlCommentPtr XmlComment::ToComment() const
00159 {
00160 return (XmlNodePtr)m_self;
00161 }
00162
00163 XmlText::XmlText (const String& initValue )
00164 : XmlNode (XmlNode::TEXT)
00165 {
00166 SetValue( initValue );
00167 m_cdata = false;
00168 }
00169
00170 XmlText::XmlText( const XmlText& copy )
00171 : XmlNode( XmlNode::TEXT )
00172 {
00173 copy.CopyTo( *this );
00174 }
00175
00176 XmlText::~XmlText()
00177 {
00178 }
00179
00180 String XmlText::Name() const
00181 {
00182 if ( m_cdata )
00183 {
00184 return String("#cdata-section");
00185 }
00186 else
00187 {
00188 return String("#text");
00189 }
00190 }
00191
00192 void XmlText::WriteTo( TextWriter& writer ) const
00193 {
00194 if ( m_cdata )
00195 {
00196 writer.WriteFormat( "<![CDATA[%s]]>", m_value.GetChars() );
00197 }
00198 else
00199 {
00200 StringBuffer buffer;
00201 _XmlEncodeString( *m_value.ToString(), buffer );
00202 writer.WriteFormat( "%s", buffer.GetChars() );
00203 }
00204 }
00205
00206 StringPtr XmlText::ToString() const
00207 {
00208 if ( m_cdata )
00209 {
00210 return String::Format( "<![CDATA[%s]]>", m_value.GetChars() );
00211 }
00212 else
00213 {
00214 StringBuffer buffer;
00215 _XmlEncodeString( *m_value.ToString(), buffer );
00216 return buffer.ToString();
00217 }
00218 }
00219
00220 void XmlText::CopyTo( XmlText& target ) const
00221 {
00222 XmlNode::CopyTo( target );
00223 target.m_cdata = m_cdata;
00224 }
00225
00226 XmlNodePtr XmlText::Clone() const
00227 {
00228 XmlTextPtr clone(new XmlText( "" ));
00229 XmlNodePtr xclone = (XmlNodePtr)clone;
00230 clone->m_self = xclone;
00231 CopyTo( *clone );
00232 return clone;
00233 }
00234
00235 XmlTextPtr XmlText::ToText() const
00236 {
00237 return (XmlNodePtr)m_self;
00238 }
00239
00240 XmlTextPtr XmlText::CreateText(const String& val)
00241 {
00242 XmlTextPtr text(new XmlText(val));
00243 XmlNodePtr xtext = (XmlNodePtr)text;
00244 text->m_self = xtext;
00245 return text;
00246 }
00247
00248 XmlDeclaration::XmlDeclaration( const String& _version,
00249 const String& _encoding,
00250 const String& _standalone )
00251 : XmlNode( XmlNode::DECLARATION ),
00252 version(_version),
00253 encoding(_encoding),
00254 standalone(_standalone)
00255 {
00256 }
00257
00258 XmlDeclaration::XmlDeclaration( const XmlDeclaration& copy )
00259 : XmlNode( XmlNode::DECLARATION ), version(), encoding(), standalone()
00260 {
00261 copy.CopyTo( *this );
00262 }
00263
00264 XmlDeclaration::~XmlDeclaration()
00265 {
00266 }
00267
00268 void XmlDeclaration::operator =( const XmlDeclaration& copy )
00269 {
00270 Clear();
00271 copy.CopyTo( *this );
00272 }
00273
00274 String XmlDeclaration::Name() const
00275 {
00276 return String("#xml-declaration");
00277 }
00278
00279 void XmlDeclaration::CopyTo( XmlDeclaration& target ) const
00280 {
00281 XmlNode::CopyTo( target );
00282
00283 target.version = version;
00284 target.encoding = encoding;
00285 target.standalone = standalone;
00286 }
00287
00288 XmlNodePtr XmlDeclaration::Clone() const
00289 {
00290 XmlDeclarationPtr clone = new XmlDeclaration();
00291 XmlNodePtr xclone = (XmlNodePtr)clone;
00292 clone->m_self = xclone;
00293 CopyTo( *clone );
00294 return clone;
00295 }
00296
00297 void XmlDeclaration::WriteTo( TextWriter& writer ) const
00298 {
00299 writer.Write( "<?xml " );
00300
00301 if ( version.Length() > 0 )
00302 {
00303 writer.WriteFormat("version=\"%s\" ", version.GetChars());
00304 }
00305 if ( encoding.Length() > 0 )
00306 {
00307 writer.WriteFormat("encoding=\"%s\" ", encoding.GetChars());
00308 }
00309 if ( standalone.Length() > 0 )
00310 {
00311 writer.WriteFormat("standalone=\"%s\" ", standalone.GetChars());
00312 }
00313 writer.Write( "?>" );
00314 }
00315
00316 StringPtr XmlDeclaration::ToString() const
00317 {
00318 StringBuffer buf;
00319 buf.Append( "<?xml " );
00320
00321 if ( version.Length() > 0 )
00322 {
00323 buf.Append("version=\"");
00324 buf.Append(version.GetChars());
00325 buf.Append("\" ");
00326 }
00327 if ( encoding.Length() > 0 )
00328 {
00329 buf.Append("encoding=\"");
00330 buf.Append(encoding.GetChars());
00331 buf.Append("\" ");
00332 }
00333 if ( standalone.Length() > 0 )
00334 {
00335 buf.Append("standalone=\"");
00336 buf.Append(standalone.GetChars());
00337 buf.Append("\" ");
00338 }
00339 buf.Append( "?>" );
00340 return buf.ToString();
00341 }
00342
00343 XmlDeclarationPtr XmlDeclaration::ToDeclaration() const
00344 {
00345 return (XmlNodePtr)m_self;
00346 }
00347
00348 #ifdef DEBUG
00349 void XmlDeclaration::ValidateMem() const
00350 {
00351 XmlNode::ValidateMem();
00352 version.ValidateMem();
00353 encoding.ValidateMem();
00354 standalone.ValidateMem();
00355 }
00356
00357 void XmlDeclaration::CheckMem() const
00358 {
00359 XmlNode::CheckMem();
00360 version.CheckMem();
00361 encoding.CheckMem();
00362 standalone.CheckMem();
00363 }
00364 #endif
00365
00366 String XmlUnknown::Name() const
00367 {
00368 return String("#text");
00369 }
00370
00371 void XmlUnknown::WriteTo( TextWriter& writer ) const
00372 {
00373 writer.WriteFormat( "<%s>", m_value.GetChars() );
00374 }
00375
00376 StringPtr XmlUnknown::ToString() const
00377 {
00378 return String::Format( "<%s>", m_value.GetChars() );
00379 }
00380
00381 void XmlUnknown::CopyTo( XmlUnknown& target ) const
00382 {
00383 XmlNode::CopyTo( target );
00384 }
00385
00386 XmlNodePtr XmlUnknown::Clone() const
00387 {
00388 XmlUnknownPtr clone(new XmlUnknown());
00389 XmlNodePtr xclone = (XmlNodePtr)clone;
00390 clone->m_self = xclone;
00391 CopyTo( *clone );
00392 return clone;
00393 }
00394
00395 XmlUnknownPtr XmlUnknown::CreateUnknown()
00396 {
00397 XmlUnknownPtr ukn(new XmlUnknown());
00398 XmlNodePtr xukn = (XmlNodePtr)ukn;
00399 ukn->m_self = xukn;
00400 return ukn;
00401 }
00402
00403 XmlNodeList::XmlNodeList()
00404 : m_nodes()
00405 {
00406 }
00407
00408 XmlNodeList::~XmlNodeList()
00409 {
00410 }
00411
00412 void XmlNodeList::Clear()
00413 {
00414 m_nodes.Clear();
00415 }
00416
00417 #ifdef DEBUG
00418 void XmlNodeList::ValidateMem() const
00419 {
00420 m_nodes.ValidateMem();
00421 }
00422
00423 void XmlNodeList::CheckMem() const
00424 {
00425 m_nodes.CheckMem();
00426 }
00427 #endif
00428
00429
00430 XmlNamedNodeMap::XmlNamedNodeMap()
00431 : m_map()
00432 {
00433 }
00434
00435 XmlNamedNodeMap::~XmlNamedNodeMap()
00436 {
00437 }
00438
00439 XmlNodePtr XmlNamedNodeMap::RemoveNamedItem(const String& name)
00440 {
00441 XmlNodePtr node;
00442 int count = m_nodes.Count();
00443 for ( int x = 0; x < count; x++ )
00444 {
00445 node = m_nodes.ElementAt(x);
00446 if ( node->Name().Equals(name) )
00447 {
00448 m_nodes.RemoveAt(x);
00449 break;
00450 }
00451 else
00452 {
00453 node = XmlNodePtr();
00454 }
00455 }
00456 m_map.Remove(name);
00457 return node;
00458 }
00459
00460 void XmlNamedNodeMap::SetNamedItem( XmlNodePtr node )
00461 {
00462 node.ValidateMem();
00463 if ( m_map.ContainsKey( node->Name() ) )
00464 {
00465 throw new InvalidArgumentException("A node with that name already exists in the collection");
00466 }
00467 m_map.Set( node->Name(), node );
00468 m_nodes.Add( node );
00469 }
00470
00471 #ifdef DEBUG
00472 void XmlNamedNodeMap::ValidateMem() const
00473 {
00474 m_map.ValidateMem();
00475 XmlNodeList::ValidateMem();
00476 }
00477
00478 void XmlNamedNodeMap::CheckMem() const
00479 {
00480 m_map.CheckMem();
00481 XmlNodeList::CheckMem();
00482 }
00483 #endif
00484
00485 XmlAttributeCollection::XmlAttributeCollection()
00486 {
00487 }
00488
00489 XmlAttributeCollection::~XmlAttributeCollection()
00490 {
00491 }
00492
00493 XmlAttributePtr XmlAttributeCollection::Find( const String& name ) const
00494 {
00495 XmlAttributePtr attrib = (XmlAttributePtr)GetNamedItem(name);
00496 return attrib;
00497 }
00498
00499 void XmlAttributeCollection::Add(XmlAttributePtr attrib)
00500 {
00501 attrib.ValidateMem();
00502 SetNamedItem(attrib);
00503 }
00504
00505 XmlAttributePtr XmlAttributeCollection::First() const
00506 {
00507 if ( Count() == 0 )
00508 {
00509 return XmlAttributePtr();
00510 }
00511 return (XmlAttributePtr)Item(0);
00512 }
00513
00514 void XmlAttributeCollection::Remove( XmlAttributePtr attrib )
00515 {
00516 attrib.ValidateMem();
00517 RemoveNamedItem(attrib->Name());
00518 }
00519
00520 StringPtr XmlAttributeCollection::ToString() const
00521 {
00522 StringBuffer buf;
00523 int count = Count();
00524
00525 for ( int x = 0; x < count; x++ )
00526 {
00527 XmlAttributePtr attrib = (XmlAttributePtr)m_nodes.ElementAt(x);
00528 if ( x != 0 )
00529 {
00530 buf.Append(' ');
00531 }
00532 buf.Append( attrib->ToString() );
00533 }
00534 return buf.ToString();
00535 }
00536
00537 void XmlAttributeCollection::Write( TextWriter& writer ) const
00538 {
00539 int count = Count();
00540
00541 for ( int x = 0; x < count; x++ )
00542 {
00543 XmlAttributePtr attrib = (XmlAttributePtr)m_nodes.ElementAt(x);
00544 attrib.ValidateMem();
00545 if ( x != 0 )
00546 {
00547 writer.Write( " " );
00548 }
00549 attrib->WriteTo( writer );
00550 }
00551 }
00552
00553 #ifdef DEBUG
00554 void XmlAttributeCollection::ValidateMem() const
00555 {
00556 XmlNamedNodeMap::ValidateMem();
00557 }
00558
00559 void XmlAttributeCollection::CheckMem() const
00560 {
00561 XmlNamedNodeMap::CheckMem();
00562 }
00563 #endif
00564