1 package org.xmldb.xupdate.lexus.commands; 2 3 55 56 import org.apache.xml.utils.PrefixResolver; 57 import org.apache.xml.utils.PrefixResolverDefault; 58 import org.w3c.dom.Attr ; 59 import org.w3c.dom.Element ; 60 import org.w3c.dom.Node ; 61 62 67 public class InsertAttribute extends InsertStates { 68 69 72 public InsertAttribute() { 73 super(); 74 } 75 76 77 80 public Node execute(Node contextNode) throws Exception { 81 if (contextNode.getNodeType() != Node.ELEMENT_NODE) { 82 throw new Exception ("can not append attribute !"); 83 } 84 String namespaceURI = (String ) attributes.get("namespace"); 85 String qName = (String ) attributes.get("name"); 86 87 String data = ""; 88 if (!characters.isEmpty()) { 90 data = (String ) characters.firstElement(); 91 } 92 93 Attr attribute; 94 if (namespaceURI != null && namespaceURI.equals("")) { 95 namespaceURI = null; 96 } 97 attribute = contextNode.getOwnerDocument().createAttributeNS(namespaceURI, qName); 98 attribute.setValue(data); 99 ((Element ) contextNode).setAttributeNodeNS(attribute); 100 if (namespaceURI != null) { 102 String prefix = attribute.getPrefix(); 103 if (prefix == null) { 104 108 if (!hasDefaultNamespace(contextNode, namespaceURI)) { 109 ((Element ) contextNode).setAttributeNS("http://www.w3.org/2000/xmlns/", 110 "xmlns", namespaceURI); 111 } 112 } else { 113 122 PrefixResolver prefixResolver = new PrefixResolverDefault(contextNode); 123 String uri = prefixResolver.getNamespaceForPrefix(prefix); 124 if (uri == null || !uri.equalsIgnoreCase(namespaceURI)) { 125 ((Element ) contextNode).setAttributeNS("http://www.w3.org/2000/xmlns/", 126 "xmlns:" + prefix, namespaceURI); 127 } 128 } 129 } 130 return attribute; 131 } 132 } 133 134 | Popular Tags |