00001 /* 00002 * Copyright (C) 2003-2009 Olivier Boudeville 00003 * 00004 * This file is part of the Ceylan library. 00005 * 00006 * The Ceylan library is free software: you can redistribute it and/or modify 00007 * it under the terms of either the GNU Lesser General Public License or 00008 * the GNU General Public License, as they are published by the Free Software 00009 * Foundation, either version 3 of these Licenses, or (at your option) 00010 * any later version. 00011 * 00012 * The Ceylan library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License and the GNU General Public License 00016 * for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License and the GNU General Public License along with the Ceylan library. 00020 * If not, see <http://www.gnu.org/licenses/>. 00021 * 00022 * Author: Olivier Boudeville (olivier.boudeville@esperide.com) 00023 * 00024 */ 00025 00026 00027 #include "CeylanXMLElement.h" 00028 00029 #include "CeylanInputStream.h" // for InputStream 00030 #include "CeylanOutputStream.h" // for OutputStream 00031 #include "CeylanXMLVisitor.h" // for (XML) Visitor 00032 00033 00034 00035 using namespace Ceylan ; 00036 using namespace Ceylan::XML ; 00037 00038 using std::string ; 00039 using std::map ; 00040 00041 00042 00043 00044 /* 00045 * To add in future releases, if necessary: 00046 * XMLBinary, with Ceylan::Byte * _data and Ceylan::Uint32 _size 00047 * 00048 */ 00049 00050 00051 00052 // Exception section. 00053 00054 00055 XMLElementException::XMLElementException( const std::string & reason ) : 00056 XMLException( reason ) 00057 { 00058 00059 } 00060 00061 00062 XMLElementException::~XMLElementException() throw() 00063 { 00064 00065 } 00066 00067 00068 00069 00070 00071 // XMLElement section. 00072 00073 00074 00075 XMLElement::XMLElement() 00076 { 00077 00078 } 00079 00080 00081 00082 XMLElement::~XMLElement() throw() 00083 { 00084 00085 } 00086 00087 00088 00089 00090 00091 00092 // XMLMarkup section. 00093 00094 00095 00096 XMLMarkup::XMLMarkup() : 00097 _name() 00098 { 00099 00100 } 00101 00102 00103 00104 XMLMarkup::XMLMarkup( const MarkupName & name ) : 00105 _name( name ) 00106 { 00107 00108 } 00109 00110 00111 00112 XMLMarkup::~XMLMarkup() throw() 00113 { 00114 00115 } 00116 00117 00118 00119 MarkupName XMLMarkup::getMarkupName() const 00120 { 00121 00122 return _name ; 00123 00124 } 00125 00126 00127 00128 string XMLMarkup::getClosingMarkup() const 00129 { 00130 00131 return "</" + _name + '>' ; 00132 00133 } 00134 00135 00136 00137 bool XMLMarkup::hasAttribute( const string & name ) const 00138 { 00139 00140 return ( _attributes.find( name ) != _attributes.end() ) ; 00141 00142 } 00143 00144 00145 00146 string XMLMarkup::getAttribute( const string & name ) const 00147 { 00148 00149 map<string,string>::const_iterator it = _attributes.find( name ) ; 00150 00151 if ( it == _attributes.end() ) 00152 return "" ; 00153 else 00154 return (*it).second ; 00155 00156 } 00157 00158 00159 00160 string XMLMarkup::getExistingAttribute( const string & name ) const 00161 { 00162 00163 map<string,string>::const_iterator it = _attributes.find( name ) ; 00164 00165 if ( it != _attributes.end() ) 00166 return (*it).second ; 00167 else 00168 throw XMLElementException( "XMLMarkup::getExistingAttribute failed: " 00169 "no attribute '" + name + "' defined." ) ; 00170 00171 } 00172 00173 00174 00175 void XMLMarkup::setAttribute( const string & name, const string & value ) 00176 { 00177 00178 _attributes[ name ] = value ; 00179 00180 } 00181 00182 00183 00184 AttributeMap & XMLMarkup::getAttributes() 00185 { 00186 00187 return _attributes ; 00188 00189 } 00190 00191 00192 00193 void XMLMarkup::loadFrom( System::InputStream & input ) 00194 { 00195 00196 throw SerializationException( "XMLMarkup::loadFrom: " 00197 "not implemented yet." ) ; 00198 00199 } 00200 00201 00202 00203 void XMLMarkup::accept( Visitor & visitor ) 00204 { 00205 00206 XMLVisitor * actualVisitor = dynamic_cast<XMLVisitor *>( & visitor ) ; 00207 00208 if ( actualVisitor == 0 ) 00209 throw VisitException( "XMLMarkup::accept failed: " 00210 "specified visitor (" + visitor.toString() 00211 + ") is not a XML-enabled visitor." ) ; 00212 00213 actualVisitor->visit( *this ) ; 00214 00215 } 00216 00217 00218 00219 void XMLMarkup::saveTo( System::OutputStream & output ) const 00220 { 00221 00222 string res = "<" + _name ; 00223 00224 for ( map<string,string>::const_iterator it = _attributes.begin(); 00225 it != _attributes.end(); it++ ) 00226 res += ' ' + (*it).first + "=\"" + (*it).second + '"' ; 00227 00228 res += ">" ; 00229 00230 // No other encoding needed: 00231 output.write( res ) ; 00232 00233 } 00234 00235 00236 00237 const string XMLMarkup::toString( Ceylan::VerbosityLevels level ) const 00238 { 00239 00240 string res = "<" + _name ; 00241 00242 for ( map<string,string>::const_iterator it = _attributes.begin(); 00243 it != _attributes.end(); it++ ) 00244 res += ' ' + (*it).first + "=\"" + (*it).second + '"' ; 00245 00246 res += ">" ; 00247 00248 // Avoid having mark-ups messing with the HTML encoding: 00249 if ( TextDisplayable::GetOutputFormat() == TextDisplayable::html ) 00250 return encodeToHTML( res ) ; 00251 else 00252 return res ; 00253 00254 } 00255 00256 00257 00258 00259 00260 00261 00262 // XMLText section. 00263 00264 00265 00266 00267 XMLText::XMLText() : 00268 _text() 00269 { 00270 00271 } 00272 00273 00274 00275 XMLText::XMLText( const std::string & text ) : 00276 _text( text ) 00277 { 00278 00279 } 00280 00281 00282 00283 XMLText::~XMLText() throw() 00284 { 00285 00286 } 00287 00288 00289 00290 string XMLText::getText() const 00291 { 00292 00293 return _text ; 00294 00295 } 00296 00297 00298 00299 void XMLText::setText( const string & newText ) 00300 { 00301 00302 _text = newText ; 00303 00304 } 00305 00306 00307 00308 void XMLText::loadFrom( System::InputStream & input ) 00309 { 00310 00311 throw SerializationException( "XMLText::loadFrom: " 00312 "not implemented yet." ) ; 00313 00314 } 00315 00316 00317 00318 void XMLText::accept( Visitor & visitor ) 00319 { 00320 00321 XMLVisitor * actualVisitor = dynamic_cast<XMLVisitor *>( & visitor ) ; 00322 00323 if ( actualVisitor == 0 ) 00324 throw VisitException( "XMLText::accept failed: " 00325 "specified visitor (" + visitor.toString() 00326 + ") is not a XML-enabled visitor." ) ; 00327 00328 actualVisitor->visit( *this ) ; 00329 00330 } 00331 00332 00333 00334 void XMLText::saveTo( System::OutputStream & output ) const 00335 { 00336 00337 output.write( _text ) ; 00338 00339 } 00340 00341 00342 00343 const string XMLText::toString( Ceylan::VerbosityLevels level ) const 00344 { 00345 00346 return _text ; 00347 00348 } 00349