1 /* 2 * $Id$ 3 * 4 * Copyright 2007 by Howard Shank (hgshank@yahoo.com) 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-2006 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-2006 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 package com.lowagie.text.rtf.parser; 50 51 52 import java.awt.Color; 53 import java.util.HashMap; 54 55 import com.lowagie.text.Document; 56 import com.lowagie.text.List; 57 import com.lowagie.text.rtf.document.RtfDocument; 58 import com.lowagie.text.rtf.list.RtfList; 59 import com.lowagie.text.rtf.style.RtfColor; 60 import com.lowagie.text.rtf.style.RtfFont; 61 62 /** 63 * The RtfImportHeader stores the document header information from 64 * an RTF document that is being imported. Currently font and 65 * color settings are stored. The RtfImportHeader maintains a mapping 66 * from font and color numbers from the imported RTF document to 67 * the RTF document that is the target of the import. This guarantees 68 * that the merged document has the correct font and color settings. 69 * It also handles other list based items that need mapping, for example 70 * stylesheets and lists. 71 * 72 * @author Mark Hall (Mark.Hall@mail.room3b.eu) 73 * @author Howard Shank (hgshank@yahoo.com) 74 * @since 2.1.0 75 */ 76 public class RtfImportMgr { 77 //TODO: Add list, stylesheet, info, etc. mappings 78 /** 79 * The HashMap storing the font number mappings. 80 */ 81 private HashMap importFontMapping = null; 82 /** 83 * The HashMap storing the color number mappings. 84 */ 85 private HashMap importColorMapping = null; 86 /** 87 * The HashMap storing the Stylesheet List number mappings. 88 */ 89 private HashMap importStylesheetListMapping = null; 90 /** 91 * The HashMap storing the List number mappings. 92 */ 93 private HashMap importListMapping = null; 94 /** 95 * The RtfDocument to get font and color numbers from. 96 */ 97 private RtfDocument rtfDoc = null; 98 /** 99 * The Document. 100 * Used for conversions, but not imports. 101 */ 102 private Document doc = null; 103 104 105 /** 106 * Constructs a new RtfImportHeader. 107 * 108 * @param rtfDoc The RtfDocument to get font and color numbers from. 109 */ RtfImportMgr(RtfDocument rtfDoc, Document doc)110 public RtfImportMgr(RtfDocument rtfDoc, Document doc) { 111 this.rtfDoc = rtfDoc; 112 this.doc = doc; 113 this.importFontMapping = new HashMap(); 114 this.importColorMapping = new HashMap(); 115 this.importStylesheetListMapping = new HashMap(); 116 this.importListMapping = new HashMap(); 117 } 118 119 /** 120 * Imports a font. The font name is looked up in the RtfDocumentHeader and 121 * then the mapping from original font number to actual font number is added. 122 * 123 * @param fontNr The original font number. 124 * @param fontName The font name to look up. 125 */ importFont(String fontNr, String fontName)126 public boolean importFont(String fontNr, String fontName) { 127 RtfFont rtfFont = new RtfFont(fontName); 128 rtfFont.setRtfDocument(this.rtfDoc); 129 this.importFontMapping.put(fontNr, Integer.toString(this.rtfDoc.getDocumentHeader().getFontNumber(rtfFont))); 130 return true; 131 } 132 /** 133 * Imports a font. The font name is looked up in the RtfDocumentHeader and 134 * then the mapping from original font number to actual font number is added. 135 * 136 * @param fontNr The original font number. 137 * @param fontName The font name to look up. 138 * @param charset The character set to use for the font. 139 */ importFont(String fontNr, String fontName, int charset)140 public boolean importFont(String fontNr, String fontName, int charset) { 141 RtfFont rtfFont = new RtfFont(fontName); 142 if(charset>= 0) 143 rtfFont.setCharset(charset); 144 rtfFont.setRtfDocument(this.rtfDoc); 145 this.importFontMapping.put(fontNr, Integer.toString(this.rtfDoc.getDocumentHeader().getFontNumber(rtfFont))); 146 return true; 147 } 148 /** 149 * Imports a font. The font name is looked up in the RtfDocumentHeader and 150 * then the mapping from original font number to actual font number is added. 151 * 152 * @param fontNr The original font number. 153 * @param fontName The font name to look up. 154 * @param charset The character set to use for the font. 155 */ importFont(String fontNr, String fontName, String fontFamily, int charset)156 public boolean importFont(String fontNr, String fontName, String fontFamily, int charset) { 157 RtfFont rtfFont = new RtfFont(fontName); 158 159 if(charset>= 0) 160 rtfFont.setCharset(charset); 161 if(fontFamily != null && fontFamily.length() > 0) 162 rtfFont.setFamily(fontFamily); 163 rtfFont.setRtfDocument(this.rtfDoc); 164 this.importFontMapping.put(fontNr, Integer.toString(this.rtfDoc.getDocumentHeader().getFontNumber(rtfFont))); 165 return true; 166 } 167 /** 168 * Performs the mapping from the original font number to the actual 169 * font number in the resulting RTF document. If the font number was not 170 * seen during import (thus no mapping) then 0 is returned, guaranteeing 171 * that the font number is always valid. 172 * 173 * @param fontNr The font number to map. 174 * @return The mapped font number. 175 */ mapFontNr(String fontNr)176 public String mapFontNr(String fontNr) { 177 if(this.importFontMapping.containsKey(fontNr)) { 178 return (String) this.importFontMapping.get(fontNr); 179 } else { 180 return "0"; 181 } 182 } 183 184 /** 185 * Imports a color value. The color number for the color defined 186 * by its red, green and blue values is determined and then the 187 * resulting mapping is added. 188 * 189 * @param colorNr The original color number. 190 * @param color The color to import. 191 */ importColor(String colorNr, Color color)192 public void importColor(String colorNr, Color color) { 193 RtfColor rtfColor = new RtfColor(this.rtfDoc, color); 194 this.importColorMapping.put(colorNr, Integer.toString(rtfColor.getColorNumber())); 195 } 196 197 /** 198 * Performs the mapping from the original font number to the actual font 199 * number used in the RTF document. If the color number was not 200 * seen during import (thus no mapping) then 0 is returned, guaranteeing 201 * that the color number is always valid. 202 * 203 * @param colorNr The color number to map. 204 * @return The mapped color number 205 */ mapColorNr(String colorNr)206 public String mapColorNr(String colorNr) { 207 if(this.importColorMapping.containsKey(colorNr)) { 208 return (String) this.importColorMapping.get(colorNr); 209 } else { 210 return "0"; 211 } 212 } 213 214 /** 215 * Imports a List value. The List number for the List defined 216 * is determined and then the resulting mapping is added. 217 */ importList(String origListNr, String newListNr)218 public void importList(String origListNr, String newListNr) { 219 this.importListMapping.put(origListNr, newListNr); 220 } 221 222 /** 223 * Performs the mapping from the original list number to the actual 224 * list number in the resulting RTF document. If the list number was not 225 * seen during import (thus no mapping) then null is returned. There is no 226 * guarantee of a valid list number. 227 */ mapListNr(String listNr)228 public String mapListNr(String listNr) { 229 if(this.importListMapping.containsKey(listNr)) { 230 return (String) this.importListMapping.get(listNr); 231 } else { 232 return null; 233 } 234 } 235 236 /** 237 * Imports a stylesheet list value. The stylesheet number for the stylesheet defined 238 * is determined and then the resulting mapping is added. 239 */ importStylesheetList(String listNr, List listIn)240 public boolean importStylesheetList(String listNr, List listIn) { 241 RtfList rtfList = new RtfList(this.rtfDoc, listIn); 242 rtfList.setRtfDocument(this.rtfDoc); 243 // TODO HGS - Finish implementation of import 244 //this.importStylesheetListMapping.put(listNr, Integer.toString(this.rtfDoc.getDocumentHeader().getRtfParagraphStyle(styleName)(rtfList))); 245 return true; 246 } 247 /** 248 * Performs the mapping from the original stylesheet number to the actual 249 * stylesheet number in the resulting RTF document. If the stylesheet number was not 250 * seen during import (thus no mapping) then 0 is returned, guaranteeing 251 * that the stylesheet number is always valid. 252 */ mapStylesheetListNr(String listNr)253 public String mapStylesheetListNr(String listNr) { 254 if(this.importStylesheetListMapping.containsKey(listNr)) { 255 return (String) this.importStylesheetListMapping.get(listNr); 256 } else { 257 return "0"; 258 } 259 } 260 261 } 262