File: XmlWriter.java

package info (click to toggle)
libitext1-java 1.4-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 7,636 kB
  • sloc: java: 86,865; xml: 396; makefile: 15
file content (893 lines) | stat: -rw-r--r-- 34,970 bytes parent folder | download | duplicates (4)
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
/*
 * $Id: XmlWriter.java,v 1.35 2005/12/09 12:33:25 psoares33 Exp $
 * $Name:  $
 *
 * Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie.
 *
 * The contents of this file are subject to the Mozilla Public 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.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the License.
 *
 * The Original Code is 'iText, a free JAVA-PDF library'.
 *
 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
 * All Rights Reserved.
 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
 *
 * Contributor(s): all the names of the contributors are added in the source code
 * where applicable.
 *
 * Alternatively, the contents of this file may be used under the terms of the
 * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
 * provisions of LGPL are applicable instead of those above.  If you wish to
 * allow use of your version of this file only under the terms of the LGPL
 * License and not to allow others to use your version of this file under
 * the MPL, indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by the LGPL.
 * If you do not delete the provisions above, a recipient may use your version
 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE 
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the MPL as stated above or under the terms of the GNU
 * Library General Public License as published by the Free Software Foundation;
 * either version 2 of the License, or any later version.
 *
 * 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 LIBRARY GENERAL PUBLIC LICENSE for more
 * details.
 *
 * If you didn't download this code from the following link, you should check if
 * you aren't using an obsolete version:
 * http://www.lowagie.com/iText/
 */

package com.lowagie.text.xml;

import java.io.OutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.HashMap;

import com.lowagie.text.*;
import com.lowagie.text.markup.MarkupTags;

/**
 * A <CODE>DocWriter</CODE> class for XML (Remark: this class is not finished yet!).
 * <P>
 * An <CODE>XmlWriter</CODE> can be added as a <CODE>DocListener</CODE>
 * to a certain <CODE>Document</CODE> by getting an instance.
 * Every <CODE>Element</CODE> added to the original <CODE>Document</CODE>
 * will be written to the <CODE>OutputStream</CODE> of this <CODE>XmlWriter</CODE>.
 * <P>
 * Example:
 * <BLOCKQUOTE><PRE>
 * // creation of the document with a certain size and certain margins
 * Document document = new Document(PageSize.A4, 50, 50, 50, 50);
 * try {
 *    // this will write XML to the Standard OutputStream
 *    <STRONG>XmlWriter.getInstance(document, System.out);</STRONG>
 *    // this will write XML to a file called text.html
 *    <STRONG>XmlWriter.getInstance(document, new FileOutputStream("text.xml"));</STRONG>
 *    // this will write XML to for instance the OutputStream of a HttpServletResponse-object
 *    <STRONG>XmlWriter.getInstance(document, response.getOutputStream());</STRONG>
 * }
 * catch(DocumentException de) {
 *    System.err.println(de.getMessage());
 * }
 * // this will close the document and all the OutputStreams listening to it
 * <STRONG>document.close();</CODE>
 * </PRE></BLOCKQUOTE>
 */

public class XmlWriter extends DocWriter implements DocListener {
    
    // static membervariables (tags)
    
/** This is the first line of the XML page. */
    public static final byte[] PROLOG = getISOBytes("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
    
/** This is the reference to the DTD. */
    public static final byte[] DOCTYPE = getISOBytes("<!DOCTYPE ITEXT SYSTEM \"");
    
/** This is the place where the DTD is located. */
    public final static byte[] DTD = getISOBytes("http://itext.sourceforge.net/itext.dtd");
    
/** This is an array containing character to XML translations. */
    private static final String[] xmlCode = new String[256];
    
    static {
        for (int i = 0; i < 10; i++) {
            xmlCode[i] = "&#00" + i + ";";
        }
        
        for (int i = 10; i < 32; i++) {
            xmlCode[i] = "&#0" + i + ";";
        }
        
        for (int i = 32; i < 128; i++) {
            xmlCode[i] = String.valueOf((char)i);
        }
        
        // Special characters
        xmlCode['\n'] = "<" + ElementTags.NEWLINE + " />\n";
        xmlCode['\"'] = "&quot;"; // double quote
        xmlCode['\''] = "&apos;"; // single quote
        xmlCode['&'] = "&amp;"; // ampersand
        xmlCode['<'] = "&lt;"; // lower than
        xmlCode['>'] = "&gt;"; // greater than
        
        for (int i = 128; i < 256; i++) {
            xmlCode[i] = "&#" + i + ";";
        }
    }
    // membervariables
    
/** This is the meta information of the document. */
    private TreeMap itext = new TreeMap(new com.lowagie.text.StringCompare());
    
    // constructors
    
/**
 * Constructs an <CODE>XmlWriter</CODE>.
 *
 * @param doc     The <CODE>Document</CODE> that has to be written as XML
 * @param os      The <CODE>OutputStream</CODE> the writer has to write to.
 */
    
    protected XmlWriter(Document doc, OutputStream os) {
        super(doc, os);
        
        document.addDocListener(this);
        try {
            os.write(PROLOG);
            os.write(DOCTYPE);
            os.write(DTD);
            os.write(QUOTE);
            os.write(GT);
            os.write(NEWLINE);
        }
        catch(IOException ioe) {
            throw new ExceptionConverter(ioe);
        }
    }
    
/**
 * Constructs an <CODE>XmlWriter</CODE>.
 *
 * @param doc     The <CODE>Document</CODE> that has to be written as XML
 * @param os      The <CODE>OutputStream</CODE> the writer has to write to.
 * @param dtd     The DTD to use
 */
    
    protected XmlWriter(Document doc, OutputStream os, String dtd) {
        super(doc, os);
        
        document.addDocListener(this);
        try {
            os.write(PROLOG);
            os.write(DOCTYPE);
            os.write(getISOBytes(dtd));
            os.write(QUOTE);
            os.write(GT);
            os.write(NEWLINE);
        }
        catch(IOException ioe) {
            throw new ExceptionConverter(ioe);
        }
    }
    
    // get an instance of the XmlWriter
    
/**
 * Gets an instance of the <CODE>XmlWriter</CODE>.
 *
 * @param document  The <CODE>Document</CODE> that has to be written
 * @param os  The <CODE>OutputStream</CODE> the writer has to write to.
 * @return  a new <CODE>XmlWriter</CODE>
 */
    
    public static XmlWriter getInstance(Document document, OutputStream os) {
        return new XmlWriter(document, os);
    }
    
/**
 * Gets an instance of the <CODE>XmlWriter</CODE>.
 *
 * @param document  The <CODE>Document</CODE> that has to be written
 * @param os  The <CODE>OutputStream</CODE> the writer has to write to.
 * @param   dtd         The DTD to use
 * @return  a new <CODE>XmlWriter</CODE>
 */
    
    public static XmlWriter getInstance(Document document, OutputStream os, String dtd) {
        return new XmlWriter(document, os, dtd);
    }
    
    // implementation of the DocListener methods
    
/**
 * Signals that an <CODE>Element</CODE> was added to the <CODE>Document</CODE>.
 * 
 * @param element A high level object that will be added to the XML
 * @return  <CODE>true</CODE> if the element was added, <CODE>false</CODE> if not.
 * @throws  DocumentException when a document isn't open yet, or has been closed
 */
    
    public boolean add(Element element) throws DocumentException {
        if (pause) {
            return false;
        }
        try {
            switch(element.type()) {
                case Element.TITLE:
                    itext.put(ElementTags.TITLE, ((Meta)element).content());
                    return true;
                case Element.SUBJECT:
                    itext.put(ElementTags.SUBJECT, ((Meta)element).content());
                    return true;
                case Element.KEYWORDS:
                    itext.put(ElementTags.KEYWORDS, ((Meta)element).content());
                    return true;
                case Element.AUTHOR:
                    itext.put(ElementTags.AUTHOR, ((Meta)element).content());
                    return true;
                    default:
                        write(element, 1);
                        return true;
            }
        }
        catch(IOException ioe) {
            return false;
        }
    }
    
/**
 * Signals that the <CODE>Document</CODE> has been opened and that
 * <CODE>Elements</CODE> can be added.
 */
    
    public void open() {
        super.open();
        try {
            itext.put(ElementTags.PRODUCER, "iTextXML by lowagie.com");
            itext.put(ElementTags.CREATIONDATE, new Date().toString());
            writeStart(ElementTags.ITEXT);
            String key;
            for (java.util.Iterator i = itext.keySet().iterator(); i.hasNext(); ) {
                key = (String) i.next();
                write(key, (String) itext.get(key));
            }
            os.write(GT);
        }
        catch(IOException ioe) {
            throw new ExceptionConverter(ioe);
        }
    }
    
/**
 * Signals that an new page has to be LTed.
 *
 * @return  <CODE>true</CODE> if the page was added, <CODE>false</CODE> if not.
 * @throws  DocumentException when a document isn't open yet, or has been closed
 */
    
    public boolean newPage() throws DocumentException {
        if (pause || !open) {
            return false;
        }
        try {
            writeStart(ElementTags.NEWPAGE);
            writeEnd();
            return true;
        }
        catch(IOException ioe) {
            return false;
        }
    }
    
/**
 * Signals that the <CODE>Document</CODE> was closed and that no other
 * <CODE>Elements</CODE> will be added.
 */
    
    public void close() {
        try {
            os.write(NEWLINE);
            writeEnd(ElementTags.ITEXT);
            super.close();
        }
        catch(IOException ioe) {
            throw new ExceptionConverter(ioe);
        }
    }
    
    // methods
    
/**
 * Writes the XML representation of an element.
 *
 * @param   element     the element
 * @param   indent      the indentation
 * @throws IOException
 */
    
    private void write(Element element, int indent) throws IOException {
        switch(element.type()) {
            case Element.CHUNK:
            {
                Chunk chunk = (Chunk) element;
                
                // if the chunk contains an image, return the image representation
                try {
                    Image image = chunk.getImage();
                    write(image, indent);
                    return;
                }
                catch(NullPointerException npe) {
                    // empty on purpose
                }
                
                addTabs(indent);
                HashMap attributes = chunk.getAttributes();
                if (chunk.font().isStandardFont() && attributes == null && !(hasMarkupAttributes(chunk))) {
                    write(encode(chunk.content(), indent));
                    return;
                }
                else {
                    if (attributes !=  null && attributes.get(Chunk.NEWPAGE) != null) {
                        writeStart(ElementTags.NEWPAGE);
                        writeEnd();
                        return;
                    }
                    writeStart(ElementTags.CHUNK);
                    if (! chunk.font().isStandardFont()) {
                        write(chunk.font());
                    }
                    if (attributes != null) {
                        for (Iterator i = attributes.keySet().iterator(); i.hasNext(); ) {
                            String key = (String) i.next();
                            if (key.equals(Chunk.LOCALGOTO)
                            || key.equals(Chunk.LOCALDESTINATION)
                            || key.equals(Chunk.GENERICTAG)) {
                                String value = (String) attributes.get(key);
                                write(key.toLowerCase(), value);
                            }
                            if (key.equals(Chunk.SUBSUPSCRIPT)) {
                                write(key.toLowerCase(), String.valueOf((Float) attributes.get(key)));
                            }
                        }
                    }
                    if (hasMarkupAttributes(chunk)) {
                      writeMarkupAttributes((MarkupAttributes)chunk);
                    }
                    os.write(GT);
                    write(encode(chunk.content(), indent));
                    writeEnd(ElementTags.CHUNK);
                }
                return;
            }
            case Element.PHRASE:
            {
                Phrase phrase = (Phrase) element;
                
                addTabs(indent);
                writeStart(ElementTags.PHRASE);
                
                write(ElementTags.LEADING, String.valueOf(phrase.leading()));
                write(phrase.font());
                if (hasMarkupAttributes(phrase)) {
                  writeMarkupAttributes((MarkupAttributes)phrase);
                }
                os.write(GT);
                
                for (Iterator i = phrase.iterator(); i.hasNext(); ) {
                    write((Element) i.next(), indent + 1);
                }
                
                addTabs(indent);
                writeEnd(ElementTags.PHRASE);
                return;
            }
            case Element.ANCHOR:
            {
                Anchor anchor = (Anchor) element;
                
                addTabs(indent);
                writeStart(ElementTags.ANCHOR);
                
                write(ElementTags.LEADING, String.valueOf(anchor.leading()));
                write(anchor.font());
                if (anchor.name() != null) {
                    write(ElementTags.NAME, anchor.name());
                }
                if (anchor.reference() != null) {
                    write(ElementTags.REFERENCE, anchor.reference());
                }
                if (hasMarkupAttributes(anchor)) {
                  writeMarkupAttributes((MarkupAttributes)anchor);
                }
                os.write(GT);
                for (Iterator i = anchor.iterator(); i.hasNext(); ) {
                    write((Element) i.next(), indent + 1);
                }
                addTabs(indent);
                writeEnd(ElementTags.ANCHOR);
                return;
            }
            case Element.PARAGRAPH:
            {
                Paragraph paragraph = (Paragraph) element;
                
                addTabs(indent);
                writeStart(ElementTags.PARAGRAPH);
                
                write(ElementTags.LEADING, String.valueOf(paragraph.leading()));
                write(paragraph.font());
                write(ElementTags.ALIGN, ElementTags.getAlignment(paragraph.alignment()));
                if (paragraph.indentationLeft() != 0) {
                    write(ElementTags.INDENTATIONLEFT, String.valueOf(paragraph.indentationLeft()));
                }
                if (paragraph.indentationRight() != 0) {
                    write(ElementTags.INDENTATIONRIGHT, String.valueOf(paragraph.indentationRight()));
                }
                if (hasMarkupAttributes(paragraph)) {
                  writeMarkupAttributes((MarkupAttributes)paragraph);
                }
                os.write(GT);
                for (Iterator i = paragraph.iterator(); i.hasNext(); ) {
                    write((Element) i.next(), indent + 1);
                }
                addTabs(indent);
                writeEnd(ElementTags.PARAGRAPH);
                return;
            }
            case Element.SECTION:
            {
                Section section = (Section) element;
                
                addTabs(indent);
                writeStart(ElementTags.SECTION);
                writeSection(section, indent);
                writeEnd(ElementTags.SECTION);
                return;
            }
            case Element.CHAPTER:
            {
                Chapter chapter = (Chapter) element;
                
                addTabs(indent);
                writeStart(ElementTags.CHAPTER);
                if (hasMarkupAttributes(chapter)) {
                  writeMarkupAttributes((MarkupAttributes)chapter);
                }
                writeSection(chapter, indent);
                writeEnd(ElementTags.CHAPTER);
                return;
                
            }
            case Element.LIST:
            {
                List list = (List) element;
                
                addTabs(indent);
                writeStart(ElementTags.LIST);
                write(ElementTags.NUMBERED, String.valueOf(list.isNumbered()));
                write(ElementTags.SYMBOLINDENT, String.valueOf(list.symbolIndent()));
                if (list.first() != 1) {
                    write(ElementTags.FIRST, String.valueOf(list.first()));
                }
                if (list.indentationLeft() != 0) {
                    write(ElementTags.INDENTATIONLEFT, String.valueOf(list.indentationLeft()));
                }
                if (list.indentationRight() != 0) {
                    write(ElementTags.INDENTATIONRIGHT, String.valueOf(list.indentationRight()));
                }
                if (!list.isNumbered()) {
                    write(ElementTags.LISTSYMBOL, list.symbol().content());
                }
                write(list.symbol().font());
                if (hasMarkupAttributes(list)) {
                  writeMarkupAttributes((MarkupAttributes)list);
                }
                os.write(GT);
                for (Iterator i = list.getItems().iterator(); i.hasNext(); ) {
                    write((Element) i.next(), indent + 1);
                }
                addTabs(indent);
                writeEnd(ElementTags.LIST);
                return;
            }
            case Element.LISTITEM:
            {
                ListItem listItem = (ListItem) element;
                
                addTabs(indent);
                writeStart(ElementTags.LISTITEM);
                write(ElementTags.LEADING, String.valueOf(listItem.leading()));
                write(listItem.font());
                write(ElementTags.ALIGN, ElementTags.getAlignment(listItem.alignment()));
                if (listItem.indentationLeft() != 0) {
                    write(ElementTags.INDENTATIONLEFT, String.valueOf(listItem.indentationLeft()));
                }
                if (listItem.indentationRight() != 0) {
                    write(ElementTags.INDENTATIONRIGHT, String.valueOf(listItem.indentationRight()));
                }
                if (hasMarkupAttributes(listItem)) {
                  writeMarkupAttributes((MarkupAttributes)listItem);
                }
                os.write(GT);
                for (Iterator i = listItem.iterator(); i.hasNext(); ) {
                    write((Element) i.next(), indent + 1);
                }
                addTabs(indent);
                writeEnd(ElementTags.LISTITEM);
                return;
            }
            case Element.CELL:
            {
                Cell cell = (Cell) element;
                
                addTabs(indent);
                writeStart(ElementTags.CELL);
                write((Rectangle) cell);
                write(ElementTags.HORIZONTALALIGN, ElementTags.getAlignment(cell.horizontalAlignment()));
                write(ElementTags.VERTICALALIGN, ElementTags.getAlignment(cell.verticalAlignment()));
                if (cell.cellWidth() != null) {
                    write(ElementTags.WIDTH, cell.cellWidth());
                }
                if (cell.colspan() != 1) {
                    write(ElementTags.COLSPAN, String.valueOf(cell.colspan()));
                }
                if (cell.rowspan() != 1) {
                    write(ElementTags.ROWSPAN, String.valueOf(cell.rowspan()));
                }
                if (cell.header()) {
                    write(ElementTags.HEADER, String.valueOf(true));
                }
                if (cell.noWrap()) {
                    write(ElementTags.NOWRAP, String.valueOf(true));
                }
                if (cell.leading() != -1) {
                    write(ElementTags.LEADING, String.valueOf(cell.leading()));
                }
                if (hasMarkupAttributes(cell)) {
                  writeMarkupAttributes((MarkupAttributes)cell);
                }
                os.write(GT);
                for (Iterator i = cell.getElements(); i.hasNext(); ) {
                    write((Element) i.next(), indent + 1);
                }
                addTabs(indent);
                writeEnd(ElementTags.CELL);
                return;
            }
            case Element.ROW:
            {
                Row row = (Row) element;
                
                addTabs(indent);
                writeStart(ElementTags.ROW);
                if (hasMarkupAttributes(row)){
                  writeMarkupAttributes((MarkupAttributes)row);
                }
                os.write(GT);
                Element cell;
                for (int i = 0; i < row.columns(); i++) {
                    if ((cell = (Element)row.getCell(i)) != null) {
                        write(cell, indent + 1);
                    }
                }
                addTabs(indent);
                writeEnd(ElementTags.ROW);
                return;
            }
            case Element.TABLE:
            {
                Table table;
                try {
                	table = (Table) element;
                }
                catch(ClassCastException cce) {
                	try {
						table = ((SimpleTable)element).createTable();
					} catch (BadElementException e) {
						throw new ExceptionConverter(e);
					}
                }
                table.complete();
                addTabs(indent);
                writeStart(ElementTags.TABLE);
                write(ElementTags.COLUMNS, String.valueOf(table.columns()));
                os.write(SPACE);
                write(ElementTags.WIDTH);
                os.write(EQUALS);
                os.write(QUOTE);
                if (! "".equals(table.absWidth())){
                    write(table.absWidth());
                }
                else{
                    write(String.valueOf(table.widthPercentage()));
                    write("%");
                }
                os.write(QUOTE);
                write(ElementTags.ALIGN, ElementTags.getAlignment(table.alignment()));
                write(ElementTags.CELLPADDING, String.valueOf(table.cellpadding()));
                write(ElementTags.CELLSPACING, String.valueOf(table.cellspacing()));
                os.write(SPACE);
                write(ElementTags.WIDTHS);
                os.write(EQUALS);
                os.write(QUOTE);
                float[] widths = table.getProportionalWidths();
                write(String.valueOf(widths[0]));
                for (int i = 1; i < widths.length; i++) {
                    write(";");
                    write(String.valueOf(widths[i]));
                }
                os.write(QUOTE);
                write((Rectangle) table);
                if (hasMarkupAttributes(table)) {
                  writeMarkupAttributes((MarkupAttributes)table);
                }
                os.write(GT);
                Row row;
                for (Iterator iterator = table.iterator(); iterator.hasNext(); ) {
                    row = (Row) iterator.next();
                    write(row, indent + 1);
                }
                addTabs(indent);
                writeEnd(ElementTags.TABLE);
                return;
            }
            case Element.ANNOTATION:
            {
                Annotation annotation = (Annotation) element;
                
                addTabs(indent);
                writeStart(ElementTags.ANNOTATION);
                if (annotation.title() != null) {
                    write(ElementTags.TITLE, annotation.title());
                }
                if (annotation.content() != null) {
                    write(ElementTags.CONTENT, annotation.content());
                }
                if (hasMarkupAttributes(annotation)) {
                  writeMarkupAttributes((MarkupAttributes)annotation);
                }
                writeEnd();
                return;
            }
            case Element.IMGRAW:
            case Element.JPEG:
            case Element.IMGTEMPLATE:
            {
                Image image = (Image) element;
                if (image.url() == null) {
                    return;
                }
                
                addTabs(indent);
                writeStart(ElementTags.IMAGE);
                write(ElementTags.URL, image.url().toString());
                if ((image.alignment() & Image.LEFT) > 0) {
                    write(ElementTags.ALIGN, ElementTags.ALIGN_LEFT);
                }
                else if ((image.alignment() & Image.RIGHT) > 0) {
                    write(ElementTags.ALIGN, ElementTags.ALIGN_RIGHT);
                }
                else if ((image.alignment() & Image.MIDDLE) > 0) {
                    write(ElementTags.ALIGN, ElementTags.ALIGN_MIDDLE);
                }
                if ((image.alignment() & Image.UNDERLYING) > 0) {
                    write(ElementTags.UNDERLYING, String.valueOf(true));
                }
                if ((image.alignment() & Image.TEXTWRAP) > 0) {
                    write(ElementTags.TEXTWRAP, String.valueOf(true));
                }
                if (image.alt() != null) {
                    write(ElementTags.ALT, image.alt());
                }
                if (image.hasAbsolutePosition()) {
                    write(ElementTags.ABSOLUTEX, String.valueOf(image.absoluteX()));
                    write(ElementTags.ABSOLUTEY, String.valueOf(image.absoluteY()));
                }
                write(ElementTags.PLAINWIDTH, String.valueOf(image.plainWidth()));
                write(ElementTags.PLAINHEIGHT, String.valueOf(image.plainHeight()));
                if (hasMarkupAttributes(image)) {
                  writeMarkupAttributes((MarkupAttributes)image);
                }
                writeEnd();
                return;
            }
            default:
                return;
        }
    }
    
/**
 * Writes the XML representation of a section.
 *
 * @param   section     the section to write
 * @param   indent      the indentation
 * @throws IOException
 */
    
    private void writeSection(Section section, int indent) throws IOException {
        write(ElementTags.NUMBERDEPTH, String.valueOf(section.numberDepth()));
        write(ElementTags.DEPTH, String.valueOf(section.depth()));
        write(ElementTags.INDENT, String.valueOf(section.indentation()));
        if (section.indentationLeft() != 0) {
            write(ElementTags.INDENTATIONLEFT, String.valueOf(section.indentationLeft()));
        }
        if (section.indentationRight() != 0) {
            write(ElementTags.INDENTATIONRIGHT, String.valueOf(section.indentationRight()));
        }
        os.write(GT);
        
        if (section.title() != null) {
            addTabs(indent + 1);
            writeStart(ElementTags.TITLE);
            write(ElementTags.LEADING, String.valueOf(section.title().leading()));
            write(ElementTags.ALIGN, ElementTags.getAlignment(section.title().alignment()));
            if (section.title().indentationLeft() != 0) {
                write(ElementTags.INDENTATIONLEFT, String.valueOf(section.title().indentationLeft()));
            }
            if (section.title().indentationRight() != 0) {
                write(ElementTags.INDENTATIONRIGHT, String.valueOf(section.title().indentationRight()));
            }
            write(section.title().font());
            os.write(GT);
            Iterator i = section.title().iterator();
            if (section.depth() > 0) {
                i.next();
            }
            while (i.hasNext()) {
                write((Element) i.next(), indent + 2);
            }
            addTabs(indent + 1);
            writeEnd(ElementTags.TITLE);
        }
        for (Iterator i = section.iterator(); i.hasNext(); ) {
            write((Element) i.next(), indent + 1);
        }
        addTabs(indent);
    }
    
/**
 * Writes the XML representation of this <CODE>Rectangle</CODE>.
 *
 * @param rectangle     a <CODE>Rectangle</CODE>
 * @throws IOException
 */
    
    private void write(Rectangle rectangle) throws IOException {
        if (rectangle.borderWidth() != Rectangle.UNDEFINED) {
            write(ElementTags.BORDERWIDTH, String.valueOf(rectangle.borderWidth()));
            if (rectangle.hasBorder(Rectangle.LEFT)) {
                write(ElementTags.LEFT, String.valueOf(true));
            }
            if (rectangle.hasBorder(Rectangle.RIGHT)) {
                write(ElementTags.RIGHT, String.valueOf(true));
            }
            if (rectangle.hasBorder(Rectangle.TOP)) {
                write(ElementTags.TOP, String.valueOf(true));
            }
            if (rectangle.hasBorder(Rectangle.BOTTOM)) {
                write(ElementTags.BOTTOM, String.valueOf(true));
            }
        }
        if (rectangle.borderColor() != null) {
            write(ElementTags.RED, String.valueOf(rectangle.borderColor().getRed()));
            write(ElementTags.GREEN, String.valueOf(rectangle.borderColor().getGreen()));
            write(ElementTags.BLUE, String.valueOf(rectangle.borderColor().getBlue()));
        }
        if (rectangle.backgroundColor() != null) {
            write(ElementTags.BGRED, String.valueOf(rectangle.backgroundColor().getRed()));
            write(ElementTags.BGGREEN, String.valueOf(rectangle.backgroundColor().getGreen()));
            write(ElementTags.BGBLUE, String.valueOf(rectangle.backgroundColor().getBlue()));
        }
    }
    
/**
 * Encodes a <CODE>String</CODE>.
 *
 * @param   string     the <CODE>String</CODE> to encode
 * @param indent counter that keeps the number of tabs that has to be added for indentation
 * @return  the encoded <CODE>String</CODE>
 */
    
    static final String encode(String string, int indent) {
        int n = string.length();
        int pos = 0;
        char character;
        StringBuffer buf = new StringBuffer();
        // loop over all the characters of the String.
        for (int i = 0; i < n; i++) {
            character = string.charAt(i);
            // the Xmlcode of these characters are added to a StringBuffer one by one
            switch(character) {
                case ' ':
                    if ((i - pos) > 60) {
                        pos = i;
                        buf.append("\n");
                        addTabs(buf, indent);
                        break;
                    }
                    default:
                        buf.append(xmlCode[(int) character]);
            }
        }
        return buf.toString();
    }
    
/**
 * Adds a number of tabs to a <CODE>StringBuffer</CODE>.
 *
 * @param   buf     the stringbuffer
 * @param   indent  the number of tabs to add
 */
    
    static final void addTabs(StringBuffer buf, int indent) {
        for (int i = 0; i < indent; i++) {
            buf.append("\t");
        }
    }
    
/**
 * Writes the XML representation of a <CODE>Font</CODE>.
 *
 * @param font  a <CODE>Font</CODE>
 * @throws IOException
 */
    
    private void write(Font font) throws IOException {
        write(ElementTags.FONT, font.getFamilyname());
        if (font.size() != Font.UNDEFINED) {
            write(ElementTags.SIZE, String.valueOf(font.size()));
        }
        if (font.style() != Font.UNDEFINED) {
            os.write(SPACE);
            write(ElementTags.STYLE);
            os.write(EQUALS);
            os.write(QUOTE);
            switch(font.style() & Font.BOLDITALIC) {
                case Font.NORMAL:
                    write(MarkupTags.CSS_VALUE_NORMAL);
                    break;
                case Font.BOLD:
                    write(MarkupTags.CSS_VALUE_BOLD);
                    break;
                case Font.ITALIC:
                    write(MarkupTags.CSS_VALUE_ITALIC);
                    break;
                case Font.BOLDITALIC:
                    write(MarkupTags.CSS_VALUE_BOLD);
                    write(", ");
                    write(MarkupTags.CSS_VALUE_ITALIC);
                    break;
            }
            if ((font.style() & Font.UNDERLINE) > 0) {
                write(", ");
                write(MarkupTags.CSS_VALUE_UNDERLINE);
            }
            if ((font.style() & Font.STRIKETHRU) > 0) {
                write(", ");
                write(MarkupTags.CSS_VALUE_LINETHROUGH);
            }
            os.write(QUOTE);
        }
        if (font.color() != null) {
            write(ElementTags.RED, String.valueOf(font.color().getRed()));
            write(ElementTags.GREEN, String.valueOf(font.color().getGreen()));
            write(ElementTags.BLUE, String.valueOf(font.color().getBlue()));
        }
    }
}