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.Document ; 59 import org.w3c.dom.Element ; 60 import org.w3c.dom.Node ; 61 62 67 public class InsertElement extends InsertStates { 68 69 72 public InsertElement() { 73 super(); 74 } 75 76 77 80 public Node execute(Node contextNode) throws Exception { 81 if (attributes == null) { 82 throw new Exception ("no attributes for element constructor!"); 83 } 84 String namespaceURI = (String ) attributes.get("namespace"); 85 String qName = (String ) attributes.get("name"); 86 if (qName == null) { 87 throw new Exception ("no name attribute for element constructor!"); 88 } 89 Document document = contextNode.getOwnerDocument(); 90 Node element; 91 if (namespaceURI != null && namespaceURI.equals("")) { 92 namespaceURI = null; 93 } 94 element = document.createElementNS(namespaceURI, qName); 95 if (namespaceURI != null) { 97 String prefix = element.getPrefix(); 98 if (prefix == null) { 99 103 if (!hasDefaultNamespace(contextNode, namespaceURI)) { 104 ((Element ) element).setAttributeNS("http://www.w3.org/2000/xmlns/", 105 "xmlns", namespaceURI); 106 } 107 } else { 108 117 PrefixResolver prefixResolver = new PrefixResolverDefault(contextNode); 118 String uri = prefixResolver.getNamespaceForPrefix(prefix); 119 if (uri == null || !uri.equalsIgnoreCase(namespaceURI)) { 120 ((Element ) element).setAttributeNS("http://www.w3.org/2000/xmlns/", 121 "xmlns:" + prefix, namespaceURI); 122 } 123 } 124 } 125 int charactersSize = characters.size(); 126 for (int i = 0; i < charactersSize; i++) { 127 element.appendChild(document.createTextNode((String ) characters.elementAt(i))); 128 } 129 return contextNode.appendChild(element); 130 } 131 } 132 133 | Popular Tags |