View Javadoc
1 /* 2 * Copyright (c) 2003 3 * Information Desire GmbH 4 * All rights reserved. 5 */ 6 package com.infodesire.infobit.external.impl; 7 8 import com.infodesire.infobit.InfobitException; 9 10 import com.infodesire.infobit.data.Content; 11 import com.infodesire.infobit.data.Version; 12 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 16 import java.io.InputStream; 17 18 import java.util.Calendar; 19 import java.util.Date; 20 import java.util.Properties; 21 22 /*** 23 * Factorizes computations required in export templates. 24 * 25 * @author peter2 26 * @created August 26, 2003 27 * @version $Revision: 1.2 $ 28 */ 29 public class ExportRenderHelper { 30 31 private final static Log _log = 32 LogFactory.getLog(ExportRenderHelper.class); 33 34 /*** 35 * Class path to properties file mapping version content class names (names 36 * of classes derived from {@link Content}) to templates rendering the 37 * specific content 38 */ 39 private final static String TEMPLATES_PATH = 40 "/com/infodesire/infobit/external/impl/templateByContent.properties"; 41 42 /*** 43 * Maps content version class names to export rendering templates. 44 */ 45 private Properties _templateByContent = new Properties(); 46 47 48 /*** 49 * Dispatches the dynamic content class of the specified version to the 50 * appropriate template to externalize the content. 51 * 52 * @param version The version to externalize 53 * @return The path of a template to render the content of <code>version</code> 54 * , with respect to the classpath 55 */ 56 public String getTemplate(Version version) { 57 Content content = version.getContent(); 58 String cn = content.getClass().getName(); 59 String tp = _templateByContent.getProperty(cn); 60 if (tp == null) { 61 throw new RuntimeException("Illegal content: " + cn); 62 } 63 64 return tp; 65 } 66 67 68 /*** 69 * Encodes binary data to base64. 70 * 71 * @param content The content to render 72 * @return Description of the Returned Value 73 * @exception InfobitException Description of Exception 74 */ 75 public String encodeBase64(InputStream content) throws InfobitException { 76 try { 77 StringBuffer text = new StringBuffer(); 78 // encoded content 79 byte[] binary = new byte[24 / 8]; 80 char[] ascii = new char[24 / 6]; 81 int l = 0; 82 // number of ascii chunks in current line 83 84 int n = 0; 85 while ((n = content.read(binary)) > -1) { 86 int i = 0; 87 // Next byte in ascii chunk to fill 88 for (; 6 * i < 8 * n; ++i) { 89 int j = 6 * i / 8; 90 int k = 6 * i % 8; 91 int low = binary[j]; 92 int high = j < n ? binary[j + 1] : 0; 93 int c = (low >>> k | high << (8 - k)) & 0x7f; 94 ascii[i] = toBase64(c); 95 } 96 97 for (; i < ascii.length; ++i) { 98 ascii[i] = '='; 99 } 100 101 text.append(ascii); 102 ++l; 103 104 if (l == 64 / ascii.length) { 105 l = 0; 106 text.append(System.getProperty("line.separator")); 107 } 108 } 109 110 if (l > 0) { 111 text.append(System.getProperty("line.separator")); 112 } 113 114 return text.toString(); 115 } catch (Exception e) { 116 _log.error("cannot read binary data", e); 117 throw new InfobitException(e); 118 } 119 } 120 121 122 /*** 123 * Gets the year of the specified date. 124 * 125 * @param date The date for which to get the field 126 * @return The year of <code>date</code> according to the Gregorian 127 * calendar 128 */ 129 public String formatYear(Date date) { 130 Calendar cal = Calendar.getInstance(); 131 int y = cal.get(Calendar.YEAR); 132 return Integer.toString(y); 133 } 134 135 136 /*** 137 * Gets the the month of the specified date. 138 * 139 * @param date The date for which to get the field 140 * @return The number the month of <code>date</code> according to the 141 * Gregorian calendar, ranging from 1 to 12 142 */ 143 public String formatMonth(Date date) { 144 Calendar cal = Calendar.getInstance(); 145 int m = cal.get(Calendar.MONTH); 146 return Integer.toString(m); 147 } 148 149 150 /*** 151 * Gets the day-of-month of the specified date. 152 * 153 * @param date The date for which to get the field 154 * @return The day of month of <code>date</code> according to the 155 * Gregorian calendar, ranging from 1 to (atmost) 31 156 */ 157 public String formatDay(Date date) { 158 Calendar cal = Calendar.getInstance(); 159 int d = cal.get(Calendar.DAY_OF_MONTH); 160 return Integer.toString(d); 161 } 162 163 164 /*** 165 * Initializes an instance ready for use. 166 * 167 * @exception InfobitException Description of Exception 168 */ 169 void init() throws InfobitException { 170 try { 171 InputStream tn = getClass().getResourceAsStream(TEMPLATES_PATH); 172 _templateByContent.load(tn); 173 } catch (Exception e) { 174 _log.error("cannot initialize from properties", e); 175 throw new InfobitException(e); 176 } 177 } 178 179 180 /*** 181 * Replaces all occurences of angle brackets and the ampers-and within the 182 * specified string by the respective predefined XML general entitiy 183 * references. 184 * 185 * @param text The text to escape 186 * @return <code>text</code> with all angle brackets and amper ands 187 * replaced by general entity references 188 */ 189 String escapeXML(String text) { 190 String s = text.replaceAll("&", "&"); 191 s = s.replaceAll("<", "<"); 192 s = s.replaceAll(">", ">"); 193 194 return s; 195 } 196 197 198 /*** 199 * Looks up a character from the base64 alphabet. The base64 alphabet is 200 * defined by e.g. RFC 3548. 201 * 202 * @param pos Position of the character to look up. Constrained to the 203 * range 0 .. 64 204 * @return Description of the Returned Value 205 */ 206 private char toBase64(int pos) { 207 int b; 208 209 if (pos < 0) { 210 throw new IllegalArgumentException("pos: " + pos); 211 } 212 else if (pos < 26) { 213 b = 'A' + pos; 214 } 215 else if (pos < 52) { 216 b = 'a' + pos - 26; 217 } 218 else if (pos < 62) { 219 b = '0' + pos - 52; 220 } 221 else if (pos == 62) { 222 b = '+'; 223 } 224 else if (pos == 63) { 225 b = '/'; 226 } 227 else { 228 throw new IllegalArgumentException("pos: " + pos); 229 } 230 231 return (char) b; 232 } 233 234 }

This page was automatically generated by Maven