KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > xml > XmlWriter


1 /*
2  * $Id: XmlWriter.java,v 1.32 2004/12/14 11:25:42 blowagie Exp $
3  * $Name: $
4  *
5  * Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie.
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU LIBRARY GENERAL PUBLIC LICENSE for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text.xml;
52
53 import java.io.OutputStream;
54 import java.io.IOException;
55 import java.util.Date;
56 import java.util.Iterator;
57 import java.util.TreeMap;
58 import java.util.HashMap;
59
60 import com.lowagie.text.*;
61 import com.lowagie.text.markup.MarkupTags;
62
63 /**
64  * A <CODE>DocWriter</CODE> class for XML (Remark: this class is not finished yet!).
65  * <P>
66  * An <CODE>XmlWriter</CODE> can be added as a <CODE>DocListener</CODE>
67  * to a certain <CODE>Document</CODE> by getting an instance.
68  * Every <CODE>Element</CODE> added to the original <CODE>Document</CODE>
69  * will be written to the <CODE>OutputStream</CODE> of this <CODE>XmlWriter</CODE>.
70  * <P>
71  * Example:
72  * <BLOCKQUOTE><PRE>
73  * // creation of the document with a certain size and certain margins
74  * Document document = new Document(PageSize.A4, 50, 50, 50, 50);
75  * try {
76  * // this will write XML to the Standard OutputStream
77  * <STRONG>XmlWriter.getInstance(document, System.out);</STRONG>
78  * // this will write XML to a file called text.html
79  * <STRONG>XmlWriter.getInstance(document, new FileOutputStream("text.xml"));</STRONG>
80  * // this will write XML to for instance the OutputStream of a HttpServletResponse-object
81  * <STRONG>XmlWriter.getInstance(document, response.getOutputStream());</STRONG>
82  * }
83  * catch(DocumentException de) {
84  * System.err.println(de.getMessage());
85  * }
86  * // this will close the document and all the OutputStreams listening to it
87  * <STRONG>document.close();</CODE>
88  * </PRE></BLOCKQUOTE>
89  */

90
91 public class XmlWriter extends DocWriter implements DocListener {
92     
93     // static membervariables (tags)
94

95 /** This is the first line of the XML page. */
96     public static final byte[] PROLOG = getISOBytes("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
97     
98 /** This is the reference to the DTD. */
99     public static final byte[] DOCTYPE = getISOBytes("<!DOCTYPE ITEXT SYSTEM \"");
100     
101 /** This is the place where the DTD is located. */
102     public final static byte[] DTD = getISOBytes("http://itext.sourceforge.net/itext.dtd");
103     
104 /** This is an array containing character to XML translations. */
105     private static final String[] xmlCode = new String[256];
106     
107     static {
108         for (int i = 0; i < 10; i++) {
109             xmlCode[i] = "&#00" + i + ";";
110         }
111         
112         for (int i = 10; i < 32; i++) {
113             xmlCode[i] = "&#0" + i + ";";
114         }
115         
116         for (int i = 32; i < 128; i++) {
117             xmlCode[i] = String.valueOf((char)i);
118         }
119         
120         // Special characters
121
xmlCode['\n'] = "<" + ElementTags.NEWLINE + " />\n";
122         xmlCode['\"'] = "&quot;"; // double quote
123
xmlCode['\''] = "&apos;"; // single quote
124
xmlCode['&'] = "&amp;"; // ampersand
125
xmlCode['<'] = "&lt;"; // lower than
126
xmlCode['>'] = "&gt;"; // greater than
127

128         for (int i = 128; i < 256; i++) {
129             xmlCode[i] = "&#" + i + ";";
130         }
131     }
132     // membervariables
133

134 /** This is the meta information of the document. */
135     private TreeMap itext = new TreeMap(new com.lowagie.text.StringCompare());
136     
137     // constructors
138

139 /**
140  * Constructs an <CODE>XmlWriter</CODE>.
141  *
142  * @param doc The <CODE>Document</CODE> that has to be written as XML
143  * @param os The <CODE>OutputStream</CODE> the writer has to write to.
144  */

145     
146     protected XmlWriter(Document doc, OutputStream os) {
147         super(doc, os);
148         
149         document.addDocListener(this);
150         try {
151             os.write(PROLOG);
152             os.write(DOCTYPE);
153             os.write(DTD);
154             os.write(QUOTE);
155             os.write(GT);
156             os.write(NEWLINE);
157         }
158         catch(IOException ioe) {
159             throw new ExceptionConverter(ioe);
160         }
161     }
162     
163 /**
164  * Constructs an <CODE>XmlWriter</CODE>.
165  *
166  * @param doc The <CODE>Document</CODE> that has to be written as XML
167  * @param os The <CODE>OutputStream</CODE> the writer has to write to.
168  * @param dtd The DTD to use
169  */

170     
171     protected XmlWriter(Document doc, OutputStream os, String dtd) {
172         super(doc, os);
173         
174         document.addDocListener(this);
175         try {
176             os.write(PROLOG);
177             os.write(DOCTYPE);
178             os.write(getISOBytes(dtd));
179             os.write(QUOTE);
180             os.write(GT);
181             os.write(NEWLINE);
182         }
183         catch(IOException ioe) {
184             throw new ExceptionConverter(ioe);
185         }
186     }
187     
188     // get an instance of the XmlWriter
189

190 /**
191  * Gets an instance of the <CODE>XmlWriter</CODE>.
192  *
193  * @param document The <CODE>Document</CODE> that has to be written
194  * @param os The <CODE>OutputStream</CODE> the writer has to write to.
195  * @return a new <CODE>XmlWriter</CODE>
196  */

197     
198     public static XmlWriter getInstance(Document document, OutputStream os) {
199         return new XmlWriter(document, os);
200     }
201     
202 /**
203  * Gets an instance of the <CODE>XmlWriter</CODE>.
204  *
205  * @param document The <CODE>Document</CODE> that has to be written
206  * @param os The <CODE>OutputStream</CODE> the writer has to write to.
207  * @param dtd The DTD to use
208  * @return a new <CODE>XmlWriter</CODE>
209  */

210     
211     public static XmlWriter getInstance(Document document, OutputStream os, String dtd) {
212         return new XmlWriter(document, os, dtd);
213     }
214     
215     // implementation of the DocListener methods
216

217 /**
218  * Signals that an <CODE>Element</CODE> was added to the <CODE>Document</CODE>.
219  *
220  * @param element A high level object that will be added to the XML
221  * @return <CODE>true</CODE> if the element was added, <CODE>false</CODE> if not.
222  * @throws DocumentException when a document isn't open yet, or has been closed
223  */

224     
225     public boolean add(Element element) throws DocumentException {
226         if (pause) {
227             return false;
228         }
229         try {
230             switch(element.type()) {
231                 case Element.TITLE:
232                     itext.put(ElementTags.TITLE, ((Meta)element).content());
233                     return true;
234                 case Element.SUBJECT:
235                     itext.put(ElementTags.SUBJECT, ((Meta)element).content());
236                     return true;
237                 case Element.KEYWORDS:
238                     itext.put(ElementTags.KEYWORDS, ((Meta)element).content());
239                     return true;
240                 case Element.AUTHOR:
241                     itext.put(ElementTags.AUTHOR, ((Meta)element).content());
242                     return true;
243                     default:
244                         write(element, 1);
245                         return true;
246             }
247         }
248         catch(IOException ioe) {
249             return false;
250         }
251     }
252     
253 /**
254  * Signals that the <CODE>Document</CODE> has been opened and that
255  * <CODE>Elements</CODE> can be added.
256  */

257     
258     public void open() {
259         super.open();
260         try {
261             itext.put(ElementTags.PRODUCER, "iTextXML by lowagie.com");
262             itext.put(ElementTags.CREATIONDATE, new Date().toString());
263             writeStart(ElementTags.ITEXT);
264             String key;
265             for (java.util.Iterator i = itext.keySet().iterator(); i.hasNext(); ) {
266                 key = (String) i.next();
267                 write(key, (String) itext.get(key));
268             }
269             os.write(GT);
270         }
271         catch(IOException ioe) {
272             throw new ExceptionConverter(ioe);
273         }
274     }
275     
276 /**
277  * Signals that an new page has to be LTed.
278  *
279  * @return <CODE>true</CODE> if the page was added, <CODE>false</CODE> if not.
280  * @throws DocumentException when a document isn't open yet, or has been closed
281  */

282     
283     public boolean newPage() throws DocumentException {
284         if (pause || !open) {
285             return false;
286         }
287         try {
288             writeStart(ElementTags.NEWPAGE);
289             writeEnd();
290             return true;
291         }
292         catch(IOException ioe) {
293             return false;
294         }
295     }
296     
297 /**
298  * Signals that the <CODE>Document</CODE> was closed and that no other
299  * <CODE>Elements</CODE> will be added.
300  */

301     
302     public void close() {
303         try {
304             os.write(NEWLINE);
305             writeEnd(ElementTags.ITEXT);
306             super.close();
307         }
308         catch(IOException ioe) {
309             throw new ExceptionConverter(ioe);
310         }
311     }
312     
313     // methods
314

315 /**
316  * Writes the XML representation of an element.
317  *
318  * @param element the element
319  * @param indent the indentation
320  * @throws IOException
321  */

322     
323     private void write(Element element, int indent) throws IOException {
324         switch(element.type()) {
325             case Element.CHUNK:
326             {
327                 Chunk chunk = (Chunk) element;
328                 
329                 // if the chunk contains an image, return the image representation
330
try {
331                     Image image = chunk.getImage();
332                     write(image, indent);
333                     return;
334                 }
335                 catch(NullPointerException npe) {
336                     // empty on purpose
337
}
338                 
339                 addTabs(indent);
340                 HashMap attributes = chunk.getAttributes();
341                 if (chunk.font().isStandardFont() && attributes == null && !(hasMarkupAttributes(chunk))) {
342                     write(encode(chunk.content(), indent));
343                     return;
344                 }
345                 else {
346                     if (attributes != null && attributes.get(Chunk.NEWPAGE) != null) {
347                         writeStart(ElementTags.NEWPAGE);
348                         writeEnd();
349                         return;
350                     }
351                     writeStart(ElementTags.CHUNK);
352                     if (! chunk.font().isStandardFont()) {
353                         write(chunk.font());
354                     }
355                     if (attributes != null) {
356                         for (Iterator i = attributes.keySet().iterator(); i.hasNext(); ) {
357                             String key = (String) i.next();
358                             if (key.equals(Chunk.LOCALGOTO)
359                             || key.equals(Chunk.LOCALDESTINATION)
360                             || key.equals(Chunk.GENERICTAG)) {
361                                 String value = (String) attributes.get(key);
362                                 write(key.toLowerCase(), value);
363                             }
364                             if (key.equals(Chunk.SUBSUPSCRIPT)) {
365                                 write(key.toLowerCase(), String.valueOf((Float) attributes.get(key)));
366                             }
367                         }
368                     }
369                     if (hasMarkupAttributes(chunk)) {
370                       writeMarkupAttributes((MarkupAttributes)chunk);
371                     }
372                     os.write(GT);
373                     write(encode(chunk.content(), indent));
374                     writeEnd(ElementTags.CHUNK);
375                 }
376                 return;
377             }
378             case Element.PHRASE:
379             {
380                 Phrase phrase = (Phrase) element;
381                 
382                 addTabs(indent);
383                 writeStart(ElementTags.PHRASE);
384                 
385                 write(ElementTags.LEADING, String.valueOf(phrase.leading()));
386                 write(phrase.font());
387                 if (hasMarkupAttributes(phrase)) {
388                   writeMarkupAttributes((MarkupAttributes)phrase);
389                 }
390                 os.write(GT);
391                 
392                 for (Iterator i = phrase.iterator(); i.hasNext(); ) {
393                     write((Element) i.next(), indent + 1);
394                 }
395                 
396                 addTabs(indent);
397                 writeEnd(ElementTags.PHRASE);
398                 return;
399             }
400             case Element.ANCHOR:
401             {
402                 Anchor anchor = (Anchor) element;
403                 
404                 addTabs(indent);
405                 writeStart(ElementTags.ANCHOR);
406                 
407                 write(ElementTags.LEADING, String.valueOf(anchor.leading()));
408                 write(anchor.font());
409                 if (anchor.name() != null) {
410                     write(ElementTags.NAME, anchor.name());
411                 }
412                 if (anchor.reference() != null) {
413                     write(ElementTags.REFERENCE, anchor.reference());
414                 }
415                 if (hasMarkupAttributes(anchor)) {
416                   writeMarkupAttributes((MarkupAttributes)anchor);
417                 }
418                 os.write(GT);
419                 for (Iterator i = anchor.iterator(); i.hasNext(); ) {
420                     write((Element) i.next(), indent + 1);
421                 }
422                 addTabs(indent);
423                 writeEnd(ElementTags.ANCHOR);
424                 return;
425             }
426             case Element.PARAGRAPH:
427             {
428                 Paragraph paragraph = (Paragraph) element;
429                 
430                 addTabs(indent);
431                 writeStart(ElementTags.PARAGRAPH);
432                 
433                 write(ElementTags.LEADING, String.valueOf(paragraph.leading()));
434                 write(paragraph.font());
435                 write(ElementTags.ALIGN, ElementTags.getAlignment(paragraph.alignment()));
436                 if (paragraph.indentationLeft() != 0) {
437                     write(ElementTags.INDENTATIONLEFT, String.valueOf(paragraph.indentationLeft()));
438                 }
439                 if (paragraph.indentationRight() != 0) {
440                     write(ElementTags.INDENTATIONRIGHT, String.valueOf(paragraph.indentationRight()));
441                 }
442                 if (hasMarkupAttributes(paragraph)) {
443                   writeMarkupAttributes((MarkupAttributes)paragraph);
444                 }
445                 os.write(GT);
446                 for (Iterator i = paragraph.iterator(); i.hasNext(); ) {
447                     write((Element) i.next(), indent + 1);
448                 }
449                 addTabs(indent);
450                 writeEnd(ElementTags.PARAGRAPH);
451                 return;
452             }
453             case Element.SECTION:
454             {
455                 Section section = (Section) element;
456                 
457                 addTabs(indent);
458                 writeStart(ElementTags.SECTION);
459                 writeSection(section, indent);
460                 writeEnd(ElementTags.SECTION);
461                 return;
462             }
463             case Element.CHAPTER:
464             {
465                 Chapter chapter = (Chapter) element;
466                 
467                 addTabs(indent);
468                 writeStart(ElementTags.CHAPTER);
469                 if (hasMarkupAttributes(chapter)) {
470                   writeMarkupAttributes((MarkupAttributes)chapter);
471                 }
472                 writeSection(chapter, indent);
473                 writeEnd(ElementTags.CHAPTER);
474                 return;
475                 
476             }
477             case Element.LIST:
478             {
479                 List list = (List) element;
480                 
481                 addTabs(indent);
482                 writeStart(ElementTags.LIST);
483                 write(ElementTags.NUMBERED, String.valueOf(list.isNumbered()));
484                 write(ElementTags.SYMBOLINDENT, String.valueOf(list.symbolIndent()));
485                 if (list.first() != 1) {
486                     write(ElementTags.FIRST, String.valueOf(list.first()));
487                 }
488                 if (list.indentationLeft() != 0) {
489                     write(ElementTags.INDENTATIONLEFT, String.valueOf(list.indentationLeft()));
490                 }
491                 if (list.indentationRight() != 0) {
492                     write(ElementTags.INDENTATIONRIGHT, String.valueOf(list.indentationRight()));
493                 }
494                 if (!list.isNumbered()) {
495                     write(ElementTags.LISTSYMBOL, list.symbol().content());
496                 }
497                 write(list.symbol().font());
498                 if (hasMarkupAttributes(list)) {
499                   writeMarkupAttributes((MarkupAttributes)list);
500                 }
501                 os.write(GT);
502                 for (Iterator i = list.getItems().iterator(); i.hasNext(); ) {
503                     write((Element) i.next(), indent + 1);
504                 }
505                 addTabs(indent);
506                 writeEnd(ElementTags.LIST);
507                 return;
508             }
509             case Element.LISTITEM:
510             {
511                 ListItem listItem = (ListItem) element;
512                 
513                 addTabs(indent);
514                 writeStart(ElementTags.LISTITEM);
515                 write(ElementTags.LEADING, String.valueOf(listItem.leading()));
516                 write(listItem.font());
517                 write(ElementTags.ALIGN, ElementTags.getAlignment(listItem.alignment()));
518                 if (listItem.indentationLeft() != 0) {
519                     write(ElementTags.INDENTATIONLEFT, String.valueOf(listItem.indentationLeft()));
520                 }
521                 if (listItem.indentationRight() != 0) {
522                     write(ElementTags.INDENTATIONRIGHT, String.valueOf(listItem.indentationRight()));
523                 }
524                 if (hasMarkupAttributes(listItem)) {
525                   writeMarkupAttributes((MarkupAttributes)listItem);
526                 }
527                 os.write(GT);
528                 for (Iterator i = listItem.iterator(); i.hasNext(); ) {
529                     write((Element) i.next(), indent + 1);
530                 }
531                 addTabs(indent);
532                 writeEnd(ElementTags.LISTITEM);
533                 return;
534             }
535             case Element.CELL:
536             {
537                 Cell cell = (Cell) element;
538                 
539                 addTabs(indent);
540                 writeStart(ElementTags.CELL);
541                 write((Rectangle) cell);
542                 write(ElementTags.HORIZONTALALIGN, ElementTags.getAlignment(cell.horizontalAlignment()));
543                 write(ElementTags.VERTICALALIGN, ElementTags.getAlignment(cell.verticalAlignment()));
544                 if (cell.cellWidth() != null) {
545                     write(ElementTags.WIDTH, cell.cellWidth());
546                 }
547                 if (cell.colspan() != 1) {
548                     write(ElementTags.COLSPAN, String.valueOf(cell.colspan()));
549                 }
550                 if (cell.rowspan() != 1) {
551                     write(ElementTags.ROWSPAN, String.valueOf(cell.rowspan()));
552                 }
553                 if (cell.header()) {
554                     write(ElementTags.HEADER, String.valueOf(true));
555                 }
556                 if (cell.noWrap()) {
557                     write(ElementTags.NOWRAP, String.valueOf(true));
558                 }
559                 if (cell.leading() != -1) {
560                     write(ElementTags.LEADING, String.valueOf(cell.leading()));
561                 }
562                 if (hasMarkupAttributes(cell)) {
563                   writeMarkupAttributes((MarkupAttributes)cell);
564                 }
565                 os.write(GT);
566                 for (Iterator i = cell.getElements(); i.hasNext(); ) {
567                     write((Element) i.next(), indent + 1);
568                 }
569                 addTabs(indent);
570                 writeEnd(ElementTags.CELL);
571                 return;
572             }
573             case Element.ROW:
574             {
575                 Row row = (Row) element;
576                 
577                 addTabs(indent);
578                 writeStart(ElementTags.ROW);
579                 if (hasMarkupAttributes(row)){
580                   writeMarkupAttributes((MarkupAttributes)row);
581                 }
582                 os.write(GT);
583                 Element cell;
584                 for (int i = 0; i < row.columns(); i++) {
585                     if ((cell = (Element)row.getCell(i)) != null) {
586                         write(cell, indent + 1);
587                     }
588                 }
589                 addTabs(indent);
590                 writeEnd(ElementTags.ROW);
591                 return;
592             }
593             case Element.TABLE:
594             {
595                 Table table = (Table) element;
596                 table.complete();
597                 addTabs(indent);
598                 writeStart(ElementTags.TABLE);
599                 write(ElementTags.COLUMNS, String.valueOf(table.columns()));
600                 os.write(SPACE);
601                 write(ElementTags.WIDTH);
602                 os.write(EQUALS);
603                 os.write(QUOTE);
604                 if (! "".equals(table.absWidth())){
605                     write(table.absWidth());
606                 }
607                 else{
608                     write(String.valueOf(table.widthPercentage()));
609                     write("%");
610                 }
611                 os.write(QUOTE);
612                 write(ElementTags.ALIGN, ElementTags.getAlignment(table.alignment()));
613                 write(ElementTags.CELLPADDING, String.valueOf(table.cellpadding()));
614                 write(ElementTags.CELLSPACING, String.valueOf(table.cellspacing()));
615                 os.write(SPACE);
616                 write(ElementTags.WIDTHS);
617                 os.write(EQUALS);
618                 os.write(QUOTE);
619                 float[] widths = table.getProportionalWidths();
620                 write(String.valueOf(widths[0]));
621                 for (int i = 1; i < widths.length; i++) {
622                     write(";");
623                     write(String.valueOf(widths[i]));
624                 }
625                 os.write(QUOTE);
626                 write((Rectangle) table);
627                 if (hasMarkupAttributes(table)) {
628                   writeMarkupAttributes((MarkupAttributes)table);
629                 }
630                 os.write(GT);
631                 Row row;
632                 for (Iterator iterator = table.iterator(); iterator.hasNext(); ) {
633                     row = (Row) iterator.next();
634                     write(row, indent + 1);
635                 }
636                 addTabs(indent);
637                 writeEnd(ElementTags.TABLE);
638                 return;
639             }
640             case Element.ANNOTATION:
641             {
642                 Annotation annotation = (Annotation) element;
643                 
644                 addTabs(indent);
645                 writeStart(ElementTags.ANNOTATION);
646                 if (annotation.title() != null) {
647                     write(ElementTags.TITLE, annotation.title());
648                 }
649                 if (annotation.content() != null) {
650                     write(ElementTags.CONTENT, annotation.content());
651                 }
652                 if (hasMarkupAttributes(annotation)) {
653                   writeMarkupAttributes((MarkupAttributes)annotation);
654                 }
655                 writeEnd();
656                 return;
657             }
658             case Element.IMGRAW:
659             case Element.JPEG:
660             case Element.IMGTEMPLATE:
661             {
662                 Image image = (Image) element;
663                 if (image.url() == null) {
664                     return;
665                 }
666                 
667                 addTabs(indent);
668                 writeStart(ElementTags.IMAGE);
669                 write(ElementTags.URL, image.url().toString());
670                 if ((image.alignment() & Image.LEFT) > 0) {
671                     write(ElementTags.ALIGN, ElementTags.ALIGN_LEFT);
672                 }
673                 else if ((image.alignment() & Image.RIGHT) > 0) {
674                     write(ElementTags.ALIGN, ElementTags.ALIGN_RIGHT);
675                 }
676                 else if ((image.alignment() & Image.MIDDLE) > 0) {
677                     write(ElementTags.ALIGN, ElementTags.ALIGN_MIDDLE);
678                 }
679                 if ((image.alignment() & Image.UNDERLYING) > 0) {
680                     write(ElementTags.UNDERLYING, String.valueOf(true));
681                 }
682                 if ((image.alignment() & Image.TEXTWRAP) > 0) {
683                     write(ElementTags.TEXTWRAP, String.valueOf(true));
684                 }
685                 if (image.alt() != null) {
686                     write(ElementTags.ALT, image.alt());
687                 }
688                 if (image.hasAbsolutePosition()) {
689                     write(ElementTags.ABSOLUTEX, String.valueOf(image.absoluteX()));
690                     write(ElementTags.ABSOLUTEY, String.valueOf(image.absoluteY()));
691                 }
692                 write(ElementTags.PLAINWIDTH, String.valueOf(image.plainWidth()));
693                 write(ElementTags.PLAINHEIGHT, String.valueOf(image.plainHeight()));
694                 if (hasMarkupAttributes(image)) {
695                   writeMarkupAttributes((MarkupAttributes)image);
696                 }
697                 writeEnd();
698                 return;
699             }
700             default:
701                 return;
702         }
703     }
704     
705 /**
706  * Writes the XML representation of a section.
707  *
708  * @param section the section to write
709  * @param indent the indentation
710  * @throws IOException
711  */

712     
713     private void writeSection(Section section, int indent) throws IOException {
714         write(ElementTags.NUMBERDEPTH, String.valueOf(section.numberDepth()));
715         write(ElementTags.DEPTH, String.valueOf(section.depth()));
716         write(ElementTags.INDENT, String.valueOf(section.indentation()));
717         if (section.indentationLeft() != 0) {
718             write(ElementTags.INDENTATIONLEFT, String.valueOf(section.indentationLeft()));
719         }
720         if (section.indentationRight() != 0) {
721             write(ElementTags.INDENTATIONRIGHT, String.valueOf(section.indentationRight()));
722         }
723         os.write(GT);
724         
725         if (section.title() != null) {
726             addTabs(indent + 1);
727             writeStart(ElementTags.TITLE);
728             write(ElementTags.LEADING, String.valueOf(section.title().leading()));
729             write(ElementTags.ALIGN, ElementTags.getAlignment(section.title().alignment()));
730             if (section.title().indentationLeft() != 0) {
731                 write(ElementTags.INDENTATIONLEFT, String.valueOf(section.title().indentationLeft()));
732             }
733             if (section.title().indentationRight() != 0) {
734                 write(ElementTags.INDENTATIONRIGHT, String.valueOf(section.title().indentationRight()));
735             }
736             write(section.title().font());
737             os.write(GT);
738             Iterator i = section.title().iterator();
739             if (section.depth() > 0) {
740                 i.next();
741             }
742             while (i.hasNext()) {
743                 write((Element) i.next(), indent + 2);
744             }
745             addTabs(indent + 1);
746             writeEnd(ElementTags.TITLE);
747         }
748         for (Iterator i = section.iterator(); i.hasNext(); ) {
749             write((Element) i.next(), indent + 1);
750         }
751         addTabs(indent);
752     }
753     
754 /**
755  * Writes the XML representation of this <CODE>Rectangle</CODE>.
756  *
757  * @param rectangle a <CODE>Rectangle</CODE>
758  * @throws IOException
759  */

760     
761     private void write(Rectangle rectangle) throws IOException {
762         if (rectangle.borderWidth() != Rectangle.UNDEFINED) {
763             write(ElementTags.BORDERWIDTH, String.valueOf(rectangle.borderWidth()));
764             if (rectangle.hasBorder(Rectangle.LEFT)) {
765                 write(ElementTags.LEFT, String.valueOf(true));
766             }
767             if (rectangle.hasBorder(Rectangle.RIGHT)) {
768                 write(ElementTags.RIGHT, String.valueOf(true));
769             }
770             if (rectangle.hasBorder(Rectangle.TOP)) {
771                 write(ElementTags.TOP, String.valueOf(true));
772             }
773             if (rectangle.hasBorder(Rectangle.BOTTOM)) {
774                 write(ElementTags.BOTTOM, String.valueOf(true));
775             }
776         }
777         if (rectangle.borderColor() != null) {
778             write(ElementTags.RED, String.valueOf(rectangle.borderColor().getRed()));
779             write(ElementTags.GREEN, String.valueOf(rectangle.borderColor().getGreen()));
780             write(ElementTags.BLUE, String.valueOf(rectangle.borderColor().getBlue()));
781         }
782         if (rectangle.backgroundColor() != null) {
783             write(ElementTags.BGRED, String.valueOf(rectangle.backgroundColor().getRed()));
784             write(ElementTags.BGGREEN, String.valueOf(rectangle.backgroundColor().getGreen()));
785             write(ElementTags.BGBLUE, String.valueOf(rectangle.backgroundColor().getBlue()));
786         }
787         if (rectangle.grayFill() > 0) {
788             write(ElementTags.GRAYFILL, String.valueOf(rectangle.grayFill()));
789         }
790     }
791     
792 /**
793  * Encodes a <CODE>String</CODE>.
794  *
795  * @param string the <CODE>String</CODE> to encode
796  * @param indent counter that keeps the number of tabs that has to be added for indentation
797  * @return the encoded <CODE>String</CODE>
798  */

799     
800     static final String encode(String string, int indent) {
801         int n = string.length();
802         int pos = 0;
803         char character;
804         StringBuffer buf = new StringBuffer();
805         // loop over all the characters of the String.
806
for (int i = 0; i < n; i++) {
807             character = string.charAt(i);
808             // the Xmlcode of these characters are added to a StringBuffer one by one
809
switch(character) {
810                 case ' ':
811                     if ((i - pos) > 60) {
812                         pos = i;
813                         buf.append("\n");
814                         addTabs(buf, indent);
815                         break;
816                     }
817                     default:
818                         buf.append(xmlCode[(int) character]);
819             }
820         }
821         return buf.toString();
822     }
823     
824 /**
825  * Adds a number of tabs to a <CODE>StringBuffer</CODE>.
826  *
827  * @param buf the stringbuffer
828  * @param indent the number of tabs to add
829  */

830     
831     static final void addTabs(StringBuffer buf, int indent) {
832         for (int i = 0; i < indent; i++) {
833             buf.append("\t");
834         }
835     }
836     
837 /**
838  * Writes the XML representation of a <CODE>Font</CODE>.
839  *
840  * @param font a <CODE>Font</CODE>
841  * @throws IOException
842  */

843     
844     private void write(Font font) throws IOException {
845         write(ElementTags.FONT, font.getFamilyname());
846         if (font.size() != Font.UNDEFINED) {
847             write(ElementTags.SIZE, String.valueOf(font.size()));
848         }
849         if (font.style() != Font.UNDEFINED) {
850             os.write(SPACE);
851             write(ElementTags.STYLE);
852             os.write(EQUALS);
853             os.write(QUOTE);
854             switch(font.style() & Font.BOLDITALIC) {
855                 case Font.NORMAL:
856                     write(MarkupTags.CSS_NORMAL);
857                     break;
858                 case Font.BOLD:
859                     write(MarkupTags.CSS_BOLD);
860                     break;
861                 case Font.ITALIC:
862                     write(MarkupTags.CSS_ITALIC);
863                     break;
864                 case Font.BOLDITALIC:
865                     write(MarkupTags.CSS_BOLD);
866                     write(", ");
867                     write(MarkupTags.CSS_ITALIC);
868                     break;
869             }
870             if ((font.style() & Font.UNDERLINE) > 0) {
871                 write(", ");
872                 write(MarkupTags.CSS_UNDERLINE);
873             }
874             if ((font.style() & Font.STRIKETHRU) > 0) {
875                 write(", ");
876                 write(MarkupTags.CSS_LINETHROUGH);
877             }
878             os.write(QUOTE);
879         }
880         if (font.color() != null) {
881             write(ElementTags.RED, String.valueOf(font.color().getRed()));
882             write(ElementTags.GREEN, String.valueOf(font.color().getGreen()));
883             write(ElementTags.BLUE, String.valueOf(font.color().getBlue()));
884         }
885     }
886 }
Popular Tags