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 import java.util.Hashtable ; 35 import java.util.Iterator ; 36 37 import org.apache.tools.ant.Project; 38 39 import com.idaremedia.antx.AntX; 40 import com.idaremedia.antx.FixtureExaminer; 41 import com.idaremedia.antx.helpers.Strings; 42 import com.idaremedia.antx.helpers.Tk; 43 44 67 68 public class PrintTask extends EchoItemsTask 69 { 70 73 public PrintTask() 74 { 75 super(AntX.print); 76 } 77 78 79 83 protected PrintTask(String iam) 84 { 85 super(iam); 86 } 87 88 89 93 public void setWith(String with) 94 { 95 require_(with!=null,"setwith- nonzro refid"); 96 m_with = with; 97 } 98 99 100 105 public final String getWith() 106 { 107 return m_with; 108 } 109 110 111 112 119 public void setFilter(String filterstring) 120 { 121 m_filterstring = filterstring; 122 } 123 124 125 130 public final String getFilterString() 131 { 132 return m_filterstring; 133 } 134 135 136 137 142 private PrinterRegistry getDefaultPrinterRegistry() 143 { 144 return PrinterRegistryContext.getPrinterRegistryNoNull(); 145 } 146 147 148 152 protected void printReferencesWith(PrinterMapping mapping, 153 final Project P, String list, 154 OutputStream os) 155 throws IOException 156 { 157 require_(mapping!=null,"print- nonzro mappin"); 158 159 PrinterRegistry registry = getDefaultPrinterRegistry(); 160 DisplayStrategy printer = mapping.getPrinter(); 161 162 Class filterClass = getKindOfFilterClass(); 163 String cantprint; 164 165 Iterator keysitr; 166 if (Strings.ALL.equals(Tk.lowercaseFrom(list))) { 167 Hashtable copy = (Hashtable )P.getReferences().clone(); 168 keysitr= copy.keySet().iterator(); 169 } else { 170 keysitr= Tk.splitList(list).iterator(); 171 } 172 173 BufferedOutputStream bos = new BufferedOutputStream (os,1024); 174 DisplayStrategy defaultPrinter= registry.getDefaultPrinter(); 175 176 while (keysitr.hasNext()) { 177 String key = (String )keysitr.next(); 178 179 Object object = FixtureExaminer.trueReference(P,key); if (object==FixtureExaminer.IGNORED_REFERENCE) { 181 if (!willIncludeUnknowns()) { 182 continue; 183 } 184 object = unresolvedString(P,key); 185 } 186 187 if (filterClass==null || object==null || 188 (filterClass.isInstance(object))) { 189 190 DisplayRequest dr = new DisplayRequest(P,key,object); 191 dr.setFilter(getFilterString()); 192 193 if (!mapping.isMatch(object)) { 194 cantprint = uistrs().get("printer.cant.print.item",mapping.getId(),key); 195 log(cantprint,Project.MSG_WARN); 196 defaultPrinter.print(dr,bos); 197 } else { 198 printer.print(dr,bos); 199 } 200 dr = null; 201 } } 204 keysitr=null; 205 } 206 207 208 209 213 protected void printReferencesWith(PrinterRegistry registry, 214 final Project P, String list, 215 OutputStream os) 216 throws IOException 217 { 218 require_(registry!=null,"print- nonzro registry"); 219 220 Class filterClass = getKindOfFilterClass(); 221 222 Iterator keysitr; 223 if (Strings.ALL.equals(Tk.lowercaseFrom(list))) { 224 Hashtable copy = (Hashtable )P.getReferences().clone(); 225 keysitr= copy.keySet().iterator(); 226 } else { 227 keysitr= Tk.splitList(list).iterator(); 228 } 229 230 BufferedOutputStream bos = new BufferedOutputStream (os,1024); 231 PrinterMapping prevHandler=null; 232 DisplayStrategy printer=null; 233 DisplayStrategy defaultPrinter= registry.getDefaultPrinter(); 234 235 while (keysitr.hasNext()) { 236 String key = (String )keysitr.next(); 237 238 Object object = FixtureExaminer.trueReference(P,key); if (object==FixtureExaminer.IGNORED_REFERENCE) { 240 if (!willIncludeUnknowns()) { 241 continue; 242 } 243 object = unresolvedString(P,key); 244 } 245 246 if (filterClass==null || object==null || 247 (filterClass.isInstance(object))) { 248 249 if (prevHandler!=null && prevHandler.isMatch(object)) { 250 printer= prevHandler.getPrinter(); 251 } else { 252 PrinterMapping handler= registry.findMappingFor(object); 253 if (handler==null) { 254 printer = defaultPrinter; 255 } else { 256 printer = handler.getPrinter(); 257 prevHandler = handler; 258 } 259 } 260 DisplayRequest dr = new DisplayRequest(P,key,object); 261 dr.setFilter(getFilterString()); 262 printer.print(dr,bos); 263 dr = null; 264 } } 267 keysitr=null; 268 } 269 270 271 277 private boolean tryCustomDisplay() 278 { 279 PrinterRegistry dfltPR= getDefaultPrinterRegistry(); 280 if (dfltPR!=PrinterRegistryContext.getEmptyPrinterRegistry()) { 281 return true; 282 } 283 return false; 284 } 285 286 287 290 protected boolean echoReferences(final Project P, OutputStream os) 291 throws IOException 292 { 293 String list= getReferencesNameList(); 294 if (list!=null) { 295 String with = getWith(); 296 if (with!=null) { 297 Object withObject = P.getReference(with); 298 if (withObject instanceof PrinterRegistry) { 299 printReferencesWith((PrinterRegistry)withObject,P,list,os); 300 } else if (withObject instanceof PrinterMapping) { 301 printReferencesWith((PrinterMapping)withObject,P,list,os); 302 } else { 303 String ermsg = uistrs().get("print.bad.with",with); 304 log(ermsg, Project.MSG_WARN); 305 if (tryCustomDisplay()) { 306 printReferencesWith(getDefaultPrinterRegistry(),P,list,os); 307 } else { 308 return super.echoReferences(P,os); 309 } 310 } 311 return true; 312 } else if (tryCustomDisplay()) { 313 printReferencesWith(getDefaultPrinterRegistry(),P,list,os); 314 } else { 315 return super.echoReferences(P,os); 316 } 317 } 318 return false; 319 } 320 321 322 323 private String m_with; private String m_filterstring;} 326 327 328 | Popular Tags |