1 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 91 public class RtfMapper { 92 93 96 RtfDocument rtfDoc; 97 98 103 public RtfMapper(RtfDocument doc) { 104 this.rtfDoc = doc; 105 } 106 107 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 e) { 175 rtfElement = new RtfTable(rtfDoc, ((SimpleTable) element).createTable()); 176 } 177 break; 178 } 179 180 return rtfElement; 181 } 182 } 183 | Popular Tags |