1 23 24 package com.sun.enterprise.deployment.node; 25 26 import java.util.*; 27 import java.lang.reflect.Method ; 28 import java.lang.reflect.InvocationTargetException ; 29 import java.util.logging.Level ; 30 31 import org.xml.sax.Attributes ; 32 import org.w3c.dom.Node ; 33 import org.w3c.dom.Document ; 34 import org.w3c.dom.Element ; 35 36 import com.sun.enterprise.deployment.Descriptor; 37 import com.sun.enterprise.deployment.EntityManagerReferenceDescriptor; 38 import com.sun.enterprise.deployment.EntityManagerFactoryReferenceDescriptor; 39 import com.sun.enterprise.deployment.EnvironmentProperty; 40 import com.sun.enterprise.deployment.LifecycleCallbackDescriptor; 41 import com.sun.enterprise.deployment.ResourceReferenceDescriptor; 42 import com.sun.enterprise.deployment.JmsDestinationReferenceDescriptor; 43 import com.sun.enterprise.deployment.MessageDestinationReferenceDescriptor; 44 import com.sun.enterprise.deployment.ServiceReferenceDescriptor; 45 import com.sun.enterprise.deployment.JndiNameEnvironment; 46 import com.sun.enterprise.deployment.types.EjbReference; 47 import com.sun.enterprise.deployment.util.DOLUtils; 48 import com.sun.enterprise.deployment.xml.TagNames; 49 import com.sun.enterprise.deployment.xml.EjbTagNames; 50 import com.sun.enterprise.deployment.xml.WebServicesTagNames; 51 import com.sun.enterprise.deployment.node.ejb.AroundInvokeNode; 52 import com.sun.enterprise.util.LocalStringManagerImpl; 53 54 75 public abstract class DeploymentDescriptorNode extends java.lang.Object 76 implements XMLNode { 77 78 private static final String QNAME_SEPARATOR = ":"; 79 80 protected Hashtable handlers = null; 83 84 private Hashtable addMethods = null; 89 90 private XMLElement xmlTag; 92 93 protected XMLNode parentNode = null; 96 97 protected Object abstractDescriptor; 102 103 104 protected static LocalStringManagerImpl localStrings= 106 new LocalStringManagerImpl(DeploymentDescriptorNode.class); 107 108 109 public DeploymentDescriptorNode() { 110 registerElementHandler(new XMLElement(TagNames.DESCRIPTION), LocalizedInfoNode.class); 111 } 112 113 116 public Object getDescriptor() { 117 118 if (abstractDescriptor==null) { 119 abstractDescriptor = DescriptorFactory.getDescriptor(getXMLPath()); 120 } 121 return abstractDescriptor; 122 } 123 124 130 public void addDescriptor(Object descriptor) { 131 if (getParentNode()==null) { 132 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.addDescriptorFailure", 133 new Object [] {descriptor , toString()}); 134 throw new RuntimeException ("Cannot add " + descriptor + " to " + toString()); 135 } else { 136 getParentNode().addDescriptor(descriptor); 137 } 138 } 139 140 147 protected void addNodeDescriptor(DeploymentDescriptorNode node) { 148 149 if (getDescriptor()==null) { 152 addDescriptor(node.getDescriptor()); 153 return; 154 } 155 String xmlRootTag = node.getXMLRootTag().getQName(); 156 if (addMethods!=null && addMethods.containsKey(xmlRootTag)) { 157 try { 158 Method toInvoke = getDescriptor().getClass().getMethod( 159 (String ) addMethods.get(xmlRootTag), 160 new Class [] { node.getDescriptor().getClass() }); 161 toInvoke.invoke(getDescriptor(), new Object [] {node.getDescriptor()}); 162 } catch (InvocationTargetException e) { 163 Throwable t = e.getTargetException(); 164 if (t instanceof IllegalArgumentException ) { 165 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.addDescriptorFailure", 168 new Object []{node.getDescriptor().getClass() , getDescriptor().getClass()}); 169 } else { 170 t.printStackTrace(); 171 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.addDescriptorFailure", 172 new Object []{t.toString(), null}); 173 } 174 } catch(Throwable t) { 175 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.addDescriptorFailure", 176 new Object [] {node.getDescriptor().getClass() , getDescriptor().getClass() }); 177 t.printStackTrace(); 178 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.addDescriptorFailure", 179 new Object []{t.toString(), null}); 180 } 181 } else { 182 addDescriptor(node.getDescriptor()); 183 } 184 } 185 186 189 public void setParentNode(XMLNode parentNode) { 190 this.parentNode = parentNode; 191 } 192 193 196 public XMLNode getParentNode() { 197 return parentNode; 198 } 199 200 206 protected void registerElementHandler(XMLElement element, Class handler) { 207 if (handlers==null) { 208 handlers = new Hashtable(); 209 } 210 handlers.put(element.getQName(), handler); 211 } 212 213 221 protected void registerElementHandler(XMLElement element, Class handler, 222 String addMethodName) { 223 224 registerElementHandler(element, handler); 225 if (addMethods==null) { 226 addMethods = new Hashtable(); 227 } 228 addMethods.put(element.getQName(), addMethodName); 229 } 230 231 234 protected XMLElement getXMLRootTag() { 235 return xmlTag; 236 } 237 238 241 protected void setXMLRootTag(XMLElement element) { 242 xmlTag = element; 243 } 244 245 248 public XMLNode getHandlerFor(XMLElement element) { 249 if (handlers==null) { 250 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure", 251 new Object [] {this , "No handler registered"}); 252 return null; 253 } else { 254 Class c = (Class ) handlers.get(element.getQName()); 255 if (c==null) { 256 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure", 257 new Object [] {element.getQName(), "No handler registered"}); 258 return null; 259 } 260 if (DOLUtils.getDefaultLogger().isLoggable(Level.FINER)) { 261 DOLUtils.getDefaultLogger().finer("New Handler requested for " + c); 262 } 263 DeploymentDescriptorNode node; 264 try { 265 node = (DeploymentDescriptorNode) c.newInstance(); 266 node.setParentNode(this); 267 node.setXMLRootTag(element); 268 node.getDescriptor(); 269 } catch(Exception e) { 270 e.printStackTrace(); 271 return null; 272 } 273 return node; 274 } 275 } 276 277 280 public void startElement(XMLElement element, Attributes attributes) { 281 if (!this.getXMLRootTag().equals(element)) 283 return; 284 285 if (attributes.getLength()>0) { 286 for (int i=0;i<attributes.getLength();i++) { 287 if (DOLUtils.getDefaultLogger().isLoggable(Level.FINER)) { 288 DOLUtils.getDefaultLogger().finer("With attribute " + attributes.getQName(i)); 289 DOLUtils.getDefaultLogger().finer("With value " + attributes.getValue(i)); 290 } 291 if (!setAttributeValue(element, new XMLElement(attributes.getQName(i)), attributes.getValue(i))) { 293 setElementValue(new XMLElement(attributes.getQName(i)), attributes.getValue(i)); 294 } 295 296 } 297 } 298 } 299 300 308 protected boolean setAttributeValue(XMLElement elementName, XMLElement attributeName, String value) { 309 if (attributeName.getQName().equals(TagNames.ID)) { 311 return true; 312 } 313 314 return false; 315 } 316 317 323 public boolean endElement(XMLElement element) { 324 boolean allDone = element.equals(getXMLRootTag()); 326 if (allDone) { 327 postParsing(); 328 if (getParentNode()!=null && getDescriptor()!=null) { 329 ((DeploymentDescriptorNode) getParentNode()).addNodeDescriptor(this); 330 } 331 } 332 return allDone; 333 } 334 335 338 public void postParsing() { 339 } 340 341 345 public boolean handlesElement(XMLElement element) { 346 if (handlers==null) 348 return true; 349 350 for (Enumeration handlersIterator = handlers.keys();handlersIterator.hasMoreElements();) { 353 String subElement = (String ) handlersIterator.nextElement(); 354 if (element.getQName().equals(subElement)) { 355 return false; 356 } 357 } 358 return true; 359 } 360 361 367 public void setElementValue(XMLElement element, String value) { 368 Map dispatchTable = getDispatchTable(); 370 371 if (dispatchTable != null) { 372 if (dispatchTable.containsKey(element.getQName())) { 373 if (dispatchTable.get(element.getQName())==null) { 374 if (DOLUtils.getDefaultLogger().isLoggable(Level.FINER)) { 376 DOLUtils.getDefaultLogger().finer("Deprecated element " + element.getQName() + " with value " + value + " is ignored"); 377 } 378 return; 379 } 380 try { 381 Object descriptor = getDescriptor(); 382 if (descriptor!=null) { 383 setDescriptorInfo(descriptor, (String ) dispatchTable.get(element.getQName()), (String ) value); 384 } else { 385 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure", 386 new Object [] {element.getQName() , value }); 387 } 388 389 return; 390 } catch (InvocationTargetException e) { 391 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure", 392 new Object [] {dispatchTable.get(element.getQName()) , getDescriptor().getClass()}); 393 Throwable t = e.getTargetException(); 394 if (t instanceof IllegalArgumentException ) { 395 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure", 398 new Object [] {element , value}); 399 } else { 400 t.printStackTrace(); 401 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure", 402 new Object [] {t.toString(), null}); 403 } 404 } catch(Throwable t) { 405 t.printStackTrace(); 406 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure", 407 new Object [] {t.toString(), null}); 408 } 409 } 410 } 411 if (value.trim().length()!=0) { 412 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure", 413 new Object [] {element.getQName() , value }); 414 } 415 return; 416 } 417 418 424 protected Map getDispatchTable() { 425 Map table = new HashMap(); 427 table.put(TagNames.DESCRIPTION, "setDescription"); 428 return table; 429 } 430 431 438 protected void setDescriptorInfo(Object target, String methodName, String value) 439 throws NoSuchMethodException , IllegalAccessException , InvocationTargetException { 440 441 if (DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) { 442 DOLUtils.getDefaultLogger().fine("in " + target.getClass() + " method " + methodName + " with " + value ); 443 } 444 445 try { 446 Method toInvoke = target.getClass().getMethod(methodName, new Class [] { String .class }); 447 toInvoke.invoke(target, new Object [] {value}); 448 } catch(NoSuchMethodException e1) { 449 try { 450 Method toInvoke = target.getClass().getMethod(methodName, new Class [] { int.class }); 452 toInvoke.invoke(target, new Object [] {Integer.valueOf(value)}); 453 } catch (NumberFormatException nfe) { 454 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.invalidDescriptorMappingFailure", 455 new Object []{ getXMLPath().toString() , nfe.toString()}); 456 } catch(NoSuchMethodException e2) { 457 Method toInvoke = target.getClass().getMethod(methodName, new Class [] { boolean.class }); 459 toInvoke.invoke(target, new Object [] {Boolean.valueOf(value)}); 460 } 461 } 462 } 463 464 467 public String getXMLPath() { 468 if (getParentNode()!=null) { 469 return getParentNode().getXMLPath() + "/" + getXMLRootTag().getQName(); 470 } else { 471 return getXMLRootTag().getQName(); 472 } 473 } 474 475 482 public Node writeDescriptor(Node parent, Descriptor descriptor) { 483 return writeDescriptor(parent, getXMLRootTag().getQName(), descriptor); 484 } 485 486 494 public Node writeDescriptor(Node parent, String nodeName, Descriptor descriptor) { 495 Node node = appendChild(parent, nodeName); 496 return node; 497 } 498 499 500 505 static protected Document getOwnerDocument(Node node) { 506 507 if (node instanceof Document ) { 508 return (Document ) node; 509 } 510 return node.getOwnerDocument(); 511 } 512 513 521 public static Element appendChild(Node parent, String elementName) { 522 Element child = getOwnerDocument(parent).createElement(elementName); 523 parent.appendChild(child); 524 return child; 525 } 526 527 536 public static Node appendTextChild(Node parent, String elementName, String text) { 537 538 if (text == null || text.length()==0) 539 return null; 540 541 Node child = appendChild(parent, elementName); 542 child.appendChild(getOwnerDocument(child).createTextNode(text)); 543 return child; 544 } 545 546 555 public static Node appendTextChild(Node parent, String elementName, int value) { 556 return appendTextChild(parent, elementName, String.valueOf(value)); 557 } 558 559 568 public static Node forceAppendTextChild(Node parent, String elementName, String text) { 569 570 Node child = appendChild(parent, elementName); 571 if (text != null && text.length()!=0) { 572 child.appendChild(getOwnerDocument(child).createTextNode(text)); 573 } 574 return child; 575 } 576 577 586 public static void setAttribute(Element parent, String elementName, String text) { 587 588 if (text == null || text.length()==0) 589 return; 590 parent.setAttribute(elementName, text); 591 } 592 593 599 public static void setAttributeNS(Element element, String prefix, 600 String namespaceURI) { 601 602 String nsPrefix = prefix.equals("") ? "xmlns" : 603 "xmlns" + QNAME_SEPARATOR + prefix; 604 605 element.setAttributeNS("http://www.w3.org/2000/xmlns/", nsPrefix, 606 namespaceURI); 607 } 608 609 615 protected void writeEnvEntryDescriptors(Node parentNode, Iterator envEntries) { 616 617 if (envEntries==null || !envEntries.hasNext()) 618 return; 619 620 EnvEntryNode subNode = new EnvEntryNode(); 621 for (;envEntries.hasNext();) { 622 EnvironmentProperty envProp = (EnvironmentProperty) envEntries.next(); 623 subNode.writeDescriptor(parentNode, TagNames.ENVIRONMENT_PROPERTY, envProp); 624 } 625 } 626 627 633 protected void writeEjbReferenceDescriptors(Node parentNode, Iterator refs) { 634 635 if (refs==null || !refs.hasNext()) 636 return; 637 638 EjbReferenceNode subNode = new EjbReferenceNode(); 639 Set localRefDescs = new HashSet(); 641 for (;refs.hasNext();) { 642 EjbReference ejbRef = (EjbReference) refs.next(); 643 if (ejbRef.isLocal()) { 644 localRefDescs.add(ejbRef); 645 } else { 646 subNode.writeDescriptor(parentNode, TagNames.EJB_REFERENCE, ejbRef); 647 } 648 } 649 for (Iterator e=localRefDescs.iterator(); e.hasNext();) { 651 EjbReference ejbRef = (EjbReference) e.next(); 652 subNode.writeDescriptor(parentNode, TagNames.EJB_LOCAL_REFERENCE,ejbRef); 653 } 654 } 655 656 protected void writeServiceReferenceDescriptors(Node parentNode, 657 Iterator refs) { 658 if( ( refs == null ) || !refs.hasNext() ) { 659 return; 660 } 661 662 ServiceReferenceNode serviceRefNode = new ServiceReferenceNode(); 663 while(refs.hasNext()) { 664 ServiceReferenceDescriptor next = (ServiceReferenceDescriptor) 665 refs.next(); 666 serviceRefNode.writeDescriptor 667 (parentNode, WebServicesTagNames.SERVICE_REF, next); 668 } 669 } 670 671 677 protected void writeResourceRefDescriptors(Node parentNode, Iterator resRefs) { 678 679 if (resRefs==null || !resRefs.hasNext()) 680 return; 681 682 ResourceRefNode subNode = new ResourceRefNode(); 683 for (;resRefs.hasNext();) { 684 ResourceReferenceDescriptor aResRef = (ResourceReferenceDescriptor) resRefs.next(); 685 subNode.writeDescriptor(parentNode, TagNames.RESOURCE_REFERENCE, aResRef); 686 } 687 } 688 689 695 protected void writeResourceEnvRefDescriptors(Node parentNode, Iterator resRefs) { 696 697 if (resRefs==null || !resRefs.hasNext()) 698 return; 699 700 ResourceEnvRefNode subNode = new ResourceEnvRefNode(); 701 for (;resRefs.hasNext();) { 702 JmsDestinationReferenceDescriptor aResRef = (JmsDestinationReferenceDescriptor) resRefs.next(); 703 subNode.writeDescriptor(parentNode, TagNames.RESOURCE_ENV_REFERENCE, aResRef); 704 } 705 } 706 707 713 protected void writeMessageDestinationRefDescriptors 714 (Node parentNode, Iterator msgDestRefs) { 715 716 if (msgDestRefs==null || !msgDestRefs.hasNext()) 717 return; 718 719 MessageDestinationRefNode subNode = new MessageDestinationRefNode(); 720 for (;msgDestRefs.hasNext();) { 721 MessageDestinationReferenceDescriptor next = 722 (MessageDestinationReferenceDescriptor) msgDestRefs.next(); 723 subNode.writeDescriptor(parentNode, 724 TagNames.MESSAGE_DESTINATION_REFERENCE, 725 next); 726 } 727 } 728 729 735 protected void writeEntityManagerReferenceDescriptors(Node parentNode, Iterator entityMgrRefs) { 736 737 if (entityMgrRefs==null || !entityMgrRefs.hasNext()) 738 return; 739 740 EntityManagerReferenceNode subNode = new EntityManagerReferenceNode(); 741 for (;entityMgrRefs.hasNext();) { 742 EntityManagerReferenceDescriptor aEntityMgrRef = (EntityManagerReferenceDescriptor)entityMgrRefs.next(); 743 subNode.writeDescriptor(parentNode, TagNames.PERSISTENCE_CONTEXT_REF, aEntityMgrRef); 744 } 745 } 746 747 754 protected void writeEntityManagerFactoryReferenceDescriptors(Node parentNode, Iterator entityMgrFactoryRefs) { 755 756 if (entityMgrFactoryRefs==null || !entityMgrFactoryRefs.hasNext()) 757 return; 758 759 EntityManagerFactoryReferenceNode subNode = 760 new EntityManagerFactoryReferenceNode(); 761 for (;entityMgrFactoryRefs.hasNext();) { 762 EntityManagerFactoryReferenceDescriptor aEntityMgrFactoryRef = 763 (EntityManagerFactoryReferenceDescriptor)entityMgrFactoryRefs.next(); 764 subNode.writeDescriptor(parentNode, TagNames.PERSISTENCE_UNIT_REF, aEntityMgrFactoryRef); 765 } 766 } 767 768 protected void writeAroundInvokeDescriptors 769 (Node parentNode, Iterator aroundInvokeDescs) { 770 if (aroundInvokeDescs == null || !aroundInvokeDescs.hasNext()) 771 return; 772 773 AroundInvokeNode subNode = new AroundInvokeNode(); 774 for(; aroundInvokeDescs.hasNext();) { 775 LifecycleCallbackDescriptor next = 776 (LifecycleCallbackDescriptor) aroundInvokeDescs.next(); 777 subNode.writeDescriptor(parentNode, 778 EjbTagNames.AROUND_INVOKE_METHOD, next); 779 } 780 781 } 782 783 protected void writePostActivateDescriptors 784 (Node parentNode, Iterator postActivateDescs) { 785 if (postActivateDescs == null || !postActivateDescs.hasNext()) 786 return; 787 788 LifecycleCallbackNode subNode = new LifecycleCallbackNode(); 789 for(; postActivateDescs.hasNext();) { 790 LifecycleCallbackDescriptor next = 791 (LifecycleCallbackDescriptor) postActivateDescs.next(); 792 subNode.writeDescriptor(parentNode, 793 EjbTagNames.POST_ACTIVATE_METHOD, next); 794 } 795 796 } 797 798 protected void writePrePassivateDescriptors 799 (Node parentNode, Iterator prePassivateDescs) { 800 if (prePassivateDescs == null || !prePassivateDescs.hasNext()) 801 return; 802 803 LifecycleCallbackNode subNode = new LifecycleCallbackNode(); 804 for(; prePassivateDescs.hasNext();) { 805 LifecycleCallbackDescriptor next = 806 (LifecycleCallbackDescriptor) prePassivateDescs.next(); 807 subNode.writeDescriptor(parentNode, 808 EjbTagNames.PRE_PASSIVATE_METHOD, next); 809 } 810 811 } 812 813 819 protected void writePostConstructDescriptors 820 (Node parentNode, Iterator<LifecycleCallbackDescriptor> postConstructDescs) { 821 if (postConstructDescs == null || !postConstructDescs.hasNext()) 822 return; 823 824 LifecycleCallbackNode subNode = new LifecycleCallbackNode(); 825 for (; postConstructDescs.hasNext();) { 826 LifecycleCallbackDescriptor next = postConstructDescs.next(); 827 subNode.writeDescriptor(parentNode, TagNames.POST_CONSTRUCT, 828 next); 829 } 830 } 831 832 838 protected void writePreDestroyDescriptors 839 (Node parentNode, Iterator<LifecycleCallbackDescriptor> preDestroyDescs) { 840 if (preDestroyDescs == null || !preDestroyDescs.hasNext()) 841 return; 842 843 LifecycleCallbackNode subNode = new LifecycleCallbackNode(); 844 for (; preDestroyDescs.hasNext();) { 845 LifecycleCallbackDescriptor next = preDestroyDescs.next(); 846 subNode.writeDescriptor(parentNode, TagNames.PRE_DESTROY, 847 next); 848 } 849 } 850 853 protected void writeLocalizedDescriptions(Node node, Descriptor desc) { 854 855 LocalizedInfoNode localizedNode = new LocalizedInfoNode(); 856 localizedNode.writeLocalizedMap(node, TagNames.DESCRIPTION, desc.getLocalizedDescriptions()); 857 } 858 859 862 protected void writeJNDIEnvironmentRefs(Node node, JndiNameEnvironment descriptor) { 863 864 868 writeEnvEntryDescriptors(node, descriptor.getEnvironmentProperties().iterator()); 869 870 874 878 writeEjbReferenceDescriptors(node, 879 descriptor.getEjbReferenceDescriptors().iterator()); 880 881 882 884 writeServiceReferenceDescriptors(node, descriptor.getServiceReferenceDescriptors().iterator()); 885 886 890 writeResourceRefDescriptors(node, descriptor.getResourceReferenceDescriptors().iterator()); 891 892 896 writeResourceEnvRefDescriptors(node, descriptor.getJmsDestinationReferenceDescriptors().iterator()); 897 898 902 writeMessageDestinationRefDescriptors(node, descriptor.getMessageDestinationReferenceDescriptors().iterator()); 903 904 908 writeEntityManagerReferenceDescriptors(node, descriptor.getEntityManagerReferenceDescriptors().iterator()); 909 910 914 writeEntityManagerFactoryReferenceDescriptors(node, descriptor.getEntityManagerFactoryReferenceDescriptors().iterator()); 915 916 921 writePostConstructDescriptors(node, descriptor.getPostConstructDescriptors().iterator()); 922 923 924 929 writePreDestroyDescriptors(node, descriptor.getPreDestroyDescriptors().iterator()); 930 } 931 932 942 private void writeDeploymentExtensionDescriptor( 943 Node parentNode, Descriptor descriptor) { 944 945 Iterator itr = descriptor.getDeploymentExtensions(); 946 if (itr==null) { 947 return; 948 } 949 DeploymentExtensionNode subNode = new DeploymentExtensionNode(); 950 subNode.writeDescriptor(parentNode, itr); 951 } 952 953 961 protected void addNamespaceDeclaration(Element node, Descriptor descriptor) { 962 963 Map prefixMapping = (descriptor != null ) ? 965 descriptor.getPrefixMapping() : null; 966 if (prefixMapping!=null) { 967 for (Iterator itr =prefixMapping.keySet().iterator();itr.hasNext();) { 968 String prefix = (String ) itr.next(); 969 String namespaceURI = (String ) prefixMapping.get(prefix); 970 setAttributeNS(node, prefix, namespaceURI); 971 } 972 } 973 } 974 975 978 public void addPrefixMapping(String prefix, String uri) { 979 Object o = getDescriptor(); 980 if (o instanceof Descriptor) { 981 Descriptor descriptor = (Descriptor) o; 982 descriptor.addPrefixMapping(prefix, uri); 983 } 984 } 985 986 990 public String resolvePrefix(XMLElement element, String prefix) { 991 return element.getPrefixURIMapping(prefix); 994 } 995 996 1002 public String getPrefixFromQName(String qname) { 1003 StringTokenizer tokenizer = new StringTokenizer(qname, QNAME_SEPARATOR); 1004 return (tokenizer.countTokens() == 2) ? 1005 tokenizer.nextToken() : ""; 1006 } 1007 1008 1013 public String getLocalPartFromQName(String qname) { 1014 StringTokenizer tokenizer = new StringTokenizer(qname, QNAME_SEPARATOR); 1015 String localPart = qname; 1016 if( tokenizer.countTokens() == 2 ) { 1017 tokenizer.nextToken(); 1019 localPart = tokenizer.nextToken(); 1020 } 1021 return localPart; 1022 } 1023 1024 public String composeQNameValue(String prefix, String localPart) { 1025 return ( (prefix != null) && !(prefix.equals("")) ) ? 1026 prefix + QNAME_SEPARATOR + localPart : localPart; 1027 } 1028 1029 public void appendQNameChild(String elementName, Node parent, 1030 String namespaceUri, String localPart, 1031 String prefix) { 1032 if( prefix == null ) { 1033 prefix = elementName + "_ns__"; 1035 } 1036 1037 String elementValue = composeQNameValue(prefix, localPart); 1038 Element element = (Element ) appendTextChild 1039 (parent, elementName, elementValue); 1040 1041 setAttributeNS(element, prefix, namespaceUri); 1049 1050 } 1051} 1052 | Popular Tags |