1 package org.xmldb.xupdate.lexus; 2 3 55 56 import org.w3c.dom.Node ; 57 import org.w3c.dom.traversal.NodeFilter; 58 import org.xml.sax.InputSource ; 59 import org.xml.sax.SAXException ; 60 import org.xml.sax.helpers.ParserAdapter ; 61 import org.xmldb.common.xml.queries.XUpdateQuery; 62 import org.xmldb.xupdate.lexus.commands.CommandConstants; 63 import org.xmldb.xupdate.lexus.commands.CommandObject; 64 import org.xmldb.xupdate.lexus.commands.DefaultCommand; 65 66 import javax.xml.parsers.DocumentBuilder ; 67 import javax.xml.parsers.DocumentBuilderFactory ; 68 import javax.xml.parsers.SAXParser ; 69 import javax.xml.parsers.SAXParserFactory ; 70 import java.io.BufferedReader ; 71 import java.io.File ; 72 import java.io.FileReader ; 73 import java.io.StringReader ; 74 import java.util.Enumeration ; 75 import java.util.HashMap ; 76 import java.util.Hashtable ; 77 import java.util.Vector ; 78 79 95 public class XUpdateQueryImpl implements XUpdateQuery { 96 org.apache.log4j.Category log = org.apache.log4j.Category.getInstance(XUpdateQueryImpl.class); 97 98 99 public static final String NAMESPACE_URI = "http://www.xmldb.org/xupdate"; 100 101 102 private Vector [] query = null; 103 104 private CommandConstants commandConstants = null; 105 106 protected Node namespace = null; 107 108 protected HashMap namespaces = null; 109 110 111 114 public XUpdateQueryImpl() { 115 commandConstants = new CommandConstants(); 116 } 117 118 119 122 public void setQString(String queryString) throws SAXException { 123 XUpdateQueryParser xuParser = new XUpdateQueryParser(commandConstants); 124 125 try { 126 SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); 127 ParserAdapter saxParser = new ParserAdapter (parser.getParser()); 128 saxParser.setContentHandler(xuParser); 129 saxParser.parse(new InputSource (new StringReader (queryString))); 130 } catch (Exception e) { 131 throw new SAXException (e.getMessage()); 132 } 133 134 namespaces = xuParser.getNamespaceMappings(); 135 query = xuParser.getCachedQuery(); 136 if (query[0].size() == 0) { 137 throw new SAXException ("query contains no XUpdateOperation !"); 138 } 139 } 140 141 142 145 public void setNamespace(Node node) { 146 namespace = node; 147 } 148 149 150 153 public void setNodeFilter(NodeFilter filter) { 154 filter = filter; 155 } 156 157 158 161 public void execute(Node contextNode) throws Exception { 162 CommandObject currentCommand = new DefaultCommand(contextNode); 164 Enumeration commands = query[0].elements(); 165 Enumeration attributes = query[1].elements(); 166 Enumeration characters = query[2].elements(); 167 while (commands.hasMoreElements()) { 168 int id = ((Integer ) commands.nextElement()).intValue(); 170 if (id > 0) { 171 switch (id) { 173 case CommandConstants.ATTRIBUTES: 174 currentCommand.submitAttributes((Hashtable ) attributes.nextElement()); 175 break; 176 case CommandConstants.CHARACTERS: 177 currentCommand.submitCharacters((String ) characters.nextElement()); 178 break; 179 default: 180 if (!currentCommand.submitInstruction(id)) { 181 commandConstants.setContextNode(contextNode); 182 currentCommand = commandConstants.commandForID(id); 183 if (currentCommand == null) { 184 throw new Exception ("operation can not have any XUpdate-instruction !"); 185 } 186 currentCommand.reset(); 187 } 188 } 189 } else { 190 if (!currentCommand.executeInstruction()) { 192 contextNode = currentCommand.execute(); 193 currentCommand = new DefaultCommand(contextNode); 194 } 195 196 } 197 } 198 } 200 201 202 205 public static void main(String args[]) throws Exception { 206 if (args.length == 0) { 207 System.err.println("usage: java org.xmldb.xupdate.lexus.XUpdateQueryImpl update document"); 208 System.err.println(" update - filename of the file which contains XUpdate operations"); 209 System.err.println(" document - filename of the file which contains the content to update"); 210 System.exit(0); 211 } 212 213 File file = new File (args[0]); 215 BufferedReader br = new BufferedReader (new FileReader (file)); 216 char[] characters = new char[new Long (file.length()).intValue()]; 217 br.read(characters, 0, new Long (file.length()).intValue()); 218 String queryStr = new String (characters); 219 220 Node myDocument = null; 222 223 DocumentBuilderFactory parserFactory = DocumentBuilderFactory.newInstance(); 224 225 parserFactory.setValidating(false); 226 parserFactory.setNamespaceAware(true); 227 parserFactory.setIgnoringElementContentWhitespace(true); 228 229 DocumentBuilder builder = parserFactory.newDocumentBuilder(); 230 myDocument = builder.parse(args[1]); 231 232 System.setProperty("org.xmldb.common.xml.queries.XPathQueryFactory", 233 "org.xmldb.common.xml.queries.xalan2.XPathQueryFactoryImpl"); 234 XUpdateQuery xq = new XUpdateQueryImpl(); 236 237 System.err.println("Starting updates..."); 238 long timeStart = System.currentTimeMillis(); 239 240 xq.setQString(queryStr); 241 xq.execute(myDocument); 242 243 long timeEnd = System.currentTimeMillis(); 244 System.err.println("Updates done in " + (timeEnd - timeStart) + " ms ..."); 245 246 247 248 249 java.io.StringWriter writer = new java.io.StringWriter (); 251 org.apache.xml.serialize.OutputFormat OutFormat = new org.apache.xml.serialize.OutputFormat("xml", "UTF-8", true); 252 org.apache.xml.serialize.XMLSerializer serializer = new org.apache.xml.serialize.XMLSerializer(writer, OutFormat); 253 serializer.asDOMSerializer().serialize((org.w3c.dom.Document ) myDocument); 254 System.err.println("Result: \n"+writer.toString()); 255 } 257 258 } 259 260 | Popular Tags |