1 package org.columba.core.print; 17 18 import java.awt.Graphics2D ; 19 import java.lang.reflect.Array ; 20 import java.lang.reflect.Method ; 21 import java.text.DateFormat ; 22 import java.util.Date ; 23 24 30 public class cPrintVariable extends cParagraph { 31 String codeString; 32 CodeStringParser[] parser; 33 int parserCount = -1; 34 String [] keys = { "PAGE_NR", "PAGE_COUNT", "DATE_TODAY" }; 35 String [] methods = { "getPageNr", "getPageCount", "getDateToday" }; 36 37 40 public cPrintVariable() { 41 Class c = this.getClass(); 42 parserCount = Array.getLength(keys); 43 parser = new CodeStringParser[parserCount]; 44 45 for (int i = 0; i < parserCount; i++) { 46 try { 47 parser[i] = new CodeStringParser(this, keys[i], c.getMethod( 48 methods[i], new Class [0])); 49 } catch (NoSuchMethodException e) { 50 e.printStackTrace(); 51 } 52 } 53 } 54 55 58 59 64 public String getPageNr() { 65 return Integer.toString(page.getDocument().getPageNr(page)); 66 } 67 68 73 public String getPageCount() { 74 return Integer.toString(page.getDocument().getPageCount()); 75 } 76 77 82 public String getDateToday() { 83 DateFormat df = DateFormat.getDateInstance(); 84 85 return df.format(new Date ()); 86 } 87 88 93 public void setCodeString(String n) { 94 codeString = n; 95 setText(n); } 97 98 101 public void print(Graphics2D g) { 102 setText(processCodeString()); 103 super.print(g); 104 } 105 106 110 public String processCodeString() { 111 StringBuffer result = new StringBuffer (); 112 boolean isDecoding = false; 113 char c; 114 115 for (int i = 0; i < codeString.length(); i++) { 116 c = codeString.charAt(i); 117 118 if (c == '%') { 119 isDecoding = !isDecoding; 120 121 if (!isDecoding) { 122 for (int j = 0; j < parserCount; j++) { 123 parser[j].reset(); 124 } 125 } 126 } else { 127 if (isDecoding) { 128 for (int j = 0; j < parserCount; j++) { 129 if (parser[j].clock(c)) { 130 result.append(parser[j].getValue()); 131 } 132 } 133 } else { 134 result.append(c); 135 } 136 } 137 } 138 return result.toString(); 139 } 140 } 141 142 148 class CodeStringParser { 149 Method hit; 150 char[] match; 151 int length; 152 Object parent; 153 int pos; 154 155 public CodeStringParser(Object p, String m, Method h) { 156 parent = p; 157 match = m.toCharArray(); 158 length = m.length(); 159 hit = h; 160 pos = 0; 161 } 162 163 170 public boolean clock(char in) { 171 if (in == match[pos]) { 172 pos++; 173 174 if (pos == length) { 175 pos = 0; 176 return true; 177 } 178 } else { 179 pos = 0; 180 } 181 return false; 182 } 183 184 189 public String getValue() { 190 try { 191 return (String ) hit.invoke((Object ) parent, (Object ) null); 192 } catch (Exception e) { 193 return new String ("<Unhandled Exception occcured>"); 194 } 195 } 196 197 200 public void reset() { 201 pos = 0; 202 } 203 } 204 | Popular Tags |