1 package org.xmldb.xupdate.lexus.commands; 2 3 55 56 import org.w3c.dom.*; 57 58 64 public class RenameCommand extends CommandObject { 65 66 69 public RenameCommand(Node contextNode) throws Exception { 70 super(contextNode); 71 } 72 73 74 77 public boolean submitInstruction(int instruction) { 78 return false; 80 } 81 82 83 86 public boolean executeInstruction() { 87 return false; 89 } 90 91 92 95 public Node execute() throws Exception { 96 if (characters.size() == 0) { 97 throw new Exception ("[rename] node name must not be empty, please type a name !"); 98 } 99 String name = (String ) characters.firstElement(); 100 String selection = (String ) attributes.get("select"); 101 selectNodes(selection); 102 int selectionLength = selectionNodeList.getLength(); 103 for (int i = 0; i < selectionLength; i++) { 104 Node current = selectionNodeList.item(i); 105 Node parent = current.getParentNode(); 106 switch (current.getNodeType()) { 107 case Node.ATTRIBUTE_NODE: 108 parent = ((Attr) current).getOwnerElement(); 111 ((Element) parent).setAttributeNS(current.getNamespaceURI(), name, current.getNodeValue()); 112 ((Element) parent).removeAttribute(current.getNodeName()); 113 break; 114 case Node.PROCESSING_INSTRUCTION_NODE: 115 ProcessingInstruction newPI = document.createProcessingInstruction(name, current.getNodeValue()); 116 parent.replaceChild(newPI, current); 117 break; 118 case Node.ELEMENT_NODE: 119 Element newElement = document.createElementNS(current.getNamespaceURI(), name); 120 NamedNodeMap attributes = current.getAttributes(); 123 int attributesLength = attributes.getLength(); 124 for (int j = 0; j < attributesLength; j++) { 125 newElement.setAttributeNS(attributes.item(j).getNamespaceURI(), attributes.item(j).getNodeName(), 126 attributes.item(j).getNodeValue()); 127 } 128 NodeList childNodes = current.getChildNodes(); 129 int childNodesLength = childNodes.getLength(); 130 for (int j = 0; j < childNodesLength; j++) { 131 newElement.appendChild(childNodes.item(j).cloneNode(true)); 132 } 133 parent.replaceChild(newElement, current); 134 break; 135 } 136 } 137 return contextNode; 138 } 139 } 140 141 | Popular Tags |