1 50 51 package com.lowagie.text.rtf.document; 52 53 import java.io.ByteArrayOutputStream ; 54 import java.io.IOException ; 55 import java.io.OutputStream ; 56 import java.text.ParseException ; 57 import java.text.SimpleDateFormat ; 58 import java.util.Date ; 59 60 import com.lowagie.text.Meta; 61 import com.lowagie.text.rtf.RtfElement; 62 63 64 72 public class RtfInfoElement extends RtfElement { 73 74 77 private static final byte[] INFO_AUTHOR = "\\author".getBytes(); 78 81 private static final byte[] INFO_SUBJECT = "\\subject".getBytes(); 82 85 private static final byte[] INFO_KEYWORDS = "\\keywords".getBytes(); 86 89 private static final byte[] INFO_TITLE = "\\title".getBytes(); 90 93 private static final byte[] INFO_PRODUCER = "\\operator".getBytes(); 94 97 private static final byte[] INFO_CREATION_DATE = "\\creationdate".getBytes(); 98 99 102 private int infoType = -1; 103 106 private String content = ""; 107 108 114 public RtfInfoElement(RtfDocument doc, Meta meta) { 115 super(doc); 116 infoType = meta.type(); 117 content = meta.getContent(); 118 } 119 120 126 public byte[] write() 127 { 128 ByteArrayOutputStream result = new ByteArrayOutputStream (); 129 try { 130 writeContent(result); 131 } catch(IOException ioe) { 132 ioe.printStackTrace(); 133 } 134 return result.toByteArray(); 135 } 136 137 140 public void writeContent(final OutputStream result) throws IOException 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 182 private String convertDate(String date) { 183 SimpleDateFormat sdf = new SimpleDateFormat ("EEE MMM dd HH:mm:ss zzz yyyy"); 184 try { 185 Date creationDate = sdf.parse(date); 186 sdf = new SimpleDateFormat ("\\'yr'yyyy\\'mo'MM\\'dy'dd\\'hr'HH\\'min'mm\\'sec'ss"); 187 return sdf.format(creationDate); 188 } catch(ParseException pe) { 189 pe.printStackTrace(); 190 return ""; 191 } 192 } 193 } 194 | Popular Tags |