1 28 29 package com.idaremedia.antx.print; 30 31 import java.io.File ; 32 import java.io.IOException ; 33 import java.net.MalformedURLException ; 34 import java.net.URL ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.HashSet ; 38 import java.util.Properties ; 39 import java.util.Set ; 40 41 import org.apache.tools.ant.BuildException; 42 import org.apache.tools.ant.Project; 43 import com.idaremedia.antx.AntX; 44 import com.idaremedia.antx.AntXFixture; 45 import com.idaremedia.antx.AssertableDataType; 46 import com.idaremedia.antx.FixtureComponent; 47 import com.idaremedia.antx.apis.AntLibFriendly; 48 import com.idaremedia.antx.helpers.InnerString; 49 import com.idaremedia.antx.helpers.InputFileLoader; 50 import com.idaremedia.antx.helpers.Tk; 51 import com.idaremedia.antx.ownhelpers.LocalTk; 52 import com.idaremedia.antx.parameters.FlexSourceSupport; 53 54 90 91 public class PrinterMapping extends AssertableDataType 92 implements Cloneable , FixtureComponent, FlexSourceSupport, AntLibFriendly 93 { 94 99 public static final String RESOURCE_KEY= "CLASSNAME"; 100 101 102 103 106 public PrinterMapping() 107 { 108 super(AntX.print); 109 } 110 111 112 117 public Object clone() 118 { 119 if (isReference()) { 120 return getPrinterRef().clone(); 121 } 122 try { 123 PrinterMapping copy = (PrinterMapping)super.clone(); 124 copy.m_targetClasses = (HashSet )m_targetClasses.clone(); 125 return copy; 126 } catch(CloneNotSupportedException clnx) { 127 throw new Error (uistrs().get(AntX.CLONE_BROKEN_MSGID)); 128 } 129 } 130 131 132 136 public void setId(String id) 137 { 138 m_Id= id; 139 } 140 141 142 145 public final String getId() 146 { 147 if (m_Id!=null) { 148 return m_Id; 149 } 150 if (isReference()) { 151 return getPrinterRef().getId(); 152 } 153 return super.getId(); 154 } 155 156 157 164 public boolean isFullyDefined() 165 { 166 if (isReference()) { 167 return getPrinterRef().isFullyDefined(); 168 } 169 return m_printWorker!=null && !m_targetClasses.isEmpty(); 170 } 171 172 173 174 182 public void setClassName(Class clas) 183 { 184 require_(clas!=null,"setClaz- nonzro clsid"); 185 if (isReference()) { 186 throw tooManyAttributes(); 187 } 188 try { 189 m_printWorker = (DisplayStrategy)clas.newInstance(); 190 } catch(Exception anyX) { 191 String ermsg = uistrs().get("printer.bad.impl.class", 192 getId(), clas.getName()); 193 log(ermsg,Project.MSG_ERR); 194 throw new BuildException(ermsg,anyX); 195 } 196 edited("setClassName"); 197 } 198 199 200 205 public void setProperty(String property) 206 { 207 require_(property!=null,"setProp- nonzro prop nam"); 208 if (isReference()) { 209 throw tooManyAttributes(); 210 } 211 String classname = getProject().getProperty(property); 212 if (classname!=null) { 213 try { 214 setClassName(Class.forName(classname)); 215 } catch(Exception anyX) { 216 String ermsg = uistrs().get("printer.bad.property", 217 getId(), property, classname); 218 log(ermsg,Project.MSG_ERR); 219 throw new BuildException(ermsg,anyX); 220 } 221 } else { 222 String ermsg = uistrs().get("task.undef.property",property); 223 log(ermsg,Project.MSG_ERR); 224 throw new BuildException(ermsg); 225 } 226 } 227 228 229 230 235 public final String getPrinterClassName() 236 { 237 if (isReference()) { 238 return getPrinterRef().getPrinterClassName(); 239 } 240 return m_printWorker!=null ? 241 m_printWorker.getClass().getName() : 242 null; 243 } 244 245 246 247 252 public void addConfiguredForClass(InnerString clsinfo) 253 { 254 require_(clsinfo!=null,"addForClaz- nonzro valu"); 255 if (isReference()) { 256 throw tooManyAttributes(); 257 } 258 String classname = clsinfo.getText(); 259 try { 260 addTargetClass(Class.forName(classname)); 261 } catch(Exception anyX) { 262 String ermsg = uistrs().get("printer.bad.item.class", 263 getId(), classname); 264 log(ermsg,Project.MSG_ERR); 265 throw new BuildException(ermsg,anyX); 266 } 267 edited("addForClass"); 268 } 269 270 271 272 277 public void addConfiguredForType(InnerString typedefItem) 278 { 279 require_(typedefItem!=null,"addForTyp- nonzro typdef"); 280 if (isReference()) { 281 throw tooManyAttributes(); 282 } 283 String defname = typedefItem.getText(); 284 Class claz = (Class )getProject().getDataTypeDefinitions().get(defname); 285 if (claz==null) { 286 String ermsg = uistrs().get("printer.bad.typedef", getId(), defname); 287 log(ermsg,Project.MSG_ERR); 288 throw new BuildException(ermsg); 289 } 290 addTargetClass(claz); 291 edited("addForTypedef"); 292 } 293 294 295 301 private void setFromPropertiesURL(URL url, String from) 302 { 303 Properties p; 304 try { 305 p= InputFileLoader.loadProperties(url,null); 306 } catch(IOException iox) { 307 String error = uistrs().get("printer.bad.source",getId(),from); 308 log(error,Project.MSG_ERR); 309 throw new BuildException(error,iox); 310 } 311 String clsid = p.getProperty(RESOURCE_KEY); 312 if (clsid!=null) { 313 try { 314 setClassName(Class.forName(clsid)); 315 } catch (Exception anyX) { 316 String error = uistrs().get("printer.bad.impl.class", getId(),clsid); 317 log(error,Project.MSG_ERR); 318 throw new BuildException(error,anyX); 319 } 320 p.remove(RESOURCE_KEY); 321 if (!p.isEmpty()) { 322 Iterator itr= p.values().iterator(); 323 while (itr.hasNext()) { 324 String classname = itr.next().toString(); 325 try { 326 addTargetClass(Class.forName(classname)); 327 } catch(Exception anyX) { 328 String error = uistrs().get("printer.bad.item.class", 329 getId(), classname); 330 log(error,Project.MSG_ERR); 331 throw new BuildException(error,anyX); 332 } 333 } } 335 } else { 336 String error = uistrs().get("printer.bad.source.class",getId(),from); 337 log(error,Project.MSG_ERR); 338 throw new BuildException(error); 339 } 340 p.clear(); 341 p=null; 342 } 343 344 345 357 public void setResource(String rn) 358 { 359 require_(rn!=null,"setrez- nonzro nam"); 360 if (isReference()) { 361 throw tooManyAttributes(); 362 } 363 URL url = LocalTk.getSystemResource(rn, getProject()); 364 if (url!=null) { 365 setFromPropertiesURL(url,rn); 366 } else { 367 String warning = uistrs().get("printer.bad.source",getId(),rn); 368 log(warning,Project.MSG_WARN); 369 } 370 } 371 372 373 374 380 public void setFile(String filepath) 381 { 382 require_(filepath!=null,"setfile- nonzro filepath"); 383 if (isReference()) { 384 throw tooManyAttributes(); 385 } 386 try { 387 File file = getProject().resolveFile(filepath); 388 setFromPropertiesURL(AntXFixture.fileUtils().getFileURL(file),filepath); 389 } catch(MalformedURLException mux) { 390 String warning = uistrs().get("printer.bad.source",getId(),filepath); 391 log(warning,Project.MSG_WARN); 392 } 393 } 394 395 396 397 403 public final void setProjectFile(File file) 404 { 405 require_(file!=null,"setFile- nonzro file"); 406 if (isReference()) { 407 throw tooManyAttributes(); 408 } 409 try { 410 setFromPropertiesURL(AntXFixture.fileUtils().getFileURL(file),file.getPath()); 411 } catch(MalformedURLException mux) { 412 String warning = uistrs().get("printer.bad.source",getId(),file.getPath()); 413 log(warning,Project.MSG_WARN); 414 } 415 } 416 417 418 419 425 public void setURL(String urlstr) 426 { 427 require_(urlstr!=null,"setURL- nonzro URL"); 428 if (isReference()) { 429 throw tooManyAttributes(); 430 } 431 URL url = null; 432 try { 433 url = new URL (urlstr); 434 } catch(MalformedURLException mux) {} 435 if (url!=null) { 436 setFromPropertiesURL(url,urlstr); 437 } else { 438 String warning = uistrs().get("printer.bad.source",getId(),urlstr); 439 log(warning,Project.MSG_WARN); 440 } 441 } 442 443 444 445 452 public final void setForClass(String classlist) 453 { 454 require_(classlist!=null,"setforclaz- nonzro list"); 455 if (isReference()) { 456 throw tooManyAttributes(); 457 } 458 List l = Tk.splitList(classlist); 459 if (!l.isEmpty()) { 460 InnerString helper= new InnerString(); 461 for (int i=0,N=l.size();i<N;i++) { 462 helper.setValue((String )l.get(i)); 463 addConfiguredForClass(helper); 464 } 465 helper=null; 466 } 467 l=null; 468 } 469 470 471 479 public final void setForType(String typelist) 480 { 481 require_(typelist!=null,"setfortype- nonzro list"); 482 if (isReference()) { 483 throw tooManyAttributes(); 484 } 485 List l = Tk.splitList(typelist); 486 if (!l.isEmpty()) { 487 InnerString helper= new InnerString(); 488 for (int i=0,N=l.size();i<N;i++) { 489 helper.setValue((String )l.get(i)); 490 addConfiguredForType(helper); 491 } 492 helper=null; 493 } 494 l=null; 495 } 496 497 498 499 503 public DisplayStrategy getPrinter() 504 { 505 if (isReference()) { 506 return getPrinterRef().getPrinter(); 507 } 508 if (m_printWorker==null) { 509 String ermsg = uistrs().get("printer.undefined",getId()); 510 log(ermsg,Project.MSG_ERR); 511 throw new BuildException(ermsg); 512 } 513 return m_printWorker; 514 } 515 516 517 518 524 public boolean isMatch(Object instance) 525 { 526 if (isReference()) { 527 return getPrinterRef().isMatch(instance); 528 } 529 if (instance!=null && isFullyDefined()) { 530 Iterator itr= m_targetClasses.iterator(); 531 while (itr.hasNext()) { 532 Class clas = (Class )itr.next(); 533 if (clas.isInstance(instance)) { return true; 535 } 536 } 537 } 538 return false; 539 } 540 541 542 545 protected final PrinterMapping getPrinterRef() 546 { 547 return (PrinterMapping)getCheckedRef(PrinterMapping.class,"printer"); 548 } 549 550 551 552 557 Set getTargetClasses() 558 { 559 return m_targetClasses; 560 } 561 562 563 564 568 private void addTargetClass(Class claz) 569 { 570 if (!m_targetClasses.add(claz)) { 571 String warning = uistrs().get("printer.class.mapping.exists", 572 getId(), claz.getName()); 573 log(warning,Project.MSG_WARN); 574 } 575 } 576 577 private String m_Id; 578 private HashSet m_targetClasses= new HashSet (11); 579 private DisplayStrategy m_printWorker; 580 } 581 582 583 584 | Popular Tags |