1 package org.xmldb.xupdate.lexus.commands; 2 3 55 56 import org.w3c.dom.Attr ; 57 import org.w3c.dom.Element ; 58 import org.w3c.dom.NamedNodeMap ; 59 import org.w3c.dom.Node ; 60 61 import java.util.Hashtable ; 62 63 68 public abstract class InsertCommand extends CommandObject { 69 70 71 private Node current = null; 72 73 protected Node result = null; 74 75 private int state = -1; 76 77 private InsertStates[] states = null; 78 79 private static final int STATE_COUNT = 7; 80 81 84 public InsertCommand(Node contextNode) throws Exception { 85 super(contextNode); 86 states = new InsertStates[STATE_COUNT]; 87 states[0] = new InsertElement(); 88 states[1] = new InsertAttribute(); 89 states[2] = new InsertComment(); 90 states[3] = new InsertText(); 91 states[4] = new InsertCDATA(); 92 states[5] = new InsertProcessingInstruction(); 93 states[6] = new InsertVariable(); 94 } 95 96 97 100 public void submitAttributes(Hashtable attributes) { 101 if (state < 0) { 102 super.submitAttributes(attributes); 103 } else { 104 states[state].submitAttributes(attributes); 105 } 106 } 107 108 109 112 public void submitCharacters(String data) { 113 if (state < 0) { 114 super.submitCharacters(data); 115 } else { 116 states[state].submitCharacters(data); 117 } 118 } 119 120 121 124 public boolean submitInstruction(int instruction) throws Exception { 125 if (result == null) { 126 result = document.createElementNS(null, "temporaryXUpdateTree"); 127 current = result; 128 document.getDocumentElement().appendChild(result); 129 } 130 if (state >= 0) { 131 current = states[state].execute(current); 132 } 133 switch (instruction) { 134 case CommandConstants.INSTRUCTION_ELEMENT: 135 state = 0; 136 break; 137 case CommandConstants.INSTRUCTION_ATTRIBUTE: 138 state = 1; 139 break; 140 case CommandConstants.INSTRUCTION_COMMENT: 141 state = 2; 142 break; 143 case CommandConstants.INSTRUCTION_TEXT: 144 state = 3; 145 break; 146 case CommandConstants.INSTRUCTION_CDATA: 147 state = 4; 148 break; 149 case CommandConstants.INSTRUCTION_PROCESSING_INSTRUCTION: 150 state = 5; 151 break; 152 case CommandConstants.INSTRUCTION_VALUE_OF: 153 state = 6; 154 break; 155 default: 156 state = -1; 157 } 158 if (state >= 0) { 159 states[state].reset(); 160 } 161 return state >= 0; 162 } 163 164 165 168 public boolean executeInstruction() throws Exception { 169 if (state >= 0) { 170 current = states[state].execute(current); 171 state = -1; 172 } 173 if (!current.equals(result)) { 174 switch (current.getNodeType()) { 175 case Node.ATTRIBUTE_NODE: 176 current = ((Attr ) current).getOwnerElement(); 177 return true; 178 default: 179 current = current.getParentNode(); 180 return true; 181 } 182 } 183 return false; 184 } 185 186 187 190 protected void insertAttributes(NamedNodeMap attributes, Node node) throws Exception { 191 if (attributes == null) { 192 return; 193 } 194 if (node.getNodeType() != Node.ELEMENT_NODE) { 195 throw new Exception ("can't append attribute to !"); 196 } 197 int attributesLength = attributes.getLength(); 198 for (int j = 0; j < attributesLength; j++) { 199 Attr attribute = (Attr ) attributes.item(j); 200 String namespaceURI = attribute.getNamespaceURI(); 201 ((Element ) node).setAttributeNS(namespaceURI, attribute.getNodeName(), attribute.getNodeValue()); 202 } 203 } 204 205 206 209 public abstract Node execute() throws Exception ; 210 } 211 212 | Popular Tags |