KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > rtf > document > RtfInfoElement


1 /*
2  * $Id: RtfInfoElement.java 2776 2007-05-23 20:01:40Z hallm $
3  * $Name$
4  *
5  * Copyright 2003, 2004 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, 2000, 2001, 2002 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, 2001, 2002 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
51 package com.lowagie.text.rtf.document;
52
53 import java.io.ByteArrayOutputStream JavaDoc;
54 import java.io.IOException JavaDoc;
55 import java.io.OutputStream JavaDoc;
56 import java.text.ParseException JavaDoc;
57 import java.text.SimpleDateFormat JavaDoc;
58 import java.util.Date JavaDoc;
59
60 import com.lowagie.text.Meta;
61 import com.lowagie.text.rtf.RtfElement;
62
63
64 /**
65  * Stores one information group element. Valid elements are
66  * author, title, subject, keywords, producer and creationdate.
67  *
68  * @version $Id: RtfInfoElement.java 2776 2007-05-23 20:01:40Z hallm $
69  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
70  * @author Thomas Bickel (tmb99@inode.at)
71  */

72 public class RtfInfoElement extends RtfElement {
73
74     /**
75      * Constant for the author element
76      */

77     private static final byte[] INFO_AUTHOR = "\\author".getBytes();
78     /**
79      * Constant for the subject element
80      */

81     private static final byte[] INFO_SUBJECT = "\\subject".getBytes();
82     /**
83      * Constant for the keywords element
84      */

85     private static final byte[] INFO_KEYWORDS = "\\keywords".getBytes();
86     /**
87      * Constant for the title element
88      */

89     private static final byte[] INFO_TITLE = "\\title".getBytes();
90     /**
91      * Constant for the producer element
92      */

93     private static final byte[] INFO_PRODUCER = "\\operator".getBytes();
94     /**
95      * Constant for the creationdate element
96      */

97     private static final byte[] INFO_CREATION_DATE = "\\creationdate".getBytes();
98
99     /**
100      * The type of this RtfInfoElement. The values from Element.INFO_ELEMENT_NAME are used.
101      */

102     private int infoType = -1;
103     /**
104      * The content of this RtfInfoElement
105      */

106     private String JavaDoc content = "";
107     
108     /**
109      * Constructs a RtfInfoElement based on the given Meta object
110      *
111      * @param doc The RtfDocument this RtfInfoElement belongs to
112      * @param meta The Meta object this RtfInfoElement is based on
113      */

114     public RtfInfoElement(RtfDocument doc, Meta meta) {
115         super(doc);
116         infoType = meta.type();
117         content = meta.getContent();
118     }
119     
120     /**
121      * Writes this RtfInfoElement
122      *
123      * @return A byte array containing the RtfInfoElement data
124      * @deprecated replaced by {@link #writeContent(OutputStream)}
125      */

126     public byte[] write()
127     {
128         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
129         try {
130             writeContent(result);
131         } catch(IOException JavaDoc ioe) {
132             ioe.printStackTrace();
133         }
134         return result.toByteArray();
135     }
136     
137     /**
138      * Writes the element content to the given output stream.
139      */

140     public void writeContent(final OutputStream JavaDoc result) throws IOException JavaDoc
141     {
142         result.write(OPEN_GROUP);
143         switch(infoType) {
144             case Meta.AUTHOR:
145                 result.write(INFO_AUTHOR);
146                 break;
147             case Meta.SUBJECT:
148                 result.write(INFO_SUBJECT);
149                 break;
150             case Meta.KEYWORDS:
151                 result.write(INFO_KEYWORDS);
152                 break;
153             case Meta.TITLE:
154                 result.write(INFO_TITLE);
155                 break;
156             case Meta.PRODUCER:
157                 result.write(INFO_PRODUCER);
158                 break;
159             case Meta.CREATIONDATE:
160                 result.write(INFO_CREATION_DATE);
161                 break;
162             default:
163                 result.write(INFO_AUTHOR);
164                 break;
165         }
166         result.write(DELIMITER);
167         if(infoType == Meta.CREATIONDATE) {
168             result.write(convertDate(content).getBytes());
169         } else {
170             result.write(content.getBytes());
171         }
172         result.write(CLOSE_GROUP);
173     }
174     
175     /**
176      * Converts a date from the format used by iText to the format required by
177      * rtf.<br>iText: EEE MMM dd HH:mm:ss zzz yyyy - rtf: \\'yr'yyyy\\'mo'MM\\'dy'dd\\'hr'HH\\'min'mm\\'sec'ss
178      *
179      * @param date The date formated by iText
180      * @return The date formated for rtf
181      */

182     private String JavaDoc convertDate(String JavaDoc date) {
183         SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc("EEE MMM dd HH:mm:ss zzz yyyy");
184         try {
185             Date JavaDoc creationDate = sdf.parse(date);
186             sdf = new SimpleDateFormat JavaDoc("\\'yr'yyyy\\'mo'MM\\'dy'dd\\'hr'HH\\'min'mm\\'sec'ss");
187             return sdf.format(creationDate);
188         } catch(ParseException JavaDoc pe) {
189             pe.printStackTrace();
190             return "";
191         }
192     }
193 }
194
Popular Tags