/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Id: XUpdate.java 511426 2007-02-25 03:25:02Z vgritsenko $
*/
package org.apache.xindice.tools.command;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xindice.tools.XMLTools;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.modules.XUpdateQueryService;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Hashtable;
/**
* XUpdate runs an XUpdate query stored in a file against the specified collection or
* document.
*
* @author Kimbro Staken <kstaken@xmldatabases.org>
* @version $Revision: 511426 $, $Date: 2007-02-24 22:25:02 -0500 (Sat, 24 Feb 2007) $
*/
public class XUpdate extends Command {
private static final Log log = LogFactory.getLog(XUpdate.class);
public boolean execute(Hashtable table) throws Exception {
Collection col = null;
try {
if ((String) table.get(XMLTools.COLLECTION) == null) {
System.out.println("ERROR : Collection name and switch required");
return false;
}
if ("".equals(table.get(XMLTools.FILE_PATH))) {
System.out.println("ERROR : Path to file containing XUpdate to execute required");
return false;
}
String name = (String) table.get(XMLTools.NAME_OF);
// Nomalize the collection URI
String colstring = normalizeCollectionURI((String) table.get(XMLTools.COLLECTION),
(String) table.get(XMLTools.LOCAL));
// Read in the XUpdate commands from the file
StringBuffer commands = new StringBuffer();
File file = new File((String) table.get(XMLTools.FILE_PATH));
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
commands.append(line);
}
reader.close();
// Get the collection reference for the requested collection
col = DatabaseManager.getCollection(colstring);
if (col == null) {
System.out.println("ERROR : Collection not found!");
return false;
}
// To run XUpdate commands we use the XUpdateQueryService
XUpdateQueryService service = null;
service = (XUpdateQueryService) col.getService("XUpdateQueryService", "1.0");
long result = 0;
// See if we're updating one document or the whole collection.
if (name == null) {
result = service.update(commands.toString());
}
else {
result = service.updateResource(name, commands.toString());
}
System.out.println(result + " documents updated");
} catch (Exception e) {
System.out.println("ERROR : " + e.getMessage());
if (table.get(XMLTools.VERBOSE).equals("true")) {
if (log.isWarnEnabled()) {
log.warn("ignored exception", e);
}
}
return false;
} finally {
if (col != null) {
col.close();
}
}
return true;
}
}