Updated core
[LibreOffice.git] / udm / source / xml / xmlitem.cxx
blob2725d3d178d693d6f43d234daadf99f3780375ed
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <precomp.h>
21 #include <udm/xml/xmlitem.hxx>
23 // NOT FULLY DECLARED SERVICES
24 #include <cosv/file.hxx>
27 namespace csi
29 namespace xml
32 char cReplacable[256] =
34 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 49
35 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
36 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
37 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, // ", &
38 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
40 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 50 - 99
41 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, // <, >
42 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
43 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 100 - 149
47 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
52 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
53 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
54 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
55 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
59 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
60 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
64 0, 0, 0, 0, 0, 1 // &nbsp;
68 class MultiItem : public Item
70 public:
71 MultiItem();
72 ~MultiItem();
74 void Add(
75 DYN Item * let_dpDatum )
76 { csv_assert( let_dpDatum != 0 );
77 aItems.push_back(let_dpDatum); }
78 void Erase() { aItems.erase_all(); }
80 private:
81 virtual void do_WriteOut(
82 csv::bostream & io_aFile ) const;
83 // DATA
84 ItemList aItems;
88 void StreamOut(
89 Dyn< Item > & o_rContent,
90 DYN Item * let_dpItem );
91 inline void
92 StreamOut( AttrList & o_rAttrs,
93 DYN Attribute * let_dpAttr )
95 csv_assert( let_dpAttr != 0 );
96 o_rAttrs.push_back( let_dpAttr );
100 inline void
101 Impl_SetContent( Dyn< Item > & o_rContent,
102 DYN Item * let_dpItem )
104 o_rContent = let_dpItem;
108 //********************* Attribute ****************************//
110 const String attrValueBegin("=\"");
111 const String attrValueEnd("\"");
113 void
114 Attribute::WriteOut( csv::bostream & io_aFile ) const
116 io_aFile.write( Name() );
117 if ( Value().length() > 0 )
119 io_aFile.write( attrValueBegin );
120 io_aFile.write( Value() );
121 io_aFile.write( attrValueEnd );
127 //************************ Element ****************************//
129 const String newline("\n");
130 const String space(" ");
131 const String beginTagBegin("<");
132 const String endTagBegin("</");
133 const String tagEnd(">");
134 const String emptyTagEnd("/>");
136 void
137 Element::do_WriteOut( csv::bostream & io_aFile ) const
139 io_aFile.write( beginTagBegin );
140 io_aFile.write( inq_TagName() );
142 const AttrList * pAttrs = inq_Attrs();
143 if ( pAttrs != 0 )
145 for ( AttrList::iterator it = pAttrs->begin();
146 it != pAttrs->end();
147 ++it )
150 io_aFile.write( space );
151 (*it)->WriteOut( io_aFile );
155 const Item * pContent = inq_Content();
156 if ( pContent != 0 )
157 io_aFile.write( tagEnd );
158 else
160 if (FinishEmptyTag_XmlStyle())
161 io_aFile.write( emptyTagEnd );
162 else
164 io_aFile.write( tagEnd );
165 io_aFile.write( endTagBegin );
166 io_aFile.write( inq_TagName() );
167 io_aFile.write( tagEnd );
170 if ( LineBreakAfterBeginTag() )
171 io_aFile.write( newline );
172 if ( pContent == 0 )
173 return;
175 pContent->WriteOut( io_aFile );
176 io_aFile.write( endTagBegin );
177 io_aFile.write( inq_TagName() );
178 io_aFile.write( tagEnd );
179 if ( LineBreakAfterEndTag() )
180 io_aFile.write( newline );
183 bool
184 Element::FinishEmptyTag_XmlStyle() const
186 return true;
189 bool
190 Element::LineBreakAfterBeginTag() const
192 return false;
195 bool
196 Element::LineBreakAfterEndTag() const
198 return LineBreakAfterBeginTag();
202 //************************ EmptyElement ****************************//
204 void
205 EmptyElement::op_streamout( DYN Item * )
207 // Does nothing.
210 void
211 EmptyElement::op_streamout( DYN Attribute * let_dpAttr )
213 StreamOut( inq_RefAttrs(), let_dpAttr );
216 void
217 EmptyElement::do_SetContent( DYN Item * )
219 // Does nothing.
222 const Item *
223 EmptyElement::inq_Content() const
225 return 0;
228 const AttrList *
229 EmptyElement::inq_Attrs() const
231 return & const_cast< EmptyElement* >(this)->inq_RefAttrs();
235 //************************ PureElement ****************************//
237 void
238 PureElement::op_streamout( DYN Item * let_dpItem )
240 StreamOut( inq_RefContent(), let_dpItem );
243 void
244 PureElement::op_streamout( DYN Attribute * )
246 // Does nothing.
249 void
250 PureElement::do_SetContent( DYN Item * let_dpItem )
252 Impl_SetContent( inq_RefContent(), let_dpItem );
255 const Item *
256 PureElement::inq_Content() const
258 return const_cast< PureElement* >(this)->inq_RefContent().Ptr();
261 const AttrList *
262 PureElement::inq_Attrs() const
264 return 0;
268 //*************************** SglTag **************************//
270 void
271 SglTag::op_streamout( DYN Item * )
273 // Does nothing.
276 void
277 SglTag::op_streamout( DYN Attribute * )
279 // Does nothing.
282 void
283 SglTag::do_SetContent( DYN Item *)
285 // Does nothing.
288 const Item *
289 SglTag::inq_Content() const
291 return 0;
294 const AttrList *
295 SglTag::inq_Attrs() const
297 return 0;
301 //*************************** AnElement **************************//
303 AnElement::AnElement( const char * i_sTagName )
304 : sTagName( i_sTagName )
305 // pContent,
306 // aAttrs
310 AnElement::~AnElement()
314 void
315 AnElement::op_streamout( DYN Item * let_dpItem )
317 StreamOut( pContent, let_dpItem );
320 void
321 AnElement::op_streamout( DYN Attribute * let_dpAttr )
323 StreamOut( aAttrs, let_dpAttr );
326 void
327 AnElement::do_SetContent( DYN Item * let_dpItem )
329 Impl_SetContent( pContent, let_dpItem );
332 const String &
333 AnElement::inq_TagName() const
335 return sTagName;
338 const Item *
339 AnElement::inq_Content() const
341 return pContent.Ptr();
344 const AttrList *
345 AnElement::inq_Attrs() const
347 return &aAttrs;
350 //*************************** APureElement **************************//
352 APureElement::APureElement( const char * i_sTagName )
353 : sTagName( i_sTagName )
354 // pContent
358 APureElement::~APureElement()
362 const String &
363 APureElement::inq_TagName() const
365 return sTagName;
368 Dyn< Item > &
369 APureElement::inq_RefContent()
371 return pContent;
374 //*************************** AnAttribute **************************//
375 AnAttribute::AnAttribute( const String & i_sName,
376 const String & i_sValue )
377 : sName(i_sName),
378 sValue(i_sValue)
382 AnAttribute::AnAttribute( const char * i_sName,
383 const char * i_sValue )
384 : sName(i_sName),
385 sValue(i_sValue)
389 AnAttribute::~AnAttribute()
393 const String &
394 AnAttribute::inq_Name() const
396 return sName;
399 const String &
400 AnAttribute::inq_Value() const
402 return sValue;
407 //*************************** Text **************************//
409 Text::Text( const String & i_sText )
410 : sText(i_sText)
414 Text::Text( const char * i_sText )
415 : sText(i_sText)
419 Text::~Text()
423 void
424 Text::do_WriteOut( csv::bostream & io_aFile ) const
426 const unsigned char *
427 pStart = reinterpret_cast< const unsigned char* >(sText.c_str());
428 const unsigned char *
429 pOut = pStart;
431 for ( ; *pOut != '\0'; ++pOut )
433 if ( cReplacable[*pOut] )
435 if ( pOut != pStart )
437 io_aFile.write( pStart, pOut-pStart );
440 switch (*pOut)
442 case '<': io_aFile.write("&lt;"); break;
443 case '>': io_aFile.write("&gt;"); break;
444 case '"': io_aFile.write("&quot;"); break;
445 case '&': io_aFile.write("&amp;"); break;
446 case 255: io_aFile.write("&nbsp;"); break;
449 pStart = pOut+1;
450 } // endif (cReplacable[*pOut])
451 } // end for
453 if ( pOut != pStart )
455 io_aFile.write( pStart, pOut-pStart );
460 //*************************** XmlCode **************************//
462 XmlCode::XmlCode( const String & i_sText )
463 : sText(i_sText)
467 XmlCode::XmlCode( const char * i_sText )
468 : sText(i_sText)
472 XmlCode::~XmlCode()
476 void
477 XmlCode::do_WriteOut( csv::bostream & io_aFile ) const
479 io_aFile.write(sText);
483 //*************************** MultiItem **************************//
485 MultiItem::MultiItem()
489 MultiItem::~MultiItem()
493 void
494 MultiItem::do_WriteOut( csv::bostream & io_aFile ) const
496 ItemList::iterator itEnd = aItems.end();
498 for ( ItemList::iterator it = aItems.begin();
499 it != itEnd;
500 ++it )
502 (*it)->WriteOut( io_aFile );
509 //*************************** Helpers **************************//
511 void
512 StreamOut( Dyn< Item > & o_rContent,
513 DYN Item * let_dpItem )
515 MultiItem * pContent = 0;
516 if ( bool(o_rContent) )
518 pContent = static_cast< MultiItem* >( o_rContent.MutablePtr() );
519 csv_assert( dynamic_cast< MultiItem* >( o_rContent.MutablePtr() ) != 0 );
521 else
523 pContent = new MultiItem;
524 o_rContent = pContent;
527 csv_assert( let_dpItem != 0 );
528 pContent->Add( let_dpItem );
534 } // namespace xml
535 } // namespace csi
537 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */