Bump for 3.6-28
[LibreOffice.git] / udm / source / xml / xmlitem.cxx
blobd613b84e43487e737072c00eb822f669b9c0012f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <precomp.h>
30 #include <udm/xml/xmlitem.hxx>
32 // NOT FULLY DECLARED SERVICES
33 #include <cosv/file.hxx>
36 namespace csi
38 namespace xml
41 char cReplacable[256] =
43 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 49
44 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, // ", &
47 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 50 - 99
50 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, // <, >
51 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,
55 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 100 - 149
56 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57 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,
61 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
64 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
67 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
69 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
73 0, 0, 0, 0, 0, 1 // &nbsp;
77 class MultiItem : public Item
79 public:
80 MultiItem();
81 ~MultiItem();
83 void Add(
84 DYN Item * let_dpDatum )
85 { csv_assert( let_dpDatum != 0 );
86 aItems.push_back(let_dpDatum); }
87 void Erase() { aItems.erase_all(); }
89 private:
90 virtual void do_WriteOut(
91 csv::bostream & io_aFile ) const;
92 // DATA
93 ItemList aItems;
97 void StreamOut(
98 Dyn< Item > & o_rContent,
99 DYN Item * let_dpItem );
100 inline void
101 StreamOut( AttrList & o_rAttrs,
102 DYN Attribute * let_dpAttr )
104 csv_assert( let_dpAttr != 0 );
105 o_rAttrs.push_back( let_dpAttr );
109 inline void
110 Impl_SetContent( Dyn< Item > & o_rContent,
111 DYN Item * let_dpItem )
113 o_rContent = let_dpItem;
117 //********************* Attribute ****************************//
119 const String attrValueBegin("=\"");
120 const String attrValueEnd("\"");
122 void
123 Attribute::WriteOut( csv::bostream & io_aFile ) const
125 io_aFile.write( Name() );
126 if ( Value().length() > 0 )
128 io_aFile.write( attrValueBegin );
129 io_aFile.write( Value() );
130 io_aFile.write( attrValueEnd );
136 //************************ Element ****************************//
138 const String newline("\n");
139 const String space(" ");
140 const String beginTagBegin("<");
141 const String endTagBegin("</");
142 const String tagEnd(">");
143 const String emptyTagEnd("/>");
145 void
146 Element::do_WriteOut( csv::bostream & io_aFile ) const
148 io_aFile.write( beginTagBegin );
149 io_aFile.write( inq_TagName() );
151 const AttrList * pAttrs = inq_Attrs();
152 if ( pAttrs != 0 )
154 for ( AttrList::iterator it = pAttrs->begin();
155 it != pAttrs->end();
156 ++it )
159 io_aFile.write( space );
160 (*it)->WriteOut( io_aFile );
164 const Item * pContent = inq_Content();
165 if ( pContent != 0 )
166 io_aFile.write( tagEnd );
167 else
169 if (FinishEmptyTag_XmlStyle())
170 io_aFile.write( emptyTagEnd );
171 else
173 io_aFile.write( tagEnd );
174 io_aFile.write( endTagBegin );
175 io_aFile.write( inq_TagName() );
176 io_aFile.write( tagEnd );
179 if ( LineBreakAfterBeginTag() )
180 io_aFile.write( newline );
181 if ( pContent == 0 )
182 return;
184 pContent->WriteOut( io_aFile );
185 io_aFile.write( endTagBegin );
186 io_aFile.write( inq_TagName() );
187 io_aFile.write( tagEnd );
188 if ( LineBreakAfterEndTag() )
189 io_aFile.write( newline );
192 bool
193 Element::FinishEmptyTag_XmlStyle() const
195 return true;
198 bool
199 Element::LineBreakAfterBeginTag() const
201 return false;
204 bool
205 Element::LineBreakAfterEndTag() const
207 return LineBreakAfterBeginTag();
211 //************************ EmptyElement ****************************//
213 void
214 EmptyElement::op_streamout( DYN Item * )
216 // Does nothing.
219 void
220 EmptyElement::op_streamout( DYN Attribute * let_dpAttr )
222 StreamOut( inq_RefAttrs(), let_dpAttr );
225 void
226 EmptyElement::do_SetContent( DYN Item * )
228 // Does nothing.
231 const Item *
232 EmptyElement::inq_Content() const
234 return 0;
237 const AttrList *
238 EmptyElement::inq_Attrs() const
240 return & const_cast< EmptyElement* >(this)->inq_RefAttrs();
244 //************************ PureElement ****************************//
246 void
247 PureElement::op_streamout( DYN Item * let_dpItem )
249 StreamOut( inq_RefContent(), let_dpItem );
252 void
253 PureElement::op_streamout( DYN Attribute * )
255 // Does nothing.
258 void
259 PureElement::do_SetContent( DYN Item * let_dpItem )
261 Impl_SetContent( inq_RefContent(), let_dpItem );
264 const Item *
265 PureElement::inq_Content() const
267 return const_cast< PureElement* >(this)->inq_RefContent().Ptr();
270 const AttrList *
271 PureElement::inq_Attrs() const
273 return 0;
277 //*************************** SglTag **************************//
279 void
280 SglTag::op_streamout( DYN Item * )
282 // Does nothing.
285 void
286 SglTag::op_streamout( DYN Attribute * )
288 // Does nothing.
291 void
292 SglTag::do_SetContent( DYN Item *)
294 // Does nothing.
297 const Item *
298 SglTag::inq_Content() const
300 return 0;
303 const AttrList *
304 SglTag::inq_Attrs() const
306 return 0;
310 //*************************** AnElement **************************//
312 AnElement::AnElement( const char * i_sTagName )
313 : sTagName( i_sTagName )
314 // pContent,
315 // aAttrs
319 AnElement::~AnElement()
323 void
324 AnElement::op_streamout( DYN Item * let_dpItem )
326 StreamOut( pContent, let_dpItem );
329 void
330 AnElement::op_streamout( DYN Attribute * let_dpAttr )
332 StreamOut( aAttrs, let_dpAttr );
335 void
336 AnElement::do_SetContent( DYN Item * let_dpItem )
338 Impl_SetContent( pContent, let_dpItem );
341 const String &
342 AnElement::inq_TagName() const
344 return sTagName;
347 const Item *
348 AnElement::inq_Content() const
350 return pContent.Ptr();
353 const AttrList *
354 AnElement::inq_Attrs() const
356 return &aAttrs;
359 //*************************** APureElement **************************//
361 APureElement::APureElement( const char * i_sTagName )
362 : sTagName( i_sTagName )
363 // pContent
367 APureElement::~APureElement()
371 const String &
372 APureElement::inq_TagName() const
374 return sTagName;
377 Dyn< Item > &
378 APureElement::inq_RefContent()
380 return pContent;
383 //*************************** AnAttribute **************************//
384 AnAttribute::AnAttribute( const String & i_sName,
385 const String & i_sValue )
386 : sName(i_sName),
387 sValue(i_sValue)
391 AnAttribute::AnAttribute( const char * i_sName,
392 const char * i_sValue )
393 : sName(i_sName),
394 sValue(i_sValue)
398 AnAttribute::~AnAttribute()
402 const String &
403 AnAttribute::inq_Name() const
405 return sName;
408 const String &
409 AnAttribute::inq_Value() const
411 return sValue;
416 //*************************** Text **************************//
418 Text::Text( const String & i_sText )
419 : sText(i_sText)
423 Text::Text( const char * i_sText )
424 : sText(i_sText)
428 Text::~Text()
432 void
433 Text::do_WriteOut( csv::bostream & io_aFile ) const
435 const unsigned char *
436 pStart = reinterpret_cast< const unsigned char* >(sText.c_str());
437 const unsigned char *
438 pOut = pStart;
440 for ( ; *pOut != '\0'; ++pOut )
442 if ( cReplacable[*pOut] )
444 if ( pOut != pStart )
446 io_aFile.write( pStart, pOut-pStart );
449 switch (*pOut)
451 case '<': io_aFile.write("&lt;"); break;
452 case '>': io_aFile.write("&gt;"); break;
453 case '"': io_aFile.write("&quot;"); break;
454 case '&': io_aFile.write("&amp;"); break;
455 case 255: io_aFile.write("&nbsp;"); break;
458 pStart = pOut+1;
459 } // endif (cReplacable[*pOut])
460 } // end for
462 if ( pOut != pStart )
464 io_aFile.write( pStart, pOut-pStart );
469 //*************************** XmlCode **************************//
471 XmlCode::XmlCode( const String & i_sText )
472 : sText(i_sText)
476 XmlCode::XmlCode( const char * i_sText )
477 : sText(i_sText)
481 XmlCode::~XmlCode()
485 void
486 XmlCode::do_WriteOut( csv::bostream & io_aFile ) const
488 io_aFile.write(sText);
492 //*************************** MultiItem **************************//
494 MultiItem::MultiItem()
498 MultiItem::~MultiItem()
502 void
503 MultiItem::do_WriteOut( csv::bostream & io_aFile ) const
505 ItemList::iterator itEnd = aItems.end();
507 for ( ItemList::iterator it = aItems.begin();
508 it != itEnd;
509 ++it )
511 (*it)->WriteOut( io_aFile );
518 //*************************** Helpers **************************//
520 void
521 StreamOut( Dyn< Item > & o_rContent,
522 DYN Item * let_dpItem )
524 MultiItem * pContent = 0;
525 if ( bool(o_rContent) )
527 pContent = static_cast< MultiItem* >( o_rContent.MutablePtr() );
528 csv_assert( dynamic_cast< MultiItem* >( o_rContent.MutablePtr() ) != 0 );
530 else
532 pContent = new MultiItem;
533 o_rContent = pContent;
536 csv_assert( let_dpItem != 0 );
537 pContent->Add( let_dpItem );
543 } // namespace xml
544 } // namespace csi
546 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */