1 package org.xmldb.xupdate.lexus; 2 3 55 56 import org.xml.sax.Attributes ; 57 import org.xml.sax.ContentHandler ; 58 import org.xml.sax.Locator ; 59 import org.xml.sax.SAXException ; 60 import org.xmldb.xupdate.lexus.commands.CommandConstants; 61 62 import java.util.HashMap ; 63 import java.util.Hashtable ; 64 import java.util.Vector ; 65 66 74 public class XUpdateQueryParser implements ContentHandler { 75 org.apache.log4j.Category log = org.apache.log4j.Category.getInstance(XUpdateQueryParser.class); 76 77 78 private Vector commands = null; 79 80 private Vector attributes = null; 81 82 private Vector characters = null; 83 84 private HashMap namespaces = null; 85 86 private CommandConstants consts = null; 87 88 private boolean xupdateInsertOperation = false; 89 90 private String lastOperation = ""; 91 92 95 public XUpdateQueryParser(CommandConstants constants) { 96 consts = constants; 97 commands = new Vector (); 98 attributes = new Vector (); 99 characters = new Vector (); 100 namespaces = new HashMap (); 101 } 102 103 104 107 public void startDocument() { 108 } 109 110 111 114 public void endDocument() { 115 } 116 117 118 121 public void startElement(String namespaceURI, String localName, 122 String qName, Attributes atts) throws SAXException { 123 Hashtable attributesHash = new Hashtable (); 124 for (int i = 0; i < atts.getLength(); i++) { 125 attributesHash.put(atts.getLocalName(i), atts.getValue(i)); 126 } 127 if (namespaceURI.equals(XUpdateQueryImpl.NAMESPACE_URI)) { 128 int id = consts.idForString(localName); 129 if (id != 0) { 130 xupdateInsertOperation = consts.isInsertOperation(id) ? true : xupdateInsertOperation; 131 commands.addElement(new Integer (id)); 132 if (consts.isInstruction(id) && !xupdateInsertOperation) { 133 throw new SAXException ("instruction <" + qName + "> is not valid for operation <" + lastOperation + "> !"); 134 } 135 if (!attributesHash.isEmpty()) { 136 commands.addElement(new Integer (consts.ATTRIBUTES)); 137 attributes.addElement(attributesHash); 138 } 139 } 140 } else if (xupdateInsertOperation) { 141 log.debug(".startElement(): xupdateInsertOperation == true" + namespaceURI + " " + qName); 142 commands.addElement(new Integer (consts.INSTRUCTION_ELEMENT)); 143 Hashtable temp = new Hashtable (); 144 temp.put("name", qName); 145 temp.put("namespace", namespaceURI); 146 commands.addElement(new Integer (consts.ATTRIBUTES)); 147 attributes.addElement(temp); 148 for (int i = 0; i < atts.getLength(); i++) { 149 commands.addElement(new Integer (consts.INSTRUCTION_ATTRIBUTE)); 150 temp = new Hashtable (); 151 temp.put("name", atts.getQName(i)); 152 if (atts.getURI(i) != null) { 153 temp.put("namespace", atts.getURI(i)); 154 } 155 commands.addElement(new Integer (consts.ATTRIBUTES)); 156 attributes.addElement(temp); 157 commands.addElement(new Integer (consts.CHARACTERS)); 158 characters.addElement(atts.getValue(i)); 159 commands.addElement(new Integer (-consts.INSTRUCTION_ATTRIBUTE)); 160 } 161 } else { 162 log.error(".startElement(): " + "no insert-operation for element <" + qName + "> or wrong XUpdate-Namespace !"); 163 throw new SAXException ("no insert-operation for element <" + qName + "> or wrong XUpdate-Namespace !"); 164 } 165 lastOperation = qName; 166 } 167 168 169 172 public void endElement(String namespaceURI, String localName, String qName) { 173 if (namespaceURI.equals(XUpdateQueryImpl.NAMESPACE_URI)) { 174 int id = consts.idForString(localName); 175 if (id != 0) { 176 commands.addElement(new Integer (-id)); 177 xupdateInsertOperation = consts.isInsertOperation(id) ? false : xupdateInsertOperation; 178 } 179 } else if (xupdateInsertOperation) { 180 commands.addElement(new Integer (-consts.INSTRUCTION_ELEMENT)); 181 } 182 } 183 184 185 192 public void startPrefixMapping(String prefix, String uri) { 193 if ((prefix != null) && (prefix.length() > 0)) { 194 namespaces.put(prefix, uri); 195 } else { 196 namespaces.put(null, uri); 197 } 198 } 199 200 201 205 public void endPrefixMapping(String prefix) { 206 } 207 208 209 212 public void characters(char[] ch, int start, int length) { 213 if (!commands.isEmpty()) { 214 int lastInteger = ((Integer ) commands.lastElement()).intValue(); 215 if (xupdateInsertOperation && lastInteger < 0) { 216 commands.addElement(new Integer (consts.INSTRUCTION_TEXT)); 217 } 218 commands.addElement(new Integer (consts.CHARACTERS)); 219 characters.addElement(new String (ch, start, length)); 220 if (xupdateInsertOperation && lastInteger < 0) { 221 commands.addElement(new Integer (-consts.INSTRUCTION_TEXT)); 222 } 223 } 224 } 225 226 227 230 public void ignorableWhitespace(char[] ch, int start, int length) { 231 } 232 233 234 237 public void processingInstruction(String target, String data) { 238 } 239 240 241 244 public void setDocumentLocator(Locator locator) { 245 } 246 247 248 251 public void skippedEntity(String name) { 252 } 253 254 255 260 public Vector [] getCachedQuery() { 261 return new Vector []{commands, attributes, characters}; 262 } 263 264 271 public HashMap getNamespaceMappings() { 272 return namespaces; 273 } 274 275 } 276 277 | Popular Tags |