1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 #ifndef CSI_XML_XMLITEM_HXX 25 #define CSI_XML_XMLITEM_HXX 26 27 // USED SERVICES 28 // BASE CLASSES 29 // COMPONENTS 30 #include <cosv/tpl/swelist.hxx> 31 #include <cosv/tpl/dyn.hxx> 32 // PARAMETERS 33 34 namespace csv 35 { 36 class bostream; 37 } 38 39 40 namespace csi 41 { 42 namespace xml 43 { 44 45 46 /* Basics: 47 Item, Attribute, Element, TextContext 48 */ 49 50 class Item 51 { 52 public: 53 virtual ~Item() {} 54 void WriteOut( 55 csv::bostream & io_aFile ) const; 56 private: 57 virtual void do_WriteOut( 58 csv::bostream & io_aFile ) const = 0; 59 }; 60 61 typedef csv::SweList_dyn< Item > ItemList; 62 63 class Attribute 64 { 65 public: 66 virtual ~Attribute() {} 67 68 void WriteOut( 69 csv::bostream & io_aFile ) const; 70 71 const ::csv::String& Name() const; 72 const ::csv::String& Value() const; 73 74 private: 75 virtual const ::csv::String & 76 inq_Name() const = 0; 77 virtual const ::csv::String & 78 inq_Value() const = 0; 79 }; 80 81 typedef csv::SweList_dyn< Attribute > AttrList; 82 83 84 class Element : public Item 85 { 86 public: 87 Element & operator<<( /// For multiple content items. 88 DYN Item * let_dpItem ); 89 Element & operator<<( /// For multiple content items. 90 const ::csv::String& let_drText ); 91 Element & operator<<( /// For multiple content items. 92 const char * let_dpText ); 93 Element & operator<<( 94 DYN Attribute * let_dpAttr ); 95 96 Element & operator>>( /// For multiple content items. @return the child Element. 97 DYN Element & let_drElement ); 98 99 100 Item * SetContent( /// For only one content item. 101 DYN Item * let_dpItem ); /// Replaces previous content. May be 0, then all content is deleted. 102 private: 103 // Interface Item: 104 virtual void do_WriteOut( 105 csv::bostream & io_aFile ) const; 106 // Local 107 virtual void op_streamout( 108 DYN Item * let_dpItem ) = 0; 109 virtual void op_streamout( 110 DYN Attribute * let_dpAttr ) = 0; 111 112 virtual void do_SetContent( 113 DYN Item * let_dpItem ) = 0; 114 // Helpers 115 virtual const ::csv::String & 116 inq_TagName() const = 0; 117 virtual const Item * 118 inq_Content() const = 0; 119 virtual const AttrList * 120 inq_Attrs() const = 0; 121 122 virtual bool FinishEmptyTag_XmlStyle() const; /// Defaulted to: true 123 124 virtual bool LineBreakAfterBeginTag() const; /// Defaulted to: false 125 virtual bool LineBreakAfterEndTag() const; /// Defaulted to: true, if LineBreakAfterBeginTag() 126 }; 127 128 class TextContent : public Item 129 { 130 }; 131 132 133 /* Implementation simplifiers: 134 EmptyElement, PureElement, SglTag 135 */ 136 137 class EmptyElement : public Element 138 { 139 private: 140 // Interface Element: 141 virtual void op_streamout( /// does nothing 142 DYN Item * let_dpItem ); 143 virtual void op_streamout( 144 DYN Attribute * let_dpAttr ); 145 virtual void do_SetContent( /// does nothing 146 DYN Item * let_dpItem ); 147 virtual const Item * 148 inq_Content() const; /// @return 0 149 virtual const AttrList * 150 inq_Attrs() const; 151 152 // Local 153 virtual AttrList & inq_RefAttrs() = 0; 154 }; 155 156 class PureElement : public Element 157 { 158 private: 159 // Interface Element: 160 virtual void op_streamout( 161 DYN Item * let_dpItem ); 162 virtual void op_streamout( /// does nothing 163 DYN Attribute * let_dpAttr ); 164 virtual void do_SetContent( 165 DYN Item * let_dpItem ); 166 virtual const Item * 167 inq_Content() const; 168 virtual const AttrList * 169 inq_Attrs() const; /// @return 0 170 // Local 171 virtual Dyn< Item > & 172 inq_RefContent() = 0; 173 }; 174 175 class SglTag : public Element 176 { 177 private: 178 // Interface Element: 179 virtual void op_streamout( /// does nothing 180 DYN Item * let_dpItem ); 181 virtual void op_streamout( /// does nothing 182 DYN Attribute * let_dpAttr ); 183 virtual void do_SetContent( /// does nothing 184 DYN Item * let_dpItem ); 185 virtual const Item * 186 inq_Content() const; /// @return 0 187 virtual const AttrList * 188 inq_Attrs() const; /// @return 0 189 }; 190 191 192 193 /* Standard Element implementations, if there are not any 194 specialized ones. 195 */ 196 197 class AnElement : public Element 198 { 199 public: 200 AnElement( 201 const ::csv::String& i_sTagName ); 202 AnElement( 203 const char * i_sTagName ); 204 ~AnElement(); 205 private: 206 // Interface Element 207 virtual void op_streamout( 208 DYN Item * let_dpItem ); 209 virtual void op_streamout( 210 DYN Attribute * let_dpAttr ); 211 virtual void do_SetContent( 212 DYN Item * let_dpItem ); 213 virtual const ::csv::String & 214 inq_TagName() const; 215 virtual const Item * 216 inq_Content() const; 217 virtual const AttrList * 218 inq_Attrs() const; 219 // DATA 220 ::csv::String sTagName; 221 Dyn< Item > pContent; 222 AttrList aAttrs; 223 }; 224 225 226 class AnEmptyElement : public EmptyElement 227 { 228 public: 229 AnEmptyElement( 230 const ::csv::String & i_sTagName ); 231 AnEmptyElement( 232 const char * i_sTagName ); 233 ~AnEmptyElement(); 234 private: 235 // Interface Element: 236 virtual const ::csv::String & 237 inq_TagName() const; 238 // Interface EmptyElement: 239 virtual AttrList & inq_RefAttrs(); 240 241 // DATA 242 ::csv::String sTagName; 243 AttrList aAttrs; 244 }; 245 246 class APureElement : public PureElement 247 { 248 public: 249 APureElement( 250 const ::csv::String & i_sTagName ); 251 APureElement( 252 const char * i_sTagName ); 253 ~APureElement(); 254 private: 255 // Interface Element: 256 virtual const ::csv::String & 257 inq_TagName() const; 258 // Interface PureElement: 259 virtual Dyn< Item > & 260 inq_RefContent(); 261 // DATA 262 ::csv::String sTagName; 263 Dyn< Item > pContent; 264 }; 265 266 class ASglTag : public SglTag 267 { 268 public: 269 ASglTag( 270 const ::csv::String & i_sTagName ); 271 ASglTag( 272 const char * i_sTagName ); 273 ~ASglTag(); 274 private: 275 // Interface Element: 276 virtual const ::csv::String & 277 inq_TagName() const; 278 // DATA 279 ::csv::String sTagName; 280 }; 281 282 283 /* Standard Attribute implementation 284 */ 285 class AnAttribute : public Attribute 286 { 287 public: 288 AnAttribute( 289 const ::csv::String & i_sName, 290 const ::csv::String & i_sValue ); 291 AnAttribute( 292 const char * i_sName, 293 const char * i_sValue ); 294 ~AnAttribute(); 295 private: 296 // Interface Attribute: 297 virtual const ::csv::String & 298 inq_Name() const; 299 virtual const ::csv::String & 300 inq_Value() const; 301 // DATA 302 ::csv::String sName; 303 ::csv::String sValue; 304 }; 305 306 307 308 /* Implementations of TextContent: 309 310 Text ( reserved characters will be replaced and appear unchanged ) 311 XmlCode ( reserved characters stay and are interpreted 312 by the XML-viewer ) 313 */ 314 class Text : public TextContent 315 { 316 public: 317 Text( 318 const ::csv::String & i_sText ); 319 Text( 320 const char * i_sText ); 321 ~Text(); 322 private: 323 virtual void do_WriteOut( 324 csv::bostream & io_aFile ) const; 325 // DATA 326 ::csv::String sText; 327 }; 328 329 330 class XmlCode : public TextContent 331 { 332 public: 333 XmlCode( 334 const ::csv::String & i_sText ); 335 XmlCode( 336 const char * i_sText ); 337 ~XmlCode(); 338 private: 339 virtual void do_WriteOut( 340 csv::bostream & io_aFile ) const; 341 // DATA 342 ::csv::String sText; 343 }; 344 345 346 347 // IMPLEMENTATION 348 349 inline void 350 Item::WriteOut( csv::bostream & io_aFile ) const 351 { do_WriteOut(io_aFile); } 352 353 inline const ::csv::String & 354 Attribute::Name() const 355 { return inq_Name(); } 356 inline const ::csv::String & 357 Attribute::Value() const 358 { return inq_Value(); } 359 360 inline Element & 361 Element::operator<<( DYN Item * let_dpItem ) 362 { op_streamout(let_dpItem); return *this; } 363 inline Element & 364 Element::operator<<( const ::csv::String & let_drText ) 365 { op_streamout( new Text(let_drText) ); return *this; } 366 inline Element & 367 Element::operator<<( const char * let_drText ) 368 { op_streamout( new Text(let_drText) ); return *this; } 369 inline Element & 370 Element::operator<<( DYN Attribute * let_dpAttr ) 371 { op_streamout(let_dpAttr); return *this; } 372 inline Element & 373 Element::operator>>( DYN Element & let_drElement ) 374 { op_streamout(&let_drElement); return let_drElement; } 375 inline Item * 376 Element::SetContent( DYN Item * let_dpItem ) 377 { do_SetContent(let_dpItem); return let_dpItem; } 378 379 380 } // namespace xml 381 } // namespace csi 382 383 #endif 384