1 14 package org.compiere.print; 15 16 import java.io.*; 17 import java.util.*; 18 19 import org.w3c.dom.*; 20 import javax.xml.parsers.*; 21 import javax.xml.transform.*; 22 import javax.xml.transform.stream.*; 23 import javax.xml.transform.dom.*; 24 import org.xml.sax.*; 25 import org.xml.sax.helpers.*; 26 27 import org.compiere.*; 28 import org.compiere.util.*; 29 30 41 public class PrintData implements Serializable 42 { 43 47 public PrintData (Properties ctx, String name) 48 { 49 if (name == null) 50 throw new IllegalArgumentException ("PrintData - Name cannot be null"); 51 m_ctx = ctx; 52 m_name = name; 53 } 55 60 public PrintData (Properties ctx, String name, ArrayList nodes) 61 { 62 if (name == null) 63 throw new IllegalArgumentException ("PrintData - Name cannot be null"); 64 m_ctx = ctx; 65 m_name = name; 66 if (nodes != null) 67 m_nodes = nodes; 68 } 70 71 private Properties m_ctx; 72 73 private String m_name; 74 75 private ArrayList m_rows = new ArrayList(); 76 77 private ArrayList m_nodes = null; 78 79 private int m_row = -1; 80 81 private ArrayList m_functionRows = new ArrayList(); 82 83 84 private boolean m_hasLevelNo = false; 85 86 private static final String LEVEL_NO = "LEVELNO"; 87 88 89 private PrintDataColumn[] m_columnInfo = null; 90 91 private String m_sql = null; 92 93 private String m_TableName = null; 94 95 96 public static final String XML_TAG = "compiereData"; 97 98 public static final String XML_ROW_TAG = "row"; 99 100 public static final String XML_ATTRIBUTE_NAME = "name"; 101 102 public static final String XML_ATTRIBUTE_COUNT = "count"; 103 104 public static final String XML_ATTRIBUTE_NO = "no"; 105 106 public static final String XML_ATTRIBUTE_FUNCTION_ROW = "function_row"; 107 108 112 public Properties getCtx() 113 { 114 return m_ctx; 115 } 117 121 public String getName() 122 { 123 return m_name; 124 } 126 127 128 132 public void setColumnInfo (PrintDataColumn[] newInfo) 133 { 134 m_columnInfo = newInfo; 135 } 137 141 public PrintDataColumn[] getColumnInfo() 142 { 143 return m_columnInfo; 144 } 146 150 public void setSQL (String sql) 151 { 152 m_sql = sql; 153 } 155 159 public String getSQL() 160 { 161 return m_sql; 162 } 164 168 public void setTableName (String TableName) 169 { 170 m_TableName = TableName; 171 } 173 177 public String getTableName() 178 { 179 return m_TableName; 180 } 182 186 public String toString() 187 { 188 StringBuffer sb = new StringBuffer ("PrintData["); 189 sb.append(m_name).append(",Rows=").append(m_rows.size()); 190 if (m_TableName != null) 191 sb.append(",TableName=").append(m_TableName); 192 sb.append("]"); 193 return sb.toString(); 194 } 196 197 198 199 203 public boolean isEmpty() 204 { 205 if (m_nodes == null) 206 return true; 207 return m_nodes.size() == 0; 208 } 210 214 public int getNodeCount() 215 { 216 if (m_nodes == null) 217 return 0; 218 return m_nodes.size(); 219 } 221 222 223 228 public void addRow (boolean functionRow, int levelNo) 229 { 230 m_nodes = new ArrayList(); 231 m_row = m_rows.size(); 232 m_rows.add (m_nodes); 233 if (functionRow) 234 m_functionRows.add(new Integer (m_row)); 235 if (m_hasLevelNo && levelNo != 0) 236 addNode(new PrintDataElement(LEVEL_NO, new Integer (levelNo), DisplayType.Integer)); 237 } 239 244 public boolean setRowIndex (int row) 245 { 246 if (row < 0 || row >= m_rows.size()) 247 return false; 248 m_row = row; 249 m_nodes = (ArrayList)m_rows.get(m_row); 250 return true; 251 } 252 253 257 public boolean setRowNext() 258 { 259 return setRowIndex(m_row+1); 260 } 262 266 public int getRowCount() 267 { 268 return m_rows.size(); 269 } 271 275 public int getRowIndex() 276 { 277 return m_row; 278 } 280 285 public boolean isFunctionRow (int row) 286 { 287 return m_functionRows.contains(new Integer (row)); 288 } 290 294 public boolean isFunctionRow () 295 { 296 return m_functionRows.contains(new Integer (m_row)); 297 } 299 303 public boolean isPageBreak () 304 { 305 if (isFunctionRow() && m_nodes != null) 307 { 308 for (int i = 0; i < m_nodes.size(); i++) 309 { 310 Object o = m_nodes.get(i); 311 if (o instanceof PrintDataElement) 312 { 313 PrintDataElement pde = (PrintDataElement)o; 314 if (pde.isPageBreak()) 315 return true; 316 } 317 } 318 } 319 return false; 320 } 322 326 public void setHasLevelNo (boolean hasLevelNo) 327 { 328 m_hasLevelNo = hasLevelNo; 329 } 331 335 public boolean hasLevelNo() 336 { 337 return m_hasLevelNo; 338 } 340 344 public int getLineLevelNo () 345 { 346 if (m_nodes == null || !m_hasLevelNo) 347 return 0; 348 349 for (int i = 0; i < m_nodes.size(); i++) 350 { 351 Object o = m_nodes.get (i); 352 if (o instanceof PrintDataElement) 353 { 354 PrintDataElement pde = (PrintDataElement)o; 355 if (LEVEL_NO.equals (pde.getColumnName())) 356 { 357 Integer ii = (Integer )pde.getValue(); 358 return ii.intValue(); 359 } 360 } 361 } 362 return 0; 363 } 365 366 367 371 public void addNode (PrintData parent) 372 { 373 if (parent == null) 374 throw new IllegalArgumentException ("PrintData.addNode - Parent cannot be null"); 375 if (m_nodes == null) 376 addRow(false, 0); 377 m_nodes.add (parent); 378 } 380 384 public void addNode (PrintDataElement node) 385 { 386 if (node == null) 387 throw new IllegalArgumentException ("PrintData.addNode - Node cannot be null"); 388 if (m_nodes == null) 389 addRow(false, 0); 390 m_nodes.add (node); 391 } 393 398 public Object getNode (int index) 399 { 400 if (m_nodes == null || index < 0 || index >= m_nodes.size()) 401 return null; 402 return m_nodes.get(index); 403 } 405 410 public Object getNode (String name) 411 { 412 int index = getIndex (name); 413 if (index < 0) 414 return null; 415 return m_nodes.get(index); 416 } 418 423 public Object getNode (Integer AD_Column_ID) 424 { 425 int index = getIndex (AD_Column_ID.intValue()); 426 if (index < 0) 427 return null; 428 return m_nodes.get(index); 429 } 431 435 public PrintDataElement getPKey() 436 { 437 if (m_nodes == null) 438 return null; 439 for (int i = 0; i < m_nodes.size(); i++) 440 { 441 Object o = m_nodes.get(i); 442 if (o instanceof PrintDataElement) 443 { 444 PrintDataElement pde = (PrintDataElement)o; 445 if (pde.isPKey()) 446 return pde; 447 } 448 } 449 return null; 450 } 452 457 public int getIndex (String columnName) 458 { 459 if (m_nodes == null) 460 return -1; 461 for (int i = 0; i < m_nodes.size(); i++) 462 { 463 Object o = m_nodes.get(i); 464 if (o instanceof PrintDataElement) 465 { 466 if (columnName.equals(((PrintDataElement)o).getColumnName())) 467 return i; 468 } 469 else if (o instanceof PrintData) 470 { 471 if (columnName.equals(((PrintData)o).getName())) 472 return i; 473 } 474 else 475 Log.error("PrintData.getIndex - Element not PrintData(Element) " + o.getClass().getName()); 476 } 477 return -1; 480 } 482 487 public int getIndex (int AD_Column_ID) 488 { 489 if (m_columnInfo == null) 490 return -1; 491 for (int i = 0; i < m_columnInfo.length; i++) 492 { 493 if (m_columnInfo[i].getAD_Column_ID() == AD_Column_ID) 494 return getIndex(m_columnInfo[i].getColumnName()); 495 } 496 Log.error("PrintData.getIndex - Column not found - AD_Column_ID=" + AD_Column_ID); 497 return -1; 498 } 500 501 502 505 public void dump() 506 { 507 dump(this); 508 } 510 513 public void dumpCurrentRow() 514 { 515 dumpRow(this, m_row); 516 } 518 522 private static void dump (PrintData pd) 523 { 524 System.out.println(""); 525 System.out.println(pd); 526 if (pd.getColumnInfo() != null) 527 { 528 System.out.println("ColumnInfo:"); 529 for (int i = 0; i < pd.getColumnInfo().length; i++) 530 System.out.println(i + ": " + pd.getColumnInfo()[i]); 531 } 532 for (int i = 0; i < pd.getRowCount(); i++) 533 dumpRow(pd, i); 534 } 536 541 private static void dumpRow (PrintData pd, int row) 542 { 543 System.out.println("Row #" + row); 544 if (row < 0 || row >= pd.getRowCount()) 545 { 546 System.out.println("- invalid -"); 547 return; 548 } 549 pd.setRowIndex(row); 550 if (pd.getNodeCount() == 0) 551 { 552 System.out.println("- n/a -"); 553 return; 554 } 555 for (int i = 0; i < pd.getNodeCount(); i++) 556 { 557 Object obj = pd.getNode(i); 558 if (obj instanceof PrintData) 559 { 560 System.out.println(""); 561 dump((PrintData)obj); 562 } 563 else if (obj instanceof PrintDataElement) 564 { 565 if (i != 0) 566 System.out.print(" - "); 567 System.out.print(((PrintDataElement)obj).dump()); 568 } 569 else 570 { 571 System.out.println(""); 572 System.out.println("- INVALID: " + obj); 573 } 574 } 575 System.out.println(""); 576 } 578 579 580 584 public Document getDocument() 585 { 586 Document document = null; 587 try 588 { 589 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 590 DocumentBuilder builder = factory.newDocumentBuilder(); 592 document = builder.newDocument(); 593 document.appendChild(document.createComment(Compiere.getSummaryAscii())); 594 } 595 catch (Exception e) 596 { 597 System.err.println(e); 598 e.printStackTrace(); 599 } 600 Element root = document.createElement(PrintData.XML_TAG); 602 root.setAttribute(XML_ATTRIBUTE_NAME, getName()); 603 root.setAttribute(XML_ATTRIBUTE_COUNT, String.valueOf(getRowCount())); 604 document.appendChild(root); 605 processXML (this, document, root); 606 return document; 607 } 609 615 private static void processXML (PrintData pd, Document document, Element root) 616 { 617 for (int r = 0; r < pd.getRowCount(); r++) 618 { 619 pd.setRowIndex(r); 620 Element row = document.createElement(PrintData.XML_ROW_TAG); 621 row.setAttribute(XML_ATTRIBUTE_NO, String.valueOf(r)); 622 if (pd.isFunctionRow()) 623 row.setAttribute(XML_ATTRIBUTE_FUNCTION_ROW, "yes"); 624 root.appendChild(row); 625 for (int i = 0; i < pd.getNodeCount(); i++) 627 { 628 Object o = pd.getNode(i); 629 if (o instanceof PrintData) 630 { 631 PrintData pd_x = (PrintData)o; 632 Element element = document.createElement(PrintData.XML_TAG); 633 element.setAttribute(XML_ATTRIBUTE_NAME, pd_x.getName()); 634 element.setAttribute(XML_ATTRIBUTE_COUNT, String.valueOf(pd_x.getRowCount())); 635 row.appendChild(element); 636 processXML (pd_x, document, element); } 638 else if (o instanceof PrintDataElement) 639 { 640 PrintDataElement pde = (PrintDataElement)o; 641 if (!pde.isNull()) 642 { 643 Element element = document.createElement(PrintDataElement.XML_TAG); 644 element.setAttribute(PrintDataElement.XML_ATTRIBUTE_NAME, pde.getColumnName()); 645 if (pde.hasKey()) 646 element.setAttribute(PrintDataElement.XML_ATTRIBUTE_KEY, pde.getValueKey()); 647 element.appendChild(document.createTextNode(pde.getValueDisplay(null))); row.appendChild(element); 649 } 650 } 651 else 652 Log.error("PrintData.processTree - Element not PrintData(Element) " + o.getClass().getName()); 653 } } } 657 658 663 public boolean createXML (StreamResult result) 664 { 665 try 666 { 667 DOMSource source = new DOMSource(getDocument()); 668 TransformerFactory tFactory = TransformerFactory.newInstance(); 669 Transformer transformer = tFactory.newTransformer(); 670 transformer.transform (source, result); 671 } 672 catch (Exception e) 673 { 674 Log.error("PrintData.createXML (StreamResult)", e); 675 return false; 676 } 677 return true; 678 } 680 685 public boolean createXML (String fileName) 686 { 687 try 688 { 689 File file = new File(fileName); 690 file.createNewFile(); 691 StreamResult result = new StreamResult(file); 692 createXML (result); 693 } 694 catch (Exception e) 695 { 696 Log.error("PrintData.createXML (file)", e); 697 return false; 698 } 699 return true; 700 } 702 703 704 709 public static PrintData parseXML (Properties ctx, File input) 710 { 711 Log.trace(Log.l3_Util, "PrintData.parseXML", input); 712 PrintData pd = null; 713 try 714 { 715 PrintDataHandler handler = new PrintDataHandler(ctx); 716 SAXParserFactory factory = SAXParserFactory.newInstance(); 717 SAXParser parser = factory.newSAXParser(); 718 parser.parse(input, handler); 719 pd = handler.getPrintData(); 720 } 721 catch (Exception e) 722 { 723 Log.error("PrintData.parseXML", e); 724 } 725 return pd; 726 } 728 729 730 731 735 public static void main(String [] args) 736 { 737 System.setProperty("javax.xml.parsers.DocumentBuilderFactory", 738 "org.apache.crimson.jaxp.DocumentBuilderFactoryImpl"); System.setProperty("javax.xml.parsers.SAXParserFactory", 741 "org.apache.crimson.jaxp.SAXParserFactoryImpl"); 744 PrintData pd = new PrintData(new Properties(), "test1"); 745 pd.addNode(new PrintDataElement("test1element1","testvalue<1>",0)); 746 pd.addNode(new PrintDataElement("test1element2","testvalue&2&",0)); 747 748 PrintData pdx = new PrintData(new Properties(), "test2"); 749 pdx.addNode(new PrintDataElement("test2element1-1","testvalue11",0)); 750 pdx.addNode(new PrintDataElement("test2element1-2","testvalue12",0)); 751 pdx.addRow(false, 0); 752 pdx.addNode(new PrintDataElement("test2element2-1","testvalue21",0)); 753 pdx.addNode(new PrintDataElement("test2element2-2","testvalue22",0)); 754 755 pd.addNode(pdx); 756 pd.addNode(new PrintDataElement("test1element3","testvalue/3/",0)); 757 758 pd.createXML("C:\\Temp\\printData.xml"); 759 pd.createXML(new StreamResult(System.out)); 760 System.out.println(""); 761 pd.dump(); 762 763 System.out.println(""); 765 PrintData pd1 = parseXML (new Properties(), new File("C:\\Temp\\printData.xml")); 766 pd1.createXML(new StreamResult(System.out)); 767 System.out.println(""); 768 pd1.dump(); 769 } 771 } | Popular Tags |