KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > rtf > direct > RtfImportHeader


1 /**
2  * $Id: RtfImportHeader.java 2337 2006-08-28 11:21:15Z blowagie $
3  * $Name$
4  *
5  * Copyright 2006 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-2006 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-2006 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 package com.lowagie.text.rtf.direct;
51
52 import java.awt.Color JavaDoc;
53 import java.util.HashMap JavaDoc;
54
55 import com.lowagie.text.rtf.document.RtfDocument;
56 import com.lowagie.text.rtf.style.RtfColor;
57 import com.lowagie.text.rtf.style.RtfFont;
58
59 /**
60  * The RtfImportHeader stores the docment header information from
61  * an RTF document that is being imported. Currently font and
62  * color settings are stored. The RtfImportHeader maintains a mapping
63  * from font and color numbers from the imported RTF document to
64  * the RTF document that is the target of the import. This guarantees
65  * that the merged document has the correct font and color settings.
66  *
67  * @version $Revision: 2337 $
68  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
69  */

70 public class RtfImportHeader {
71     /**
72      * The HashMap storing the font number mappings.
73      */

74     private HashMap JavaDoc importFontMapping = null;
75     /**
76      * The HashMap storing the color number mapings.
77      */

78     private HashMap JavaDoc importColorMapping = null;
79     /**
80      * The RtfDocument to get font and color numbers from.
81      */

82     private RtfDocument rtfDoc = null;
83     
84     /**
85      * Constructs a new RtfImportHeader.
86      *
87      * @param rtfDoc The RtfDocument to get font and color numbers from.
88      */

89     public RtfImportHeader(RtfDocument rtfDoc) {
90         this.rtfDoc = rtfDoc;
91         this.importFontMapping = new HashMap JavaDoc();
92         this.importColorMapping = new HashMap JavaDoc();
93     }
94     
95     /**
96      * Imports a font. The font name is looked up in the RtfDocumentHeader and
97      * then the mapping from original font number to actual font number is added.
98      *
99      * @param fontNr The original font number.
100      * @param fontName The font name to look up.
101      */

102     public void importFont(String JavaDoc fontNr, String JavaDoc fontName) {
103         RtfFont rtfFont = new RtfFont(fontName);
104         rtfFont.setRtfDocument(this.rtfDoc);
105         this.importFontMapping.put(fontNr, Integer.toString(this.rtfDoc.getDocumentHeader().getFontNumber(rtfFont)));
106     }
107     
108     /**
109      * Performs the mapping from the original font number to the actual
110      * font number in the resulting RTF document. If the font number was not
111      * seen during import (thus no mapping) then 0 is returned, guaranteeing
112      * that the font number is always valid.
113      *
114      * @param fontNr The font number to map.
115      * @return The mapped font number.
116      */

117     public String JavaDoc mapFontNr(String JavaDoc fontNr) {
118         if(this.importFontMapping.containsKey(fontNr)) {
119             return (String JavaDoc) this.importFontMapping.get(fontNr);
120         } else {
121             return "0";
122         }
123     }
124     
125     /**
126      * Imports a color value. The color number for the color defined
127      * by its red, green and blue values is determined and then the
128      * resulting mapping is added.
129      *
130      * @param colorNr The original color number.
131      * @param color The color to import.
132      */

133     public void importColor(String JavaDoc colorNr, Color JavaDoc color) {
134         RtfColor rtfColor = new RtfColor(this.rtfDoc, color);
135         this.importColorMapping.put(colorNr, Integer.toString(rtfColor.getColorNumber()));
136     }
137     
138     /**
139      * Performs the mapping from the original font number to the actual font
140      * number used in the RTF document. If the color number was not
141      * seen during import (thus no mapping) then 0 is returned, guaranteeing
142      * that the color number is always valid.
143      *
144      * @param colorNr The color number to map.
145      * @return The mapped color number
146      */

147     public String JavaDoc mapColorNr(String JavaDoc colorNr) {
148         if(this.importColorMapping.containsKey(colorNr)) {
149             return (String JavaDoc) this.importColorMapping.get(colorNr);
150         } else {
151             return "0";
152         }
153     }
154 }
155
Popular Tags