1 package org.xmldb.xupdate.lexus.commands; 2 3 55 56 import org.w3c.dom.Attr ; 57 import org.w3c.dom.NamedNodeMap ; 58 import org.w3c.dom.Node ; 59 import org.w3c.dom.NodeList ; 60 61 67 public class InsertBeforeCommand extends InsertCommand { 68 69 72 public InsertBeforeCommand(Node contextNode) throws Exception { 73 super(contextNode); 74 } 75 76 77 80 public Node execute() throws Exception { 81 if (result == null) { 82 return contextNode; 83 } 84 85 String query = (String ) attributes.get("select"); 86 selectNodes(query); 87 88 NamedNodeMap attributes = result.getAttributes(); 89 NodeList children = result.getChildNodes(); 90 91 int selectionLength = selectionNodeList.getLength(); 92 for (int i = 0; i < selectionLength; i++) { 93 Node current = selectionNodeList.item(i); 94 Node parent; 95 switch (current.getNodeType()) { 96 case Node.ATTRIBUTE_NODE: 97 parent = ((Attr ) current).getOwnerElement(); 98 break; 99 default: 100 parent = current.getParentNode(); 101 } 102 103 insertAttributes(attributes, parent); 104 105 int childrenLength = children.getLength(); 106 for (int j = 0; j < childrenLength; j++) { 107 parent.insertBefore(children.item(j).cloneNode(true), current); 108 } 109 110 } 111 document.getDocumentElement().removeChild(result); 112 113 return contextNode; 114 } 115 } 116 117 | Popular Tags |