1 16 19 20 package com.sun.org.apache.xalan.internal.xsltc.runtime; 21 22 import java.text.DecimalFormat ; 23 import java.text.FieldPosition ; 24 import java.text.MessageFormat ; 25 import java.text.NumberFormat ; 26 import java.util.Locale ; 27 import java.util.ResourceBundle ; 28 29 import javax.xml.transform.dom.DOMSource ; 30 31 import com.sun.org.apache.xalan.internal.xsltc.DOM; 32 import com.sun.org.apache.xalan.internal.xsltc.Translet; 33 import com.sun.org.apache.xalan.internal.xsltc.dom.AbsoluteIterator; 34 import com.sun.org.apache.xalan.internal.xsltc.dom.Axis; 35 import com.sun.org.apache.xalan.internal.xsltc.dom.DOMAdapter; 36 import com.sun.org.apache.xalan.internal.xsltc.dom.MultiDOM; 37 import com.sun.org.apache.xalan.internal.xsltc.dom.SingletonIterator; 38 import com.sun.org.apache.xalan.internal.xsltc.dom.StepIterator; 39 import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator; 40 import com.sun.org.apache.xml.internal.dtm.DTMManager; 41 import com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBase; 42 43 import org.w3c.dom.DOMException ; 44 import org.w3c.dom.Document ; 45 import org.w3c.dom.NodeList ; 46 import org.xml.sax.SAXException ; 47 import com.sun.org.apache.xml.internal.serializer.NamespaceMappings; 48 import com.sun.org.apache.xml.internal.serializer.SerializationHandler; 49 import com.sun.org.apache.xml.internal.utils.XMLChar; 50 51 55 public final class BasisLibrary implements Operators { 56 57 private final static String EMPTYSTRING = ""; 58 59 62 public static int countF(DTMAxisIterator iterator) { 63 return(iterator.getLast()); 64 } 65 66 71 public static int positionF(DTMAxisIterator iterator) { 72 return iterator.isReverse() 73 ? iterator.getLast() - iterator.getPosition() + 1 74 : iterator.getPosition(); 75 } 76 77 81 public static double sumF(DTMAxisIterator iterator, DOM dom) { 82 try { 83 double result = 0.0; 84 int node; 85 while ((node = iterator.next()) != DTMAxisIterator.END) { 86 result += Double.parseDouble(dom.getStringValueX(node)); 87 } 88 return result; 89 } 90 catch (NumberFormatException e) { 91 return Double.NaN; 92 } 93 } 94 95 98 public static String stringF(int node, DOM dom) { 99 return dom.getStringValueX(node); 100 } 101 102 105 public static String stringF(Object obj, DOM dom) { 106 if (obj instanceof DTMAxisIterator) { 107 return dom.getStringValueX(((DTMAxisIterator)obj).reset().next()); 108 } 109 else if (obj instanceof Node) { 110 return dom.getStringValueX(((Node)obj).node); 111 } 112 else if (obj instanceof DOM) { 113 return ((DOM)obj).getStringValue(); 114 } 115 else { 116 return obj.toString(); 117 } 118 } 119 120 123 public static String stringF(Object obj, int node, DOM dom) { 124 if (obj instanceof DTMAxisIterator) { 125 return dom.getStringValueX(((DTMAxisIterator)obj).reset().next()); 126 } 127 else if (obj instanceof Node) { 128 return dom.getStringValueX(((Node)obj).node); 129 } 130 else if (obj instanceof DOM) { 131 return ((DOM)obj).getStringValue(); 135 } 136 else if (obj instanceof Double ) { 137 Double d = (Double )obj; 138 final String result = d.toString(); 139 final int length = result.length(); 140 if ((result.charAt(length-2)=='.') && 141 (result.charAt(length-1) == '0')) 142 return result.substring(0, length-2); 143 else 144 return result; 145 } 146 else { 147 if (obj != null) 148 return obj.toString(); 149 else 150 return stringF(node, dom); 151 } 152 } 153 154 157 public static double numberF(int node, DOM dom) { 158 return stringToReal(dom.getStringValueX(node)); 159 } 160 161 164 public static double numberF(Object obj, DOM dom) { 165 if (obj instanceof Double ) { 166 return ((Double ) obj).doubleValue(); 167 } 168 else if (obj instanceof Integer ) { 169 return ((Integer ) obj).doubleValue(); 170 } 171 else if (obj instanceof Boolean ) { 172 return ((Boolean ) obj).booleanValue() ? 1.0 : 0.0; 173 } 174 else if (obj instanceof String ) { 175 return stringToReal((String ) obj); 176 } 177 else if (obj instanceof DTMAxisIterator) { 178 DTMAxisIterator iter = (DTMAxisIterator) obj; 179 return stringToReal(dom.getStringValueX(iter.reset().next())); 180 } 181 else if (obj instanceof Node) { 182 return stringToReal(dom.getStringValueX(((Node) obj).node)); 183 } 184 else if (obj instanceof DOM) { 185 return stringToReal(((DOM) obj).getStringValue()); 186 } 187 else { 188 final String className = obj.getClass().getName(); 189 runTimeError(INVALID_ARGUMENT_ERR, className, "number()"); 190 return 0.0; 191 } 192 } 193 194 197 public static double roundF(double d) { 198 return (d<-0.5 || d>0.0)?Math.floor(d+0.5):((d==0.0)? 199 d:(Double.isNaN(d)?Double.NaN:-0.0)); 200 } 201 202 205 public static boolean booleanF(Object obj) { 206 if (obj instanceof Double ) { 207 final double temp = ((Double ) obj).doubleValue(); 208 return temp != 0.0 && !Double.isNaN(temp); 209 } 210 else if (obj instanceof Integer ) { 211 return ((Integer ) obj).doubleValue() != 0; 212 } 213 else if (obj instanceof Boolean ) { 214 return ((Boolean ) obj).booleanValue(); 215 } 216 else if (obj instanceof String ) { 217 return !((String ) obj).equals(EMPTYSTRING); 218 } 219 else if (obj instanceof DTMAxisIterator) { 220 DTMAxisIterator iter = (DTMAxisIterator) obj; 221 return iter.reset().next() != DTMAxisIterator.END; 222 } 223 else if (obj instanceof Node) { 224 return true; 225 } 226 else if (obj instanceof DOM) { 227 String temp = ((DOM) obj).getStringValue(); 228 return !temp.equals(EMPTYSTRING); 229 } 230 else { 231 final String className = obj.getClass().getName(); 232 runTimeError(INVALID_ARGUMENT_ERR, className, "number()"); 233 } 234 return false; 235 } 236 237 241 public static String substringF(String value, double start) { 242 try { 243 final int strlen = value.length(); 244 int istart = (int)Math.round(start) - 1; 245 246 if (Double.isNaN(start)) return(EMPTYSTRING); 247 if (istart > strlen) return(EMPTYSTRING); 248 if (istart < 1) istart = 0; 249 250 return value.substring(istart); 251 } 252 catch (IndexOutOfBoundsException e) { 253 runTimeError(RUN_TIME_INTERNAL_ERR, "substring()"); 254 return null; 255 } 256 } 257 258 262 public static String substringF(String value, double start, double length) { 263 try { 264 final int strlen = value.length(); 265 int istart = (int)Math.round(start) - 1; 266 int isum = istart + (int)Math.round(length); 267 268 if (Double.isInfinite(length)) isum = Integer.MAX_VALUE; 269 270 if (Double.isNaN(start) || Double.isNaN(length)) 271 return(EMPTYSTRING); 272 if (Double.isInfinite(start)) return(EMPTYSTRING); 273 if (istart > strlen) return(EMPTYSTRING); 274 if (isum < 0) return(EMPTYSTRING); 275 if (istart < 0) istart = 0; 276 277 if (isum > strlen) 278 return value.substring(istart); 279 else 280 return value.substring(istart, isum); 281 } 282 catch (IndexOutOfBoundsException e) { 283 runTimeError(RUN_TIME_INTERNAL_ERR, "substring()"); 284 return null; 285 } 286 } 287 288 291 public static String substring_afterF(String value, String substring) { 292 final int index = value.indexOf(substring); 293 if (index >= 0) 294 return value.substring(index + substring.length()); 295 else 296 return EMPTYSTRING; 297 } 298 299 302 public static String substring_beforeF(String value, String substring) { 303 final int index = value.indexOf(substring); 304 if (index >= 0) 305 return value.substring(0, index); 306 else 307 return EMPTYSTRING; 308 } 309 310 313 public static String translateF(String value, String from, String to) { 314 final int tol = to.length(); 315 final int froml = from.length(); 316 final int valuel = value.length(); 317 318 final StringBuffer result = new StringBuffer (); 319 for (int j, i = 0; i < valuel; i++) { 320 final char ch = value.charAt(i); 321 for (j = 0; j < froml; j++) { 322 if (ch == from.charAt(j)) { 323 if (j < tol) 324 result.append(to.charAt(j)); 325 break; 326 } 327 } 328 if (j == froml) 329 result.append(ch); 330 } 331 return result.toString(); 332 } 333 334 337 public static String normalize_spaceF(int node, DOM dom) { 338 return normalize_spaceF(dom.getStringValueX(node)); 339 } 340 341 344 public static String normalize_spaceF(String value) { 345 int i = 0, n = value.length(); 346 StringBuffer result = new StringBuffer (); 347 348 while (i < n && isWhiteSpace(value.charAt(i))) 349 i++; 350 351 while (true) { 352 while (i < n && !isWhiteSpace(value.charAt(i))) { 353 result.append(value.charAt(i++)); 354 } 355 if (i == n) 356 break; 357 while (i < n && isWhiteSpace(value.charAt(i))) { 358 i++; 359 } 360 if (i < n) 361 result.append(' '); 362 } 363 return result.toString(); 364 } 365 366 369 public static String generate_idF(int node) { 370 if (node > 0) 371 return "N" + node; 373 else 374 return EMPTYSTRING; 376 } 377 378 381 public static String getLocalName(String value) { 382 int idx = value.lastIndexOf(':'); 383 if (idx >= 0) value = value.substring(idx + 1); 384 idx = value.lastIndexOf('@'); 385 if (idx >= 0) value = value.substring(idx + 1); 386 return(value); 387 } 388 389 398 public static void unresolved_externalF(String name) { 399 runTimeError(EXTERNAL_FUNC_ERR, name); 400 } 401 402 406 public static void unallowed_extension_functionF(String name) { 407 runTimeError(UNALLOWED_EXTENSION_FUNCTION_ERR, name); 408 } 409 410 414 public static void unallowed_extension_elementF(String name) { 415 runTimeError(UNALLOWED_EXTENSION_ELEMENT_ERR, name); 416 } 417 418 425 public static void unsupported_ElementF(String qname, boolean isExtension) { 426 if (isExtension) 427 runTimeError(UNSUPPORTED_EXT_ERR, qname); 428 else 429 runTimeError(UNSUPPORTED_XSL_ERR, qname); 430 } 431 432 435 public static String namespace_uriF(DTMAxisIterator iter, DOM dom) { 436 return namespace_uriF(iter.next(), dom); 437 } 438 439 442 public static String system_propertyF(String name) { 443 if (name.equals("xsl:version")) 444 return("1.0"); 445 if (name.equals("xsl:vendor")) 446 return("Apache Software Foundation (Xalan XSLTC)"); 447 if (name.equals("xsl:vendor-url")) 448 return("http://xml.apache.org/xalan-j"); 449 450 runTimeError(INVALID_ARGUMENT_ERR, name, "system-property()"); 451 return(EMPTYSTRING); 452 } 453 454 457 public static String namespace_uriF(int node, DOM dom) { 458 final String value = dom.getNodeName(node); 459 final int colon = value.lastIndexOf(':'); 460 if (colon >= 0) 461 return value.substring(0, colon); 462 else 463 return EMPTYSTRING; 464 } 465 466 471 public static String objectTypeF(Object obj) 472 { 473 if (obj instanceof String ) 474 return "string"; 475 else if (obj instanceof Boolean ) 476 return "boolean"; 477 else if (obj instanceof Number ) 478 return "number"; 479 else if (obj instanceof DOM) 480 return "RTF"; 481 else if (obj instanceof DTMAxisIterator) 482 return "node-set"; 483 else 484 return "unknown"; 485 } 486 487 490 public static DTMAxisIterator nodesetF(Object obj) { 491 if (obj instanceof DOM) { 492 final DOM dom = (DOM)obj; 494 return new SingletonIterator(dom.getDocument(), true); 495 } 496 else if (obj instanceof DTMAxisIterator) { 497 return (DTMAxisIterator) obj; 498 } 499 else { 500 final String className = obj.getClass().getName(); 501 runTimeError(DATA_CONVERSION_ERR, "node-set", className); 502 return null; 503 } 504 } 505 506 508 private static boolean isWhiteSpace(char ch) { 509 return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'; 510 } 511 512 private static boolean compareStrings(String lstring, String rstring, 513 int op, DOM dom) { 514 switch (op) { 515 case EQ: 516 return lstring.equals(rstring); 517 518 case NE: 519 return !lstring.equals(rstring); 520 521 case GT: 522 return numberF(lstring, dom) > numberF(rstring, dom); 523 524 case LT: 525 return numberF(lstring, dom) < numberF(rstring, dom); 526 527 case GE: 528 return numberF(lstring, dom) >= numberF(rstring, dom); 529 530 case LE: 531 return numberF(lstring, dom) <= numberF(rstring, dom); 532 533 default: 534 runTimeError(RUN_TIME_INTERNAL_ERR, "compare()"); 535 return false; 536 } 537 } 538 539 542 public static boolean compare(DTMAxisIterator left, DTMAxisIterator right, 543 int op, DOM dom) { 544 int lnode; 545 left.reset(); 546 547 while ((lnode = left.next()) != DTMAxisIterator.END) { 548 final String lvalue = dom.getStringValueX(lnode); 549 550 int rnode; 551 right.reset(); 552 while ((rnode = right.next()) != DTMAxisIterator.END) { 553 if (lnode == rnode) { 555 if (op == EQ) { 556 return true; 557 } else if (op == NE) { 558 continue; 559 } 560 } 561 if (compareStrings(lvalue, dom.getStringValueX(rnode), op, 562 dom)) { 563 return true; 564 } 565 } 566 } 567 return false; 568 } 569 570 public static boolean compare(int node, DTMAxisIterator iterator, 571 int op, DOM dom) { 572 574 int rnode; 575 String value; 576 577 switch(op) { 578 case EQ: 579 rnode = iterator.next(); 580 if (rnode != DTMAxisIterator.END) { 581 value = dom.getStringValueX(node); 582 do { 583 if (node == rnode 584 || value.equals(dom.getStringValueX(rnode))) { 585 return true; 586 } 587 } while ((rnode = iterator.next()) != DTMAxisIterator.END); 588 } 589 break; 590 case NE: 591 rnode = iterator.next(); 592 if (rnode != DTMAxisIterator.END) { 593 value = dom.getStringValueX(node); 594 do { 595 if (node != rnode 596 && !value.equals(dom.getStringValueX(rnode))) { 597 return true; 598 } 599 } while ((rnode = iterator.next()) != DTMAxisIterator.END); 600 } 601 break; 602 case LT: 603 while ((rnode = iterator.next()) != DTMAxisIterator.END) { 605 if (rnode > node) return true; 606 } 607 break; 608 case GT: 609 while ((rnode = iterator.next()) != DTMAxisIterator.END) { 611 if (rnode < node) return true; 612 } 613 break; 614 } 615 return(false); 616 } 617 618 621 public static boolean compare(DTMAxisIterator left, final double rnumber, 622 final int op, DOM dom) { 623 int node; 624 626 switch (op) { 627 case EQ: 628 while ((node = left.next()) != DTMAxisIterator.END) { 629 if (numberF(dom.getStringValueX(node), dom) == rnumber) 630 return true; 631 } 632 break; 633 634 case NE: 635 while ((node = left.next()) != DTMAxisIterator.END) { 636 if (numberF(dom.getStringValueX(node), dom) != rnumber) 637 return true; 638 } 639 break; 640 641 case GT: 642 while ((node = left.next()) != DTMAxisIterator.END) { 643 if (numberF(dom.getStringValueX(node), dom) > rnumber) 644 return true; 645 } 646 break; 647 648 case LT: 649 while ((node = left.next()) != DTMAxisIterator.END) { 650 if (numberF(dom.getStringValueX(node), dom) < rnumber) 651 return true; 652 } 653 break; 654 655 case GE: 656 while ((node = left.next()) != DTMAxisIterator.END) { 657 if (numberF(dom.getStringValueX(node), dom) >= rnumber) 658 return true; 659 } 660 break; 661 662 case LE: 663 while ((node = left.next()) != DTMAxisIterator.END) { 664 if (numberF(dom.getStringValueX(node), dom) <= rnumber) 665 return true; 666 } 667 break; 668 669 default: 670 runTimeError(RUN_TIME_INTERNAL_ERR, "compare()"); 671 } 672 673 return false; 674 } 675 676 679 public static boolean compare(DTMAxisIterator left, final String rstring, 680 int op, DOM dom) { 681 int node; 682 while ((node = left.next()) != DTMAxisIterator.END) { 684 if (compareStrings(dom.getStringValueX(node), rstring, op, dom)) { 685 return true; 686 } 687 } 688 return false; 689 } 690 691 692 public static boolean compare(Object left, Object right, 693 int op, DOM dom) 694 { 695 boolean result = false; 696 boolean hasSimpleArgs = hasSimpleType(left) && hasSimpleType(right); 697 698 if (op != EQ && op != NE) { 699 if (left instanceof Node || right instanceof Node) { 701 if (left instanceof Boolean ) { 702 right = new Boolean (booleanF(right)); 703 hasSimpleArgs = true; 704 } 705 if (right instanceof Boolean ) { 706 left = new Boolean (booleanF(left)); 707 hasSimpleArgs = true; 708 } 709 } 710 711 if (hasSimpleArgs) { 712 switch (op) { 713 case GT: 714 return numberF(left, dom) > numberF(right, dom); 715 716 case LT: 717 return numberF(left, dom) < numberF(right, dom); 718 719 case GE: 720 return numberF(left, dom) >= numberF(right, dom); 721 722 case LE: 723 return numberF(left, dom) <= numberF(right, dom); 724 725 default: 726 runTimeError(RUN_TIME_INTERNAL_ERR, "compare()"); 727 } 728 } 729 } 731 732 if (hasSimpleArgs) { 733 if (left instanceof Boolean || right instanceof Boolean ) { 734 result = booleanF(left) == booleanF(right); 735 } 736 else if (left instanceof Double || right instanceof Double || 737 left instanceof Integer || right instanceof Integer ) { 738 result = numberF(left, dom) == numberF(right, dom); 739 } 740 else { result = stringF(left, dom).equals(stringF(right, dom)); 742 } 743 744 if (op == Operators.NE) { 745 result = !result; 746 } 747 } 748 else { 749 if (left instanceof Node) { 750 left = new SingletonIterator(((Node)left).node); 751 } 752 if (right instanceof Node) { 753 right = new SingletonIterator(((Node)right).node); 754 } 755 756 if (hasSimpleType(left) || 757 left instanceof DOM && right instanceof DTMAxisIterator) { 758 final Object temp = right; right = left; left = temp; 760 } 761 762 if (left instanceof DOM) { 763 if (right instanceof Boolean ) { 764 result = ((Boolean )right).booleanValue(); 765 return result == (op == Operators.EQ); 766 } 767 768 final String sleft = ((DOM)left).getStringValue(); 769 770 if (right instanceof Number ) { 771 result = ((Number )right).doubleValue() == 772 stringToReal(sleft); 773 } 774 else if (right instanceof String ) { 775 result = sleft.equals((String )right); 776 } 777 else if (right instanceof DOM) { 778 result = sleft.equals(((DOM)right).getStringValue()); 779 } 780 781 if (op == Operators.NE) { 782 result = !result; 783 } 784 return result; 785 } 786 787 789 DTMAxisIterator iter = ((DTMAxisIterator)left).reset(); 790 791 if (right instanceof DTMAxisIterator) { 792 result = compare(iter, (DTMAxisIterator)right, op, dom); 793 } 794 else if (right instanceof String ) { 795 result = compare(iter, (String )right, op, dom); 796 } 797 else if (right instanceof Number ) { 798 final double temp = ((Number )right).doubleValue(); 799 result = compare(iter, temp, op, dom); 800 } 801 else if (right instanceof Boolean ) { 802 boolean temp = ((Boolean )right).booleanValue(); 803 result = (iter.reset().next() != DTMAxisIterator.END) == temp; 804 } 805 else if (right instanceof DOM) { 806 result = compare(iter, ((DOM)right).getStringValue(), 807 op, dom); 808 } 809 else if (right == null) { 810 return(false); 811 } 812 else { 813 final String className = right.getClass().getName(); 814 runTimeError(INVALID_ARGUMENT_ERR, className, "compare()"); 815 } 816 } 817 return result; 818 } 819 820 823 public static boolean testLanguage(String testLang, DOM dom, int node) { 824 String nodeLang = dom.getLanguage(node); 826 if (nodeLang == null) 827 return(false); 828 else 829 nodeLang = nodeLang.toLowerCase(); 830 831 testLang = testLang.toLowerCase(); 833 if (testLang.length() == 2) { 834 return(nodeLang.startsWith(testLang)); 835 } 836 else { 837 return(nodeLang.equals(testLang)); 838 } 839 } 840 841 private static boolean hasSimpleType(Object obj) { 842 return obj instanceof Boolean || obj instanceof Double || 843 obj instanceof Integer || obj instanceof String || 844 obj instanceof Node || obj instanceof DOM; 845 } 846 847 850 public static double stringToReal(String s) { 851 try { 852 return Double.valueOf(s).doubleValue(); 853 } 854 catch (NumberFormatException e) { 855 return Double.NaN; 856 } 857 } 858 859 862 public static int stringToInt(String s) { 863 try { 864 return Integer.parseInt(s); 865 } 866 catch (NumberFormatException e) { 867 return(-1); } 869 } 870 871 private static final int DOUBLE_FRACTION_DIGITS = 340; 872 private static final double lowerBounds = 0.001; 873 private static final double upperBounds = 10000000; 874 private static DecimalFormat defaultFormatter; 875 private static String defaultPattern = ""; 876 877 static { 878 NumberFormat f = NumberFormat.getInstance(Locale.getDefault()); 879 defaultFormatter = (f instanceof DecimalFormat ) ? 880 (DecimalFormat ) f : new DecimalFormat (); 881 defaultFormatter.setMaximumFractionDigits(DOUBLE_FRACTION_DIGITS); 884 defaultFormatter.setMinimumFractionDigits(0); 885 defaultFormatter.setMinimumIntegerDigits(1); 886 defaultFormatter.setGroupingUsed(false); 887 } 888 889 893 public static String realToString(double d) { 894 final double m = Math.abs(d); 895 if ((m >= lowerBounds) && (m < upperBounds)) { 896 final String result = Double.toString(d); 897 final int length = result.length(); 898 if ((result.charAt(length-2) == '.') && 900 (result.charAt(length-1) == '0')) 901 return result.substring(0, length-2); 902 else 903 return result; 904 } 905 else { 906 if (Double.isNaN(d) || Double.isInfinite(d)) 907 return(Double.toString(d)); 908 return formatNumber(d, defaultPattern, defaultFormatter); 909 } 910 } 911 912 915 public static int realToInt(double d) { 916 return (int)d; 917 } 918 919 924 private static FieldPosition _fieldPosition = new FieldPosition (0); 925 926 public static String formatNumber(double number, String pattern, 927 DecimalFormat formatter) { 928 if (formatter == null) { 930 formatter = defaultFormatter; 931 } 932 try { 933 StringBuffer result = new StringBuffer (); 934 if (pattern != defaultPattern) { 935 formatter.applyLocalizedPattern(pattern); 936 } 937 formatter.format(number, result, _fieldPosition); 938 return result.toString(); 939 } 940 catch (IllegalArgumentException e) { 941 runTimeError(FORMAT_NUMBER_ERR, Double.toString(number), pattern); 942 return(EMPTYSTRING); 943 } 944 } 945 946 950 public static DTMAxisIterator referenceToNodeSet(Object obj) { 951 if (obj instanceof Node) { 953 return(new SingletonIterator(((Node)obj).node)); 954 } 955 else if (obj instanceof DTMAxisIterator) { 957 return(((DTMAxisIterator)obj).cloneIterator()); 958 } 959 else { 960 final String className = obj.getClass().getName(); 961 runTimeError(DATA_CONVERSION_ERR, className, "node-set"); 962 return null; 963 } 964 } 965 966 969 public static NodeList referenceToNodeList(Object obj, DOM dom) { 970 if (obj instanceof Node || obj instanceof DTMAxisIterator) { 971 DTMAxisIterator iter = referenceToNodeSet(obj); 972 return dom.makeNodeList(iter); 973 } 974 else if (obj instanceof DOM) { 975 dom = (DOM)obj; 976 return dom.makeNodeList(DTMDefaultBase.ROOTNODE); 977 } 978 else { 979 final String className = obj.getClass().getName(); 980 runTimeError(DATA_CONVERSION_ERR, className, 981 "org.w3c.dom.NodeList"); 982 return null; 983 } 984 } 985 986 989 public static org.w3c.dom.Node referenceToNode(Object obj, DOM dom) { 990 if (obj instanceof Node || obj instanceof DTMAxisIterator) { 991 DTMAxisIterator iter = referenceToNodeSet(obj); 992 return dom.makeNode(iter); 993 } 994 else if (obj instanceof DOM) { 995 dom = (DOM)obj; 996 DTMAxisIterator iter = dom.getChildren(DTMDefaultBase.ROOTNODE); 997 return dom.makeNode(iter); 998 } 999 else { 1000 final String className = obj.getClass().getName(); 1001 runTimeError(DATA_CONVERSION_ERR, className, "org.w3c.dom.Node"); 1002 return null; 1003 } 1004 } 1005 1006 1009 public static long referenceToLong(Object obj) { 1010 if (obj instanceof Number ) { 1011 return ((Number ) obj).longValue(); } 1013 else { 1014 final String className = obj.getClass().getName(); 1015 runTimeError(DATA_CONVERSION_ERR, className, Long.TYPE); 1016 return 0; 1017 } 1018 } 1019 1020 1023 public static double referenceToDouble(Object obj) { 1024 if (obj instanceof Number ) { 1025 return ((Number ) obj).doubleValue(); } 1027 else { 1028 final String className = obj.getClass().getName(); 1029 runTimeError(DATA_CONVERSION_ERR, className, Double.TYPE); 1030 return 0; 1031 } 1032 } 1033 1034 1037 public static boolean referenceToBoolean(Object obj) { 1038 if (obj instanceof Boolean ) { 1039 return ((Boolean ) obj).booleanValue(); 1040 } 1041 else { 1042 final String className = obj.getClass().getName(); 1043 runTimeError(DATA_CONVERSION_ERR, className, Boolean.TYPE); 1044 return false; 1045 } 1046 } 1047 1048 1051 public static String referenceToString(Object obj, DOM dom) { 1052 if (obj instanceof String ) { 1053 return (String ) obj; 1054 } 1055 else if (obj instanceof DTMAxisIterator) { 1056 return dom.getStringValueX(((DTMAxisIterator)obj).reset().next()); 1057 } 1058 else if (obj instanceof Node) { 1059 return dom.getStringValueX(((Node)obj).node); 1060 } 1061 else if (obj instanceof DOM) { 1062 return ((DOM) obj).getStringValue(); 1063 } 1064 else { 1065 final String className = obj.getClass().getName(); 1066 runTimeError(DATA_CONVERSION_ERR, className, String .class); 1067 return null; 1068 } 1069 } 1070 1071 1074 public static DTMAxisIterator node2Iterator(org.w3c.dom.Node node, 1075 Translet translet, DOM dom) 1076 { 1077 final org.w3c.dom.Node inNode = node; 1078 org.w3c.dom.NodeList nodelist = new org.w3c.dom.NodeList () { 1081 public int getLength() { 1082 return 1; 1083 } 1084 1085 public org.w3c.dom.Node item(int index) { 1086 if (index == 0) 1087 return inNode; 1088 else 1089 return null; 1090 } 1091 }; 1092 1093 return nodeList2Iterator(nodelist, translet, dom); 1094 } 1095 1096 1099 private static void copyNodes(org.w3c.dom.NodeList nodeList, 1100 org.w3c.dom.Document doc, org.w3c.dom.Node parent) 1101 { 1102 final int size = nodeList.getLength(); 1103 1104 for (int i = 0; i < size; i++) 1106 { 1107 org.w3c.dom.Node curr = nodeList.item(i); 1108 int nodeType = curr.getNodeType(); 1109 String value = null; 1110 try { 1111 value = curr.getNodeValue(); 1112 } catch (DOMException ex) { 1113 runTimeError(RUN_TIME_INTERNAL_ERR, ex.getMessage()); 1114 return; 1115 } 1116 1117 String nodeName = curr.getNodeName(); 1118 org.w3c.dom.Node newNode = null; 1119 switch (nodeType){ 1120 case org.w3c.dom.Node.ATTRIBUTE_NODE: 1121 newNode = doc.createAttributeNS(curr.getNamespaceURI(), 1122 nodeName); 1123 break; 1124 case org.w3c.dom.Node.CDATA_SECTION_NODE: 1125 newNode = doc.createCDATASection(value); 1126 break; 1127 case org.w3c.dom.Node.COMMENT_NODE: 1128 newNode = doc.createComment(value); 1129 break; 1130 case org.w3c.dom.Node.DOCUMENT_FRAGMENT_NODE: 1131 newNode = doc.createDocumentFragment(); 1132 break; 1133 case org.w3c.dom.Node.DOCUMENT_NODE: 1134 newNode = doc.createElementNS(null, "__document__"); 1135 copyNodes(curr.getChildNodes(), doc, newNode); 1136 break; 1137 case org.w3c.dom.Node.DOCUMENT_TYPE_NODE: 1138 break; 1140 case org.w3c.dom.Node.ELEMENT_NODE: 1141 org.w3c.dom.Element element = doc.createElementNS( 1144 curr.getNamespaceURI(), nodeName); 1145 if (curr.hasAttributes()) 1146 { 1147 org.w3c.dom.NamedNodeMap attributes = curr.getAttributes(); 1148 for (int k = 0; k < attributes.getLength(); k++) { 1149 org.w3c.dom.Node attr = attributes.item(k); 1150 element.setAttributeNS(attr.getNamespaceURI(), 1151 attr.getNodeName(), attr.getNodeValue()); 1152 } 1153 } 1154 copyNodes(curr.getChildNodes(), doc, element); 1155 newNode = element; 1156 break; 1157 case org.w3c.dom.Node.ENTITY_NODE: 1158 break; 1160 case org.w3c.dom.Node.ENTITY_REFERENCE_NODE: 1161 newNode = doc.createEntityReference(nodeName); 1162 break; 1163 case org.w3c.dom.Node.NOTATION_NODE: 1164 break; 1166 case org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE: 1167 newNode = doc.createProcessingInstruction(nodeName, 1168 value); 1169 break; 1170 case org.w3c.dom.Node.TEXT_NODE: 1171 newNode = doc.createTextNode(value); 1172 break; 1173 } 1174 try { 1175 parent.appendChild(newNode); 1176 } catch (DOMException e) { 1177 runTimeError(RUN_TIME_INTERNAL_ERR, e.getMessage()); 1178 return; 1179 } 1180 } 1181 } 1182 1183 1187 public static DTMAxisIterator nodeList2Iterator( 1188 org.w3c.dom.NodeList nodeList, 1189 Translet translet, DOM dom) 1190 { 1191 Document doc = null; 1193 try { 1194 doc = ((AbstractTranslet) translet).newDocument("", "__top__"); 1195 } catch (javax.xml.parsers.ParserConfigurationException e) { 1196 runTimeError(RUN_TIME_INTERNAL_ERR, e.getMessage()); 1197 return null; 1198 } 1199 1200 copyNodes(nodeList, doc, doc.getDocumentElement()); 1201 1202 if (dom instanceof MultiDOM) { 1204 final MultiDOM multiDOM = (MultiDOM) dom; 1205 1206 DTMDefaultBase dtm = (DTMDefaultBase)((DOMAdapter)multiDOM.getMain()).getDOMImpl(); 1207 DTMManager dtmManager = dtm.getManager(); 1208 1209 DOM idom = (DOM)dtmManager.getDTM(new DOMSource (doc), false, 1210 null, true, false); 1211 DOMAdapter domAdapter = new DOMAdapter(idom, 1213 translet.getNamesArray(), 1214 translet.getUrisArray(), 1215 translet.getTypesArray(), 1216 translet.getNamespaceArray()); 1217 multiDOM.addDOMAdapter(domAdapter); 1218 1219 DTMAxisIterator iter1 = idom.getAxisIterator(Axis.CHILD); 1220 DTMAxisIterator iter2 = idom.getAxisIterator(Axis.CHILD); 1221 DTMAxisIterator iter = new AbsoluteIterator( 1222 new StepIterator(iter1, iter2)); 1223 1224 iter.setStartNode(DTMDefaultBase.ROOTNODE); 1225 return iter; 1226 } 1227 else { 1228 runTimeError(RUN_TIME_INTERNAL_ERR, "nodeList2Iterator()"); 1229 return null; 1230 } 1231 } 1232 1233 1236 public static DOM referenceToResultTree(Object obj) { 1237 try { 1238 return ((DOM) obj); 1239 } 1240 catch (IllegalArgumentException e) { 1241 final String className = obj.getClass().getName(); 1242 runTimeError(DATA_CONVERSION_ERR, "reference", className); 1243 return null; 1244 } 1245 } 1246 1247 1251 public static DTMAxisIterator getSingleNode(DTMAxisIterator iterator) { 1252 int node = iterator.next(); 1253 return(new SingletonIterator(node)); 1254 } 1255 1256 1259 private static char[] _characterArray = new char[32]; 1260 1261 public static void copy(Object obj, 1262 SerializationHandler handler, 1263 int node, 1264 DOM dom) { 1265 try { 1266 if (obj instanceof DTMAxisIterator) 1267 { 1268 DTMAxisIterator iter = (DTMAxisIterator) obj; 1269 dom.copy(iter.reset(), handler); 1270 } 1271 else if (obj instanceof Node) { 1272 dom.copy(((Node) obj).node, handler); 1273 } 1274 else if (obj instanceof DOM) { 1275 DOM newDom = (DOM)obj; 1277 newDom.copy(newDom.getDocument(), handler); 1278 } 1279 else { 1280 String string = obj.toString(); final int length = string.length(); 1282 if (length > _characterArray.length) 1283 _characterArray = new char[length]; 1284 string.getChars(0, length, _characterArray, 0); 1285 handler.characters(_characterArray, 0, length); 1286 } 1287 } 1288 catch (SAXException e) { 1289 runTimeError(RUN_TIME_COPY_ERR); 1290 } 1291 } 1292 1293 1297 public static void checkAttribQName(String name) { 1298 final int firstOccur = name.indexOf(":"); 1299 final int lastOccur = name.lastIndexOf(":"); 1300 final String localName = name.substring(lastOccur + 1); 1301 1302 if (firstOccur > 0) { 1303 final String newPrefix = name.substring(0, firstOccur); 1304 1305 if (firstOccur != lastOccur) { 1306 final String oriPrefix = name.substring(firstOccur+1, lastOccur); 1307 if (!XMLChar.isValidNCName(oriPrefix)) { 1308 runTimeError(INVALID_QNAME_ERR,oriPrefix+":"+localName); 1310 } 1311 } 1312 1313 if (!XMLChar.isValidNCName(newPrefix)) { 1315 runTimeError(INVALID_QNAME_ERR,newPrefix+":"+localName); 1316 } 1317 } 1318 1319 if ((!XMLChar.isValidNCName(localName))||(localName.equals(Constants.XMLNS_PREFIX))) { 1321 runTimeError(INVALID_QNAME_ERR,localName); 1322 } 1323 } 1324 1325 1329 public static void checkNCName(String name) { 1330 if (!XMLChar.isValidNCName(name)) { 1331 runTimeError(INVALID_NCNAME_ERR,name); 1332 } 1333 } 1334 1335 1339 public static void checkQName(String name) { 1340 if (!XMLChar.isValidQName(name)) { 1341 runTimeError(INVALID_QNAME_ERR,name); 1342 } 1343 } 1344 1345 1348 public static String startXslElement(String qname, String namespace, 1349 SerializationHandler handler, DOM dom, int node) 1350 { 1351 try { 1352 String prefix; 1354 final int index = qname.indexOf(':'); 1355 1356 if (index > 0) { 1357 prefix = qname.substring(0, index); 1358 1359 if (namespace == null || namespace.length() == 0) { 1361 try { 1362 namespace = dom.lookupNamespace(node, prefix); 1364 } 1365 catch(RuntimeException e) { 1366 handler.flushPending(); NamespaceMappings nm = handler.getNamespaceMappings(); 1368 namespace = nm.lookupNamespace(prefix); 1369 if (namespace == null) { 1370 runTimeError(NAMESPACE_PREFIX_ERR,prefix); 1371 } 1372 } 1373 } 1374 1375 handler.startElement(namespace, qname.substring(index+1), 1376 qname); 1377 handler.namespaceAfterStartElement(prefix, namespace); 1378 } 1379 else { 1380 if (namespace != null && namespace.length() > 0) { 1382 prefix = generatePrefix(); 1383 qname = prefix + ':' + qname; 1384 handler.startElement(namespace, qname, qname); 1385 handler.namespaceAfterStartElement(prefix, namespace); 1386 } 1387 else { 1388 handler.startElement(null, null, qname); 1389 } 1390 } 1391 } 1392 catch (SAXException e) { 1393 throw new RuntimeException (e.getMessage()); 1394 } 1395 1396 return qname; 1397 } 1398 1399 1402 public static String getPrefix(String qname) { 1403 final int index = qname.indexOf(':'); 1404 return (index > 0) ? qname.substring(0, index) : null; 1405 } 1406 1407 1410 private static int prefixIndex = 0; public static String generatePrefix() { 1412 return ("ns" + prefixIndex++); 1413 } 1414 1415 public static final String RUN_TIME_INTERNAL_ERR = 1416 "RUN_TIME_INTERNAL_ERR"; 1417 public static final String RUN_TIME_COPY_ERR = 1418 "RUN_TIME_COPY_ERR"; 1419 public static final String DATA_CONVERSION_ERR = 1420 "DATA_CONVERSION_ERR"; 1421 public static final String EXTERNAL_FUNC_ERR = 1422 "EXTERNAL_FUNC_ERR"; 1423 public static final String EQUALITY_EXPR_ERR = 1424 "EQUALITY_EXPR_ERR"; 1425 public static final String INVALID_ARGUMENT_ERR = 1426 "INVALID_ARGUMENT_ERR"; 1427 public static final String FORMAT_NUMBER_ERR = 1428 "FORMAT_NUMBER_ERR"; 1429 public static final String ITERATOR_CLONE_ERR = 1430 "ITERATOR_CLONE_ERR"; 1431 public static final String AXIS_SUPPORT_ERR = 1432 "AXIS_SUPPORT_ERR"; 1433 public static final String TYPED_AXIS_SUPPORT_ERR = 1434 "TYPED_AXIS_SUPPORT_ERR"; 1435 public static final String STRAY_ATTRIBUTE_ERR = 1436 "STRAY_ATTRIBUTE_ERR"; 1437 public static final String STRAY_NAMESPACE_ERR = 1438 "STRAY_NAMESPACE_ERR"; 1439 public static final String NAMESPACE_PREFIX_ERR = 1440 "NAMESPACE_PREFIX_ERR"; 1441 public static final String DOM_ADAPTER_INIT_ERR = 1442 "DOM_ADAPTER_INIT_ERR"; 1443 public static final String PARSER_DTD_SUPPORT_ERR = 1444 "PARSER_DTD_SUPPORT_ERR"; 1445 public static final String NAMESPACES_SUPPORT_ERR = 1446 "NAMESPACES_SUPPORT_ERR"; 1447 public static final String CANT_RESOLVE_RELATIVE_URI_ERR = 1448 "CANT_RESOLVE_RELATIVE_URI_ERR"; 1449 public static final String UNSUPPORTED_XSL_ERR = 1450 "UNSUPPORTED_XSL_ERR"; 1451 public static final String UNSUPPORTED_EXT_ERR = 1452 "UNSUPPORTED_EXT_ERR"; 1453 public static final String UNKNOWN_TRANSLET_VERSION_ERR = 1454 "UNKNOWN_TRANSLET_VERSION_ERR"; 1455 public static final String INVALID_QNAME_ERR = "INVALID_QNAME_ERR"; 1456 public static final String INVALID_NCNAME_ERR = "INVALID_NCNAME_ERR"; 1457 public static final String UNALLOWED_EXTENSION_FUNCTION_ERR = "UNALLOWED_EXTENSION_FUNCTION_ERR"; 1458 public static final String UNALLOWED_EXTENSION_ELEMENT_ERR = "UNALLOWED_EXTENSION_ELEMENT_ERR"; 1459 1460 protected static ResourceBundle m_bundle; 1462 1463 public final static String ERROR_MESSAGES_KEY = "error-messages"; 1464 1465 static { 1466 String resource = "com.sun.org.apache.xalan.internal.xsltc.runtime.ErrorMessages"; 1467 m_bundle = ResourceBundle.getBundle(resource); 1468 } 1469 1470 1473 public static void runTimeError(String code) { 1474 throw new RuntimeException (m_bundle.getString(code)); 1475 } 1476 1477 public static void runTimeError(String code, Object [] args) { 1478 final String message = MessageFormat.format(m_bundle.getString(code), 1479 args); 1480 throw new RuntimeException (message); 1481 } 1482 1483 public static void runTimeError(String code, Object arg0) { 1484 runTimeError(code, new Object []{ arg0 } ); 1485 } 1486 1487 public static void runTimeError(String code, Object arg0, Object arg1) { 1488 runTimeError(code, new Object []{ arg0, arg1 } ); 1489 } 1490 1491 public static void consoleOutput(String msg) { 1492 System.out.println(msg); 1493 } 1494 1495 1498 public static String replace(String base, char ch, String str) { 1499 return (base.indexOf(ch) < 0) ? base : 1500 replace(base, String.valueOf(ch), new String [] { str }); 1501 } 1502 1503 public static String replace(String base, String delim, String [] str) { 1504 final int len = base.length(); 1505 final StringBuffer result = new StringBuffer (); 1506 1507 for (int i = 0; i < len; i++) { 1508 final char ch = base.charAt(i); 1509 final int k = delim.indexOf(ch); 1510 1511 if (k >= 0) { 1512 result.append(str[k]); 1513 } 1514 else { 1515 result.append(ch); 1516 } 1517 } 1518 return result.toString(); 1519 } 1520 1521 1522 1531 public static String mapQNameToJavaName (String base ) { 1532 return replace(base, ".-:/{}?#%*", 1533 new String [] { "$dot$", "$dash$" ,"$colon$", "$slash$", 1534 "","$colon$","$ques$","$hash$","$per$", 1535 "$aster$"}); 1536 1537 } 1538 1539 } 1541 | Popular Tags |