1 /*
2  * $Id:RtfMapper.java 3126 2008-02-07 20:30:46Z hallm $
3  *
4  * Copyright 2003, 2004 by Mark Hall
5  *
6  * The contents of this file are subject to the Mozilla Public License Version 1.1
7  * (the "License"); you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the License.
13  *
14  * The Original Code is 'iText, a free JAVA-PDF library'.
15  *
16  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18  * All Rights Reserved.
19  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source code
23  * where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
27  * provisions of LGPL are applicable instead of those above.  If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the MPL as stated above or under the terms of the GNU
37  * Library General Public License as published by the Free Software Foundation;
38  * either version 2 of the License, or any later version.
39  *
40  * This library is distributed in the hope that it will be useful, but WITHOUT
41  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43  * details.
44  *
45  * If you didn't download this code from the following link, you should check if
46  * you aren't using an obsolete version:
47  * http://www.lowagie.com/iText/
48  */
49 
50 package com.lowagie.text.rtf;
51 
52 import java.util.ArrayList;
53 
54 import com.lowagie.text.Anchor;
55 import com.lowagie.text.Annotation;
56 import com.lowagie.text.Chapter;
57 import com.lowagie.text.Chunk;
58 import com.lowagie.text.DocumentException;
59 import com.lowagie.text.Element;
60 import com.lowagie.text.Image;
61 import com.lowagie.text.List;
62 import com.lowagie.text.ListItem;
63 import com.lowagie.text.Meta;
64 import com.lowagie.text.Paragraph;
65 import com.lowagie.text.Phrase;
66 import com.lowagie.text.Section;
67 import com.lowagie.text.SimpleTable;
68 import com.lowagie.text.Table;
69 import com.lowagie.text.pdf.PdfPTable;
70 import com.lowagie.text.rtf.document.RtfDocument;
71 import com.lowagie.text.rtf.document.RtfInfoElement;
72 import com.lowagie.text.rtf.field.RtfAnchor;
73 import com.lowagie.text.rtf.graphic.RtfImage;
74 import com.lowagie.text.rtf.list.RtfList;
75 import com.lowagie.text.rtf.list.RtfListItem;
76 import com.lowagie.text.rtf.table.RtfTable;
77 import com.lowagie.text.rtf.text.RtfAnnotation;
78 import com.lowagie.text.rtf.text.RtfChapter;
79 import com.lowagie.text.rtf.text.RtfChunk;
80 import com.lowagie.text.rtf.text.RtfNewPage;
81 import com.lowagie.text.rtf.text.RtfParagraph;
82 import com.lowagie.text.rtf.text.RtfPhrase;
83 import com.lowagie.text.rtf.text.RtfSection;
84 import com.lowagie.text.rtf.text.RtfTab;
85 
86 
87 /**
88  * The RtfMapper provides mappings between com.lowagie.text.* classes
89  * and the corresponding com.lowagie.text.rtf.** classes.
90  *
91  * @version $Revision$
92  * @author Mark Hall (Mark.Hall@mail.room3b.eu)
93  */
94 public class RtfMapper {
95 
96     /**
97      * The RtfDocument this RtfMapper belongs to
98      */
99     RtfDocument rtfDoc;
100 
101     /**
102      * Constructs a RtfMapper for a RtfDocument
103      *
104      * @param doc The RtfDocument this RtfMapper belongs to
105      */
RtfMapper(RtfDocument doc)106     public RtfMapper(RtfDocument doc) {
107         this.rtfDoc = doc;
108     }
109 
110     /**
111      * Takes an Element subclass and returns an array of RtfBasicElement
112      * subclasses, that contained the mapped RTF equivalent to the Element
113      * passed in.
114      *
115      * @param element The Element to wrap
116      * @return An array of RtfBasicElement wrapping the Element
117      * @throws DocumentException
118      */
mapElement(Element element)119     public RtfBasicElement[] mapElement(Element element) throws DocumentException {
120         ArrayList rtfElements = new ArrayList();
121 
122         if(element instanceof RtfBasicElement) {
123             RtfBasicElement rtfElement = (RtfBasicElement) element;
124             rtfElement.setRtfDocument(this.rtfDoc);
125             return new RtfBasicElement[]{rtfElement};
126         }
127         switch(element.type()) {
128     		case Element.CHUNK:
129     		    Chunk chunk = (Chunk) element;
130     		    if(chunk.hasAttributes()) {
131     		        if(chunk.getAttributes().containsKey(Chunk.IMAGE)) {
132     		            rtfElements.add(new RtfImage(rtfDoc, chunk.getImage()));
133     		        } else if(chunk.getAttributes().containsKey(Chunk.NEWPAGE)) {
134     		            rtfElements.add(new RtfNewPage(rtfDoc));
135     		        } else if(chunk.getAttributes().containsKey(Chunk.TAB)) {
136                         Float tabPos = (Float) ((Object[]) chunk.getAttributes().get(Chunk.TAB))[1];
137                         RtfTab tab = new RtfTab(tabPos.floatValue(), RtfTab.TAB_LEFT_ALIGN);
138                         tab.setRtfDocument(rtfDoc);
139                         rtfElements.add(tab);
140                         rtfElements.add(new RtfChunk(rtfDoc, new Chunk("\t")));
141     		        } else {
142                         rtfElements.add(new RtfChunk(rtfDoc, (Chunk) element));
143     		        }
144     		    } else {
145                     rtfElements.add(new RtfChunk(rtfDoc, (Chunk) element));
146     		    }
147     			break;
148     		case Element.PHRASE:
149     		    rtfElements.add(new RtfPhrase(rtfDoc, (Phrase) element));
150     			break;
151     		case Element.PARAGRAPH:
152     		    rtfElements.add(new RtfParagraph(rtfDoc, (Paragraph) element));
153     			break;
154     		case Element.ANCHOR:
155     			rtfElements.add(new RtfAnchor(rtfDoc, (Anchor) element));
156     			break;
157     		case Element.ANNOTATION:
158     		    rtfElements.add(new RtfAnnotation(rtfDoc, (Annotation) element));
159     			break;
160             case Element.IMGRAW:
161             case Element.IMGTEMPLATE:
162             case Element.JPEG:
163                 rtfElements.add(new RtfImage(rtfDoc, (Image) element));
164             	break;
165     		case Element.AUTHOR:
166     		case Element.SUBJECT:
167     		case Element.KEYWORDS:
168     		case Element.TITLE:
169     		case Element.PRODUCER:
170     		case Element.CREATIONDATE:
171     		    rtfElements.add(new RtfInfoElement(rtfDoc, (Meta) element));
172     			break;
173     		case Element.LIST:
174     		    rtfElements.add(new RtfList(rtfDoc, (List) element));	// TODO: Testing
175     			break;
176     		case Element.LISTITEM:
177     		    rtfElements.add(new RtfListItem(rtfDoc, (ListItem) element));	// TODO: Testing
178     			break;
179     		case Element.SECTION:
180     		    rtfElements.add(new RtfSection(rtfDoc, (Section) element));
181     			break;
182     		case Element.CHAPTER:
183     		    rtfElements.add(new RtfChapter(rtfDoc, (Chapter) element));
184     			break;
185     		case Element.TABLE:
186     			try {
187     				rtfElements.add(new RtfTable(rtfDoc, (Table) element));
188     			}
189     			catch(ClassCastException e) {
190     				rtfElements.add(new RtfTable(rtfDoc, ((SimpleTable) element).createTable()));
191     			}
192     			break;
193     		case Element.PTABLE:
194     			try {
195     				rtfElements.add(new RtfTable(rtfDoc, (PdfPTable) element));
196     			}
197     			catch(ClassCastException e) {
198     				rtfElements.add(new RtfTable(rtfDoc, ((SimpleTable) element).createTable()));
199     			}
200     			break;
201         }
202 
203         return (RtfBasicElement[]) rtfElements.toArray(new RtfBasicElement[rtfElements.size()]);
204     }
205 }
206