1 package org.xmldb.xupdate.lexus.commands; 2 3 55 56 import org.w3c.dom.Attr ; 57 import org.w3c.dom.Comment ; 58 import org.w3c.dom.Node ; 59 import org.w3c.dom.NodeList ; 60 61 import org.apache.log4j.Category; 62 63 68 public class UpdateCommand extends CommandObject { 69 Category log = Category.getInstance(UpdateCommand.class); 70 71 72 75 public UpdateCommand(Node contextNode) throws Exception { 76 super(contextNode); 77 } 78 79 80 83 public boolean submitInstruction(int instruction) { 84 return false; 86 } 87 88 89 92 public boolean executeInstruction() { 93 return false; 95 } 96 97 98 101 public Node execute() throws Exception { 102 String data = ""; 103 for (int i = 0; i < characters.size(); i++) { 104 data += (String ) characters.elementAt(i); 105 } 106 String selection = (String ) attributes.get("select"); 107 selectNodes(selection); 108 int selectionLength = selectionNodeList.getLength(); 109 for (int i = 0; i < selectionNodeList.getLength(); i++) { 110 Node current = selectionNodeList.item(i); 111 switch (current.getNodeType()) { 112 case Node.ELEMENT_NODE: 113 current.normalize(); 114 NodeList children = current.getChildNodes(); 115 int childrenLength = children.getLength(); 116 for (int j = childrenLength -1 ; j >= 0; j--) { 117 log.debug(".execute(): Remove child (" + j + "): " + children.item(j).getNodeType()); 118 if (children.item(j).getNodeType() == Node.TEXT_NODE) { 119 log.debug(".execute(): TEXT_NODE: " + children.item(j).getNodeValue()); 120 } 121 current.removeChild(children.item(j)); 122 } 123 if (characters.size() > 0) { 124 current.appendChild(document.createTextNode(data)); 125 } 126 break; 127 case Node.TEXT_NODE: 128 if (characters.size() > 0) { 129 current.setNodeValue(data); 130 } else { 131 Node parent = current.getParentNode(); 132 parent.removeChild(current); 133 } 134 break; 135 case Node.COMMENT_NODE: 136 ((Comment ) current).setData(data); 137 break; 138 case Node.ATTRIBUTE_NODE: 139 ((Attr ) current).setValue(data); 140 break; 141 default: 142 if (characters.size() == 0) { 143 throw new Exception ("[update] new values must not be empty !"); 144 } 145 current.setNodeValue(data); 146 } 147 } 148 return contextNode; 149 } 150 } 151 | Popular Tags |