1 package org.xmldb.xupdate.lexus.commands; 2 3 55 56 import org.w3c.dom.NamedNodeMap ; 57 import org.w3c.dom.Node ; 58 import org.w3c.dom.NodeList ; 59 60 66 public class AppendCommand extends InsertCommand { 67 68 71 public AppendCommand(Node contextNode) throws Exception { 72 super(contextNode); 73 } 74 75 78 public Node execute() throws Exception { 79 if (result == null) { 80 return contextNode; 81 } 82 83 String query = (String ) attributes.get("select"); 84 selectNodes(query); 85 86 int child = -1; 87 String childStr = (String ) attributes.get("child"); 88 if (childStr != null && childStr.equals("last()")) { 89 child = -1; 90 } else if (childStr != null) { 91 child = Integer.parseInt(childStr); 92 } 93 94 NamedNodeMap attributes = result.getAttributes(); 95 NodeList children = result.getChildNodes(); 96 97 int selectionLength = selectionNodeList.getLength(); 98 for (int i = 0; i < selectionLength; i++) { 99 Node current = selectionNodeList.item(i); 100 insertAttributes(attributes, current); 101 102 int childrenLength = children.getLength(); 103 for (int j = 0; j < childrenLength; j++) { 104 NodeList currentChildren = current.getChildNodes(); 105 if (0 < child && child <= currentChildren.getLength()) { 106 current.insertBefore(children.item(j).cloneNode(true), currentChildren.item(child - 1)); 107 } else { 108 current.appendChild(children.item(j).cloneNode(true)); 109 } 110 } 111 } 112 document.getDocumentElement().removeChild(result); 113 114 return contextNode; 115 } 116 } 117 118 | Popular Tags |