| 1 28 29 package com.idaremedia.antx.print; 30 31 import java.io.BufferedOutputStream ; 32 import java.io.IOException ; 33 import java.io.OutputStream ; 34 35 import org.apache.tools.ant.BuildException; 36 import org.apache.tools.ant.Project; 37 38 import com.idaremedia.antx.AntX; 39 import com.idaremedia.antx.AssertableDataType; 40 import com.idaremedia.antx.helpers.Strings; 41 import com.idaremedia.antx.helpers.Tk; 42 43 68 69 public class AnyPrinter extends AssertableDataType implements DisplayStrategy 70 { 71 74 public AnyPrinter() 75 { 76 super(AntX.print); 77 } 78 79 80 84 public void setId(String id) 85 { 86 m_Id= id; 87 } 88 89 90 93 public final String getId() 94 { 95 if (m_Id!=null) { 96 return m_Id; 97 } 98 if (isReference()) { 99 return getPrinterRef().getId(); 100 } 101 return super.getId(); 102 } 103 104 105 111 public void setSplitEntries(boolean splitEm) 112 { 113 if (isReference()) { 114 throw tooManyAttributes(); 115 } 116 m_forceNL = splitEm; 117 edited("splitEntries"); 118 } 119 120 121 126 public boolean willSplitEntries() 127 { 128 if (isReference()) { 129 return getPrinterRef().willSplitEntries(); 130 } 131 return m_forceNL; 132 } 133 134 135 136 143 public void setClassName(String classname) 144 { 145 require_(classname!=null,"setClaz- nonzro name"); 146 if (isReference()) { 147 throw tooManyAttributes(); 148 } 149 if (!AnyPrinter.class.getName().equals(classname)) { try { 151 Class strategyClass = Class.forName(classname); 152 m_printWorker = (DisplayStrategy)strategyClass.newInstance(); 153 154 } catch (Exception anyX) { 155 String ermsg = uistrs().get("printer.bad.impl.class", 156 getId(), classname); 157 log(ermsg, Project.MSG_ERR); 158 throw new BuildException(ermsg,anyX); 159 } 160 } 161 edited("setClassName"); 162 } 163 164 165 171 public final String getStrategyClassName() 172 { 173 if (isReference()) { 174 return getPrinterRef().getStrategyClassName(); 175 } 176 return m_printWorker!=null ? 177 m_printWorker.getClass().getName() : 178 AnyPrinter.class.getName(); 179 } 180 181 182 183 192 public String stringFrom(Object thing) 193 { 194 if (isReference()) { 195 return getPrinterRef().stringFrom(thing); 196 } 197 return Tk.stringFrom(thing,getProject()); 198 } 199 200 201 206 public void print(DisplayRequest req, OutputStream out) 207 throws IOException , BuildException 208 { 209 if (isReference()) { 210 getPrinterRef().print(req,out); 211 212 } else if (m_printWorker!=null) { 213 m_printWorker.print(req,out); 214 215 } else { 216 String s = stringFrom(req.getObjectToBeDisplayed()); 217 ensure_(s!=null,"print- strFrm is non-nul"); 218 219 BufferedOutputStream bout; 220 if (out instanceof BufferedOutputStream ) { 221 bout = (BufferedOutputStream )out; 222 } else { 223 bout = new BufferedOutputStream (out,s.length()); } 225 226 if (willSplitEntries()) { 227 bout.write(NL_); 228 } 229 230 byte[] raw = s.getBytes(); 231 bout.write(raw,0,raw.length); 232 bout.flush(); 233 234 raw = null; 236 s = null; 237 bout = null; 238 } 239 } 240 241 242 245 protected final AnyPrinter getPrinterRef() 246 { 247 return (AnyPrinter)getCheckedRef(AnyPrinter.class,"defaultprinter"); 248 } 249 250 251 private String m_Id; 252 private DisplayStrategy m_printWorker; 253 private boolean m_forceNL=true; 254 private static final byte[] NL_= Strings.NL.getBytes(); 255 } 256 257 258 | Popular Tags |