summaryrefslogtreecommitdiff
path: root/sax/source/expatwrap/saxwriter.cxx
blob: ac83bbb4f01aa21c1aab6d901cc662bb7694a8bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/*************************************************************************
 *
 *  $RCSfile: saxwriter.cxx,v $
 *
 *  $Revision: 1.2 $
 *
 *  last change: $Author: jbu $ $Date: 2000-10-25 08:13:18 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/
#include <string.h>

#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/util/XCloneable.hpp>
#include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
#include <com/sun/star/xml/sax/XParser.hpp>
#include <com/sun/star/xml/sax/SAXParseException.hpp>

#include <com/sun/star/io/XActiveDataSource.hpp>

#include <cppuhelper/factory.hxx>
#include <cppuhelper/weak.hxx>
#include <cppuhelper/implbase3.hxx>

#include <rtl/strbuf.hxx>

#include <assert.h>

using namespace ::rtl;
using namespace ::std;
using namespace ::osl;
using namespace ::cppu;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::registry;
using namespace ::com::sun::star::xml::sax;
using namespace ::com::sun::star::util;
using namespace ::com::sun::star::io;

#include "factory.hxx"
#include "xml2utf.hxx"

#define LINEFEED 10

/******
*
*
* Character conversion functions
*
*
*****/

namespace sax_expatwrap {
/*****
*
* Does special conversions (beside encoding) that is needed for xml. E.g. &<>"' plus some more are
* special characters in XML that need to be transformed
*
* @param bConvertAll For Attributes it is necessary to convert every symbol (including line feed and tab)
*                    Set this to true, if you want to perform this special conversion
*
****/
sal_Int32 CalcXMLLen( const Sequence<sal_Int8> & seq , sal_Bool bConvertAll )
{
    sal_Int32 nLen = 0;
    const sal_Int8 *pArray = seq.getConstArray();


    for( int i = 0 ; i < seq.getLength() ; i ++ ) {

        sal_Int8 c = pArray[i];
        if( '&' == c ) {  // resemble to &amp;
            nLen += 5;
        }
        else if( '<' == c ) {
            nLen += 4;        // &lt;
        }
        else if( '>' == c ) {
            nLen += 4;        // &gt;
        }
        else if( 39 == c ) {    // 39 == '''
            nLen += 6;        // &apos;
        }
        else if( '"' == c ) {
            nLen += 6;        // &quot;
        }
        else if( 13 == c ) {
            nLen += 6;         // &#x0d;
        }
        else if( bConvertAll && LINEFEED == c ) {
            nLen += 6;
        }
        else if( bConvertAll &&  9 == c ) {
            nLen += 6;
        }
        else {
            nLen ++;
        }
    }

    return nLen;
}


inline sal_Int32 getFirstLineBreak( const Sequence<sal_Int8> & seq)
{
    const sal_Int8 *pSource = seq.getConstArray();

    sal_Int32 nLen  = seq.getLength();
    for( int n = 0; n < nLen ; n ++ )
    {
        if( LINEFEED == pSource[n] ) {
            return n;
        }
    }
    return -1;
}

inline sal_Int32 getLastLineBreak( const Sequence<sal_Int8>  & seq)
{
    const sal_Int8 *pSource = seq.getConstArray();
    sal_Int32 nLen  = seq.getLength();

    for( int n = nLen-1; n >= 0 ; n -- )
    {
        if( LINEFEED == pSource[n] ) {
            return n;
        }
    }
    return -1;
}


class SAXWriter :
    public WeakImplHelper3<
            XActiveDataSource,
            XExtendedDocumentHandler,
              XServiceInfo >
{
public:
    SAXWriter( ) :
        m_nMaxColumn(72),
        m_bForceLineBreak(sal_False),
        m_bAllowLineBreak(sal_False),
        m_unicode2utf8( RTL_TEXTENCODING_UTF8 )
        {}

public: // XActiveDataSource
    virtual void SAL_CALL setOutputStream(const Reference< XOutputStream > & aStream)
        throw (RuntimeException)
            {
                m_out = aStream;
                m_bDocStarted = sal_False;
                m_nLevel = 0;
                m_bIsCDATA = sal_False;
                m_nColumn = 0;
            }
    virtual Reference< XOutputStream >  SAL_CALL getOutputStream(void)
        throw(RuntimeException)
            { return m_out; }

public: // XDocumentHandler
    virtual void SAL_CALL startDocument(void)
        throw(SAXException, RuntimeException);

    virtual void SAL_CALL endDocument(void)
        throw(SAXException, RuntimeException);

    virtual void SAL_CALL startElement(const OUString& aName,
                                       const Reference< XAttributeList > & xAttribs)
        throw (SAXException, RuntimeException);

    virtual void SAL_CALL endElement(const OUString& aName)
        throw(SAXException, RuntimeException);

    virtual void SAL_CALL characters(const OUString& aChars)
        throw(SAXException, RuntimeException);

    virtual void SAL_CALL ignorableWhitespace(const OUString& aWhitespaces)
        throw(SAXException, RuntimeException);
    virtual void SAL_CALL processingInstruction(const OUString& aTarget,
                                                const OUString& aData)
        throw(SAXException, RuntimeException);
    virtual void SAL_CALL setDocumentLocator(const Reference< XLocator > & xLocator)
        throw(SAXException, RuntimeException);

public: // XExtendedDocumentHandler
    virtual void SAL_CALL startCDATA(void) throw(SAXException, RuntimeException);
    virtual void SAL_CALL endCDATA(void) throw(RuntimeException);
    virtual void SAL_CALL comment(const OUString& sComment)
        throw(SAXException, RuntimeException);
    virtual void SAL_CALL unknown(const OUString& sString)
        throw(SAXException, RuntimeException);
    virtual void SAL_CALL allowLineBreak(void)
        throw(SAXException,RuntimeException);

public: // XServiceInfo
    OUString                     SAL_CALL getImplementationName() throw();
    Sequence< OUString >         SAL_CALL getSupportedServiceNames(void) throw();
    sal_Bool                    SAL_CALL supportsService(const OUString& ServiceName) throw();

private:

    void doIndent( Sequence<sal_Int8> &);
    void writeSequence( const Sequence<sal_Int8> & seq );

    inline void pushStartElement()
        {
            if( m_seqStartElement.getLength() )
            {
                writeSequence( m_seqStartElement );
                m_seqStartElement = Sequence < sal_Int8 > ();
            }
        }

    Sequence < sal_Int8 > ustring2UTF8( const OUString &sValue )
        {
            return m_unicode2utf8.convert( sValue );
        }

    Sequence < sal_Int8 > utf8ToXML( const Sequence< sal_Int8 > & , sal_Bool bConvertAll );

    /****
     * @param bConvertAll  TRUE,  when LINEFEED plus tab shall also be normalized
     *                     sal_False  otherwise
     *
     ****/
    Sequence < sal_Int8 > ustring2XML( const OUString &sValue , sal_Bool bConvertAll )
        {
            return utf8ToXML( ustring2UTF8( sValue ) , bConvertAll );
        }


    Unicode2TextConverter m_unicode2utf8;
    Reference< XOutputStream > m_out;
    Sequence < sal_Int8 > m_seqStartElement;

    // Status information
    sal_Bool m_bDocStarted;
    sal_Bool m_bIsCDATA;
    sal_Bool m_bForceLineBreak;
    sal_Bool m_bAllowLineBreak;
    sal_Int32 m_nLevel;
    sal_Int32 m_nColumn;
    sal_Int32 m_nMaxColumn;
};


//--------------------------------------
// the extern interface
//---------------------------------------
Reference < XInterface > SAL_CALL SaxWriter_CreateInstance(
    const Reference < XMultiServiceFactory >  & rSMgr )
    throw (Exception)
{
    SAXWriter *p = new SAXWriter;
    return Reference< XInterface > ( SAL_STATIC_CAST(OWeakObject *, p ) );
}

OUString SaxWriter_getServiceName()
{
    return OUString::createFromAscii( "com.sun.star.xml.sax.Writer" );
}

OUString SaxWriter_getImplementationName()
{
    return OUString::createFromAscii( "com.sun.star.extensions.xml.sax.Writer" );
}

Sequence< OUString >    SaxWriter_getSupportedServiceNames(void) throw()
{
    Sequence<OUString> aRet(1);
    aRet.getArray()[0] = SaxWriter_getImplementationName();
    return aRet;
}


Sequence < sal_Int8 >  SAXWriter::utf8ToXML( const Sequence<sal_Int8> & seqSource ,  sal_Bool bConvertAll )
{
    Sequence< sal_Int8 > seqTarget( CalcXMLLen( seqSource , bConvertAll ) );
    sal_Int32 nMaxSource = seqSource.getLength();
    const sal_Int8 *pSource = seqSource.getConstArray();
    sal_Int8  *pTarget = seqTarget.getArray();
    sal_Int32 nTarget = 0;

    for( int nSource = 0 ; nSource < nMaxSource ; nSource ++ ) {
        sal_Int8 c = pSource[nSource];
        if( '&' == c ) {  // resemble to &amp;
            memcpy( &(pTarget[nTarget]) , "&amp;" , 5 );
            nTarget += 5;
        }
        else if( '<' == c ) {
            memcpy( &(pTarget[nTarget]) , "&lt;" , 4 );
            nTarget += 4;        // &lt;
        }
        else if( '>' == c ) {
            memcpy( &(pTarget[nTarget]) , "&gt;" , 4 );
            nTarget += 4;        // &gt;
        }
        else if( 39 == c ) {    // 39 == '''
            memcpy( &(pTarget[nTarget]) , "&apos;" , 6 );
            nTarget += 6;        // &apos;
        }
        else if( '"' == c ) {
            memcpy( &(pTarget[nTarget]) , "&quot;" , 6 );
            nTarget += 6;         // &quot;
        }
        else if( 13 == c ) {
            memcpy( &(pTarget[nTarget]) , "&#x0d;" , 6 );
            nTarget += 6;
        }
        else if( LINEFEED == c && bConvertAll ) {
            memcpy( &(pTarget[nTarget]) , "&#x0a;" , 6 );
            nTarget += 6;
        }
        else if( 9  == c && bConvertAll ) {
            memcpy( &(pTarget[nTarget]) , "&#x09;" , 6 );
            nTarget += 6;
        }
        else {
            pTarget[nTarget] = c;
            nTarget ++;
        }
    }
    return seqTarget;
}


void SAXWriter::doIndent( Sequence<sal_Int8> &seq )
{
    sal_Int32 nLength = getFirstLineBreak( seq );
    nLength = ( nLength >= 0 ) ? nLength : seq.getLength();
    if( ( m_bForceLineBreak ) ||
        ( m_bAllowLineBreak && nLength + m_nColumn > m_nMaxColumn  )
        ) {

        // write the linebreaks !
        Sequence<sal_Int8> seqIndent( m_nLevel + 1 );
        seqIndent.getArray()[0] = 10;
        memset( &(seqIndent.getArray()[1] ) , 32 , m_nLevel );
        writeSequence( seqIndent );

        // remove one leading space in the sequence
        if( seq.getLength() && 32 == seq.getArray()[0] ) {
            memmove( seq.getArray() , &(seq.getArray()[1]) , seq.getLength() -1 );
            seq.realloc( seq.getLength()-1 );
        }

    }

    m_bForceLineBreak = sal_False;
    m_bAllowLineBreak = sal_False;
}


/********
* write through to the output stream and counts columns
*
*****/
void SAXWriter::writeSequence( const Sequence<sal_Int8> & seq )
{

    sal_Int32 nPos = getLastLineBreak( seq );
    try
    {
        m_out->writeBytes( seq );
    }
    catch( IOException & e )
    {
        Any a;
        a <<= e;
        throw SAXException(
            OUString::createFromAscii( "io exception during writing" ),
            Reference< XInterface > (),
            a );
    }

    if( nPos >= 0 ) {
        m_nColumn = seq.getLength() - (nPos+1);
    }
    else {
        m_nColumn += seq.getLength();
    }
}




// XServiceInfo
OUString SAXWriter::getImplementationName() throw()
{
    return SaxWriter_getImplementationName();
}

// XServiceInfo
sal_Bool SAXWriter::supportsService(const OUString& ServiceName) throw()
{
    Sequence< OUString > aSNL = getSupportedServiceNames();
    const OUString * pArray = aSNL.getConstArray();

    for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
        if( pArray[i] == ServiceName )
            return sal_True;

    return sal_False;
}

// XServiceInfo
Sequence< OUString > SAXWriter::getSupportedServiceNames(void) throw ()
{
    Sequence<OUString> seq(1);
    seq.getArray()[0] = SaxWriter_getServiceName();
    return seq;
}



void SAXWriter::startDocument()                     throw(SAXException, RuntimeException )
{
    if( m_bDocStarted || ! m_out.is() ) {
        throw SAXException();
    }
    m_bDocStarted = sal_True;
    const char pc[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    const int nLen = strlen( pc );
    Sequence<sal_Int8> seqWrite( nLen+1 );
    memcpy( seqWrite.getArray() , pc , nLen );
    seqWrite.getArray()[nLen] = LINEFEED;
    writeSequence( seqWrite );
}


void SAXWriter::endDocument(void)                   throw(SAXException, RuntimeException)
{
    if( ! m_bDocStarted )
    {
        throw SAXException();
    }
    if( m_nLevel ) {
        throw SAXException(
            OUString::createFromAscii( "unexpected end of document" ),
            Reference< XInterface >() , Any() );
    }
    m_out->closeOutput();
}


void SAXWriter::startElement(const OUString& aName, const Reference< XAttributeList >& xAttribs)
    throw(SAXException, RuntimeException)
{
    if( ! m_bDocStarted )
    {
        throw SAXException();
    }
    if( m_bIsCDATA )
    {
        throw SAXException();
    }
    pushStartElement();

    sal_Int32 nAttribCount = xAttribs.is() ? xAttribs->getLength() : 0;

    OStringBuffer str(64 *( nAttribCount+1) );
    str.append( "<" );

    // Tags may only contain ascii chars !
    str.append( OUStringToOString( aName.getStr() , RTL_TEXTENCODING_UTF8 ) );

    for( int n = 0 ; n < nAttribCount ; n ++ ) {
        str.append( " " );
        str.append( OUStringToOString( xAttribs->getNameByIndex( n ) , RTL_TEXTENCODING_UTF8 ) );
        str.append( "=\"" );

        Sequence<sal_Int8> seq = ustring2XML( xAttribs->getValueByIndex( n ) , sal_True );
        str.append( ( const sal_Char * ) seq.getConstArray() , seq.getLength() );
        str.append( "\"" );
    }

    // preparing for empty tag
    str.append( ">" );

    Sequence<sal_Int8> seqWrite( str.getLength() );
    memcpy( seqWrite.getArray() , str.getStr() , str.getLength()  );

    doIndent( seqWrite );

    m_seqStartElement = seqWrite;

    m_nLevel ++;
}

void SAXWriter::endElement(const OUString& aName)   throw (SAXException, RuntimeException)
{
    if( ! m_bDocStarted ) {
        throw SAXException ();
    }
    m_nLevel --;

    if( m_nLevel < 0 ) {
        throw SAXException();
    }

    if( m_seqStartElement.getLength() )
    {
        m_seqStartElement.realloc( m_seqStartElement.getLength() + 1 );

        sal_Int8 *p = m_seqStartElement.getArray();
        p[m_seqStartElement.getLength()-2] = '/';
        p[m_seqStartElement.getLength()-1] = '>';
        writeSequence( m_seqStartElement );
        m_seqStartElement = Sequence< sal_Int8 > ();
    }
    else {
        // only ascii chars allowed
        sal_Int32 nLen = aName.getLength();
        Sequence< sal_Int8 > seqWrite( nLen + 3 );

        sal_Int8 *p = seqWrite.getArray();
        sal_Unicode *pStr = (sal_Unicode * )aName.getStr();

        p[0] = '<';
        p[1] = '/';
        for( sal_Int32 i = 0 ; i < nLen ; i ++ )
        {
            p[2+i] = (sal_Int8) pStr[i];
        }
        p[nLen+2] = '>';

        doIndent( seqWrite );
        writeSequence( seqWrite );
    }
}

void SAXWriter::characters(const OUString& aChars)  throw(SAXException, RuntimeException)
{
    if( ! m_bDocStarted )
    {
        throw SAXException();
    }
    pushStartElement();

    if( m_bIsCDATA ) {
        Sequence<sal_Int8> seqWrite = ustring2UTF8( aChars);
        writeSequence( seqWrite );
    }
    else {
        Sequence<sal_Int8> seqWrite =  ustring2XML( aChars  , sal_False );
        doIndent( seqWrite );
        writeSequence( seqWrite );
    }
}


void SAXWriter::ignorableWhitespace(const OUString& aWhitespaces) throw(SAXException, RuntimeException)
{
    if( ! m_bDocStarted )
    {
        throw SAXException ();
    }

    m_bForceLineBreak = sal_True;
}

void SAXWriter::processingInstruction(const OUString& aTarget, const OUString& aData)
    throw (SAXException, RuntimeException)
{
    if( ! m_bDocStarted || m_bIsCDATA )
    {
        throw SAXException();
    }

    pushStartElement();

    OStringBuffer str( 128 );
    str.append( "<?" );
    str.append( OUStringToOString( aTarget , RTL_TEXTENCODING_UTF8 ) );
    str.append( " " );
    str.append( OUStringToOString( aData , RTL_TEXTENCODING_UTF8 ) );   // only ascii chars allowed
    str.append( "?>" );

    Sequence<sal_Int8> seq( str.getLength() );
    memcpy( seq.getArray() , str.getStr() , str.getLength() );

    doIndent( seq );
    writeSequence( seq );
}


void SAXWriter::setDocumentLocator(const Reference< XLocator >& xLocator)
        throw (SAXException, RuntimeException)
{

}

void SAXWriter::startCDATA(void) throw(SAXException, RuntimeException)
{
    if( ! m_bDocStarted || m_bIsCDATA)
    {
        throw SAXException ();
    }

    pushStartElement();

    Sequence < sal_Int8 > seq( ( sal_Int8 * ) "<![CDATA[" ,  9 );

    doIndent( seq );
    writeSequence( seq );
    m_bIsCDATA = sal_True;
}

void SAXWriter::endCDATA(void) throw (RuntimeException)
{
    if( ! m_bDocStarted | ! m_bIsCDATA)
    {
        throw SAXException();
    }

    pushStartElement();
    Sequence<sal_Int8> seq( ( sal_Int8 * ) "]]>" , 3 );

    doIndent( seq );
    writeSequence( seq );
    m_bIsCDATA = sal_False;
}


void SAXWriter::comment(const OUString& sComment) throw(SAXException, RuntimeException)
{
    if( ! m_bDocStarted || m_bIsCDATA )
    {
        throw SAXException();
    }

    pushStartElement();

    Sequence<sal_Int8> seq = ustring2XML( sComment , sal_False );

    Sequence<sal_Int8> seqWrite( seq.getLength() + 7 );
    sal_Int8 *p = seqWrite.getArray();
    p[0] = '<';
    p[1] = '!';
    p[2] = '-';
    p[3] = '-';
    memcpy( &(p[4]) , seq.getConstArray() , seq.getLength() );
    p[4+seq.getLength()] = '-';
    p[5+seq.getLength()] = '-';
    p[6+seq.getLength()] = '>';

    doIndent( seqWrite );
    writeSequence( seqWrite );
}


void SAXWriter::allowLineBreak( )   throw ( SAXException , RuntimeException)
{
    if( ! m_bDocStarted || m_bAllowLineBreak ) {
        throw SAXException();
    }

     m_bAllowLineBreak = sal_True;
}

void SAXWriter::unknown(const OUString& sString) throw (SAXException, RuntimeException)
{

    if( ! m_bDocStarted )
    {
        throw SAXException ();
    }
    if( m_bIsCDATA )
    {
        throw SAXException();
    }

    pushStartElement();

    OString str = OUStringToOString( sString , RTL_TEXTENCODING_UTF8 );

    Sequence<sal_Int8> seq( str.getLength() );
    memcpy( seq.getArray() , str.getStr() , str.getLength() );

    doIndent( seq );
    writeSequence( seq );
}

}