1 55 56 package org.jboss.axis.encoding.ser; 57 58 import org.jboss.axis.Constants; 59 import org.jboss.axis.MessageContext; 60 import org.jboss.axis.encoding.DeserializationContext; 61 import org.jboss.axis.encoding.Deserializer; 62 import org.jboss.axis.encoding.DeserializerImpl; 63 import org.jboss.axis.encoding.DeserializerTarget; 64 import org.jboss.axis.message.SOAPHandler; 65 import org.jboss.axis.soap.SOAPConstants; 66 import org.jboss.axis.utils.ClassUtils; 67 import org.jboss.axis.utils.JavaUtils; 68 import org.jboss.axis.utils.Messages; 69 import org.jboss.axis.wsdl.symbolTable.SchemaUtils; 70 import org.jboss.logging.Logger; 71 import org.xml.sax.Attributes ; 72 import org.xml.sax.SAXException ; 73 74 import javax.xml.namespace.QName ; 75 import java.util.ArrayList ; 76 import java.util.HashMap ; 77 import java.util.StringTokenizer ; 78 79 80 91 public class ArrayDeserializer extends DeserializerImpl 92 { 93 private static Logger log = Logger.getLogger(ArrayDeserializer.class.getName()); 94 95 public QName arrayType = null; 96 public int curIndex = 0; 97 QName defaultItemType; 98 int length; 99 Class arrayClass = null; 100 ArrayList mDimLength = null; ArrayList mDimFactor = null; SOAPConstants soapConstants = SOAPConstants.SOAP11_CONSTANTS; 103 104 116 public void onStartElement(String namespace, String localName, 117 String prefix, Attributes attributes, 118 DeserializationContext context) 119 throws SAXException 120 { 121 147 if (log.isDebugEnabled()) 148 { 149 log.debug("Enter: ArrayDeserializer::startElement()"); 150 } 151 152 MessageContext msgContext = context.getMessageContext(); 153 if (msgContext != null) 154 { 155 soapConstants = msgContext.getSOAPConstants(); 156 } 157 158 QName typeQName = context.getTypeFromAttributes(namespace, 161 localName, 162 attributes); 163 if (typeQName == null) 164 { 165 typeQName = getDefaultType(); 166 } 167 168 if (typeQName != null && 169 Constants.equals(Constants.SOAP_ARRAY, typeQName)) 170 { 171 typeQName = null; 172 } 173 174 QName arrayTypeValue = context.getQNameFromString(Constants.getValue(attributes, 176 Constants.URIS_SOAP_ENC, 177 soapConstants.getAttrItemType())); 178 179 String dimString = null; 183 QName innerQName = null; 184 String innerDimString = ""; 185 if (arrayTypeValue != null) 186 { 187 if (soapConstants != SOAPConstants.SOAP12_CONSTANTS) 188 { 189 String arrayTypeValueNamespaceURI = 190 arrayTypeValue.getNamespaceURI(); 191 String arrayTypeValueLocalPart = 192 arrayTypeValue.getLocalPart(); 193 194 int leftBracketIndex = 195 arrayTypeValueLocalPart.lastIndexOf('['); 196 int rightBracketIndex = 197 arrayTypeValueLocalPart.lastIndexOf(']'); 198 if (leftBracketIndex == -1 199 || rightBracketIndex == -1 200 || rightBracketIndex < leftBracketIndex) 201 { 202 throw new IllegalArgumentException (Messages.getMessage("badArrayType00", 203 "" + arrayTypeValue)); 204 } 205 206 dimString = 207 arrayTypeValueLocalPart.substring(leftBracketIndex + 1, 208 rightBracketIndex); 209 arrayTypeValueLocalPart = 210 arrayTypeValueLocalPart.substring(0, leftBracketIndex); 211 212 if (arrayTypeValueLocalPart.endsWith("]")) 214 { 215 defaultItemType = Constants.SOAP_ARRAY; 216 innerQName = new QName (arrayTypeValueNamespaceURI, 217 arrayTypeValueLocalPart.substring(0, 218 arrayTypeValueLocalPart.indexOf("["))); 219 innerDimString = arrayTypeValueLocalPart.substring(arrayTypeValueLocalPart.indexOf("[")); 220 } 221 else 222 { 223 defaultItemType = new QName (arrayTypeValueNamespaceURI, 224 arrayTypeValueLocalPart); 225 } 226 227 } 228 else 229 { 230 String arraySizeValue = attributes.getValue(soapConstants.getEncodingURI(), Constants.ATTR_ARRAY_SIZE); 231 int leftStarIndex = arraySizeValue.lastIndexOf('*'); 232 233 if (leftStarIndex != -1) 235 { 236 if (leftStarIndex == 0 && arraySizeValue.length() == 1) 238 { 239 } 241 else if (leftStarIndex == (arraySizeValue.length() - 1)) 242 { 243 throw new IllegalArgumentException (Messages.getMessage("badArraySize00", 244 "" + arraySizeValue)); 245 } 247 else 248 { 249 dimString = arraySizeValue.substring(leftStarIndex + 2); 250 innerQName = arrayTypeValue; 251 innerDimString = arraySizeValue.substring(0, leftStarIndex + 1); 252 } 253 } 254 else 255 { 256 dimString = arraySizeValue; 257 } 258 259 if (innerDimString == null || innerDimString.length() == 0) 260 { 261 defaultItemType = arrayTypeValue; 262 } 263 else 264 { 265 defaultItemType = Constants.SOAP_ARRAY12; 266 } 267 } 268 } 269 270 if (defaultItemType == null && typeQName == null) 272 { 273 defaultItemType = Constants.XSD_ANYTYPE; 274 } 275 276 arrayClass = null; 278 if (typeQName != null) 279 { 280 arrayClass = context.getTypeMapping(). 281 getClassForQName(typeQName); 282 } 283 else 284 { 285 Class arrayItemClass = null; 288 QName compQName = defaultItemType; 289 290 String dims = "[]"; 292 if (innerQName != null) 293 { 294 compQName = innerQName; 295 296 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) 297 { 298 int offset = 0; 299 while ((offset = innerDimString.indexOf('*', offset)) != -1) 300 { 301 dims += "[]"; 302 offset++; 303 } 304 } 305 else 306 { 307 dims += innerDimString; 308 } 309 } 310 311 arrayItemClass = context.getTypeMapping(). 312 getClassForQName(compQName); 313 if (arrayItemClass != null) 314 { 315 try 316 { 317 String textClassName = JavaUtils.getTextClassName(arrayItemClass.getName()) + dims; 318 String loadableClassName = JavaUtils.getLoadableClassName(textClassName); 319 arrayClass = ClassUtils.forName(loadableClassName); 320 } 321 catch (Exception e) 322 { 323 throw new SAXException (Messages.getMessage("noComponent00", 324 "" + defaultItemType)); 325 } 326 } 327 } 328 329 if (arrayClass == null) 330 { 331 throw new SAXException (Messages.getMessage("noComponent00", "" + defaultItemType)); 332 } 333 334 if (dimString == null || dimString.length() == 0) 335 { 336 value = new ArrayListExtension(arrayClass); 338 } 339 else 340 { 341 try 342 { 343 StringTokenizer tokenizer; 344 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) 345 { 346 tokenizer = new StringTokenizer (dimString); 347 } 348 else 349 { 350 tokenizer = new StringTokenizer (dimString, "[],"); 351 } 352 353 length = Integer.parseInt(tokenizer.nextToken()); 354 if (tokenizer.hasMoreTokens()) 355 { 356 mDimLength = new ArrayList (); 362 mDimLength.add(new Integer (length)); 363 364 while (tokenizer.hasMoreTokens()) 365 { 366 mDimLength.add(new Integer (Integer.parseInt(tokenizer.nextToken()))); 367 } 368 } 369 370 ArrayList list = new ArrayListExtension(arrayClass, length); 373 for (int i = 0; i < length; i++) 375 { 376 list.add(null); 377 } 378 value = list; 379 380 } 381 catch (NumberFormatException e) 382 { 383 throw new IllegalArgumentException (Messages.getMessage("badInteger00", dimString)); 384 } 385 } 386 387 String offset = Constants.getValue(attributes, 389 Constants.URIS_SOAP_ENC, 390 Constants.ATTR_OFFSET); 391 if (offset != null) 392 { 393 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) 394 { 395 throw new SAXException (Messages.getMessage("noSparseArray")); 396 } 397 398 int leftBracketIndex = offset.lastIndexOf('['); 399 int rightBracketIndex = offset.lastIndexOf(']'); 400 401 if (leftBracketIndex == -1 402 || rightBracketIndex == -1 403 || rightBracketIndex < leftBracketIndex) 404 { 405 throw new SAXException (Messages.getMessage("badOffset00", offset)); 406 } 407 408 curIndex = 409 convertToIndex(offset.substring(leftBracketIndex + 1, 410 rightBracketIndex), 411 "badOffset00"); 412 } 413 414 if (log.isDebugEnabled()) 415 { 416 log.debug("Exit: ArrayDeserializer::startElement()"); 417 } 418 } 419 420 421 433 public SOAPHandler onStartChild(String namespace, 434 String localName, 435 String prefix, 436 Attributes attributes, 437 DeserializationContext context) 438 throws SAXException 439 { 440 if (log.isDebugEnabled()) 441 { 442 log.debug("Enter: ArrayDeserializer.onStartChild()"); 443 } 444 445 if (attributes != null) 448 { 449 String pos = 450 Constants.getValue(attributes, 451 Constants.URIS_SOAP_ENC, 452 Constants.ATTR_POSITION); 453 if (pos != null) 454 { 455 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) 456 { 457 throw new SAXException (Messages.getMessage("noSparseArray")); 458 } 459 460 int leftBracketIndex = pos.lastIndexOf('['); 461 int rightBracketIndex = pos.lastIndexOf(']'); 462 463 if (leftBracketIndex == -1 464 || rightBracketIndex == -1 465 || rightBracketIndex < leftBracketIndex) 466 { 467 throw new SAXException (Messages.getMessage("badPosition00", pos)); 468 } 469 470 curIndex = 471 convertToIndex(pos.substring(leftBracketIndex + 1, 472 rightBracketIndex), 473 "badPosition00"); 474 } 475 476 if (context.isNil(attributes)) 479 { 480 setChildValue(null, new Integer (curIndex++)); 481 return null; 482 } 483 } 484 485 QName itemType = context.getTypeFromAttributes(namespace, 487 localName, 488 attributes); 489 490 Deserializer dSer = null; 492 if (itemType != null && (context.getCurElement().getHref() == null)) 493 { 494 dSer = context.getDeserializerForType(itemType); 495 } 496 497 if (dSer == null) 498 { 499 QName defaultType = defaultItemType; 501 Class javaType = null; 502 if (arrayClass != null && 503 arrayClass.isArray() && 504 defaultType == null) 505 { 506 javaType = arrayClass.getComponentType(); 507 defaultType = context.getTypeMapping().getTypeQName(javaType); 508 } 509 510 if (itemType == null && dSer == null) 520 { 521 if (defaultType != null && SchemaUtils.isSimpleSchemaType(defaultType)) 522 { 523 dSer = context.getDeserializer(javaType, defaultType); 524 } 525 } 526 527 if (dSer == null) 532 { 533 dSer = new DeserializerImpl(); 534 if (itemType == null) 536 { 537 dSer.setDefaultType(defaultType); 538 } 539 } 540 } 541 542 543 dSer.registerValueTarget(new DeserializerTarget(this, new Integer (curIndex))); 546 547 addChildDeserializer(dSer); 550 551 curIndex++; 552 553 if (log.isDebugEnabled()) 554 { 555 log.debug("Exit: ArrayDeserializer.onStartChild()"); 556 } 557 558 return (SOAPHandler)dSer; 559 } 560 561 public void characters(char[] chars, int i, int i1) throws SAXException 562 { 563 for (int idx = i; i < i1; i++) 564 { 565 if (!Character.isWhitespace(chars[idx])) 566 throw new SAXException (Messages.getMessage("charsInArray")); 567 } 568 } 569 570 586 public void setChildValue(Object value, Object hint) throws SAXException 587 { 588 if (log.isDebugEnabled()) 589 { 590 log.debug("Enter: ArrayDeserializer::setValue(" + value + ", " + hint + ")"); 591 } 592 ArrayList list = (ArrayList )this.value; 593 int offset = ((Integer )hint).intValue(); 594 595 if (this.mDimLength == null) 596 { 597 while (list.size() <= offset) 600 { 601 list.add(null); 602 } 603 604 list.set(offset, value); 605 } 606 else 607 { 608 611 ArrayList mDimIndex = toMultiIndex(offset); 613 614 for (int i = 0; i < mDimLength.size(); i++) 616 { 617 int length = ((Integer )mDimLength.get(i)).intValue(); 618 int index = ((Integer )mDimIndex.get(i)).intValue(); 619 while (list.size() < length) 620 { 621 list.add(null); 622 } 623 if (i < mDimLength.size() - 1) 626 { 627 if (list.get(index) == null) 628 { 629 list.set(index, new ArrayList ()); 630 } 631 list = (ArrayList )list.get(index); 632 } 633 else 634 { 635 list.set(index, value); 636 } 637 } 638 } 639 } 640 641 647 public void valueComplete() throws SAXException 648 { 649 if (componentsReady()) 650 { 651 try 652 { 653 if (arrayClass != null) 654 { 655 value = JavaUtils.convert(value, arrayClass); 656 } 657 } 658 catch (RuntimeException e) 659 { 660 } 662 } 663 664 super.valueComplete(); 665 } 666 667 680 private int convertToIndex(String text, String exceptKey) 681 throws SAXException 682 { 683 StringTokenizer tokenizer = new StringTokenizer (text, "[],"); 684 int index = 0; 685 try 686 { 687 if (mDimLength == null) 688 { 689 index = Integer.parseInt(tokenizer.nextToken()); 691 if (tokenizer.hasMoreTokens()) 692 { 693 throw new SAXException (Messages.getMessage(exceptKey, text)); 694 } 695 } 696 else 697 { 698 int dim = -1; 700 ArrayList work = new ArrayList (); 701 while (tokenizer.hasMoreTokens()) 702 { 703 dim++; 706 if (dim >= mDimLength.size()) 707 { 708 throw new SAXException (Messages.getMessage(exceptKey, text)); 709 } 710 int workIndex = Integer.parseInt(tokenizer.nextToken()); 712 713 if (workIndex < 0 || 715 workIndex >= 716 ((Integer )mDimLength.get(dim)).intValue()) 717 { 718 throw new SAXException (Messages.getMessage(exceptKey, text)); 719 } 720 work.add(new Integer (workIndex)); 721 } 722 index = toSingleIndex(work); } 724 } 725 catch (SAXException e) 726 { 727 throw e; 728 } 729 catch (Exception e) 730 { 731 throw new SAXException (Messages.getMessage(exceptKey, text)); 732 } 733 return index; 734 } 735 736 742 private ArrayList toMultiIndex(int single) 743 { 744 if (mDimLength == null) 745 return null; 746 747 if (mDimFactor == null) 749 { 750 mDimFactor = new ArrayList (); 751 for (int i = 0; i < mDimLength.size(); i++) 752 { 753 int factor = 1; 754 for (int j = i + 1; j < mDimLength.size(); j++) 755 { 756 factor *= ((Integer )mDimLength.get(j)).intValue(); 757 } 758 mDimFactor.add(new Integer (factor)); 759 } 760 } 761 762 ArrayList rc = new ArrayList (); 763 for (int i = 0; i < mDimLength.size(); i++) 764 { 765 int factor = ((Integer )mDimFactor.get(i)).intValue(); 766 rc.add(new Integer (single / factor)); 767 single = single % factor; 768 } 769 return rc; 770 } 771 772 778 private int toSingleIndex(ArrayList indexArray) 779 { 780 if (mDimLength == null || indexArray == null) 781 return -1; 782 783 if (mDimFactor == null) 785 { 786 mDimFactor = new ArrayList (); 787 for (int i = 0; i < mDimLength.size(); i++) 788 { 789 int factor = 1; 790 for (int j = i + 1; j < mDimLength.size(); j++) 791 { 792 factor *= ((Integer )mDimLength.get(j)).intValue(); 793 } 794 mDimFactor.add(new Integer (factor)); 795 } 796 } 797 798 int single = 0; 799 for (int i = 0; i < indexArray.size(); i++) 800 { 801 single += ((Integer )mDimFactor.get(i)).intValue() * 802 ((Integer )indexArray.get(i)).intValue(); 803 } 804 return single; 805 } 806 807 814 public class ArrayListExtension extends ArrayList 815 implements JavaUtils.ConvertCache 816 { 817 private HashMap table = null; 818 private Class arrayClass = null; 820 823 ArrayListExtension(Class arrayClass) 824 { 825 super(); 826 this.arrayClass = arrayClass; 827 if (arrayClass == null || 830 arrayClass.isInterface() || 831 java.lang.reflect.Modifier.isAbstract(arrayClass.getModifiers())) 832 { 833 arrayClass = null; 834 } 835 } 836 837 ArrayListExtension(Class arrayClass, int length) 838 { 839 super(length); 840 this.arrayClass = arrayClass; 841 if (arrayClass == null || 844 arrayClass.isInterface() || 845 java.lang.reflect.Modifier.isAbstract(arrayClass.getModifiers())) 846 { 847 arrayClass = null; 848 } 849 } 850 851 854 public void setConvertedValue(Class cls, Object value) 855 { 856 if (table == null) 857 table = new HashMap (); 858 table.put(cls, value); 859 } 860 861 864 public Object getConvertedValue(Class cls) 865 { 866 if (table == null) 867 return null; 868 return table.get(cls); 869 } 870 871 874 public Class getDestClass() 875 { 876 return arrayClass; 877 } 878 } 879 880 } 881 | Popular Tags |