| 1 18 package sync4j.framework.tools; 19 20 import java.io.*; 21 import java.util.ArrayList ; 22 23 import sync4j.framework.core.*; 24 25 31 public class SizeCalculator { 32 33 35 42 public static long getXMLOverheadSyncML() { 43 return 43; 44 } 45 46 52 public static long getWBXMLOverheadSyncML() { 53 return 29; 54 } 55 56 67 public static long getXMLSize(SyncML syncML) { 68 SyncHdr syncHdr = syncML.getSyncHdr() ; 69 SyncBody syncBody = syncML.getSyncBody(); 70 71 return 43 72 + getXMLSize(syncHdr ) 73 + getXMLSize(syncBody) 74 ; 75 } 76 77 84 public static long getWBXMLSize(SyncML syncML) { 85 SyncHdr syncHdr = syncML.getSyncHdr() ; 86 SyncBody syncBody = syncML.getSyncBody(); 87 88 return 29 89 + getWBXMLSize(syncHdr ) 90 + getWBXMLSize(syncBody) 91 ; 92 } 93 94 126 public static long getXMLSize(SyncHdr syncHdr) { 127 128 VerDTD verDTD = syncHdr.getVerDTD() ; 129 VerProto verProto = syncHdr.getVerProto() ; 130 SessionID sessionID = syncHdr.getSessionID(); 131 String msgID = syncHdr.getMsgID() ; 132 Target target = syncHdr.getTarget() ; 133 Source source = syncHdr.getSource() ; 134 String respURI = syncHdr.getRespURI() ; 135 boolean noResp = syncHdr.isNoResp() ; 136 Cred cred = syncHdr.getCred() ; 137 Meta meta = syncHdr.getMeta() ; 138 139 return 21 140 + ((verDTD != null) ? getXMLSize(verDTD) : 0) 141 + ((verProto != null) ? getXMLSize(verProto) : 0) 142 + ((sessionID != null) ? getXMLSize(sessionID) : 0) 143 + ((msgID != null) ? 16 + msgID.length() : 0) 144 + ((target != null) ? getXMLSize(target) : 0) 145 + ((source != null) ? getXMLSize(source) : 0) 146 + ((respURI != null) ? 19 + respURI.length() : 90) 147 + (noResp ? 18 : 0) 148 + ((cred != null) ? getXMLSize(cred) : 0) 149 + ((meta != null) ? getXMLSize(meta) : 0) 150 ; 151 } 152 153 160 public static long getWBXMLSize(SyncHdr syncHdr) { 161 VerDTD verDTD = syncHdr.getVerDTD() ; 162 VerProto verProto = syncHdr.getVerProto() ; 163 SessionID sessionID = syncHdr.getSessionID(); 164 String msgID = syncHdr.getMsgID() ; 165 Target target = syncHdr.getTarget() ; 166 Source source = syncHdr.getSource() ; 167 String respURI = syncHdr.getRespURI() ; 168 boolean noResp = syncHdr.isNoResp() ; 169 Cred cred = syncHdr.getCred() ; 170 Meta meta = syncHdr.getMeta() ; 171 172 return 4 173 + ((verDTD != null) ? getWBXMLSize(verDTD) : 0) 174 + ((verProto != null) ? getWBXMLSize(verProto) : 0) 175 + ((sessionID != null) ? getWBXMLSize(sessionID) : 0) 176 + ((msgID != null) ? 4 + msgID.length() : 0) 177 + ((target != null) ? getWBXMLSize(target) : 0) 178 + ((source != null) ? getWBXMLSize(source) : 0) 179 + ((respURI != null) ? 4 + respURI.length() : 80) 180 + (noResp ? 1 : 0) 181 + ((cred != null) ? getWBXMLSize(cred) : 0) 182 + ((meta != null) ? getWBXMLSize(meta) : 0) 183 ; 184 } 185 186 193 public static long getXMLOverheadSyncBody() { 194 return 23; 195 } 196 197 203 public static long getWBXMLOverheadSyncBody() { 204 return 4; 205 } 206 207 220 public static long getXMLSize(SyncBody syncBody) { 221 ArrayList commands = syncBody.getCommands(); 222 223 long size = 23 224 + ((syncBody.isFinalMsg()) ? 16 : 0) 225 ; 226 227 for (int i=0; i<commands.size(); i++) { 228 size += getCommandXMLSize((AbstractCommand)commands.get(i)); 229 } 230 return size; 231 } 232 233 240 public static long getWBXMLSize(SyncBody syncBody) { 241 ArrayList commands = syncBody.getCommands(); 242 243 long size = 4 244 + ((syncBody.isFinalMsg()) ? 1 : 0) 245 ; 246 247 for (int i=0; i<commands.size(); ++i) { 248 size += getCommandWBXMLSize((AbstractCommand)commands.get(i)); 249 } 250 return size; 251 } 252 253 262 public static long getXMLSize(VerDTD verDTD) { 263 return 18 264 + verDTD.getValue().length() 265 ; 266 } 267 268 273 public static long getWBXMLSize(VerDTD verDTD) { 274 return 4 275 + verDTD.getValue().length() 276 ; 277 } 278 279 288 public static long getXMLSize(VerProto verProto) { 289 return 22 290 + verProto.getVersion().length() 291 ; 292 } 293 294 299 public static long getWBXMLSize(VerProto verProto) { 300 return 4 301 + verProto.getVersion().length() 302 ; 303 } 304 305 314 public static long getXMLSize(SessionID sessionID) { 315 return 24 316 + sessionID.getSessionID().length() 317 ; 318 } 319 320 325 public static long getWBXMLSize(SessionID sessionID) { 326 return 4 327 + sessionID.getSessionID().length() 328 ; 329 } 330 331 347 public static long getXMLSize(Target target) { 348 String locURI = target.getLocURI() ; 349 String locName = target.getLocName(); 350 351 return 19 352 + ((locURI != null) ? (18 + locURI.length() ) : 0) 353 + ((locName != null) ? (20 + locName.length()) : 0) 354 ; 355 } 356 360 public static long getWBXMLSize(Target target) { 361 String locURI = target.getLocURI() ; 362 String locName = target.getLocName(); 363 364 return 4 365 + ((locURI != null) ? (4 + locURI.length() ) : 0) 366 + ((locName != null) ? (4 + locName.length()) : 0) 367 ; 368 } 369 370 386 public static long getXMLSize(Source source) { 387 String locURI = source.getLocURI() ; 388 String locName = source.getLocName(); 389 390 return 19 391 + ((locURI != null) ? (18 + locURI.length() ) : 0) 392 + ((locName != null) ? (20 + locName.length()) : 0) 393 ; 394 } 395 399 public static long getWBXMLSize(Source source) { 400 String locURI = source.getLocURI() ; 401 String locName = source.getLocName(); 402 403 return 4 404 + ((locURI != null) ? (4 + locURI.length() ) : 0) 405 + ((locName != null) ? (4 + locName.length()) : 0) 406 ; 407 } 408 409 423 public static long getXMLSize(Cred cred) { 424 Authentication auth = cred.getAuthentication(); 425 Meta meta = auth.getMeta(); 426 String data = cred.getData(); 427 428 return 15 429 + ((meta != null) ? getXMLSize(meta) : 0) 430 + ((data != null) ? 14 + data.length() : 0) 431 ; 432 } 433 434 438 public static long getWBXMLSize(Cred cred) { 439 Authentication auth = cred.getAuthentication(); 440 Meta meta = auth.getMeta(); 441 String data = cred.getData(); 442 443 return 4 444 + ((meta != null) ? getWBXMLSize(meta) : 0) 445 + ((data != null) ? 4 + data.length() : 0) 446 ; 447 } 448 449 490 public static long getXMLSize(Meta meta) { 491 long sizeMeta = 0; 492 493 String format = meta.getFormat() ; 494 String type = meta.getType() ; 495 String mark = meta.getMark() ; 496 Long size = meta.getSize() ; 497 Anchor anchor = meta.getAnchor() ; 498 String version = meta.getVersion() ; 499 NextNonce nextNonce = meta.getNextNonce() ; 500 Long maxMsgSize = meta.getMaxMsgSize(); 501 Long maxObjSize = meta.getMaxObjSize(); 502 ArrayList emi = meta.getEMI() ; 503 Mem mem = meta.getMem() ; 504 505 sizeMeta = 37 506 + ((format != null) ? 18 + format.length() : 0) 507 + ((type != null) ? 14 + type.length() : 0) 508 + ((mark != null) ? 14 + mark.length() : 0) 509 + ((size != null) ? 14 + String.valueOf(size).length(): 0) 510 + ((anchor != null) ? getXMLSize(anchor) : 0) 511 + ((version != null) ? 20 + version.length() : 0) 512 + ((nextNonce != null) ? getXMLSize(nextNonce) : 0) 513 + ((maxMsgSize!= null) ? 26 + String.valueOf(maxMsgSize).length() : 0) 514 + ((maxObjSize!= null) ? 26 + String.valueOf(maxObjSize).length() : 0) 515 + ((mem != null) ? getXMLSize(mem) : 0) 516 ; 517 518 for (int i=0; emi != null && i < emi.size(); i++) { 519 sizeMeta += getXMLSize((EMI)emi.get(i)); 520 } 521 522 return sizeMeta; 523 } 524 525 526 527 532 public static long getWBXMLSize(Meta meta) { 533 long sizeMeta = 0; 534 535 String format = meta.getFormat() ; 536 String type = meta.getType() ; 537 String mark = meta.getMark() ; 538 Long size = meta.getSize() ; 539 Anchor anchor = meta.getAnchor() ; 540 String version = meta.getVersion() ; 541 NextNonce nextNonce = meta.getNextNonce() ; 542 Long maxMsgSize = meta.getMaxMsgSize(); 543 Long maxObjSize = meta.getMaxObjSize(); 544 ArrayList emi = meta.getEMI() ; 545 Mem mem = meta.getMem() ; 546 547 sizeMeta = 4 548 + ((format != null) ? 4 + format.length() : 0) 549 + ((type != null) ? 4 + type.length() : 0) 550 + ((mark != null) ? 4 + mark.length() : 0) 551 + ((size != null) ? 4 + String.valueOf(size).length() : 0) 552 + ((anchor != null) ? getWBXMLSize(anchor) : 0) 553 + ((version != null) ? 4 + version.length() : 0) 554 + ((nextNonce != null) ? getWBXMLSize(nextNonce) : 0) 555 + ((maxMsgSize!= null) ? 4 + String.valueOf(maxMsgSize).length() : 0) 556 + ((maxObjSize!= null) ? 4 + String.valueOf(maxObjSize).length() : 0) 557 + ((mem != null) ? getWBXMLSize(mem) : 0) 558 ; 559 560 for (int i=0; emi != null && i < emi.size(); i++) { 561 sizeMeta += getWBXMLSize((EMI)emi.get(i)); 562 } 563 564 return sizeMeta; 565 } 566 567 583 public static long getXMLSize(Anchor anchor) { 584 String last = anchor.getLast(); 585 String next = anchor.getNext(); 586 587 return 41 588 + ((last != null) ? 14 + last.length() : 0) 589 + ((next != null) ? 14 + next.length() : 0) 590 ; 591 } 592 593 598 public static long getWBXMLSize(Anchor anchor) { 599 String last = anchor.getLast(); 600 String next = anchor.getNext(); 601 602 return 25 603 + ((last != null) ? 4 + last.length() : 0) 604 + ((next != null) ? 4 + next.length() : 0) 605 ; 606 } 607 608 617 public static long getXMLSize(EMI emi) { 618 return 12 619 + emi.getValue().length() 620 ; 621 } 622 623 628 public static long getWBXMLSize(EMI emi) { 629 return 4 630 + emi.getValue().length() 631 ; 632 } 633 634 643 public static long getXMLSize(NextNonce nextNonce) { 644 return 24 645 + nextNonce.getValueAsBase64().length() 646 ; 647 } 648 649 654 public static long getWBXMLSize(NextNonce nextNonce) { 655 return 4 656 + nextNonce.getValueAsBase64().length() 657 ; 658 } 659 660 678 public static long getXMLSize(Mem mem) { 679 boolean sharedMem = mem.isSharedMem(); 680 long freeMem = mem.getFreeMem() ; 681 long freeID = mem.getFreeID() ; 682 683 return 13 684 + ((sharedMem) ? 18 : 0) 685 + ((freeMem != 0) ? 20 + String.valueOf(freeMem).length() : 0) 686 + ((freeID != 0) ? 18 + String.valueOf(freeID).length() : 0) 687 ; 688 } 689 690 695 public static long getWBXMLSize(Mem mem) { 696 boolean sharedMem = mem.isSharedMem(); 697 long freeMem = mem.getFreeMem() ; 698 long freeID = mem.getFreeID() ; 699 700 return 4 701 + ((sharedMem) ? 1 : 0) 702 + ((freeMem != 0) ? 4 + String.valueOf(freeMem).length() : 0) 703 + ((freeID != 0) ? 4 + String.valueOf(freeID).length() : 0) 704 ; 705 } 706 707 718 public static long getXMLSize(AbstractCommand command) { 719 CmdID cmdID = command.getCmdID(); 720 Cred cred = command.getCred() ; 721 722 return ((cmdID != null) ? getXMLSize(cmdID) : 0) 723 + ((command.isNoResp()) ? 18 : 0) 724 + ((cred != null) ? getXMLSize(cred) : 0) 725 ; 726 } 727 728 732 public static long getWBXMLSize(AbstractCommand command) { 733 CmdID cmdID = command.getCmdID(); 734 Cred cred = command.getCred() ; 735 736 return ((cmdID != null) ? getWBXMLSize(cmdID) : 0) 737 + ((command.isNoResp()) ? 1 : 0) 738 + ((cred != null) ? getWBXMLSize(cred) : 0) 739 ; 740 } 741 742 747 public static long getCommandXMLSize(AbstractCommand command) { 748 long size = 0; 749 if (command instanceof Add) { 750 size = getXMLSize((Add)command); 751 } else if (command instanceof Alert) { 752 size = getXMLSize((Alert)command); 753 } else if (command instanceof Atomic) { 754 size = getXMLSize((Atomic)command); 755 } else if (command instanceof Copy) { 756 size = getXMLSize((Copy)command); 757 } else if (command instanceof Delete) { 758 size = getXMLSize((Delete)command); 759 } else if (command instanceof Exec) { 760 size = getXMLSize((Exec)command); 761 } else if (command instanceof Get) { 762 size = getXMLSize((Get)command); 763 } else if (command instanceof Map) { 764 size = getXMLSize((Map)command); 765 } else if (command instanceof Put) { 766 size = getXMLSize((Put)command); 767 } else if (command instanceof Replace) { 768 size = getXMLSize((Replace)command); 769 } else if (command instanceof Results) { 770 size = getXMLSize((Results)command); 771 } else if (command instanceof Search) { 772 size = getXMLSize((Search)command); 773 } else if (command instanceof Sequence) { 774 size = getXMLSize((Sequence)command); 775 } else if (command instanceof Status) { 776 size = getXMLSize((Status)command); 777 } else if (command instanceof Sync) { 778 size = getXMLSize((Sync)command); 779 } 780 781 return size; 782 } 783 784 789 public static long getCommandWBXMLSize(AbstractCommand command) { 790 long size = 0; 791 if (command instanceof Add) { 792 size = getWBXMLSize((Add)command); 793 } else if (command instanceof Alert) { 794 size = getWBXMLSize((Alert)command); 795 } else if (command instanceof Atomic) { 796 size = getWBXMLSize((Atomic)command); 797 } else if (command instanceof Copy) { 798 size = getWBXMLSize((Copy)command); 799 } else if (command instanceof Delete) { 800 size = getWBXMLSize((Delete)command); 801 } else if (command instanceof Exec) { 802 size = getWBXMLSize((Exec)command); 803 } else if (command instanceof Get) { 804 size = getWBXMLSize((Get)command); 805 } else if (command instanceof Map) { 806 size = getWBXMLSize((Map)command); 807 } else if (command instanceof Put) { 808 size = getWBXMLSize((Put)command); 809 } else if (command instanceof Replace) { 810 size = getWBXMLSize((Replace)command); 811 } else if (command instanceof Results) { 812 size = getWBXMLSize((Results)command); 813 } else if (command instanceof Search) { 814 size = getWBXMLSize((Search)command); 815 } else if (command instanceof Sequence) { 816 size = getWBXMLSize((Sequence)command); 817 } else if (command instanceof Status) { 818 size = getWBXMLSize((Status)command); 819 } else if (command instanceof Sync) { 820 size = getWBXMLSize((Sync)command); 821 } 822 823 return size; 824 } 825 826 835 public static long getXMLSize(CmdID cmdID) { 836 return 16 837 + cmdID.getCmdID().length(); 838 } 839 840 844 public static long getWBXMLSize(CmdID cmdID) { 845 return 2 846 + cmdID.getCmdID().length(); 847 } 848 849 867 public static long getXMLSize(Add add) { 868 Meta meta = add.getMeta() ; 869 ArrayList items = add.getItems(); 870 871 long size = 13 872 + getXMLSize((AbstractCommand)add) 873 + ((meta != null) ? getXMLSize(meta) : 0) 874 ; 875 for (int i=0; items != null && i<items.size(); i++) { 876 size += getXMLSize((Item)items.get(i)); 877 } 878 return size; 879 } 880 881 886 public static long getWBXMLSize(Add add) { 887 Meta meta = add.getMeta() ; 888 ArrayList items = add.getItems(); 889 890 long size = 4 891 + getWBXMLSize((AbstractCommand)add) 892 + ((meta != null) ? getWBXMLSize(meta) : 0) 893 ; 894 for (int i=0; items != null && i<items.size(); i++) { 895 size += getWBXMLSize((Item)items.get(i)); 896 } 897 return size; 898 } 899 900 918 public static long getXMLSize(Item item) { 919 Target target = item.getTarget(); 920 Source source = item.getSource(); 921 Meta meta = item.getMeta() ; 922 ComplexData data = item.getData() ; 923 924 return 15 925 + ((target != null) ? getXMLSize(target) : 0) 926 + ((source != null) ? getXMLSize(source) : 0) 927 + ((meta != null) ? getXMLSize(meta) : 0) 928 + ((data != null) ? getXMLSize(data) : 0) 929 &nb
|