summaryrefslogtreecommitdiff
path: root/sax/source/expatwrap/saxwriter.cxx
blob: 7465250b5d5b1edbbd3efeb6f222dcfe1ca1c1bd (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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
/*************************************************************************
 *
 *  $RCSfile: saxwriter.cxx,v $
 *
 *  $Revision: 1.5 $
 *
 *  last change: $Author: jbu $ $Date: 2001-03-01 12:46:53 $
 *
 *  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 <rtl/byteseq.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
#define SAXWRITER_CHECK_FOR_INVALID_CHARS

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

namespace sax_expatwrap {
/*****
*
* Calculates the length of the sequence after conversion, but the conversion is not done.
* .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
* @return The returned value is equal to the length of the incoming sequence, when no
+         conversion is necessary, otherwise it is larger than the length of the sequence.
****/
//  inline sal_Int32 CalcXMLLen( const Sequence<sal_Int8> & seq , sal_Bool bConvertAll ) throw()
//  {
//      sal_Int32 nLen = 0;
//      const sal_Int8 *pArray = seq.getConstArray();

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

//          sal_Int8 c = pArray[i];
//          switch( c )
//          {
//          case '&':       // resemble to &amp;
//              nLen +=5;
//              break;
//          case '<':       // &lt;
//          case '>':       // &gt;
//              nLen +=4;
//              break;
//          case 39:        // 39 == ''', &apos;
//          case '"':       // &quot;
//          case 13:        // &#x0d;
//              nLen += 6;
//              break;

//          case 10:        // &#x0a;
//          case 9:         // &#x09;
//              if( bConvertAll )
//              {
//                  nLen += 6;        //
//              }
//              break;
//          default:
//              nLen ++;
//          }
//      }

//      return nLen;
//  }

const sal_Bool g_bValidCharsBelow32[31] =
{
//  0 1 2 3 4 5 6 7
    0,0,0,0,0,0,0,0,  //0
    0,1,1,0,0,1,0,0,  //8
    0,0,0,0,0,0,0,0,  //16
    0,0,0,0,0,0,0
};

inline sal_Int32 calcXMLByteLength( const sal_Unicode *pStr, sal_Int32 nStrLen,
                                    sal_Bool bDoNormalization,
                                    sal_Bool bNormalizeWhitespace ) throw( SAXException )
{
    sal_Int32 nOutputLength = 0;

    for( sal_Int32 i = 0 ; i < nStrLen ; i++ )
    {
        sal_uInt16 c = pStr[i];
        if( (c >= 0x0001) && (c <= 0x007F) )
        {
            if( bDoNormalization )
            {
                switch( c )
                {
                case '&':       // resemble to &amp;
                    nOutputLength +=5;
                    break;
                case '<':       // &lt;
                case '>':       // &gt;
                    nOutputLength +=4;
                    break;
                case 39:        // 39 == ''', &apos;
                case '"':       // &quot;
                case 13:        // &#x0d;
                    nOutputLength += 6;
                    break;

                case 10:        // &#x0a;
                case 9:         // &#x09;
                    if( bNormalizeWhitespace )
                    {
                        nOutputLength += 6;       //
                    }
                    else
                    {
                        nOutputLength ++;
                    }
                    break;
                default:
                    nOutputLength ++;
                }
            }
            else
            {
                nOutputLength ++;
            }
        }
        else if( c > 0x07FF )
        {
            nOutputLength += 3;
        }
        else
        {
            nOutputLength += 2;
        }
#ifdef SAXWRITER_CHECK_FOR_INVALID_CHARS
        // check first for the most common characters
        if( c < 32 || c >= 0xd800 )
        {
            if( (c < 32 && ! g_bValidCharsBelow32[c]) ||
                (c >= 0xd800 && c <= 0xdfff) ||
                c == 0xffff ||
                c == 0xfffe )
            {
                SAXException except;
                except.Message = OUString( RTL_CONSTASCII_USTRINGPARAM( "Invalid charcter during XML-Export: " ) );
                except.Message += OUString::valueOf( (sal_Int32) c );
                throw except;
            }
        }
#endif
    }

    return nOutputLength;
}

/** Converts an UTF16 string to UTF8 and does XML normalization

    @param pTarget
           Pointer to a piece of memory, to where the output should be written. The caller
           must call calcXMLByteLength on the same string, to ensure,
           that there is enough memory for converting.
 */
sal_Int32 convertToXML( const sal_Unicode * pStr, sal_Int32 nStrLen,
                        sal_Bool bDoNormalization,
                        sal_Bool bNormalizeWhitespace,
                        sal_Int8 *pTarget )
{
    sal_Int32 nOutputLength = 0;
    sal_Int32 nPos = 0;

    for( sal_Int32 i = 0 ; i < nStrLen ; i ++ )
    {
        sal_uInt16 c = pStr[i];
        if( (c >= 0x0001) && (c <= 0x007F) )
        {
            if( bDoNormalization )
            {
                switch( c )
                {
                case '&':  // resemble to &amp;
                    memcpy( &(pTarget[nPos]) , "&amp;" , 5 );
                    nPos += 5;
                    break;
                case '<':
                    memcpy( &(pTarget[nPos]) , "&lt;" , 4 );
                    nPos += 4;        // &lt;
                    break;
                case '>':
                    memcpy( &(pTarget[nPos]) , "&gt;" , 4 );
                    nPos += 4;        // &gt;
                    break;
                case 39:                 // 39 == '''
                    memcpy( &(pTarget[nPos]) , "&apos;" , 6 );
                    nPos += 6;        // &apos;
                    break;
                case '"':
                    memcpy( &(pTarget[nPos]) , "&quot;" , 6 );
                    nPos += 6;        // &quot;
                    break;
                case 13:
                    memcpy( &(pTarget[nPos]) , "&#x0d;" , 6 );
                    nPos += 6;
                    break;
                case LINEFEED:
                    if( bNormalizeWhitespace )
                    {
                        memcpy( &(pTarget[nPos]) , "&#x0a;" , 6 );
                        nPos += 6;
                    }
                    else
                    {
                        pTarget[nPos] = LINEFEED;
                        nPos ++;
                    }
                    break;
                case 9:
                    if( bNormalizeWhitespace )
                    {
                        memcpy( &(pTarget[nPos]) , "&#x09;" , 6 );
                        nPos += 6;
                    }
                    else
                    {
                        pTarget[nPos] = 9;
                        nPos ++;
                    }
                    break;
                default:
                    pTarget[nPos] = (sal_Int8)c;
                    nPos ++;
                }
            }
            else
            {
                pTarget[nPos] = (sal_Int8)c;
                nPos ++;
            }
        }
        else if( c > 0x07FF )
        {
            pTarget[nPos] = sal_Int8(0xE0 | ((c >> 12) & 0x0F));
            nPos ++;
            pTarget[nPos] = sal_Int8(0x80 | ((c >>  6) & 0x3F));
            nPos ++;
            pTarget[nPos] = sal_Int8(0x80 | ((c >>  0) & 0x3F));
            nPos ++;
        }
        else
        {
            pTarget[nPos] = sal_Int8(0xC0 | ((c >>  6) & 0x1F));
            nPos ++;
            pTarget[nPos] = sal_Int8(0x80 | ((c >>  0) & 0x3F));
            nPos ++;
        }
    }
    return nPos;
}


/** returns position of first ascii 10 within the string, -1 when no 10 in string.
 */
static inline sal_Int32 getFirstLineBreak( const OUString & str ) throw ()
{
    const sal_Unicode *pSource = str.getStr();
    sal_Int32 nLen  = str.getLength();

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

/** returns position of last ascii 10 within sequence, -1 when no 10 in string.
 */
static inline sal_Int32 getLastLineBreak( const Sequence<sal_Int8>  & seq) throw ()
{
    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)
        {}

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 writeSequence( const Sequence<sal_Int8> & seq );
    void insertIndentation( sal_Int8 *pTarget ) throw();
    sal_Int32 getIndentPrefixLength( sal_Int32 nFirstLineBreakOccurence ) throw();

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



    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() throw()
{
    return OUString::createFromAscii( "com.sun.star.xml.sax.Writer" );
}

OUString SaxWriter_getImplementationName() throw()
{
    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;
}



sal_Int32 SAXWriter::getIndentPrefixLength( sal_Int32 nFirstLineBreakOccurence ) throw()
{
    sal_Int32 nLength =-1;
    if( m_bForceLineBreak ||
        m_bAllowLineBreak && nFirstLineBreakOccurence + m_nColumn > m_nMaxColumn )
    {
        nLength = m_nLevel +1;
    }
    m_bForceLineBreak = sal_False;
    m_bAllowLineBreak = sal_False;
    return nLength;
}

void SAXWriter::insertIndentation( sal_Int8 *pTarget ) throw()
{
    pTarget[0] = 10;
    memset( &(pTarget[1]) , 32 , m_nLevel );
}

static inline sal_Bool isFirstCharWhitespace( const sal_Unicode *p ) throw()
{
    return *p == ' ';
}



/********
* 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(
            OUString::createFromAscii( "endDocument called before startDocument" ),
            Reference< XInterface >() , Any() );
    }
    if( m_nLevel ) {
        throw SAXException(
            OUString::createFromAscii( "unexpected end of document" ),
            Reference< XInterface >() , Any() );
    }
    try
    {
        m_out->closeOutput();
    }
    catch( IOException & e )
    {
        Any a;
        a <<= e;
        throw SAXException(
            OUString::createFromAscii( "IO exception during closing the IO Stream" ),
            Reference< XInterface > (),
            a );
    }
}


void SAXWriter::startElement(const OUString& aName, const Reference< XAttributeList >& xAttribs)
    throw(SAXException, RuntimeException)
{
    if( ! m_bDocStarted )
    {
        SAXException except;
        except.Message = OUString( RTL_CONSTASCII_USTRINGPARAM( "startElement called before startDocument" ));
        throw except;
    }
    if( m_bIsCDATA )
    {
        SAXException except;
        except.Message = OUString( RTL_CONSTASCII_USTRINGPARAM( "startElement call not allowed with CDATA sections" ));
        throw except;
    }
    pushStartElement();

    // first calculate the sequence length
    sal_Int32 nLength = 0;
    sal_Int32 nAttribCount = xAttribs.is() ? xAttribs->getLength() : 0;

    nLength ++; // "<"
    nLength += calcXMLByteLength( aName.getStr() , aName.getLength(),
                                  sal_False, sal_False ); // the tag name

    int n;
    for( n = 0 ; n < nAttribCount ; n ++ ) {
        nLength ++; // " "
        OUString tmp =  xAttribs->getNameByIndex( n );

        nLength += calcXMLByteLength( tmp.getStr() , tmp.getLength() , sal_False, sal_False );

        nLength += 2; // ="

        tmp = xAttribs->getValueByIndex( n );

        nLength += calcXMLByteLength( tmp.getStr(), tmp.getLength(), sal_True, sal_True );

        nLength += 1; // "
    }

    nLength ++;  // '>'

    // Is there a new indentation necesarry ?
    sal_Int32 nPrefix = getIndentPrefixLength( nLength  );
    if( nPrefix >= 0 )
        nLength += nPrefix;

    // now create the sequence, store it ( maybe later an empty tag ? )
    m_seqStartElement = Sequence< sal_Int8 > ( nLength );

    sal_Int8 *pTarget = (sal_Int8*) m_seqStartElement.getConstArray();  // we OWN the sequence
    sal_Int32 nPos =0;

    // write into sequence
    if( nPrefix >= 0 )
    {
        insertIndentation( pTarget );
        nPos += nPrefix;
    }
    pTarget[nPos] = '<';
    nPos ++;

    nPos += convertToXML( aName.getStr(), aName.getLength(), sal_False, sal_False, &(pTarget[nPos]) );

    for( n = 0 ; n < nAttribCount ; n ++ )
    {
        pTarget[nPos] = ' ';
        nPos ++;

        OUString tmp = xAttribs->getNameByIndex( n );
        nPos += convertToXML( tmp.getStr(), tmp.getLength(), sal_False, sal_False, &(pTarget[nPos]) );

        pTarget[nPos] = '=';
        nPos ++;
        pTarget[nPos] = '"';
        nPos ++;

        tmp = xAttribs->getValueByIndex( n );
        nPos += convertToXML( tmp.getStr(), tmp.getLength(), sal_True, sal_True , &(pTarget[nPos] ) );

        pTarget[nPos] = '"';
        nPos ++;
    }

    pTarget[nPos] = '>';
    nPos ++;
    OSL_ASSERT( nPos == nLength );

    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 nLength = 3 + calcXMLByteLength( aName.getStr(), aName.getLength(), sal_False, sal_False );
        sal_Int32 nPrefix = getIndentPrefixLength( nLength );

        if( nPrefix >= 0 )
        {
            nLength += nPrefix;
        }
        Sequence< sal_Int8 > seqWrite( nLength );

        sal_Int8 *pTarget = ( sal_Int8 * ) seqWrite.getConstArray(); // we OWN it

        sal_Int32 nPos = 0;
        if( nPrefix >= 0 )
        {
            nPos += nPrefix;
            insertIndentation( pTarget );
        }
        pTarget[nPos] = '<';
        nPos ++;
        pTarget[nPos] = '/';
        nPos ++;

        nPos += convertToXML( aName.getStr(), aName.getLength(), sal_False, sal_False, &(pTarget[nPos] ) );

        pTarget[nPos] = '>';
        nPos ++;

        OSL_ASSERT( nPos == nLength );
        writeSequence( seqWrite );
    }
}

void SAXWriter::characters(const OUString& aChars)  throw(SAXException, RuntimeException)
{
    if( ! m_bDocStarted )
    {
        SAXException except;
        except.Message = OUString( RTL_CONSTASCII_USTRINGPARAM( "characters method called before startDocument" ) );
        throw except;
    }

    if( aChars.getLength() )
    {
        pushStartElement();

        sal_Int32 nLength = calcXMLByteLength( aChars.getStr(), aChars.getLength(),
                                               ! m_bIsCDATA , sal_False );
        if( m_bIsCDATA )
        {
            // no indentation, no normalization
            Sequence<sal_Int8> seqWrite( nLength );
            convertToXML( aChars.getStr(), aChars.getLength() , sal_False, sal_False ,
                          (sal_Int8*)seqWrite.getConstArray() );
            writeSequence( seqWrite );
        }
        else
        {
            // Note : nFirstLineBreakOccurence is not exact, because we don't know, how
            //        many 2 and 3 byte chars are inbetween. However this whole stuff
            //        is eitherway for pretty printing only, so it does not need to be exact.
            sal_Int32 nFirstLineBreakOccurence = getFirstLineBreak( aChars );

            sal_Int32 nIdentPrefix = getIndentPrefixLength(
                nFirstLineBreakOccurence >= 0 ? nFirstLineBreakOccurence : nLength );

            if( nIdentPrefix >= 0 )
            {
                nLength += nIdentPrefix;
                if( isFirstCharWhitespace( aChars.getStr() ) )
                {
                    nLength --;
                }
            }

            // create the sequence
            Sequence< sal_Int8 > seqWrite( nLength );
            sal_Int8 *pTarget = (sal_Int8 * ) seqWrite.getConstArray();

            // insert indentation
            sal_Int32 nPos = 0;
            if( nIdentPrefix >= 0 )
            {
                insertIndentation( pTarget );
                nPos += nIdentPrefix;
                if( isFirstCharWhitespace( aChars.getStr() ) )
                {
                    nPos += convertToXML(
                        &(aChars.getStr()[1]) , aChars.getLength() -1 ,
                        sal_True , sal_False , &(pTarget[nPos]) );
                }
                else
                {
                    nPos += convertToXML(
                        aChars.getStr() , aChars.getLength() , sal_True, sal_False,
                        &(pTarget[nPos]));
                }
            }
            else
            {
                nPos += convertToXML(
                    aChars.getStr() , aChars.getLength() , sal_True, sal_False, &(pTarget[nPos]) );
            }

            OSL_ASSERT( nPos == nLength );
            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();
    }

    sal_Int32 nLength = 2;  // "<?"
    nLength += calcXMLByteLength( aTarget.getStr(), aTarget.getLength(), sal_False, sal_False );

    nLength += 1;  // " "

    nLength += calcXMLByteLength( aData.getStr(), aData.getLength(), sal_False, sal_False );

    nLength += 2; // "?>"

    pushStartElement();

    sal_Int32 nPrefix = getIndentPrefixLength( nLength );
    if( nPrefix > 0 )
    {
        nLength += nPrefix;
    }

    Sequence< sal_Int8 > seqWrite( nLength );

    sal_Int32 nPos = 0;
    sal_Int8 * pTarget = ( sal_Int8 * ) seqWrite.getConstArray();

    if( nPrefix >= 0 )
    {
        insertIndentation( pTarget );
        nPos += nPos;
    }

    pTarget[nPos] = '<';
    nPos ++;
    pTarget[nPos] = '?';
    nPos ++;

    nPos += convertToXML( aTarget.getStr(), aTarget.getLength(), sal_False, sal_False, &(pTarget[nPos] ) );

    pTarget[nPos] = ' ';
    nPos ++;

    nPos += convertToXML( aData.getStr(), aData.getLength(), sal_False, sal_False, &(pTarget[nPos]) );
    pTarget[nPos] = '?';
    nPos ++;
    pTarget[nPos] = '>';
    nPos ++;

    OSL_ASSERT( nPos == nLength );

    writeSequence( seqWrite );
}


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();


    sal_Int32 nLength = 9;
    sal_Int32 nPrefix = getIndentPrefixLength( 9 );
    if( nPrefix >= 0)
    {
        nLength += nPrefix;
    }

    Sequence< sal_Int8 > seq( nLength );
    sal_Int8 *pTarget = ( sal_Int8* )seq.getConstArray();
    sal_Int32 nPos = 0;

    if( nPrefix >= 0 )
    {
        insertIndentation( pTarget );
        nPos += nPrefix;
    }

    memcpy( &(pTarget[nPos]), "<![CDATA[" , 9 );

    writeSequence( seq );

    m_bIsCDATA = sal_True;
}

void SAXWriter::endCDATA(void) throw (RuntimeException)
{
    if( ! m_bDocStarted | ! m_bIsCDATA)
    {
        SAXException except;
        except.Message = OUString( RTL_CONSTASCII_USTRINGPARAM( "endCDATA was called without startCDATA" ) );
        throw except;
    }

    sal_Int32 nLength = 3;
    sal_Int32 nPrefix = getIndentPrefixLength( 3 );
    if( nPrefix >= 0)
    {
        nLength += nPrefix;
    }

    Sequence< sal_Int8 > seq( nLength );
    sal_Int8 *pTarget = ( sal_Int8* )seq.getConstArray();
    sal_Int32 nPos = 0;

    if( nPrefix >= 0 )
    {
        insertIndentation( pTarget );
        nPos += nPrefix;
    }

    memcpy( &(pTarget[nPos]), "]]>" , 3 );

    writeSequence( seq );
    m_bIsCDATA = sal_False;
}


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

    pushStartElement();

    sal_Int32 nLength = 4; // "<!--"
    nLength += calcXMLByteLength( sComment.getStr(), sComment.getLength(), sal_False, sal_False);

    nLength += 3;

    sal_Int32 nPrefix = getIndentPrefixLength( nLength );
    if( nPrefix >= 0 )
    {
        nLength += nPrefix;
    }

    Sequence<sal_Int8> seq( nLength );

    sal_Int8 *pTarget = (sal_Int8*) seq.getConstArray();
    sal_Int32 nPos = 0;

    if( nPrefix >= 0 )
    {
        insertIndentation( pTarget );
        nPos += nPrefix;
    }
    pTarget[nPos] = '<';
    pTarget[nPos+1] = '!';
    pTarget[nPos+2] = '-';
    pTarget[nPos+3] = '-';
    nPos += 4;

    nPos += convertToXML( sComment.getStr(), sComment.getLength(), sal_False, sal_False,
                          &(pTarget[nPos]));

    pTarget[nPos] = '-';
    pTarget[nPos+1] = '-';
    pTarget[nPos+2] = '>';
    nPos += 3;

    OSL_ASSERT( nPos == nLength );

    writeSequence( seq );
}


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();

    sal_Int32 nLength = calcXMLByteLength( sString.getStr(), sString.getLength(), sal_False, sal_False );

    sal_Int32 nPrefix = getIndentPrefixLength( nLength );
    if( nPrefix >= 0 )
    {
        nLength += nPrefix;
    }
    Sequence< sal_Int8 > seq( nLength );

    sal_Int8 *pTarget = (sal_Int8*) seq.getConstArray();
    sal_Int32 nPos = 0;

    if( nPrefix >= 0 )
    {
        insertIndentation( pTarget );
        nPos += nPrefix;
    }

    nPos += convertToXML( sString.getStr(), sString.getLength(), sal_False, sal_False,
                          &(pTarget[nPos]));

    OSL_ASSERT( nPos == nLength );
    writeSequence( seq );
}

}