KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > rtf > RtfMapper


1 /*
2  * $Id: RtfMapper.java 1734 2005-05-04 14:41:15Z blowagie $
3  * $Name$
4  *
5  * Copyright 2003, 2004 by Mark Hall
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.rtf;
52
53 import com.lowagie.text.Anchor;
54 import com.lowagie.text.Annotation;
55 import com.lowagie.text.Chapter;
56 import com.lowagie.text.Chunk;
57 import com.lowagie.text.DocumentException;
58 import com.lowagie.text.Element;
59 import com.lowagie.text.Image;
60 import com.lowagie.text.List;
61 import com.lowagie.text.ListItem;
62 import com.lowagie.text.Meta;
63 import com.lowagie.text.Paragraph;
64 import com.lowagie.text.Phrase;
65 import com.lowagie.text.Section;
66 import com.lowagie.text.SimpleTable;
67 import com.lowagie.text.Table;
68 import com.lowagie.text.rtf.document.RtfDocument;
69 import com.lowagie.text.rtf.document.RtfInfoElement;
70 import com.lowagie.text.rtf.field.RtfAnchor;
71 import com.lowagie.text.rtf.graphic.RtfImage;
72 import com.lowagie.text.rtf.list.RtfList;
73 import com.lowagie.text.rtf.list.RtfListItem;
74 import com.lowagie.text.rtf.table.RtfTable;
75 import com.lowagie.text.rtf.text.RtfAnnotation;
76 import com.lowagie.text.rtf.text.RtfChapter;
77 import com.lowagie.text.rtf.text.RtfChunk;
78 import com.lowagie.text.rtf.text.RtfNewPage;
79 import com.lowagie.text.rtf.text.RtfParagraph;
80 import com.lowagie.text.rtf.text.RtfPhrase;
81 import com.lowagie.text.rtf.text.RtfSection;
82
83
84 /**
85  * The RtfMapper provides mappings between com.lowagie.text.* classes
86  * and the corresponding com.lowagie.text.rtf.** classes.
87  *
88  * @version $Version:$
89  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
90  */

91 public class RtfMapper {
92
93     /**
94      * The RtfDocument this RtfMapper belongs to
95      */

96     RtfDocument rtfDoc;
97     
98     /**
99      * Constructs a RtfMapper for a RtfDocument
100      *
101      * @param doc The RtfDocument this RtfMapper belongs to
102      */

103     public RtfMapper(RtfDocument doc) {
104         this.rtfDoc = doc;
105     }
106     
107     /**
108      * Takes an Element subclass and returns the correct RtfBasicElement
109      * subclass, that wraps the Element subclass.
110      *
111      * @param element The Element to wrap
112      * @return A RtfBasicElement wrapping the Element
113      * @throws DocumentException
114      */

115     public RtfBasicElement mapElement(Element element) throws DocumentException {
116         RtfBasicElement rtfElement = null;
117
118         if(element instanceof RtfBasicElement) {
119             rtfElement = (RtfBasicElement) element;
120             rtfElement.setRtfDocument(this.rtfDoc);
121             return rtfElement;
122         }
123         switch(element.type()) {
124             case Element.CHUNK:
125                 if(((Chunk) element).getImage() != null) {
126                     rtfElement = new RtfImage(rtfDoc, ((Chunk) element).getImage());
127                 } else if(((Chunk) element).hasAttributes() && ((Chunk) element).getAttributes().containsKey(Chunk.NEWPAGE)) {
128                     rtfElement = new RtfNewPage(rtfDoc);
129                 } else {
130                     rtfElement = new RtfChunk(rtfDoc, (Chunk) element);
131                 }
132                 break;
133             case Element.PHRASE:
134                 rtfElement = new RtfPhrase(rtfDoc, (Phrase) element);
135                 break;
136             case Element.PARAGRAPH:
137                 rtfElement = new RtfParagraph(rtfDoc, (Paragraph) element);
138                 break;
139             case Element.ANCHOR:
140                 rtfElement = new RtfAnchor(rtfDoc, (Anchor) element);
141                 break;
142             case Element.ANNOTATION:
143                 rtfElement = new RtfAnnotation(rtfDoc, (Annotation) element);
144                 break;
145             case Element.IMGRAW:
146             case Element.IMGTEMPLATE:
147             case Element.JPEG:
148                 rtfElement = new RtfImage(rtfDoc, (Image) element);
149                 break;
150             case Element.AUTHOR:
151             case Element.SUBJECT:
152             case Element.KEYWORDS:
153             case Element.TITLE:
154             case Element.PRODUCER:
155             case Element.CREATIONDATE:
156                 rtfElement = new RtfInfoElement(rtfDoc, (Meta) element);
157                 break;
158             case Element.LIST:
159                 rtfElement = new RtfList(rtfDoc, (List) element);
160                 break;
161             case Element.LISTITEM:
162                 rtfElement = new RtfListItem(rtfDoc, (ListItem) element);
163                 break;
164             case Element.SECTION:
165                 rtfElement = new RtfSection(rtfDoc, (Section) element);
166                 break;
167             case Element.CHAPTER:
168                 rtfElement = new RtfChapter(rtfDoc, (Chapter) element);
169                 break;
170             case Element.TABLE:
171                 try {
172                     rtfElement = new RtfTable(rtfDoc, (Table) element);
173                 }
174                 catch(ClassCastException JavaDoc e) {
175                     rtfElement = new RtfTable(rtfDoc, ((SimpleTable) element).createTable());
176                 }
177                 break;
178         }
179         
180         return rtfElement;
181     }
182 }
183
Popular Tags