1 16 19 package org.apache.xalan.processor; 20 21 import java.util.Enumeration ; 22 import java.util.Hashtable ; 23 24 import org.apache.xalan.templates.Constants; 25 import org.apache.xml.utils.QName; 26 27 33 public class XSLTElementDef 34 { 35 36 40 XSLTElementDef(){} 41 42 53 XSLTElementDef(XSLTSchema schema, String namespace, String name, String nameAlias, 54 XSLTElementDef[] elements, XSLTAttributeDef[] attributes, 55 XSLTElementProcessor contentHandler, Class classObject) 56 { 57 build(namespace, name, nameAlias, elements, attributes, contentHandler, 58 classObject); 59 if ( (null != namespace) 60 && (namespace.equals(Constants.S_XSLNAMESPACEURL) 61 || namespace.equals(Constants.S_BUILTIN_EXTENSIONS_URL) 62 || namespace.equals(Constants.S_BUILTIN_OLD_EXTENSIONS_URL))) 63 { 64 schema.addAvailableElement(new QName(namespace, name)); 65 if(null != nameAlias) 66 schema.addAvailableElement(new QName(namespace, nameAlias)); 67 } 68 } 69 70 82 XSLTElementDef(XSLTSchema schema, String namespace, String name, String nameAlias, 83 XSLTElementDef[] elements, XSLTAttributeDef[] attributes, 84 XSLTElementProcessor contentHandler, Class classObject, boolean has_required) 85 { 86 this.m_has_required = has_required; 87 build(namespace, name, nameAlias, elements, attributes, contentHandler, 88 classObject); 89 if ( (null != namespace) 90 && (namespace.equals(Constants.S_XSLNAMESPACEURL) 91 || namespace.equals(Constants.S_BUILTIN_EXTENSIONS_URL) 92 || namespace.equals(Constants.S_BUILTIN_OLD_EXTENSIONS_URL))) 93 { 94 schema.addAvailableElement(new QName(namespace, name)); 95 if(null != nameAlias) 96 schema.addAvailableElement(new QName(namespace, nameAlias)); 97 } 98 99 } 100 101 114 XSLTElementDef(XSLTSchema schema, String namespace, String name, String nameAlias, 115 XSLTElementDef[] elements, XSLTAttributeDef[] attributes, 116 XSLTElementProcessor contentHandler, Class classObject, 117 boolean has_required, boolean required) 118 { 119 this(schema, namespace, name, nameAlias, 120 elements, attributes, 121 contentHandler, classObject, has_required); 122 this.m_required = required; 123 } 124 125 140 XSLTElementDef(XSLTSchema schema, String namespace, String name, String nameAlias, 141 XSLTElementDef[] elements, XSLTAttributeDef[] attributes, 142 XSLTElementProcessor contentHandler, Class classObject, 143 boolean has_required, boolean required, int order, 144 boolean multiAllowed) 145 { 146 this(schema, namespace, name, nameAlias, 147 elements, attributes, 148 contentHandler, classObject, has_required, required); 149 this.m_order = order; 150 this.m_multiAllowed = multiAllowed; 151 } 152 153 169 XSLTElementDef(XSLTSchema schema, String namespace, String name, String nameAlias, 170 XSLTElementDef[] elements, XSLTAttributeDef[] attributes, 171 XSLTElementProcessor contentHandler, Class classObject, 172 boolean has_required, boolean required, boolean has_order, int order, 173 boolean multiAllowed) 174 { 175 this(schema, namespace, name, nameAlias, 176 elements, attributes, 177 contentHandler, classObject, has_required, required); 178 this.m_order = order; 179 this.m_multiAllowed = multiAllowed; 180 this.m_isOrdered = has_order; 181 } 182 183 197 XSLTElementDef(XSLTSchema schema, String namespace, String name, String nameAlias, 198 XSLTElementDef[] elements, XSLTAttributeDef[] attributes, 199 XSLTElementProcessor contentHandler, Class classObject, 200 boolean has_order, int order, boolean multiAllowed) 201 { 202 this(schema, namespace, name, nameAlias, 203 elements, attributes, 204 contentHandler, classObject, 205 order, multiAllowed); 206 this.m_isOrdered = has_order; 207 } 208 209 222 XSLTElementDef(XSLTSchema schema, String namespace, String name, String nameAlias, 223 XSLTElementDef[] elements, XSLTAttributeDef[] attributes, 224 XSLTElementProcessor contentHandler, Class classObject, 225 int order, boolean multiAllowed) 226 { 227 this(schema, namespace, name, nameAlias, elements, attributes, contentHandler, 228 classObject); 229 this.m_order = order; 230 this.m_multiAllowed = multiAllowed; 231 } 232 233 240 XSLTElementDef(Class classObject, XSLTElementProcessor contentHandler, 241 int type) 242 { 243 244 this.m_classObject = classObject; 245 this.m_type = type; 246 247 setElementProcessor(contentHandler); 248 } 249 250 261 void build(String namespace, String name, String nameAlias, 262 XSLTElementDef[] elements, XSLTAttributeDef[] attributes, 263 XSLTElementProcessor contentHandler, Class classObject) 264 { 265 266 this.m_namespace = namespace; 267 this.m_name = name; 268 this.m_nameAlias = nameAlias; 269 this.m_elements = elements; 270 this.m_attributes = attributes; 271 272 setElementProcessor(contentHandler); 273 274 this.m_classObject = classObject; 275 276 if (hasRequired() && m_elements != null) 277 { 278 int n = m_elements.length; 279 for (int i = 0; i < n; i++) 280 { 281 XSLTElementDef def = m_elements[i]; 282 283 if (def != null && def.getRequired()) 284 { 285 if (m_requiredFound == null) 286 m_requiredFound = new Hashtable (); 287 m_requiredFound.put(def.getName(), "xsl:" +def.getName()); 288 } 289 } 290 } 291 } 292 293 303 private static boolean equalsMayBeNull(Object obj1, Object obj2) 304 { 305 return (obj2 == obj1) 306 || ((null != obj1) && (null != obj2) && obj2.equals(obj1)); 307 } 308 309 324 private static boolean equalsMayBeNullOrZeroLen(String s1, String s2) 325 { 326 327 int len1 = (s1 == null) ? 0 : s1.length(); 328 int len2 = (s2 == null) ? 0 : s2.length(); 329 330 return (len1 != len2) ? false 331 : (len1 == 0) ? true 332 : s1.equals(s2); 333 } 334 335 336 static final int T_ELEMENT = 1, T_PCDATA = 2, T_ANY = 3; 337 338 341 private int m_type = T_ELEMENT; 342 343 348 int getType() 349 { 350 return m_type; 351 } 352 353 358 void setType(int t) 359 { 360 m_type = t; 361 } 362 363 366 private String m_namespace; 367 368 373 String getNamespace() 374 { 375 return m_namespace; 376 } 377 378 381 private String m_name; 382 383 388 String getName() 389 { 390 return m_name; 391 } 392 393 396 private String m_nameAlias; 397 398 403 String getNameAlias() 404 { 405 return m_nameAlias; 406 } 407 408 411 private XSLTElementDef[] m_elements; 412 413 418 XSLTElementDef[] getElements() 419 { 420 return m_elements; 421 } 422 423 428 void setElements(XSLTElementDef[] defs) 429 { 430 m_elements = defs; 431 } 432 433 442 private boolean QNameEquals(String uri, String localName) 443 { 444 445 return (equalsMayBeNullOrZeroLen(m_namespace, uri) 446 && (equalsMayBeNullOrZeroLen(m_name, localName) 447 || equalsMayBeNullOrZeroLen(m_nameAlias, localName))); 448 } 449 450 459 XSLTElementProcessor getProcessorFor(String uri, String localName) 460 { 461 462 XSLTElementProcessor elemDef = null; 464 if (null == m_elements) 465 return null; 466 467 int n = m_elements.length; 468 int order = -1; 469 boolean multiAllowed = true; 470 for (int i = 0; i < n; i++) 471 { 472 XSLTElementDef def = m_elements[i]; 473 474 if (def.m_name.equals("*")) 478 { 479 480 if (!equalsMayBeNullOrZeroLen(uri, Constants.S_XSLNAMESPACEURL)) 482 { 483 elemDef = def.m_elementProcessor; 484 order = def.getOrder(); 485 multiAllowed = def.getMultiAllowed(); 486 } 487 } 488 else if (def.QNameEquals(uri, localName)) 489 { 490 if (def.getRequired()) 491 this.setRequiredFound(def.getName(), true); 492 order = def.getOrder(); 493 multiAllowed = def.getMultiAllowed(); 494 elemDef = def.m_elementProcessor; 495 break; 496 } 497 } 498 499 if (elemDef != null && this.isOrdered()) 500 { 501 int lastOrder = getLastOrder(); 502 if (order > lastOrder) 503 setLastOrder(order); 504 else if (order == lastOrder && !multiAllowed) 505 { 506 return null; 507 } 508 else if (order < lastOrder && order > 0) 509 { 510 return null; 511 } 512 } 513 514 return elemDef; 515 } 516 517 527 XSLTElementProcessor getProcessorForUnknown(String uri, String localName) 528 { 529 530 if (null == m_elements) 532 return null; 533 534 int n = m_elements.length; 535 536 for (int i = 0; i < n; i++) 537 { 538 XSLTElementDef def = m_elements[i]; 539 540 if (def.m_name.equals("unknown") && uri.length() > 0) 541 { 542 return def.m_elementProcessor; 543 } 544 } 545 546 return null; 547 } 548 549 552 private XSLTAttributeDef[] m_attributes; 553 554 559 XSLTAttributeDef[] getAttributes() 560 { 561 return m_attributes; 562 } 563 564 573 XSLTAttributeDef getAttributeDef(String uri, String localName) 574 { 575 576 XSLTAttributeDef defaultDef = null; 577 XSLTAttributeDef[] attrDefs = getAttributes(); 578 int nAttrDefs = attrDefs.length; 579 580 for (int k = 0; k < nAttrDefs; k++) 581 { 582 XSLTAttributeDef attrDef = attrDefs[k]; 583 String uriDef = attrDef.getNamespace(); 584 String nameDef = attrDef.getName(); 585 586 if (nameDef.equals("*") && (equalsMayBeNullOrZeroLen(uri, uriDef) || 587 (uriDef != null && uriDef.equals("*") && uri!=null && uri.length() > 0 ))) 588 { 589 return attrDef; 590 } 591 else if (nameDef.equals("*") && (uriDef == null)) 592 { 593 594 defaultDef = attrDef; 597 } 598 else if (equalsMayBeNullOrZeroLen(uri, uriDef) 599 && localName.equals(nameDef)) 600 { 601 return attrDef; 602 } 603 } 604 605 if (null == defaultDef) 606 { 607 if (uri.length() > 0 && !equalsMayBeNullOrZeroLen(uri, Constants.S_XSLNAMESPACEURL)) 608 { 609 return XSLTAttributeDef.m_foreignAttr; 610 } 611 } 612 613 return defaultDef; 614 } 615 616 619 private XSLTElementProcessor m_elementProcessor; 620 621 626 XSLTElementProcessor getElementProcessor() 627 { 628 return m_elementProcessor; 629 } 630 631 636 void setElementProcessor(XSLTElementProcessor handler) 637 { 638 639 if (handler != null) 640 { 641 m_elementProcessor = handler; 642 643 m_elementProcessor.setElemDef(this); 644 } 645 } 646 647 651 private Class m_classObject; 652 653 659 Class getClassObject() 660 { 661 return m_classObject; 662 } 663 664 667 private boolean m_has_required = false; 668 669 674 boolean hasRequired() 675 { 676 return m_has_required; 677 } 678 679 682 private boolean m_required = false; 683 684 689 boolean getRequired() 690 { 691 return m_required; 692 } 693 694 Hashtable m_requiredFound; 695 696 700 void setRequiredFound(String elem, boolean found) 701 { 702 if (m_requiredFound.get(elem) != null) 703 m_requiredFound.remove(elem); 704 } 705 706 711 boolean getRequiredFound() 712 { 713 if (m_requiredFound == null) 714 return true; 715 return m_requiredFound.isEmpty(); 716 } 717 718 723 String getRequiredElem() 724 { 725 if (m_requiredFound == null) 726 return null; 727 Enumeration elems = m_requiredFound.elements(); 728 String s = ""; 729 boolean first = true; 730 while (elems.hasMoreElements()) 731 { 732 if (first) 733 first = false; 734 else 735 s = s + ", "; 736 s = s + (String )elems.nextElement(); 737 } 738 return s; 739 } 740 741 boolean m_isOrdered = false; 742 743 748 boolean isOrdered() 749 { 750 770 return m_isOrdered; 771 } 772 773 776 private int m_order = -1; 777 778 783 int getOrder() 784 { 785 return m_order; 786 } 787 788 792 private int m_lastOrder = -1; 793 794 799 int getLastOrder() 800 { 801 return m_lastOrder; 802 } 803 804 809 void setLastOrder(int order) 810 { 811 m_lastOrder = order ; 812 } 813 814 817 private boolean m_multiAllowed = true; 818 819 824 boolean getMultiAllowed() 825 { 826 return m_multiAllowed; 827 } 828 } 829 | Popular Tags |