001: /*
002: * $Id: RtfMapper.java 1734 2005-05-04 14:41:15Z blowagie $
003: * $Name$
004: *
005: * Copyright 2003, 2004 by Mark Hall
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050:
051: package com.lowagie.text.rtf;
052:
053: import com.lowagie.text.Anchor;
054: import com.lowagie.text.Annotation;
055: import com.lowagie.text.Chapter;
056: import com.lowagie.text.Chunk;
057: import com.lowagie.text.DocumentException;
058: import com.lowagie.text.Element;
059: import com.lowagie.text.Image;
060: import com.lowagie.text.List;
061: import com.lowagie.text.ListItem;
062: import com.lowagie.text.Meta;
063: import com.lowagie.text.Paragraph;
064: import com.lowagie.text.Phrase;
065: import com.lowagie.text.Section;
066: import com.lowagie.text.SimpleTable;
067: import com.lowagie.text.Table;
068: import com.lowagie.text.rtf.document.RtfDocument;
069: import com.lowagie.text.rtf.document.RtfInfoElement;
070: import com.lowagie.text.rtf.field.RtfAnchor;
071: import com.lowagie.text.rtf.graphic.RtfImage;
072: import com.lowagie.text.rtf.list.RtfList;
073: import com.lowagie.text.rtf.list.RtfListItem;
074: import com.lowagie.text.rtf.table.RtfTable;
075: import com.lowagie.text.rtf.text.RtfAnnotation;
076: import com.lowagie.text.rtf.text.RtfChapter;
077: import com.lowagie.text.rtf.text.RtfChunk;
078: import com.lowagie.text.rtf.text.RtfNewPage;
079: import com.lowagie.text.rtf.text.RtfParagraph;
080: import com.lowagie.text.rtf.text.RtfPhrase;
081: import com.lowagie.text.rtf.text.RtfSection;
082:
083: /**
084: * The RtfMapper provides mappings between com.lowagie.text.* classes
085: * and the corresponding com.lowagie.text.rtf.** classes.
086: *
087: * @version $Version:$
088: * @author Mark Hall (mhall@edu.uni-klu.ac.at)
089: */
090: public class RtfMapper {
091:
092: /**
093: * The RtfDocument this RtfMapper belongs to
094: */
095: RtfDocument rtfDoc;
096:
097: /**
098: * Constructs a RtfMapper for a RtfDocument
099: *
100: * @param doc The RtfDocument this RtfMapper belongs to
101: */
102: public RtfMapper(RtfDocument doc) {
103: this .rtfDoc = doc;
104: }
105:
106: /**
107: * Takes an Element subclass and returns the correct RtfBasicElement
108: * subclass, that wraps the Element subclass.
109: *
110: * @param element The Element to wrap
111: * @return A RtfBasicElement wrapping the Element
112: * @throws DocumentException
113: */
114: public RtfBasicElement mapElement(Element element)
115: 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)
127: .getImage());
128: } else if (((Chunk) element).hasAttributes()
129: && ((Chunk) element).getAttributes().containsKey(
130: Chunk.NEWPAGE)) {
131: rtfElement = new RtfNewPage(rtfDoc);
132: } else {
133: rtfElement = new RtfChunk(rtfDoc, (Chunk) element);
134: }
135: break;
136: case Element.PHRASE:
137: rtfElement = new RtfPhrase(rtfDoc, (Phrase) element);
138: break;
139: case Element.PARAGRAPH:
140: rtfElement = new RtfParagraph(rtfDoc, (Paragraph) element);
141: break;
142: case Element.ANCHOR:
143: rtfElement = new RtfAnchor(rtfDoc, (Anchor) element);
144: break;
145: case Element.ANNOTATION:
146: rtfElement = new RtfAnnotation(rtfDoc, (Annotation) element);
147: break;
148: case Element.IMGRAW:
149: case Element.IMGTEMPLATE:
150: case Element.JPEG:
151: rtfElement = new RtfImage(rtfDoc, (Image) element);
152: break;
153: case Element.AUTHOR:
154: case Element.SUBJECT:
155: case Element.KEYWORDS:
156: case Element.TITLE:
157: case Element.PRODUCER:
158: case Element.CREATIONDATE:
159: rtfElement = new RtfInfoElement(rtfDoc, (Meta) element);
160: break;
161: case Element.LIST:
162: rtfElement = new RtfList(rtfDoc, (List) element);
163: break;
164: case Element.LISTITEM:
165: rtfElement = new RtfListItem(rtfDoc, (ListItem) element);
166: break;
167: case Element.SECTION:
168: rtfElement = new RtfSection(rtfDoc, (Section) element);
169: break;
170: case Element.CHAPTER:
171: rtfElement = new RtfChapter(rtfDoc, (Chapter) element);
172: break;
173: case Element.TABLE:
174: try {
175: rtfElement = new RtfTable(rtfDoc, (Table) element);
176: } catch (ClassCastException e) {
177: rtfElement = new RtfTable(rtfDoc,
178: ((SimpleTable) element).createTable());
179: }
180: break;
181: }
182:
183: return rtfElement;
184: }
185: }
|